1 from __future__
import unicode_literals
7 from Crypto
.Cipher
import AES
8 can_decrypt_frag
= True
10 can_decrypt_frag
= False
12 import concurrent
.futures
13 can_threaded_download
= True
15 can_threaded_download
= False
17 from ..downloader
import _get_real_downloader
18 from .fragment
import FragmentFD
19 from .external
import FFmpegFD
21 from ..compat
import (
27 parse_m3u8_attributes
,
33 class HlsFD(FragmentFD
):
35 Download segments in a m3u8 manifest. External downloaders can take over
36 the fragment downloads by supporting the 'm3u8_frag_urls' protocol and
37 re-defining 'supports_manifest' function
43 def can_download(manifest
, info_dict
, allow_unplayable_formats
=False, with_crypto
=can_decrypt_frag
):
44 UNSUPPORTED_FEATURES
= [
45 # r'#EXT-X-BYTERANGE', # playlists composed of byte ranges of media files [2]
47 # Live streams heuristic does not always work (e.g. geo restricted to Germany
48 # 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)
49 # r'#EXT-X-MEDIA-SEQUENCE:(?!0$)', # live streams [3]
51 # This heuristic also is not correct since segments may not be appended as well.
52 # Twitch vods of finished streams have EXT-X-PLAYLIST-TYPE:EVENT despite
53 # no segments will definitely be appended to the end of the playlist.
54 # r'#EXT-X-PLAYLIST-TYPE:EVENT', # media segments may be appended to the end of
55 # # event media playlists [4]
56 # r'#EXT-X-MAP:', # media initialization [5]
57 # 1. https://tools.ietf.org/html/draft-pantos-http-live-streaming-17#section-4.3.2.4
58 # 2. https://tools.ietf.org/html/draft-pantos-http-live-streaming-17#section-4.3.2.2
59 # 3. https://tools.ietf.org/html/draft-pantos-http-live-streaming-17#section-4.3.3.2
60 # 4. https://tools.ietf.org/html/draft-pantos-http-live-streaming-17#section-4.3.3.5
61 # 5. https://tools.ietf.org/html/draft-pantos-http-live-streaming-17#section-4.3.2.5
63 if not allow_unplayable_formats
:
64 UNSUPPORTED_FEATURES
+= [
65 r
'#EXT-X-KEY:METHOD=(?!NONE|AES-128)', # encrypted streams [1]
69 yield not info_dict
.get('is_live')
70 is_aes128_enc
= '#EXT-X-KEY:METHOD=AES-128' in manifest
71 yield with_crypto
or not is_aes128_enc
72 yield not (is_aes128_enc
and r
'#EXT-X-BYTERANGE' in manifest
)
73 for feature
in UNSUPPORTED_FEATURES
:
74 yield not re
.search(feature
, manifest
)
75 return all(check_results())
77 def real_download(self
, filename
, info_dict
):
78 man_url
= info_dict
['url']
79 self
.to_screen('[%s] Downloading m3u8 manifest' % self
.FD_NAME
)
81 urlh
= self
.ydl
.urlopen(self
._prepare
_url
(info_dict
, man_url
))
82 man_url
= urlh
.geturl()
83 s
= urlh
.read().decode('utf-8', 'ignore')
85 if not self
.can_download(s
, info_dict
, self
.params
.get('allow_unplayable_formats')):
86 if info_dict
.get('extra_param_to_segment_url') or info_dict
.get('_decryption_key_url'):
87 self
.report_error('pycryptodome not found. Please install')
89 if self
.can_download(s
, info_dict
, with_crypto
=True):
90 self
.report_warning('pycryptodome is needed to download this file natively')
91 fd
= FFmpegFD(self
.ydl
, self
.params
)
93 '%s detected unsupported features; extraction will be delegated to %s' % (self
.FD_NAME
, fd
.get_basename()))
94 # TODO: Make progress updates work without hooking twice
95 # for ph in self._progress_hooks:
96 # fd.add_progress_hook(ph)
97 return fd
.real_download(filename
, info_dict
)
99 real_downloader
= _get_real_downloader(info_dict
, 'm3u8_frag_urls', self
.params
, None)
100 if real_downloader
and not real_downloader
.supports_manifest(s
):
101 real_downloader
= None
104 '[%s] Fragment downloads will be delegated to %s' % (self
.FD_NAME
, real_downloader
.get_basename()))
106 def is_ad_fragment_start(s
):
107 return (s
.startswith('#ANVATO-SEGMENT-INFO') and 'type=ad' in s
108 or s
.startswith('#UPLYNK-SEGMENT') and s
.endswith(',ad'))
110 def is_ad_fragment_end(s
):
111 return (s
.startswith('#ANVATO-SEGMENT-INFO') and 'type=master' in s
112 or s
.startswith('#UPLYNK-SEGMENT') and s
.endswith(',segment'))
119 for line
in s
.splitlines():
123 if line
.startswith('#'):
124 if is_ad_fragment_start(line
):
126 elif is_ad_fragment_end(line
):
135 'filename': filename
,
136 'total_frags': media_frags
,
137 'ad_frags': ad_frags
,
141 self
._prepare
_external
_frag
_download
(ctx
)
143 self
._prepare
_and
_start
_frag
_download
(ctx
)
145 fragment_retries
= self
.params
.get('fragment_retries', 0)
146 skip_unavailable_fragments
= self
.params
.get('skip_unavailable_fragments', True)
147 test
= self
.params
.get('test', False)
149 format_index
= info_dict
.get('format_index')
151 extra_param_to_segment_url
= info_dict
.get('extra_param_to_segment_url')
152 if extra_param_to_segment_url
:
153 extra_query
= compat_urlparse
.parse_qs(extra_param_to_segment_url
)
156 decrypt_info
= {'METHOD': 'NONE'}
158 discontinuity_count
= 0
161 for line
in s
.splitlines():
164 if not line
.startswith('#'):
165 if format_index
and discontinuity_count
!= format_index
:
170 if frag_index
<= ctx
['fragment_index']:
174 if re
.match(r
'^https?://', line
)
175 else compat_urlparse
.urljoin(man_url
, line
))
177 frag_url
= update_url_query(frag_url
, extra_query
)
180 'frag_index': frag_index
,
182 'decrypt_info': decrypt_info
,
183 'byte_range': byte_range
,
184 'media_sequence': media_sequence
,
187 elif line
.startswith('#EXT-X-MAP'):
188 if format_index
and discontinuity_count
!= format_index
:
192 'Initialization fragment found after media fragments, unable to download')
195 map_info
= parse_m3u8_attributes(line
[11:])
198 if re
.match(r
'^https?://', map_info
.get('URI'))
199 else compat_urlparse
.urljoin(man_url
, map_info
.get('URI')))
201 frag_url
= update_url_query(frag_url
, extra_query
)
204 'frag_index': frag_index
,
206 'decrypt_info': decrypt_info
,
207 'byte_range': byte_range
,
208 'media_sequence': media_sequence
211 if map_info
.get('BYTERANGE'):
212 splitted_byte_range
= map_info
.get('BYTERANGE').split('@')
213 sub_range_start
= int(splitted_byte_range
[1]) if len(splitted_byte_range
) == 2 else byte_range
['end']
215 'start': sub_range_start
,
216 'end': sub_range_start
+ int(splitted_byte_range
[0]),
219 elif line
.startswith('#EXT-X-KEY'):
220 decrypt_url
= decrypt_info
.get('URI')
221 decrypt_info
= parse_m3u8_attributes(line
[11:])
222 if decrypt_info
['METHOD'] == 'AES-128':
223 if 'IV' in decrypt_info
:
224 decrypt_info
['IV'] = binascii
.unhexlify(decrypt_info
['IV'][2:].zfill(32))
225 if not re
.match(r
'^https?://', decrypt_info
['URI']):
226 decrypt_info
['URI'] = compat_urlparse
.urljoin(
227 man_url
, decrypt_info
['URI'])
229 decrypt_info
['URI'] = update_url_query(decrypt_info
['URI'], extra_query
)
230 if decrypt_url
!= decrypt_info
['URI']:
231 decrypt_info
['KEY'] = None
233 elif line
.startswith('#EXT-X-MEDIA-SEQUENCE'):
234 media_sequence
= int(line
[22:])
235 elif line
.startswith('#EXT-X-BYTERANGE'):
236 splitted_byte_range
= line
[17:].split('@')
237 sub_range_start
= int(splitted_byte_range
[1]) if len(splitted_byte_range
) == 2 else byte_range
['end']
239 'start': sub_range_start
,
240 'end': sub_range_start
+ int(splitted_byte_range
[0]),
242 elif is_ad_fragment_start(line
):
244 elif is_ad_fragment_end(line
):
246 elif line
.startswith('#EXT-X-DISCONTINUITY'):
247 discontinuity_count
+= 1
251 # We only download the first fragment during the test
253 fragments
= [fragments
[0] if fragments
else None]
256 info_copy
= info_dict
.copy()
257 info_copy
['fragments'] = fragments
258 fd
= real_downloader(self
.ydl
, self
.params
)
259 # TODO: Make progress updates work without hooking twice
260 # for ph in self._progress_hooks:
261 # fd.add_progress_hook(ph)
262 success
= fd
.real_download(filename
, info_copy
)
266 def download_fragment(fragment
):
267 frag_index
= fragment
['frag_index']
268 frag_url
= fragment
['url']
269 decrypt_info
= fragment
['decrypt_info']
270 byte_range
= fragment
['byte_range']
271 media_sequence
= fragment
['media_sequence']
273 ctx
['fragment_index'] = frag_index
276 headers
= info_dict
.get('http_headers', {})
278 headers
['Range'] = 'bytes=%d-%d' % (byte_range
['start'], byte_range
['end'] - 1)
279 while count
<= fragment_retries
:
281 success
, frag_content
= self
._download
_fragment
(
282 ctx
, frag_url
, info_dict
, headers
)
284 return False, frag_index
286 except compat_urllib_error
.HTTPError
as err
:
287 # Unavailable (possibly temporary) fragments may be served.
288 # First we try to retry then either skip or abort.
289 # See https://github.com/ytdl-org/youtube-dl/issues/10165,
290 # https://github.com/ytdl-org/youtube-dl/issues/10448).
292 if count
<= fragment_retries
:
293 self
.report_retry_fragment(err
, frag_index
, count
, fragment_retries
)
294 if count
> fragment_retries
:
295 self
.report_error('Giving up after %s fragment retries' % fragment_retries
)
296 return False, frag_index
298 if decrypt_info
['METHOD'] == 'AES-128':
299 iv
= decrypt_info
.get('IV') or compat_struct_pack('>8xq', media_sequence
)
300 decrypt_info
['KEY'] = decrypt_info
.get('KEY') or self
.ydl
.urlopen(
301 self
._prepare
_url
(info_dict
, info_dict
.get('_decryption_key_url') or decrypt_info
['URI'])).read()
302 # Don't decrypt the content in tests since the data is explicitly truncated and it's not to a valid block
303 # size (see https://github.com/ytdl-org/youtube-dl/pull/27660). Tests only care that the correct data downloaded,
304 # not what it decrypts to.
306 frag_content
= AES
.new(
307 decrypt_info
['KEY'], AES
.MODE_CBC
, iv
).decrypt(frag_content
)
309 return frag_content
, frag_index
311 def append_fragment(frag_content
, frag_index
):
313 fragment_filename
= '%s-Frag%d' % (ctx
['tmpfilename'], frag_index
)
315 file, frag_sanitized
= sanitize_open(fragment_filename
, 'rb')
316 ctx
['fragment_filename_sanitized'] = frag_sanitized
318 self
._append
_fragment
(ctx
, frag_content
)
320 except EnvironmentError as ose
:
321 if ose
.errno
!= errno
.ENOENT
:
324 if skip_unavailable_fragments
:
325 self
.report_skip_fragment(frag_index
)
329 'fragment %s not found, unable to continue' % frag_index
)
332 if skip_unavailable_fragments
:
333 self
.report_skip_fragment(frag_index
)
337 'fragment %s not found, unable to continue' % frag_index
)
340 max_workers
= self
.params
.get('concurrent_fragment_downloads', 1)
341 if can_threaded_download
and max_workers
> 1:
342 self
.report_warning('The download speed shown is only of one thread. This is a known issue')
343 with concurrent
.futures
.ThreadPoolExecutor(max_workers
) as pool
:
344 futures
= [pool
.submit(download_fragment
, fragment
) for fragment
in fragments
]
345 # timeout must be 0 to return instantly
346 done
, not_done
= concurrent
.futures
.wait(futures
, timeout
=0)
349 # Check every 1 second for KeyboardInterrupt
350 freshly_done
, not_done
= concurrent
.futures
.wait(not_done
, timeout
=1)
352 except KeyboardInterrupt:
353 for future
in not_done
:
355 # timeout must be none to cancel
356 concurrent
.futures
.wait(not_done
, timeout
=None)
357 raise KeyboardInterrupt
358 results
= [future
.result() for future
in futures
]
360 for frag_content
, frag_index
in results
:
361 result
= append_fragment(frag_content
, frag_index
)
365 for fragment
in fragments
:
366 frag_content
, frag_index
= download_fragment(fragment
)
367 result
= append_fragment(frag_content
, frag_index
)
371 self
._finish
_frag
_download
(ctx
)