]> jfr.im git - yt-dlp.git/blob - yt_dlp/downloader/hls.py
[cleanup] Add more ruff rules (#10149)
[yt-dlp.git] / yt_dlp / downloader / hls.py
1 import binascii
2 import io
3 import re
4 import urllib.parse
5
6 from . import get_suitable_downloader
7 from .external import FFmpegFD
8 from .fragment import FragmentFD
9 from .. import webvtt
10 from ..dependencies import Cryptodome
11 from ..utils import (
12 bug_reports_message,
13 parse_m3u8_attributes,
14 remove_start,
15 traverse_obj,
16 update_url_query,
17 urljoin,
18 )
19
20
21 class HlsFD(FragmentFD):
22 """
23 Download segments in a m3u8 manifest. External downloaders can take over
24 the fragment downloads by supporting the 'm3u8_frag_urls' protocol and
25 re-defining 'supports_manifest' function
26 """
27
28 FD_NAME = 'hlsnative'
29
30 @staticmethod
31 def _has_drm(manifest): # TODO: https://github.com/yt-dlp/yt-dlp/pull/5039
32 return bool(re.search('|'.join((
33 r'#EXT-X-(?:SESSION-)?KEY:.*?URI="skd://', # Apple FairPlay
34 r'#EXT-X-(?:SESSION-)?KEY:.*?KEYFORMAT="com\.apple\.streamingkeydelivery"', # Apple FairPlay
35 r'#EXT-X-(?:SESSION-)?KEY:.*?KEYFORMAT="com\.microsoft\.playready"', # Microsoft PlayReady
36 r'#EXT-X-FAXS-CM:', # Adobe Flash Access
37 )), manifest))
38
39 @classmethod
40 def can_download(cls, manifest, info_dict, allow_unplayable_formats=False):
41 UNSUPPORTED_FEATURES = [
42 # r'#EXT-X-BYTERANGE', # playlists composed of byte ranges of media files [2]
43
44 # Live streams heuristic does not always work (e.g. geo restricted to Germany
45 # http://hls-geo.daserste.de/i/videoportal/Film/c_620000/622873/format,716451,716457,716450,716458,716459,.mp4.csmil/index_4_av.m3u8?null=0)
46 # r'#EXT-X-MEDIA-SEQUENCE:(?!0$)', # live streams [3]
47
48 # This heuristic also is not correct since segments may not be appended as well.
49 # Twitch vods of finished streams have EXT-X-PLAYLIST-TYPE:EVENT despite
50 # no segments will definitely be appended to the end of the playlist.
51 # r'#EXT-X-PLAYLIST-TYPE:EVENT', # media segments may be appended to the end of
52 # # event media playlists [4]
53 # r'#EXT-X-MAP:', # media initialization [5]
54 # 1. https://tools.ietf.org/html/draft-pantos-http-live-streaming-17#section-4.3.2.4
55 # 2. https://tools.ietf.org/html/draft-pantos-http-live-streaming-17#section-4.3.2.2
56 # 3. https://tools.ietf.org/html/draft-pantos-http-live-streaming-17#section-4.3.3.2
57 # 4. https://tools.ietf.org/html/draft-pantos-http-live-streaming-17#section-4.3.3.5
58 # 5. https://tools.ietf.org/html/draft-pantos-http-live-streaming-17#section-4.3.2.5
59 ]
60 if not allow_unplayable_formats:
61 UNSUPPORTED_FEATURES += [
62 r'#EXT-X-KEY:METHOD=(?!NONE|AES-128)', # encrypted streams [1], but not necessarily DRM
63 ]
64
65 def check_results():
66 yield not info_dict.get('is_live')
67 for feature in UNSUPPORTED_FEATURES:
68 yield not re.search(feature, manifest)
69 if not allow_unplayable_formats:
70 yield not cls._has_drm(manifest)
71 return all(check_results())
72
73 def real_download(self, filename, info_dict):
74 man_url = info_dict['url']
75 self.to_screen(f'[{self.FD_NAME}] Downloading m3u8 manifest')
76
77 urlh = self.ydl.urlopen(self._prepare_url(info_dict, man_url))
78 man_url = urlh.url
79 s = urlh.read().decode('utf-8', 'ignore')
80
81 can_download, message = self.can_download(s, info_dict, self.params.get('allow_unplayable_formats')), None
82 if can_download:
83 has_ffmpeg = FFmpegFD.available()
84 no_crypto = not Cryptodome.AES and '#EXT-X-KEY:METHOD=AES-128' in s
85 if no_crypto and has_ffmpeg:
86 can_download, message = False, 'The stream has AES-128 encryption and pycryptodomex is not available'
87 elif no_crypto:
88 message = ('The stream has AES-128 encryption and neither ffmpeg nor pycryptodomex are available; '
89 'Decryption will be performed natively, but will be extremely slow')
90 elif info_dict.get('extractor_key') == 'Generic' and re.search(r'(?m)#EXT-X-MEDIA-SEQUENCE:(?!0$)', s):
91 install_ffmpeg = '' if has_ffmpeg else 'install ffmpeg and '
92 message = ('Live HLS streams are not supported by the native downloader. If this is a livestream, '
93 f'please {install_ffmpeg}add "--downloader ffmpeg --hls-use-mpegts" to your command')
94 if not can_download:
95 if self._has_drm(s) and not self.params.get('allow_unplayable_formats'):
96 if info_dict.get('has_drm') and self.params.get('test'):
97 self.to_screen(f'[{self.FD_NAME}] This format is DRM protected', skip_eol=True)
98 else:
99 self.report_error(
100 'This format is DRM protected; Try selecting another format with --format or '
101 'add --check-formats to automatically fallback to the next best format', tb=False)
102 return False
103 message = message or 'Unsupported features have been detected'
104 fd = FFmpegFD(self.ydl, self.params)
105 self.report_warning(f'{message}; extraction will be delegated to {fd.get_basename()}')
106 return fd.real_download(filename, info_dict)
107 elif message:
108 self.report_warning(message)
109
110 is_webvtt = info_dict['ext'] == 'vtt'
111 if is_webvtt:
112 real_downloader = None # Packing the fragments is not currently supported for external downloader
113 else:
114 real_downloader = get_suitable_downloader(
115 info_dict, self.params, None, protocol='m3u8_frag_urls', to_stdout=(filename == '-'))
116 if real_downloader and not real_downloader.supports_manifest(s):
117 real_downloader = None
118 if real_downloader:
119 self.to_screen(f'[{self.FD_NAME}] Fragment downloads will be delegated to {real_downloader.get_basename()}')
120
121 def is_ad_fragment_start(s):
122 return (s.startswith('#ANVATO-SEGMENT-INFO') and 'type=ad' in s
123 or s.startswith('#UPLYNK-SEGMENT') and s.endswith(',ad'))
124
125 def is_ad_fragment_end(s):
126 return (s.startswith('#ANVATO-SEGMENT-INFO') and 'type=master' in s
127 or s.startswith('#UPLYNK-SEGMENT') and s.endswith(',segment'))
128
129 fragments = []
130
131 media_frags = 0
132 ad_frags = 0
133 ad_frag_next = False
134 for line in s.splitlines():
135 line = line.strip()
136 if not line:
137 continue
138 if line.startswith('#'):
139 if is_ad_fragment_start(line):
140 ad_frag_next = True
141 elif is_ad_fragment_end(line):
142 ad_frag_next = False
143 continue
144 if ad_frag_next:
145 ad_frags += 1
146 continue
147 media_frags += 1
148
149 ctx = {
150 'filename': filename,
151 'total_frags': media_frags,
152 'ad_frags': ad_frags,
153 }
154
155 if real_downloader:
156 self._prepare_external_frag_download(ctx)
157 else:
158 self._prepare_and_start_frag_download(ctx, info_dict)
159
160 extra_state = ctx.setdefault('extra_state', {})
161
162 format_index = info_dict.get('format_index')
163 extra_query = None
164 extra_param_to_segment_url = info_dict.get('extra_param_to_segment_url')
165 if extra_param_to_segment_url:
166 extra_query = urllib.parse.parse_qs(extra_param_to_segment_url)
167 i = 0
168 media_sequence = 0
169 decrypt_info = {'METHOD': 'NONE'}
170 external_aes_key = traverse_obj(info_dict, ('hls_aes', 'key'))
171 if external_aes_key:
172 external_aes_key = binascii.unhexlify(remove_start(external_aes_key, '0x'))
173 assert len(external_aes_key) in (16, 24, 32), 'Invalid length for HLS AES-128 key'
174 external_aes_iv = traverse_obj(info_dict, ('hls_aes', 'iv'))
175 if external_aes_iv:
176 external_aes_iv = binascii.unhexlify(remove_start(external_aes_iv, '0x').zfill(32))
177 byte_range = {}
178 discontinuity_count = 0
179 frag_index = 0
180 ad_frag_next = False
181 for line in s.splitlines():
182 line = line.strip()
183 if line:
184 if not line.startswith('#'):
185 if format_index and discontinuity_count != format_index:
186 continue
187 if ad_frag_next:
188 continue
189 frag_index += 1
190 if frag_index <= ctx['fragment_index']:
191 continue
192 frag_url = urljoin(man_url, line)
193 if extra_query:
194 frag_url = update_url_query(frag_url, extra_query)
195
196 fragments.append({
197 'frag_index': frag_index,
198 'url': frag_url,
199 'decrypt_info': decrypt_info,
200 'byte_range': byte_range,
201 'media_sequence': media_sequence,
202 })
203 media_sequence += 1
204
205 elif line.startswith('#EXT-X-MAP'):
206 if format_index and discontinuity_count != format_index:
207 continue
208 if frag_index > 0:
209 self.report_error(
210 'Initialization fragment found after media fragments, unable to download')
211 return False
212 frag_index += 1
213 map_info = parse_m3u8_attributes(line[11:])
214 frag_url = urljoin(man_url, map_info.get('URI'))
215 if extra_query:
216 frag_url = update_url_query(frag_url, extra_query)
217
218 if map_info.get('BYTERANGE'):
219 splitted_byte_range = map_info.get('BYTERANGE').split('@')
220 sub_range_start = int(splitted_byte_range[1]) if len(splitted_byte_range) == 2 else byte_range['end']
221 byte_range = {
222 'start': sub_range_start,
223 'end': sub_range_start + int(splitted_byte_range[0]),
224 }
225
226 fragments.append({
227 'frag_index': frag_index,
228 'url': frag_url,
229 'decrypt_info': decrypt_info,
230 'byte_range': byte_range,
231 'media_sequence': media_sequence,
232 })
233 media_sequence += 1
234
235 elif line.startswith('#EXT-X-KEY'):
236 decrypt_url = decrypt_info.get('URI')
237 decrypt_info = parse_m3u8_attributes(line[11:])
238 if decrypt_info['METHOD'] == 'AES-128':
239 if external_aes_iv:
240 decrypt_info['IV'] = external_aes_iv
241 elif 'IV' in decrypt_info:
242 decrypt_info['IV'] = binascii.unhexlify(decrypt_info['IV'][2:].zfill(32))
243 if external_aes_key:
244 decrypt_info['KEY'] = external_aes_key
245 else:
246 decrypt_info['URI'] = urljoin(man_url, decrypt_info['URI'])
247 if extra_query:
248 decrypt_info['URI'] = update_url_query(decrypt_info['URI'], extra_query)
249 if decrypt_url != decrypt_info['URI']:
250 decrypt_info['KEY'] = None
251
252 elif line.startswith('#EXT-X-MEDIA-SEQUENCE'):
253 media_sequence = int(line[22:])
254 elif line.startswith('#EXT-X-BYTERANGE'):
255 splitted_byte_range = line[17:].split('@')
256 sub_range_start = int(splitted_byte_range[1]) if len(splitted_byte_range) == 2 else byte_range['end']
257 byte_range = {
258 'start': sub_range_start,
259 'end': sub_range_start + int(splitted_byte_range[0]),
260 }
261 elif is_ad_fragment_start(line):
262 ad_frag_next = True
263 elif is_ad_fragment_end(line):
264 ad_frag_next = False
265 elif line.startswith('#EXT-X-DISCONTINUITY'):
266 discontinuity_count += 1
267 i += 1
268
269 # We only download the first fragment during the test
270 if self.params.get('test', False):
271 fragments = [fragments[0] if fragments else None]
272
273 if real_downloader:
274 info_dict['fragments'] = fragments
275 fd = real_downloader(self.ydl, self.params)
276 # TODO: Make progress updates work without hooking twice
277 # for ph in self._progress_hooks:
278 # fd.add_progress_hook(ph)
279 return fd.real_download(filename, info_dict)
280
281 if is_webvtt:
282 def pack_fragment(frag_content, frag_index):
283 output = io.StringIO()
284 adjust = 0
285 overflow = False
286 mpegts_last = None
287 for block in webvtt.parse_fragment(frag_content):
288 if isinstance(block, webvtt.CueBlock):
289 extra_state['webvtt_mpegts_last'] = mpegts_last
290 if overflow:
291 extra_state['webvtt_mpegts_adjust'] += 1
292 overflow = False
293 block.start += adjust
294 block.end += adjust
295
296 dedup_window = extra_state.setdefault('webvtt_dedup_window', [])
297
298 ready = []
299
300 i = 0
301 is_new = True
302 while i < len(dedup_window):
303 wcue = dedup_window[i]
304 wblock = webvtt.CueBlock.from_json(wcue)
305 i += 1
306 if wblock.hinges(block):
307 wcue['end'] = block.end
308 is_new = False
309 continue
310 if wblock == block:
311 is_new = False
312 continue
313 if wblock.end > block.start:
314 continue
315 ready.append(wblock)
316 i -= 1
317 del dedup_window[i]
318
319 if is_new:
320 dedup_window.append(block.as_json)
321 for block in ready:
322 block.write_into(output)
323
324 # we only emit cues once they fall out of the duplicate window
325 continue
326 elif isinstance(block, webvtt.Magic):
327 # take care of MPEG PES timestamp overflow
328 if block.mpegts is None:
329 block.mpegts = 0
330 extra_state.setdefault('webvtt_mpegts_adjust', 0)
331 block.mpegts += extra_state['webvtt_mpegts_adjust'] << 33
332 if block.mpegts < extra_state.get('webvtt_mpegts_last', 0):
333 overflow = True
334 block.mpegts += 1 << 33
335 mpegts_last = block.mpegts
336
337 if frag_index == 1:
338 extra_state['webvtt_mpegts'] = block.mpegts or 0
339 extra_state['webvtt_local'] = block.local or 0
340 # XXX: block.local = block.mpegts = None ?
341 else:
342 if block.mpegts is not None and block.local is not None:
343 adjust = (
344 (block.mpegts - extra_state.get('webvtt_mpegts', 0))
345 - (block.local - extra_state.get('webvtt_local', 0))
346 )
347 continue
348 elif isinstance(block, webvtt.HeaderBlock):
349 if frag_index != 1:
350 # XXX: this should probably be silent as well
351 # or verify that all segments contain the same data
352 self.report_warning(bug_reports_message(
353 f'Discarding a {type(block).__name__} block found in the middle of the stream; '
354 'if the subtitles display incorrectly,'))
355 continue
356 block.write_into(output)
357
358 return output.getvalue().encode()
359
360 def fin_fragments():
361 dedup_window = extra_state.get('webvtt_dedup_window')
362 if not dedup_window:
363 return b''
364
365 output = io.StringIO()
366 for cue in dedup_window:
367 webvtt.CueBlock.from_json(cue).write_into(output)
368
369 return output.getvalue().encode()
370
371 if len(fragments) == 1:
372 self.download_and_append_fragments(ctx, fragments, info_dict)
373 else:
374 self.download_and_append_fragments(
375 ctx, fragments, info_dict, pack_func=pack_fragment, finish_func=fin_fragments)
376 else:
377 return self.download_and_append_fragments(ctx, fragments, info_dict)