- formats = []
-
- def add_format(format_id, format_dict, protocol='http'):
- # rtmps URLs does not seem to work
- if protocol == 'rtmps':
- return
- format_url = format_dict.get('url')
- if not format_url:
- return
- qs = compat_parse_qs(compat_urllib_parse_urlparse(format_url).query)
- stream = qs.get('stream', [None])[0]
- if stream:
- format_url = base64.b64decode(stream).decode()
- if format_id in ('m3u8', 'hls'):
- m3u8_formats = self._extract_m3u8_formats(
- format_url, video_id, 'mp4',
- entry_protocol='m3u8_native',
- m3u8_id='m3u8-%s' % protocol, fatal=False)
- # Despite CODECS metadata in m3u8 all video-only formats
- # are actually video+audio
- for f in m3u8_formats:
- if not self.get_param('allow_unplayable_formats') and '_drm/index_' in f['url']:
- continue
- if f.get('acodec') == 'none' and f.get('vcodec') != 'none':
- f['acodec'] = None
- formats.append(f)
- elif format_id in ('mpd', 'dash'):
- formats.extend(self._extract_mpd_formats(
- format_url, video_id, 'mpd-%s' % protocol, fatal=False))
- elif format_url.startswith('rtmp'):
- mobj = re.search(
- r'^(?P<url>rtmp://[^/]+/(?P<app>.+?))/(?P<playpath>mp4:.+)$',
- format_url)
- if not mobj:
- return
- formats.append({
- 'format_id': 'rtmp-%s' % format_id,
- 'ext': 'flv',
- 'url': mobj.group('url'),
- 'play_path': mobj.group('playpath'),
- 'app': mobj.group('app'),
- 'page_url': url,
- })
- else:
- urlh = self._request_webpage(
- HEADRequest(format_url), video_id, 'Checking file size', fatal=False)
- formats.append({
- 'url': format_url,
- 'format_id': '%s-%s' % (format_id, protocol),
- 'height': int_or_none(self._search_regex(
- r'^(\d+)[pP]$', format_id, 'height', default=None)),
- 'filesize': int_or_none(urlh.headers.get('Content-Length')),
- })
-
- for format_id, format_dict in (resp.get('streams') or {}).items():
- add_format(format_id, format_dict)
- if not formats:
- streams = self._call_api(
- 'videos/%s/streams.json' % video_id, video_id,
- 'Downloading video streams JSON')
-
- if 'external' in streams:
- result.update({
- '_type': 'url_transparent',
- 'url': streams['external']['url'],
- })
- return result
-
- for format_id, stream_dict in streams.items():
- for protocol, format_dict in stream_dict.items():
- add_format(format_id, format_dict, protocol)
- self._sort_formats(formats)
-
- result['formats'] = formats
- return result
-