Replace filename mp3 when downloading

怎甘沉沦 提交于 2019-12-01 12:51:50

问题


How to get auto file name when do downloading? so when I download file, and file name itself automatic save with name song/artist, ex: from name ( amgdgapwgd.mp3) to ( Artist - Song title.mp3 ).


回答1:


What language are you trying to do this in? Also, if the file returns downloadable equal to true, the file should have the proper name.

Example:

https://soundcloud.com/msmrsounds/ms-mr-hurricane-chvrches-remix

From the JSON:

"download_url": "https://api.soundcloud.com/tracks/90787841/download"

That links to this file: Hurricane (CHVRCHES remix).wav

The stream_url mp3 does not return a properly named file. Here's a small Python script I just wrote to take the track name from the API and download the streaming file with that filename. Just replace the URL variable with the soundcloud.com URL of the track you wish to download.

import json, requests

url = 'https://api.soundcloud.com/resolve.json'

your_client_id = '[PUT YOUR client_id HERE]'

params = dict(
    url='https://soundcloud.com/msmrsounds/ms-mr-hurricane-chvrches-remix',
    client_id=your_client_id,
)

# resolve
resp = requests.get(url=url, params=params)
data = json.loads(resp.text)

# get api url
track_url = data.get('location')

track_resp = requests.get(url=url, params=params)
track_data = json.loads(resp.text)

# get stream_url

track_title = track_data.get('title')

stream_url = track_data.get('stream_url')

print track_title
print stream_url

stream_params = dict(
    client_id=your_client_id,
)

stream_resp = requests.get(url=url, params=params)

# pass in title + '.mp3' for filename
with open(track_title + '.mp3', 'wb') as handle:
    response = requests.get(url=stream_url, params=stream_params, stream=True)

    if not response.ok:
        # Something went wrong
        print 'Error downloading mp3'

    for block in response.iter_content(1024):
        if not block:
            break

        handle.write(block)


来源:https://stackoverflow.com/questions/25825590/replace-filename-mp3-when-downloading

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!