]>
Commit | Line | Data |
---|---|---|
222516d9 PH |
1 | from __future__ import unicode_literals |
2 | ||
3 | import os.path | |
f0298f65 | 4 | import re |
222516d9 | 5 | import subprocess |
12b84ac8 | 6 | import sys |
f0298f65 | 7 | import time |
5219cb3e | 8 | |
9 | try: | |
10 | from Crypto.Cipher import AES | |
11 | can_decrypt_frag = True | |
12 | except ImportError: | |
13 | can_decrypt_frag = False | |
222516d9 PH |
14 | |
15 | from .common import FileDownloader | |
a50862b7 S |
16 | from ..compat import ( |
17 | compat_setenv, | |
18 | compat_str, | |
19 | ) | |
a755f825 | 20 | from ..postprocessor.ffmpeg import FFmpegPostProcessor, EXT_TO_OUT_FORMATS |
222516d9 | 21 | from ..utils import ( |
1195a38f S |
22 | cli_option, |
23 | cli_valueless_option, | |
24 | cli_bool_option, | |
25 | cli_configuration_args, | |
222516d9 | 26 | encodeFilename, |
5219cb3e | 27 | error_to_compat_str, |
74f8654a | 28 | encodeArgument, |
12b84ac8 | 29 | handle_youtubedl_headers, |
99cbe98c | 30 | check_executable, |
8bdc1494 | 31 | is_outdated_version, |
f5b1bca9 | 32 | process_communicate_or_kill, |
5219cb3e | 33 | sanitized_Request, |
539d158c | 34 | sanitize_open, |
222516d9 PH |
35 | ) |
36 | ||
37 | ||
38 | class ExternalFD(FileDownloader): | |
5219cb3e | 39 | SUPPORTED_PROTOCOLS = ('http', 'https', 'ftp', 'ftps') |
40 | ||
222516d9 PH |
41 | def real_download(self, filename, info_dict): |
42 | self.report_destination(filename) | |
43 | tmpfilename = self.temp_name(filename) | |
44 | ||
e7db6759 | 45 | try: |
f0298f65 | 46 | started = time.time() |
e7db6759 S |
47 | retval = self._call_downloader(tmpfilename, info_dict) |
48 | except KeyboardInterrupt: | |
49 | if not info_dict.get('is_live'): | |
50 | raise | |
51 | # Live stream downloading cancellation should be considered as | |
52 | # correct and expected termination thus all postprocessing | |
53 | # should take place | |
54 | retval = 0 | |
55 | self.to_screen('[%s] Interrupted by user' % self.get_basename()) | |
56 | ||
222516d9 | 57 | if retval == 0: |
f0298f65 S |
58 | status = { |
59 | 'filename': filename, | |
60 | 'status': 'finished', | |
61 | 'elapsed': time.time() - started, | |
62 | } | |
63 | if filename != '-': | |
80aa2460 JH |
64 | fsize = os.path.getsize(encodeFilename(tmpfilename)) |
65 | self.to_screen('\r[%s] Downloaded %s bytes' % (self.get_basename(), fsize)) | |
66 | self.try_rename(tmpfilename, filename) | |
f0298f65 | 67 | status.update({ |
80aa2460 JH |
68 | 'downloaded_bytes': fsize, |
69 | 'total_bytes': fsize, | |
80aa2460 | 70 | }) |
f0298f65 | 71 | self._hook_progress(status) |
222516d9 PH |
72 | return True |
73 | else: | |
74 | self.to_stderr('\n') | |
75 | self.report_error('%s exited with code %d' % ( | |
76 | self.get_basename(), retval)) | |
77 | return False | |
78 | ||
79 | @classmethod | |
80 | def get_basename(cls): | |
81 | return cls.__name__[:-2].lower() | |
82 | ||
83 | @property | |
84 | def exe(self): | |
85 | return self.params.get('external_downloader') | |
86 | ||
99cbe98c | 87 | @classmethod |
88 | def available(cls): | |
91ee320b | 89 | return check_executable(cls.get_basename(), [cls.AVAILABLE_OPT]) |
99cbe98c | 90 | |
222516d9 PH |
91 | @classmethod |
92 | def supports(cls, info_dict): | |
5219cb3e | 93 | return info_dict['protocol'] in cls.SUPPORTED_PROTOCOLS |
222516d9 | 94 | |
2cb99ebb | 95 | @classmethod |
96 | def can_download(cls, info_dict): | |
97 | return cls.available() and cls.supports(info_dict) | |
98 | ||
bf812ef7 | 99 | def _option(self, command_option, param): |
1195a38f | 100 | return cli_option(self.params, command_option, param) |
bf812ef7 | 101 | |
266b0ad6 | 102 | def _bool_option(self, command_option, param, true_value='true', false_value='false', separator=None): |
1195a38f | 103 | return cli_bool_option(self.params, command_option, param, true_value, false_value, separator) |
266b0ad6 | 104 | |
dc534b67 | 105 | def _valueless_option(self, command_option, param, expected_value=True): |
1195a38f | 106 | return cli_valueless_option(self.params, command_option, param, expected_value) |
f30c2e8e | 107 | |
c75f0b36 | 108 | def _configuration_args(self, default=[]): |
eab9b2bc | 109 | return cli_configuration_args( |
110 | self.params, 'external_downloader_args', self.get_basename(), default)[0] | |
c75f0b36 | 111 | |
222516d9 PH |
112 | def _call_downloader(self, tmpfilename, info_dict): |
113 | """ Either overwrite this or implement _make_cmd """ | |
74f8654a | 114 | cmd = [encodeArgument(a) for a in self._make_cmd(tmpfilename, info_dict)] |
222516d9 | 115 | |
74f8654a | 116 | self._debug_cmd(cmd) |
222516d9 PH |
117 | |
118 | p = subprocess.Popen( | |
384b6202 | 119 | cmd, stderr=subprocess.PIPE) |
f5b1bca9 | 120 | _, stderr = process_communicate_or_kill(p) |
222516d9 | 121 | if p.returncode != 0: |
e69f9f5d | 122 | self.to_stderr(stderr.decode('utf-8', 'replace')) |
5219cb3e | 123 | |
124 | if 'url_list' in info_dict: | |
125 | file_list = [] | |
126 | for [i, url] in enumerate(info_dict['url_list']): | |
127 | tmpsegmentname = '%s_%s.frag' % (tmpfilename, i) | |
128 | file_list.append(tmpsegmentname) | |
539d158c | 129 | dest, _ = sanitize_open(tmpfilename, 'wb') |
130 | for i in file_list: | |
131 | src, _ = sanitize_open(i, 'rb') | |
132 | if 'decrypt_info' in info_dict: | |
133 | decrypt_info = info_dict['decrypt_info'] | |
134 | if decrypt_info['METHOD'] == 'AES-128': | |
135 | iv = decrypt_info.get('IV') | |
136 | decrypt_info['KEY'] = decrypt_info.get('KEY') or self.ydl.urlopen( | |
137 | self._prepare_url(info_dict, info_dict.get('_decryption_key_url') or decrypt_info['URI'])).read() | |
138 | encrypted_data = src.read() | |
139 | decrypted_data = AES.new( | |
140 | decrypt_info['KEY'], AES.MODE_CBC, iv).decrypt(encrypted_data) | |
141 | dest.write(decrypted_data) | |
5219cb3e | 142 | else: |
539d158c | 143 | fragment_data = src.read() |
144 | dest.write(fragment_data) | |
145 | else: | |
146 | fragment_data = src.read() | |
147 | dest.write(fragment_data) | |
148 | src.close() | |
149 | dest.close() | |
5219cb3e | 150 | if not self.params.get('keep_fragments', False): |
151 | for file_path in file_list: | |
152 | try: | |
153 | os.remove(file_path) | |
154 | except OSError as ose: | |
155 | self.report_error("Unable to delete file %s; %s" % (file_path, error_to_compat_str(ose))) | |
156 | try: | |
157 | file_path = '%s.frag.urls' % tmpfilename | |
158 | os.remove(file_path) | |
159 | except OSError as ose: | |
160 | self.report_error("Unable to delete file %s; %s" % (file_path, error_to_compat_str(ose))) | |
161 | ||
222516d9 PH |
162 | return p.returncode |
163 | ||
5219cb3e | 164 | def _prepare_url(self, info_dict, url): |
165 | headers = info_dict.get('http_headers') | |
166 | return sanitized_Request(url, None, headers) if headers else url | |
167 | ||
222516d9 | 168 | |
384b6202 | 169 | class CurlFD(ExternalFD): |
91ee320b | 170 | AVAILABLE_OPT = '-V' |
99cbe98c | 171 | |
384b6202 | 172 | def _make_cmd(self, tmpfilename, info_dict): |
163d9667 | 173 | cmd = [self.exe, '--location', '-o', tmpfilename] |
002ea8fe | 174 | if info_dict.get('http_headers') is not None: |
175 | for key, val in info_dict['http_headers'].items(): | |
176 | cmd += ['--header', '%s: %s' % (key, val)] | |
177 | ||
98e698f1 RA |
178 | cmd += self._bool_option('--continue-at', 'continuedl', '-', '0') |
179 | cmd += self._valueless_option('--silent', 'noprogress') | |
180 | cmd += self._valueless_option('--verbose', 'verbose') | |
181 | cmd += self._option('--limit-rate', 'ratelimit') | |
37b239b3 S |
182 | retry = self._option('--retry', 'retries') |
183 | if len(retry) == 2: | |
184 | if retry[1] in ('inf', 'infinite'): | |
185 | retry[1] = '2147483647' | |
186 | cmd += retry | |
98e698f1 | 187 | cmd += self._option('--max-filesize', 'max_filesize') |
9f3da138 | 188 | cmd += self._option('--interface', 'source_address') |
e7a8c303 | 189 | cmd += self._option('--proxy', 'proxy') |
dc534b67 | 190 | cmd += self._valueless_option('--insecure', 'nocheckcertificate') |
c75f0b36 | 191 | cmd += self._configuration_args() |
384b6202 PH |
192 | cmd += ['--', info_dict['url']] |
193 | return cmd | |
194 | ||
98e698f1 RA |
195 | def _call_downloader(self, tmpfilename, info_dict): |
196 | cmd = [encodeArgument(a) for a in self._make_cmd(tmpfilename, info_dict)] | |
197 | ||
198 | self._debug_cmd(cmd) | |
199 | ||
acfccaca | 200 | # curl writes the progress to stderr so don't capture it. |
98e698f1 | 201 | p = subprocess.Popen(cmd) |
f5b1bca9 | 202 | process_communicate_or_kill(p) |
98e698f1 RA |
203 | return p.returncode |
204 | ||
384b6202 | 205 | |
e0ac5214 | 206 | class AxelFD(ExternalFD): |
91ee320b | 207 | AVAILABLE_OPT = '-V' |
99cbe98c | 208 | |
e0ac5214 | 209 | def _make_cmd(self, tmpfilename, info_dict): |
210 | cmd = [self.exe, '-o', tmpfilename] | |
002ea8fe | 211 | if info_dict.get('http_headers') is not None: |
212 | for key, val in info_dict['http_headers'].items(): | |
213 | cmd += ['-H', '%s: %s' % (key, val)] | |
e0ac5214 | 214 | cmd += self._configuration_args() |
215 | cmd += ['--', info_dict['url']] | |
216 | return cmd | |
217 | ||
218 | ||
222516d9 | 219 | class WgetFD(ExternalFD): |
91ee320b | 220 | AVAILABLE_OPT = '--version' |
99cbe98c | 221 | |
222516d9 PH |
222 | def _make_cmd(self, tmpfilename, info_dict): |
223 | cmd = [self.exe, '-O', tmpfilename, '-nv', '--no-cookies'] | |
002ea8fe | 224 | if info_dict.get('http_headers') is not None: |
225 | for key, val in info_dict['http_headers'].items(): | |
226 | cmd += ['--header', '%s: %s' % (key, val)] | |
8c80603f S |
227 | cmd += self._option('--limit-rate', 'ratelimit') |
228 | retry = self._option('--tries', 'retries') | |
229 | if len(retry) == 2: | |
230 | if retry[1] in ('inf', 'infinite'): | |
231 | retry[1] = '0' | |
232 | cmd += retry | |
9f3da138 | 233 | cmd += self._option('--bind-address', 'source_address') |
bf812ef7 | 234 | cmd += self._option('--proxy', 'proxy') |
dc534b67 | 235 | cmd += self._valueless_option('--no-check-certificate', 'nocheckcertificate') |
c75f0b36 | 236 | cmd += self._configuration_args() |
222516d9 PH |
237 | cmd += ['--', info_dict['url']] |
238 | return cmd | |
239 | ||
240 | ||
384b6202 | 241 | class Aria2cFD(ExternalFD): |
91ee320b | 242 | AVAILABLE_OPT = '-v' |
5219cb3e | 243 | SUPPORTED_PROTOCOLS = ('http', 'https', 'ftp', 'ftps', 'frag_urls') |
99cbe98c | 244 | |
384b6202 | 245 | def _make_cmd(self, tmpfilename, info_dict): |
c75f0b36 | 246 | cmd = [self.exe, '-c'] |
384b6202 | 247 | dn = os.path.dirname(tmpfilename) |
5219cb3e | 248 | if 'url_list' not in info_dict: |
249 | cmd += ['--out', os.path.basename(tmpfilename)] | |
250 | verbose_level_args = ['--console-log-level=warn', '--summary-interval=0'] | |
251 | cmd += self._configuration_args(['--file-allocation=none', '-x16', '-j16', '-s16'] + verbose_level_args) | |
384b6202 PH |
252 | if dn: |
253 | cmd += ['--dir', dn] | |
002ea8fe | 254 | if info_dict.get('http_headers') is not None: |
255 | for key, val in info_dict['http_headers'].items(): | |
256 | cmd += ['--header', '%s: %s' % (key, val)] | |
9f3da138 | 257 | cmd += self._option('--interface', 'source_address') |
bf812ef7 | 258 | cmd += self._option('--all-proxy', 'proxy') |
266b0ad6 | 259 | cmd += self._bool_option('--check-certificate', 'nocheckcertificate', 'false', 'true', '=') |
71f47617 | 260 | cmd += self._bool_option('--remote-time', 'updatetime', 'true', 'false', '=') |
5219cb3e | 261 | cmd += ['--auto-file-renaming=false'] |
262 | if 'url_list' in info_dict: | |
263 | cmd += verbose_level_args | |
264 | cmd += ['--uri-selector', 'inorder', '--download-result=hide'] | |
265 | url_list_file = '%s.frag.urls' % tmpfilename | |
266 | url_list = [] | |
267 | for [i, url] in enumerate(info_dict['url_list']): | |
268 | tmpsegmentname = '%s_%s.frag' % (os.path.basename(tmpfilename), i) | |
269 | url_list.append('%s\n\tout=%s' % (url, tmpsegmentname)) | |
539d158c | 270 | stream, _ = sanitize_open(url_list_file, 'wb') |
271 | stream.write('\n'.join(url_list).encode('utf-8')) | |
272 | stream.close() | |
5219cb3e | 273 | |
274 | cmd += ['-i', url_list_file] | |
275 | else: | |
276 | cmd += ['--', info_dict['url']] | |
384b6202 PH |
277 | return cmd |
278 | ||
906e2f0e JMF |
279 | |
280 | class HttpieFD(ExternalFD): | |
99cbe98c | 281 | @classmethod |
282 | def available(cls): | |
283 | return check_executable('http', ['--version']) | |
284 | ||
906e2f0e JMF |
285 | def _make_cmd(self, tmpfilename, info_dict): |
286 | cmd = ['http', '--download', '--output', tmpfilename, info_dict['url']] | |
002ea8fe | 287 | |
288 | if info_dict.get('http_headers') is not None: | |
289 | for key, val in info_dict['http_headers'].items(): | |
290 | cmd += ['%s:%s' % (key, val)] | |
906e2f0e JMF |
291 | return cmd |
292 | ||
12b84ac8 | 293 | |
294 | class FFmpegFD(ExternalFD): | |
5219cb3e | 295 | SUPPORTED_PROTOCOLS = ('http', 'https', 'ftp', 'ftps', 'm3u8', 'rtsp', 'rtmp', 'mms') |
12b84ac8 | 296 | |
99cbe98c | 297 | @classmethod |
298 | def available(cls): | |
299 | return FFmpegPostProcessor().available | |
300 | ||
12b84ac8 | 301 | def _call_downloader(self, tmpfilename, info_dict): |
302 | url = info_dict['url'] | |
303 | ffpp = FFmpegPostProcessor(downloader=self) | |
77dea16a | 304 | if not ffpp.available: |
e3b771a8 | 305 | self.report_error('m3u8 download detected but ffmpeg could not be found. Please install') |
77dea16a | 306 | return False |
12b84ac8 | 307 | ffpp.check_version() |
308 | ||
309 | args = [ffpp.executable, '-y'] | |
310 | ||
a609e61a S |
311 | for log_level in ('quiet', 'verbose'): |
312 | if self.params.get(log_level, False): | |
313 | args += ['-loglevel', log_level] | |
314 | break | |
315 | ||
36fce548 RA |
316 | seekable = info_dict.get('_seekable') |
317 | if seekable is not None: | |
318 | # setting -seekable prevents ffmpeg from guessing if the server | |
319 | # supports seeking(by adding the header `Range: bytes=0-`), which | |
320 | # can cause problems in some cases | |
067aa17e | 321 | # https://github.com/ytdl-org/youtube-dl/issues/11800#issuecomment-275037127 |
36fce548 RA |
322 | # http://trac.ffmpeg.org/ticket/6125#comment:10 |
323 | args += ['-seekable', '1' if seekable else '0'] | |
324 | ||
d8515fd4 | 325 | args += self._configuration_args() |
326 | ||
694c47b2 | 327 | # start_time = info_dict.get('start_time') or 0 |
328 | # if start_time: | |
329 | # args += ['-ss', compat_str(start_time)] | |
330 | # end_time = info_dict.get('end_time') | |
331 | # if end_time: | |
332 | # args += ['-t', compat_str(end_time - start_time)] | |
12b84ac8 | 333 | |
002ea8fe | 334 | if info_dict.get('http_headers') is not None and re.match(r'^https?://', url): |
12b84ac8 | 335 | # Trailing \r\n after each HTTP header is important to prevent warning from ffmpeg/avconv: |
336 | # [http @ 00000000003d2fa0] No trailing CRLF found in HTTP header. | |
337 | headers = handle_youtubedl_headers(info_dict['http_headers']) | |
338 | args += [ | |
339 | '-headers', | |
340 | ''.join('%s: %s\r\n' % (key, val) for key, val in headers.items())] | |
341 | ||
e62d9c5c S |
342 | env = None |
343 | proxy = self.params.get('proxy') | |
344 | if proxy: | |
345 | if not re.match(r'^[\da-zA-Z]+://', proxy): | |
346 | proxy = 'http://%s' % proxy | |
20bad91d YCH |
347 | |
348 | if proxy.startswith('socks'): | |
349 | self.report_warning( | |
6c9b71bc YCH |
350 | '%s does not support SOCKS proxies. Downloading is likely to fail. ' |
351 | 'Consider adding --hls-prefer-native to your command.' % self.get_basename()) | |
20bad91d | 352 | |
e62d9c5c S |
353 | # Since December 2015 ffmpeg supports -http_proxy option (see |
354 | # http://git.videolan.org/?p=ffmpeg.git;a=commit;h=b4eb1f29ebddd60c41a2eb39f5af701e38e0d3fd) | |
355 | # We could switch to the following code if we are able to detect version properly | |
356 | # args += ['-http_proxy', proxy] | |
357 | env = os.environ.copy() | |
358 | compat_setenv('HTTP_PROXY', proxy, env=env) | |
50ce1c33 | 359 | compat_setenv('http_proxy', proxy, env=env) |
e62d9c5c | 360 | |
4230c489 | 361 | protocol = info_dict.get('protocol') |
362 | ||
363 | if protocol == 'rtmp': | |
364 | player_url = info_dict.get('player_url') | |
365 | page_url = info_dict.get('page_url') | |
366 | app = info_dict.get('app') | |
367 | play_path = info_dict.get('play_path') | |
368 | tc_url = info_dict.get('tc_url') | |
369 | flash_version = info_dict.get('flash_version') | |
370 | live = info_dict.get('rtmp_live', False) | |
d7d86fdd | 371 | conn = info_dict.get('rtmp_conn') |
4230c489 | 372 | if player_url is not None: |
373 | args += ['-rtmp_swfverify', player_url] | |
374 | if page_url is not None: | |
375 | args += ['-rtmp_pageurl', page_url] | |
376 | if app is not None: | |
377 | args += ['-rtmp_app', app] | |
378 | if play_path is not None: | |
379 | args += ['-rtmp_playpath', play_path] | |
380 | if tc_url is not None: | |
381 | args += ['-rtmp_tcurl', tc_url] | |
382 | if flash_version is not None: | |
383 | args += ['-rtmp_flashver', flash_version] | |
384 | if live: | |
385 | args += ['-rtmp_live', 'live'] | |
d7d86fdd RA |
386 | if isinstance(conn, list): |
387 | for entry in conn: | |
388 | args += ['-rtmp_conn', entry] | |
389 | elif isinstance(conn, compat_str): | |
390 | args += ['-rtmp_conn', conn] | |
4230c489 | 391 | |
12b84ac8 | 392 | args += ['-i', url, '-c', 'copy'] |
6d0fe752 JH |
393 | |
394 | if self.params.get('test', False): | |
a50862b7 | 395 | args += ['-fs', compat_str(self._TEST_FILE_SIZE)] |
6d0fe752 | 396 | |
f5436c5d | 397 | if protocol in ('m3u8', 'm3u8_native'): |
ce599d5a | 398 | if self.params.get('hls_use_mpegts', False) or tmpfilename == '-': |
12b84ac8 | 399 | args += ['-f', 'mpegts'] |
400 | else: | |
8bdc1494 | 401 | args += ['-f', 'mp4'] |
be670b8e | 402 | if (ffpp.basename == 'ffmpeg' and is_outdated_version(ffpp._versions['ffmpeg'], '3.2', False)) and (not info_dict.get('acodec') or info_dict['acodec'].split('.')[0] in ('aac', 'mp4a')): |
8bdc1494 | 403 | args += ['-bsf:a', 'aac_adtstoasc'] |
4230c489 | 404 | elif protocol == 'rtmp': |
405 | args += ['-f', 'flv'] | |
12b84ac8 | 406 | else: |
a755f825 | 407 | args += ['-f', EXT_TO_OUT_FORMATS.get(info_dict['ext'], info_dict['ext'])] |
12b84ac8 | 408 | |
409 | args = [encodeArgument(opt) for opt in args] | |
d868f43c | 410 | args.append(encodeFilename(ffpp._ffmpeg_filename_argument(tmpfilename), True)) |
12b84ac8 | 411 | |
412 | self._debug_cmd(args) | |
413 | ||
e62d9c5c | 414 | proc = subprocess.Popen(args, stdin=subprocess.PIPE, env=env) |
12b84ac8 | 415 | try: |
416 | retval = proc.wait() | |
f5b1bca9 | 417 | except BaseException as e: |
12b84ac8 | 418 | # subprocces.run would send the SIGKILL signal to ffmpeg and the |
419 | # mp4 file couldn't be played, but if we ask ffmpeg to quit it | |
420 | # produces a file that is playable (this is mostly useful for live | |
421 | # streams). Note that Windows is not affected and produces playable | |
067aa17e | 422 | # files (see https://github.com/ytdl-org/youtube-dl/issues/8300). |
f5b1bca9 | 423 | if isinstance(e, KeyboardInterrupt) and sys.platform != 'win32': |
424 | process_communicate_or_kill(proc, b'q') | |
425 | else: | |
426 | proc.kill() | |
427 | proc.wait() | |
12b84ac8 | 428 | raise |
429 | return retval | |
430 | ||
431 | ||
432 | class AVconvFD(FFmpegFD): | |
433 | pass | |
434 | ||
582be358 | 435 | |
222516d9 PH |
436 | _BY_NAME = dict( |
437 | (klass.get_basename(), klass) | |
438 | for name, klass in globals().items() | |
439 | if name.endswith('FD') and name != 'ExternalFD' | |
440 | ) | |
441 | ||
442 | ||
443 | def list_external_downloaders(): | |
444 | return sorted(_BY_NAME.keys()) | |
445 | ||
446 | ||
447 | def get_external_downloader(external_downloader): | |
448 | """ Given the name of the executable, see whether we support the given | |
449 | downloader . """ | |
6c4d20cd S |
450 | # Drop .exe extension on Windows |
451 | bn = os.path.splitext(os.path.basename(external_downloader))[0] | |
222516d9 | 452 | return _BY_NAME[bn] |