Stream URL of soundcloud track where streamble is

2019-05-11 08:29发布

问题:

There is a property "streamable" of a track which is if "false", I'm unable to get stream_url of that track.

But I see there are few downloaders which gets stream_url of the very same song & then gets then cdn link of that track.

I even did authenticate my app but no luck finding the stream_url. Here is what I did.

import soundcloud

# create client object with app credentials
client = soundcloud.Client(client_id='YOUR_CLIENT_ID',
                           client_secret='YOUR_CLIENT_SECRET',
                           redirect_uri='REDIRECT_URL')

# exchange authorization code for access token
code = params['code']
access_token = client.exchange_token(code)

#'YOUR_ACCESS_TOKEN' is access_token which I got in above step.
client = soundcloud.Client(access_token='YOUR_ACCESS_TOKEN')

track = client.get('/tracks/161730338')

Below is the track object

>track.__dict__

{'obj': {u'attachments_uri': u'https://api.soundcloud.com/tracks/161730338/attachments', u'video_url': None, u'track_type': None, u'release_month': None, u'original_format': u'wav', u'label_name': None, u'duration': 198443, u'id': 161730338, u'streamable': False, u'user_id': 14090772, u'user_favorite': False, u'title': u'Jessie J + Ariana Grande + Nicki Minaj - Bang Bang', u'favoritings_count': 94563, u'commentable': True, u'label_id': None, u'state': u'finished', u'downloadable': False, u'policy': u'ALLOW', u'waveform_url': u'https://w1.sndcdn.com/EDiPRlpWkJ2V_m.png', u'sharing': u'public', u'description': u'', u'release_day': None, u'purchase_url': None, u'permalink': u'jessie-j-ariana-grande-nicki-minaj-bang-bang', u'comment_count': 957, u'purchase_title': None, u'last_modified': u'2015/03/25 04:05:16 +0000', u'user': {u'username': u'RepublicRecords', u'permalink': u'universalrepublic', u'avatar_url': u'https://i1.sndcdn.com/avatars-000025014448-peqymq-large.jpg', u'kind': u'user', u'uri': u'https://api.soundcloud.com/users/14090772', u'last_modified': u'2014/11/10 20:57:55 +0000', u'permalink_url': u'http://soundcloud.com/universalrepublic', u'id': 14090772}, u'genre': u'playbook', u'isrc': None, u'download_count': 0, u'permalink_url': u'http://soundcloud.com/universalrepublic/jessie-j-ariana-grande-nicki-minaj-bang-bang', u'playback_count': 4883398, u'kind': u'track', u'release_year': None, u'license': u'all-rights-reserved', u'artwork_url': None, u'created_at': u'2014/08/05 00:19:41 +0000', u'bpm': None, u'uri': u'https://api.soundcloud.com/tracks/161730338', u'original_content_size': 34992416, u'key_signature': None, u'user_playback_count': 1, u'release': None, u'tag_list': u'', u'embeddable_by': u'all'}, 'encoding': 'utf-8', 'url': u'https://api.soundcloud.com/tracks/161730338.json?oauth_token=1-124233-146384181-db7bf19453bd8', 'raw_data': u'{"kind":"track","id":161730338,"created_at":"2014/08/05 00:19:41 +0000","user_id":14090772,"duration":198443,"commentable":true,"state":"finished","original_content_size":34992416,"last_modified":"2015/03/25 04:05:16 +0000","sharing":"public","tag_list":"","permalink":"jessie-j-ariana-grande-nicki-minaj-bang-bang","streamable":false,"embeddable_by":"all","downloadable":false,"purchase_url":null,"label_id":null,"purchase_title":null,"genre":"playbook","title":"Jessie J + Ariana Grande + Nicki Minaj - Bang Bang","description":"","label_name":null,"release":null,"track_type":null,"key_signature":null,"isrc":null,"video_url":null,"bpm":null,"release_year":null,"release_month":null,"release_day":null,"original_format":"wav","license":"all-rights-reserved","uri":"https://api.soundcloud.com/tracks/161730338","user":{"id":14090772,"kind":"user","permalink":"universalrepublic","username":"RepublicRecords","last_modified":"2014/11/10 20:57:55 +0000","uri":"https://api.soundcloud.com/users/14090772","permalink_url":"http://soundcloud.com/universalrepublic","avatar_url":"https://i1.sndcdn.com/avatars-000025014448-peqymq-large.jpg"},"user_playback_count":1,"user_favorite":false,"permalink_url":"http://soundcloud.com/universalrepublic/jessie-j-ariana-grande-nicki-minaj-bang-bang","artwork_url":null,"waveform_url":"https://w1.sndcdn.com/EDiPRlpWkJ2V_m.png","playback_count":4883398,"download_count":0,"favoritings_count":94563,"comment_count":957,"attachments_uri":"https://api.soundcloud.com/tracks/161730338/attachments","policy":"ALLOW"}', 'reason': 'OK', 'status_code': 200}

According to documentation I was supposed to get the stream_url from

# get the tracks streaming URL
stream_url = client.get(track.stream_url, allow_redirects=False)

# print the tracks stream URL
print stream_url.location

I'm stuck here from 4 days. Is there a way to get the final cdn link of the MP3 track or How do these soundclouddownlaoder do this.

回答1:

If the stream_url key returns a null value, I've found that the streams endpoint will return a valid URL.

HTTP GET: https://api.soundcloud.com/tracks/[TRACK_ID]/streams?client_id=[YOUR_CLIENT_ID]

Disclaimer: This is an undocumented endpoint and is subject to change at anytime by SoundCloud.

HTTP GET: https://api.soundcloud.com/tracks/161730338/streams?client_id=[YOUR_CLIENT_ID]

Response:

{
  "hls_mp3_128_url": "https://ec-hls-media.soundcloud.com/playlist/EDiPRlpWkJ2V.128.mp3/playlist.m3u8?f10880d39085a94a0418a7e062b03d52bbdc0e179b82bde1d76ce6ad1a416907458c1576abbf27bccb1984bf2890809c25cb2c95f826eba5858b2322a2cc08c48f3664ebef63724f43e9baa7d819586e497b7175430826823cbebd16",
  "rtmp_mp3_128_url": "rtmp://ec-rtmp-media.soundcloud.com/mp3:EDiPRlpWkJ2V.128?9527d18f1063a01f059bf10590159adb10dea0996b8c0cdb674f9d20261b8798a306771c4e0fd691edd62d0b7db08ebc6cc0c5a4837c348c5d07f977a600bc6e3847081fc6ea55a327f8ef9f5f635a99c5921392ed3ec8030f3e",
  "preview_mp3_128_url": "https://ec-preview-media.sndcdn.com/preview/0/90/EDiPRlpWkJ2V.128.mp3?f10880d39085a94a0418a7e162b03d52e21adf826af17a391e1b7510312a99f96d1a7322537c26fabab56b86080be7194bc01c2056f9c615253e34d8b61dd76b963654b4c4c58c44dcb70f1176081fc49e3f"
}

The preview_mp3_128_url key-value pair has the 128kbps streaming mp3 you want.

If the preview_mp3_128_url is not the full track, you'll need to use a tool such as ffmpeg to download the HLS transport stream

ffmpeg -i "https://ec-hls-media.soundcloud.com/playlist/EDiPRlpWkJ2V.128.mp3/playlist.m3u8?f10880d39085a94a0418a7e062b03d52bbdc0e179b82bde1d76ce6ad1a416907458c1576abbf27bccb1984bf2890809c25cb2c95f826eba5858b2322a2cc08c48f3664ebef63724f43e9baa7d819586e497b7175430826823cbebd16" -c copy test.ts

That will give you the full mp3 file of the track.

Additional information can also be found in this question: Get HTTP MP3 stream from every song