]>
Commit | Line | Data |
---|---|---|
f0b5d6af PH |
1 | from __future__ import unicode_literals |
2 | ||
12b84ac8 | 3 | import os.path |
f0b5d6af | 4 | import re |
e154c651 | 5 | import binascii |
6 | try: | |
7 | from Crypto.Cipher import AES | |
8 | can_decrypt_frag = True | |
9 | except ImportError: | |
10 | can_decrypt_frag = False | |
3bc2ddcc | 11 | |
f9a5affa | 12 | from .fragment import FragmentFD |
0d66bd0e | 13 | from .external import FFmpegFD |
f9a5affa | 14 | |
e154c651 | 15 | from ..compat import ( |
25afc2a7 | 16 | compat_urllib_error, |
e154c651 | 17 | compat_urlparse, |
18 | compat_struct_pack, | |
19 | ) | |
1cc79574 | 20 | from ..utils import ( |
3bc2ddcc | 21 | encodeFilename, |
fcd9e423 | 22 | sanitize_open, |
e154c651 | 23 | parse_m3u8_attributes, |
aaf44a2f | 24 | update_url_query, |
3bc2ddcc JMF |
25 | ) |
26 | ||
27 | ||
12b84ac8 | 28 | class HlsFD(FragmentFD): |
29 | """ A limited implementation that does not require ffmpeg """ | |
f0b5d6af | 30 | |
f9a5affa S |
31 | FD_NAME = 'hlsnative' |
32 | ||
fb4fc449 RA |
33 | def _delegate_to_ffmpeg(self, filename, info_dict): |
34 | self.report_warning( | |
35 | 'hlsnative has detected features it does not support, ' | |
36 | 'extraction will be delegated to ffmpeg') | |
37 | fd = FFmpegFD(self.ydl, self.params) | |
38 | for ph in self._progress_hooks: | |
39 | fd.add_progress_hook(ph) | |
40 | return fd.real_download(filename, info_dict) | |
41 | ||
0d66bd0e | 42 | @staticmethod |
6f126d90 | 43 | def can_download(manifest, info_dict): |
0d66bd0e | 44 | UNSUPPORTED_FEATURES = ( |
e154c651 | 45 | r'#EXT-X-KEY:METHOD=(?!NONE|AES-128)', # encrypted streams [1] |
0d66bd0e | 46 | r'#EXT-X-BYTERANGE', # playlists composed of byte ranges of media files [2] |
1e236d7e | 47 | |
c15c47d1 S |
48 | # Live streams heuristic does not always work (e.g. geo restricted to Germany |
49 | # 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 | 50 | # r'#EXT-X-MEDIA-SEQUENCE:(?!0$)', # live streams [3] |
1e236d7e S |
51 | |
52 | # This heuristic also is not correct since segments may not be appended as well. | |
633b444f S |
53 | # Twitch vods of finished streams have EXT-X-PLAYLIST-TYPE:EVENT despite |
54 | # no segments will definitely be appended to the end of the playlist. | |
1e236d7e | 55 | # r'#EXT-X-PLAYLIST-TYPE:EVENT', # media segments may be appended to the end of |
51c4d85c | 56 | # # event media playlists [4] |
1e236d7e | 57 | |
0d66bd0e S |
58 | # 1. https://tools.ietf.org/html/draft-pantos-http-live-streaming-17#section-4.3.2.4 |
59 | # 2. https://tools.ietf.org/html/draft-pantos-http-live-streaming-17#section-4.3.2.2 | |
60 | # 3. https://tools.ietf.org/html/draft-pantos-http-live-streaming-17#section-4.3.3.2 | |
6104cc29 | 61 | # 4. https://tools.ietf.org/html/draft-pantos-http-live-streaming-17#section-4.3.3.5 |
0d66bd0e | 62 | ) |
e154c651 | 63 | check_results = [not re.search(feature, manifest) for feature in UNSUPPORTED_FEATURES] |
8369a4fe | 64 | check_results.append(can_decrypt_frag or '#EXT-X-KEY:METHOD=AES-128' not in manifest) |
e154c651 | 65 | return all(check_results) |
0d66bd0e | 66 | |
f0b5d6af | 67 | def real_download(self, filename, info_dict): |
fb4fc449 RA |
68 | if info_dict.get('is_live'): |
69 | return self._delegate_to_ffmpeg(filename, info_dict) | |
70 | ||
f9a5affa S |
71 | man_url = info_dict['url'] |
72 | self.to_screen('[%s] Downloading m3u8 manifest' % self.FD_NAME) | |
69035555 S |
73 | |
74 | manifest = self.ydl.urlopen(self._prepare_url(info_dict, man_url)).read() | |
f0b5d6af | 75 | |
f9a5affa | 76 | s = manifest.decode('utf-8', 'ignore') |
0d66bd0e | 77 | |
6f126d90 | 78 | if not self.can_download(s, info_dict): |
bfa1073e RA |
79 | if info_dict.get('extra_param_to_segment_url'): |
80 | self.report_error('pycrypto not found. Please install it.') | |
81 | return False | |
fb4fc449 | 82 | return self._delegate_to_ffmpeg(filename, info_dict) |
0d66bd0e | 83 | |
e154c651 | 84 | total_frags = 0 |
f0b5d6af PH |
85 | for line in s.splitlines(): |
86 | line = line.strip() | |
87 | if line and not line.startswith('#'): | |
e154c651 | 88 | total_frags += 1 |
f0b5d6af | 89 | |
f9a5affa | 90 | ctx = { |
f0b5d6af | 91 | 'filename': filename, |
e154c651 | 92 | 'total_frags': total_frags, |
f9a5affa S |
93 | } |
94 | ||
95 | self._prepare_and_start_frag_download(ctx) | |
96 | ||
25afc2a7 S |
97 | fragment_retries = self.params.get('fragment_retries', 0) |
98 | skip_unavailable_fragments = self.params.get('skip_unavailable_fragments', True) | |
99 | test = self.params.get('test', False) | |
100 | ||
b8079a40 | 101 | extra_query = None |
aaf44a2f | 102 | extra_param_to_segment_url = info_dict.get('extra_param_to_segment_url') |
b8079a40 RA |
103 | if extra_param_to_segment_url: |
104 | extra_query = compat_urlparse.parse_qs(extra_param_to_segment_url) | |
e154c651 | 105 | i = 0 |
106 | media_sequence = 0 | |
107 | decrypt_info = {'METHOD': 'NONE'} | |
f9a5affa | 108 | frags_filenames = [] |
e154c651 | 109 | for line in s.splitlines(): |
110 | line = line.strip() | |
111 | if line: | |
112 | if not line.startswith('#'): | |
113 | frag_url = ( | |
114 | line | |
115 | if re.match(r'^https?://', line) | |
116 | else compat_urlparse.urljoin(man_url, line)) | |
25afc2a7 S |
117 | frag_name = 'Frag%d' % i |
118 | frag_filename = '%s-%s' % (ctx['tmpfilename'], frag_name) | |
b8079a40 RA |
119 | if extra_query: |
120 | frag_url = update_url_query(frag_url, extra_query) | |
25afc2a7 S |
121 | count = 0 |
122 | while count <= fragment_retries: | |
123 | try: | |
69035555 S |
124 | success = ctx['dl'].download(frag_filename, { |
125 | 'url': frag_url, | |
126 | 'http_headers': info_dict.get('http_headers'), | |
127 | }) | |
25afc2a7 S |
128 | if not success: |
129 | return False | |
130 | down, frag_sanitized = sanitize_open(frag_filename, 'rb') | |
131 | frag_content = down.read() | |
132 | down.close() | |
133 | break | |
2e99cd30 | 134 | except compat_urllib_error.HTTPError as err: |
25afc2a7 S |
135 | # Unavailable (possibly temporary) fragments may be served. |
136 | # First we try to retry then either skip or abort. | |
137 | # See https://github.com/rg3/youtube-dl/issues/10165, | |
138 | # https://github.com/rg3/youtube-dl/issues/10448). | |
139 | count += 1 | |
140 | if count <= fragment_retries: | |
2e99cd30 | 141 | self.report_retry_fragment(err, frag_name, count, fragment_retries) |
25afc2a7 S |
142 | if count > fragment_retries: |
143 | if skip_unavailable_fragments: | |
144 | i += 1 | |
145 | media_sequence += 1 | |
146 | self.report_skip_fragment(frag_name) | |
147 | continue | |
148 | self.report_error( | |
149 | 'giving up after %s fragment retries' % fragment_retries) | |
e154c651 | 150 | return False |
e154c651 | 151 | if decrypt_info['METHOD'] == 'AES-128': |
8369a4fe S |
152 | iv = decrypt_info.get('IV') or compat_struct_pack('>8xq', media_sequence) |
153 | frag_content = AES.new( | |
154 | decrypt_info['KEY'], AES.MODE_CBC, iv).decrypt(frag_content) | |
e154c651 | 155 | ctx['dest_stream'].write(frag_content) |
156 | frags_filenames.append(frag_sanitized) | |
157 | # We only download the first fragment during the test | |
25afc2a7 | 158 | if test: |
e154c651 | 159 | break |
160 | i += 1 | |
161 | media_sequence += 1 | |
162 | elif line.startswith('#EXT-X-KEY'): | |
163 | decrypt_info = parse_m3u8_attributes(line[11:]) | |
164 | if decrypt_info['METHOD'] == 'AES-128': | |
165 | if 'IV' in decrypt_info: | |
07ea9c9b | 166 | decrypt_info['IV'] = binascii.unhexlify(decrypt_info['IV'][2:].zfill(32)) |
e154c651 | 167 | if not re.match(r'^https?://', decrypt_info['URI']): |
8369a4fe S |
168 | decrypt_info['URI'] = compat_urlparse.urljoin( |
169 | man_url, decrypt_info['URI']) | |
b8079a40 RA |
170 | if extra_query: |
171 | decrypt_info['URI'] = update_url_query(decrypt_info['URI'], extra_query) | |
e154c651 | 172 | decrypt_info['KEY'] = self.ydl.urlopen(decrypt_info['URI']).read() |
173 | elif line.startswith('#EXT-X-MEDIA-SEQUENCE'): | |
174 | media_sequence = int(line[22:]) | |
f9a5affa S |
175 | |
176 | self._finish_frag_download(ctx) | |
177 | ||
178 | for frag_file in frags_filenames: | |
fcd9e423 | 179 | os.remove(encodeFilename(frag_file)) |
f9a5affa | 180 | |
f0b5d6af | 181 | return True |