]> jfr.im git - yt-dlp.git/blame - youtube_dlc/downloader/hls.py
Cleanup some code and fix typos
[yt-dlp.git] / youtube_dlc / downloader / hls.py
CommitLineData
f0b5d6af
PH
1from __future__ import unicode_literals
2
f0b5d6af 3import re
e154c651 4import binascii
5try:
6 from Crypto.Cipher import AES
7 can_decrypt_frag = True
8except ImportError:
9 can_decrypt_frag = False
3bc2ddcc 10
5219cb3e 11from ..downloader import _get_real_downloader
f9a5affa 12from .fragment import FragmentFD
0d66bd0e 13from .external import FFmpegFD
f9a5affa 14
e154c651 15from ..compat import (
25afc2a7 16 compat_urllib_error,
e154c651 17 compat_urlparse,
18 compat_struct_pack,
19)
1cc79574 20from ..utils import (
e154c651 21 parse_m3u8_attributes,
aaf44a2f 22 update_url_query,
3bc2ddcc
JMF
23)
24
25
12b84ac8 26class HlsFD(FragmentFD):
27 """ A limited implementation that does not require ffmpeg """
f0b5d6af 28
f9a5affa
S
29 FD_NAME = 'hlsnative'
30
0d66bd0e 31 @staticmethod
63ad4d43 32 def can_download(manifest, info_dict, allow_unplayable_formats=False):
33 UNSUPPORTED_FEATURES = [
f5974637 34 # r'#EXT-X-BYTERANGE', # playlists composed of byte ranges of media files [2]
1e236d7e 35
c15c47d1
S
36 # Live streams heuristic does not always work (e.g. geo restricted to Germany
37 # 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)
2937590e 38 # r'#EXT-X-MEDIA-SEQUENCE:(?!0$)', # live streams [3]
1e236d7e
S
39
40 # This heuristic also is not correct since segments may not be appended as well.
633b444f
S
41 # Twitch vods of finished streams have EXT-X-PLAYLIST-TYPE:EVENT despite
42 # no segments will definitely be appended to the end of the playlist.
1e236d7e 43 # r'#EXT-X-PLAYLIST-TYPE:EVENT', # media segments may be appended to the end of
51c4d85c 44 # # event media playlists [4]
29f7c58a 45 r'#EXT-X-MAP:', # media initialization [5]
1e236d7e 46
0d66bd0e
S
47 # 1. https://tools.ietf.org/html/draft-pantos-http-live-streaming-17#section-4.3.2.4
48 # 2. https://tools.ietf.org/html/draft-pantos-http-live-streaming-17#section-4.3.2.2
49 # 3. https://tools.ietf.org/html/draft-pantos-http-live-streaming-17#section-4.3.3.2
6104cc29 50 # 4. https://tools.ietf.org/html/draft-pantos-http-live-streaming-17#section-4.3.3.5
29f7c58a 51 # 5. https://tools.ietf.org/html/draft-pantos-http-live-streaming-17#section-4.3.2.5
63ad4d43 52 ]
53 if not allow_unplayable_formats:
54 UNSUPPORTED_FEATURES += [
55 r'#EXT-X-KEY:METHOD=(?!NONE|AES-128)', # encrypted streams [1]
56 ]
e154c651 57 check_results = [not re.search(feature, manifest) for feature in UNSUPPORTED_FEATURES]
f5974637
RA
58 is_aes128_enc = '#EXT-X-KEY:METHOD=AES-128' in manifest
59 check_results.append(can_decrypt_frag or not is_aes128_enc)
60 check_results.append(not (is_aes128_enc and r'#EXT-X-BYTERANGE' in manifest))
2bfaf89b 61 check_results.append(not info_dict.get('is_live'))
e154c651 62 return all(check_results)
0d66bd0e 63
f0b5d6af 64 def real_download(self, filename, info_dict):
f9a5affa
S
65 man_url = info_dict['url']
66 self.to_screen('[%s] Downloading m3u8 manifest' % self.FD_NAME)
69035555 67
c5a49ff0
S
68 urlh = self.ydl.urlopen(self._prepare_url(info_dict, man_url))
69 man_url = urlh.geturl()
70 s = urlh.read().decode('utf-8', 'ignore')
0d66bd0e 71
63ad4d43 72 if not self.can_download(s, info_dict, self.params.get('allow_unplayable_formats')):
c712b16d 73 if info_dict.get('extra_param_to_segment_url') or info_dict.get('_decryption_key_url'):
bfa1073e
RA
74 self.report_error('pycrypto not found. Please install it.')
75 return False
2bfaf89b
RA
76 self.report_warning(
77 'hlsnative has detected features it does not support, '
78 'extraction will be delegated to ffmpeg')
79 fd = FFmpegFD(self.ydl, self.params)
5219cb3e 80 # TODO: Make progress updates work without hooking twice
81 # for ph in self._progress_hooks:
82 # fd.add_progress_hook(ph)
2bfaf89b 83 return fd.real_download(filename, info_dict)
0d66bd0e 84
5219cb3e 85 real_downloader = _get_real_downloader(info_dict, 'frag_urls', self.params, None)
86
f1ab3b7d 87 def is_ad_fragment_start(s):
3089bc74
S
88 return (s.startswith('#ANVATO-SEGMENT-INFO') and 'type=ad' in s
89 or s.startswith('#UPLYNK-SEGMENT') and s.endswith(',ad'))
74c42d9e 90
f1ab3b7d 91 def is_ad_fragment_end(s):
3089bc74
S
92 return (s.startswith('#ANVATO-SEGMENT-INFO') and 'type=master' in s
93 or s.startswith('#UPLYNK-SEGMENT') and s.endswith(',segment'))
f1ab3b7d 94
5219cb3e 95 fragment_urls = []
96
74c42d9e
S
97 media_frags = 0
98 ad_frags = 0
99 ad_frag_next = False
f0b5d6af
PH
100 for line in s.splitlines():
101 line = line.strip()
74c42d9e
S
102 if not line:
103 continue
104 if line.startswith('#'):
f1ab3b7d 105 if is_ad_fragment_start(line):
a9ee4f6e 106 ad_frag_next = True
f1ab3b7d
RA
107 elif is_ad_fragment_end(line):
108 ad_frag_next = False
74c42d9e
S
109 continue
110 if ad_frag_next:
f1ab3b7d 111 ad_frags += 1
74c42d9e
S
112 continue
113 media_frags += 1
f0b5d6af 114
f9a5affa 115 ctx = {
f0b5d6af 116 'filename': filename,
74c42d9e
S
117 'total_frags': media_frags,
118 'ad_frags': ad_frags,
f9a5affa
S
119 }
120
5219cb3e 121 if real_downloader:
122 self._prepare_external_frag_download(ctx)
123 else:
124 self._prepare_and_start_frag_download(ctx)
f9a5affa 125
25afc2a7
S
126 fragment_retries = self.params.get('fragment_retries', 0)
127 skip_unavailable_fragments = self.params.get('skip_unavailable_fragments', True)
128 test = self.params.get('test', False)
129
b8079a40 130 extra_query = None
aaf44a2f 131 extra_param_to_segment_url = info_dict.get('extra_param_to_segment_url')
b8079a40
RA
132 if extra_param_to_segment_url:
133 extra_query = compat_urlparse.parse_qs(extra_param_to_segment_url)
e154c651 134 i = 0
135 media_sequence = 0
136 decrypt_info = {'METHOD': 'NONE'}
f5974637 137 byte_range = {}
75a24854 138 frag_index = 0
74c42d9e 139 ad_frag_next = False
e154c651 140 for line in s.splitlines():
141 line = line.strip()
142 if line:
143 if not line.startswith('#'):
74c42d9e 144 if ad_frag_next:
74c42d9e 145 continue
75a24854 146 frag_index += 1
3e0304fe 147 if frag_index <= ctx['fragment_index']:
75a24854 148 continue
e154c651 149 frag_url = (
150 line
151 if re.match(r'^https?://', line)
152 else compat_urlparse.urljoin(man_url, line))
b8079a40
RA
153 if extra_query:
154 frag_url = update_url_query(frag_url, extra_query)
5219cb3e 155
156 if real_downloader:
157 fragment_urls.append(frag_url)
158 continue
159
25afc2a7 160 count = 0
f5974637
RA
161 headers = info_dict.get('http_headers', {})
162 if byte_range:
6e65a2a6 163 headers['Range'] = 'bytes=%d-%d' % (byte_range['start'], byte_range['end'] - 1)
25afc2a7
S
164 while count <= fragment_retries:
165 try:
75a24854
RA
166 success, frag_content = self._download_fragment(
167 ctx, frag_url, info_dict, headers)
25afc2a7
S
168 if not success:
169 return False
25afc2a7 170 break
2e99cd30 171 except compat_urllib_error.HTTPError as err:
25afc2a7
S
172 # Unavailable (possibly temporary) fragments may be served.
173 # First we try to retry then either skip or abort.
067aa17e
S
174 # See https://github.com/ytdl-org/youtube-dl/issues/10165,
175 # https://github.com/ytdl-org/youtube-dl/issues/10448).
25afc2a7
S
176 count += 1
177 if count <= fragment_retries:
75a24854 178 self.report_retry_fragment(err, frag_index, count, fragment_retries)
25afc2a7
S
179 if count > fragment_retries:
180 if skip_unavailable_fragments:
181 i += 1
182 media_sequence += 1
75a24854 183 self.report_skip_fragment(frag_index)
25afc2a7
S
184 continue
185 self.report_error(
186 'giving up after %s fragment retries' % fragment_retries)
e154c651 187 return False
5219cb3e 188
e154c651 189 if decrypt_info['METHOD'] == 'AES-128':
8369a4fe 190 iv = decrypt_info.get('IV') or compat_struct_pack('>8xq', media_sequence)
538d4f86 191 decrypt_info['KEY'] = decrypt_info.get('KEY') or self.ydl.urlopen(
c712b16d 192 self._prepare_url(info_dict, info_dict.get('_decryption_key_url') or decrypt_info['URI'])).read()
00dd0cd5 193 # Don't decrypt the content in tests since the data is explicitly truncated and it's not to a valid block
194 # size (see https://github.com/ytdl-org/youtube-dl/pull/27660). Tests only care that the correct data downloaded,
195 # not what it decrypts to.
196 if not test:
197 frag_content = AES.new(
198 decrypt_info['KEY'], AES.MODE_CBC, iv).decrypt(frag_content)
75a24854 199 self._append_fragment(ctx, frag_content)
e154c651 200 # We only download the first fragment during the test
25afc2a7 201 if test:
e154c651 202 break
203 i += 1
204 media_sequence += 1
205 elif line.startswith('#EXT-X-KEY'):
75a24854 206 decrypt_url = decrypt_info.get('URI')
e154c651 207 decrypt_info = parse_m3u8_attributes(line[11:])
208 if decrypt_info['METHOD'] == 'AES-128':
209 if 'IV' in decrypt_info:
07ea9c9b 210 decrypt_info['IV'] = binascii.unhexlify(decrypt_info['IV'][2:].zfill(32))
e154c651 211 if not re.match(r'^https?://', decrypt_info['URI']):
8369a4fe
S
212 decrypt_info['URI'] = compat_urlparse.urljoin(
213 man_url, decrypt_info['URI'])
b8079a40
RA
214 if extra_query:
215 decrypt_info['URI'] = update_url_query(decrypt_info['URI'], extra_query)
75a24854
RA
216 if decrypt_url != decrypt_info['URI']:
217 decrypt_info['KEY'] = None
e154c651 218 elif line.startswith('#EXT-X-MEDIA-SEQUENCE'):
219 media_sequence = int(line[22:])
f5974637
RA
220 elif line.startswith('#EXT-X-BYTERANGE'):
221 splitted_byte_range = line[17:].split('@')
222 sub_range_start = int(splitted_byte_range[1]) if len(splitted_byte_range) == 2 else byte_range['end']
223 byte_range = {
224 'start': sub_range_start,
225 'end': sub_range_start + int(splitted_byte_range[0]),
226 }
f1ab3b7d 227 elif is_ad_fragment_start(line):
74c42d9e 228 ad_frag_next = True
f1ab3b7d
RA
229 elif is_ad_fragment_end(line):
230 ad_frag_next = False
f9a5affa 231
5219cb3e 232 if real_downloader:
233 info_copy = info_dict.copy()
234 info_copy['url_list'] = fragment_urls
235 info_copy['decrypt_info'] = decrypt_info
236 fd = real_downloader(self.ydl, self.params)
237 # TODO: Make progress updates work without hooking twice
238 # for ph in self._progress_hooks:
239 # fd.add_progress_hook(ph)
240 success = fd.real_download(filename, info_copy)
241 if not success:
242 return False
243 else:
244 self._finish_frag_download(ctx)
f0b5d6af 245 return True