- streams_url,
- track_id, 'Downloading track url')
-
- for key, stream_url in format_dict.items():
- if key.startswith('http'):
- formats.append({
- 'format_id': key,
- 'ext': ext,
- 'url': stream_url,
- 'vcodec': 'none',
- })
- elif key.startswith('rtmp'):
- # The url doesn't have an rtmp app, we have to extract the playpath
- url, path = stream_url.split('mp3:', 1)
- formats.append({
- 'format_id': key,
- 'url': url,
- 'play_path': 'mp3:' + path,
- 'ext': 'flv',
- 'vcodec': 'none',
- })
-
- if not formats:
- # We fallback to the stream_url in the original info, this
- # cannot be always used, sometimes it can give an HTTP 404 error
- formats.append({
- 'format_id': 'fallback',
- 'url': info['stream_url'] + '?client_id=' + self._CLIENT_ID,
- 'ext': ext,
- 'vcodec': 'none',
- })
-
- for f in formats:
- if f['format_id'].startswith('http'):
- f['protocol'] = 'http'
- if f['format_id'].startswith('rtmp'):
- f['protocol'] = 'rtmp'
-
- self._check_formats(formats, track_id)
+ 'https://api.soundcloud.com/i1/tracks/%s/streams' % track_id,
+ track_id, 'Downloading track url', query=query, fatal=False)
+
+ if format_dict:
+ for key, stream_url in format_dict.items():
+ if stream_url in format_urls:
+ continue
+ format_urls.add(stream_url)
+ ext, abr = 'mp3', None
+ mobj = re.search(r'_([^_]+)_(\d+)_url', key)
+ if mobj:
+ ext, abr = mobj.groups()
+ abr = int(abr)
+ if key.startswith('http'):
+ stream_formats = [{
+ 'format_id': key,
+ 'ext': ext,
+ 'url': stream_url,
+ }]
+ elif key.startswith('rtmp'):
+ # The url doesn't have an rtmp app, we have to extract the playpath
+ url, path = stream_url.split('mp3:', 1)
+ stream_formats = [{
+ 'format_id': key,
+ 'url': url,
+ 'play_path': 'mp3:' + path,
+ 'ext': 'flv',
+ }]
+ elif key.startswith('hls'):
+ stream_formats = self._extract_m3u8_formats(
+ stream_url, track_id, ext, entry_protocol='m3u8_native',
+ m3u8_id=key, fatal=False)
+ else:
+ continue
+
+ if abr:
+ for f in stream_formats:
+ f['abr'] = abr
+
+ formats.extend(stream_formats)
+
+ # New API
+ transcodings = try_get(
+ info, lambda x: x['media']['transcodings'], list) or []
+ for t in transcodings:
+ if not isinstance(t, dict):
+ continue
+ format_url = url_or_none(t.get('url'))
+ if not format_url:
+ continue
+ stream = self._download_json(
+ update_url_query(format_url, query), track_id, fatal=False)
+ if not isinstance(stream, dict):
+ continue
+ stream_url = url_or_none(stream.get('url'))
+ if not stream_url:
+ continue
+ if stream_url in format_urls:
+ continue
+ format_urls.add(stream_url)
+ protocol = try_get(t, lambda x: x['format']['protocol'], compat_str)
+ if protocol != 'hls' and '/hls' in format_url:
+ protocol = 'hls'
+ ext = None
+ preset = str_or_none(t.get('preset'))
+ if preset:
+ ext = preset.split('_')[0]
+ if ext not in KNOWN_EXTENSIONS:
+ mimetype = try_get(
+ t, lambda x: x['format']['mime_type'], compat_str)
+ ext = mimetype2ext(mimetype) or 'mp3'
+ format_id_list = []
+ if protocol:
+ format_id_list.append(protocol)
+ format_id_list.append(ext)
+ format_id = '_'.join(format_id_list)
+ formats.append({
+ 'url': stream_url,
+ 'format_id': format_id,
+ 'ext': ext,
+ 'protocol': 'm3u8_native' if protocol == 'hls' else 'http',
+ })
+
+ if not formats:
+ # We fallback to the stream_url in the original info, this
+ # cannot be always used, sometimes it can give an HTTP 404 error
+ formats.append({
+ 'format_id': 'fallback',
+ 'url': update_url_query(info['stream_url'], query),
+ 'ext': 'mp3',
+ })
+ self._check_formats(formats, track_id)
+
+ for f in formats:
+ f['vcodec'] = 'none'
+