Replace filename mp3 when downloading

前端 未结 1 520
不思量自难忘°
不思量自难忘° 2021-01-17 02:18

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 ( Artis

相关标签:
1条回答
  • 2021-01-17 03:05

    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)
    
    0 讨论(0)
提交回复
热议问题