]> jfr.im git - yt-dlp.git/blob - yt_dlp/downloader/external.py
[cleanup] Misc
[yt-dlp.git] / yt_dlp / downloader / external.py
1 import enum
2 import os.path
3 import re
4 import subprocess
5 import sys
6 import time
7
8 from .fragment import FragmentFD
9 from ..compat import functools # isort: split
10 from ..compat import compat_setenv
11 from ..postprocessor.ffmpeg import EXT_TO_OUT_FORMATS, FFmpegPostProcessor
12 from ..utils import (
13 Popen,
14 _configuration_args,
15 check_executable,
16 classproperty,
17 cli_bool_option,
18 cli_option,
19 cli_valueless_option,
20 determine_ext,
21 encodeArgument,
22 encodeFilename,
23 handle_youtubedl_headers,
24 remove_end,
25 traverse_obj,
26 )
27
28
29 class Features(enum.Enum):
30 TO_STDOUT = enum.auto()
31 MULTIPLE_FORMATS = enum.auto()
32
33
34 class ExternalFD(FragmentFD):
35 SUPPORTED_PROTOCOLS = ('http', 'https', 'ftp', 'ftps')
36 SUPPORTED_FEATURES = ()
37
38 def real_download(self, filename, info_dict):
39 self.report_destination(filename)
40 tmpfilename = self.temp_name(filename)
41
42 try:
43 started = time.time()
44 retval = self._call_downloader(tmpfilename, info_dict)
45 except KeyboardInterrupt:
46 if not info_dict.get('is_live'):
47 raise
48 # Live stream downloading cancellation should be considered as
49 # correct and expected termination thus all postprocessing
50 # should take place
51 retval = 0
52 self.to_screen('[%s] Interrupted by user' % self.get_basename())
53
54 if retval == 0:
55 status = {
56 'filename': filename,
57 'status': 'finished',
58 'elapsed': time.time() - started,
59 }
60 if filename != '-':
61 fsize = os.path.getsize(encodeFilename(tmpfilename))
62 self.to_screen(f'\r[{self.get_basename()}] Downloaded {fsize} bytes')
63 self.try_rename(tmpfilename, filename)
64 status.update({
65 'downloaded_bytes': fsize,
66 'total_bytes': fsize,
67 })
68 self._hook_progress(status, info_dict)
69 return True
70 else:
71 self.to_stderr('\n')
72 self.report_error('%s exited with code %d' % (
73 self.get_basename(), retval))
74 return False
75
76 @classmethod
77 def get_basename(cls):
78 return cls.__name__[:-2].lower()
79
80 @classproperty
81 def EXE_NAME(cls):
82 return cls.get_basename()
83
84 @functools.cached_property
85 def exe(self):
86 return self.EXE_NAME
87
88 @classmethod
89 def available(cls, path=None):
90 path = check_executable(
91 cls.EXE_NAME if path in (None, cls.get_basename()) else path,
92 [cls.AVAILABLE_OPT])
93 if not path:
94 return False
95 cls.exe = path
96 return path
97
98 @classmethod
99 def supports(cls, info_dict):
100 return all((
101 not info_dict.get('to_stdout') or Features.TO_STDOUT in cls.SUPPORTED_FEATURES,
102 '+' not in info_dict['protocol'] or Features.MULTIPLE_FORMATS in cls.SUPPORTED_FEATURES,
103 all(proto in cls.SUPPORTED_PROTOCOLS for proto in info_dict['protocol'].split('+')),
104 ))
105
106 @classmethod
107 def can_download(cls, info_dict, path=None):
108 return cls.available(path) and cls.supports(info_dict)
109
110 def _option(self, command_option, param):
111 return cli_option(self.params, command_option, param)
112
113 def _bool_option(self, command_option, param, true_value='true', false_value='false', separator=None):
114 return cli_bool_option(self.params, command_option, param, true_value, false_value, separator)
115
116 def _valueless_option(self, command_option, param, expected_value=True):
117 return cli_valueless_option(self.params, command_option, param, expected_value)
118
119 def _configuration_args(self, keys=None, *args, **kwargs):
120 return _configuration_args(
121 self.get_basename(), self.params.get('external_downloader_args'), self.EXE_NAME,
122 keys, *args, **kwargs)
123
124 def _call_downloader(self, tmpfilename, info_dict):
125 """ Either overwrite this or implement _make_cmd """
126 cmd = [encodeArgument(a) for a in self._make_cmd(tmpfilename, info_dict)]
127
128 self._debug_cmd(cmd)
129
130 if 'fragments' not in info_dict:
131 p = Popen(cmd, stderr=subprocess.PIPE)
132 _, stderr = p.communicate_or_kill()
133 if p.returncode != 0:
134 self.to_stderr(stderr.decode('utf-8', 'replace'))
135 return p.returncode
136
137 fragment_retries = self.params.get('fragment_retries', 0)
138 skip_unavailable_fragments = self.params.get('skip_unavailable_fragments', True)
139
140 count = 0
141 while count <= fragment_retries:
142 p = Popen(cmd, stderr=subprocess.PIPE)
143 _, stderr = p.communicate_or_kill()
144 if p.returncode == 0:
145 break
146 # TODO: Decide whether to retry based on error code
147 # https://aria2.github.io/manual/en/html/aria2c.html#exit-status
148 self.to_stderr(stderr.decode('utf-8', 'replace'))
149 count += 1
150 if count <= fragment_retries:
151 self.to_screen(
152 '[%s] Got error. Retrying fragments (attempt %d of %s)...'
153 % (self.get_basename(), count, self.format_retries(fragment_retries)))
154 self.sleep_retry('fragment', count)
155 if count > fragment_retries:
156 if not skip_unavailable_fragments:
157 self.report_error('Giving up after %s fragment retries' % fragment_retries)
158 return -1
159
160 decrypt_fragment = self.decrypter(info_dict)
161 dest, _ = self.sanitize_open(tmpfilename, 'wb')
162 for frag_index, fragment in enumerate(info_dict['fragments']):
163 fragment_filename = '%s-Frag%d' % (tmpfilename, frag_index)
164 try:
165 src, _ = self.sanitize_open(fragment_filename, 'rb')
166 except OSError as err:
167 if skip_unavailable_fragments and frag_index > 1:
168 self.report_skip_fragment(frag_index, err)
169 continue
170 self.report_error(f'Unable to open fragment {frag_index}; {err}')
171 return -1
172 dest.write(decrypt_fragment(fragment, src.read()))
173 src.close()
174 if not self.params.get('keep_fragments', False):
175 self.try_remove(encodeFilename(fragment_filename))
176 dest.close()
177 self.try_remove(encodeFilename('%s.frag.urls' % tmpfilename))
178 return 0
179
180
181 class CurlFD(ExternalFD):
182 AVAILABLE_OPT = '-V'
183
184 def _make_cmd(self, tmpfilename, info_dict):
185 cmd = [self.exe, '--location', '-o', tmpfilename, '--compressed']
186 if info_dict.get('http_headers') is not None:
187 for key, val in info_dict['http_headers'].items():
188 cmd += ['--header', f'{key}: {val}']
189
190 cmd += self._bool_option('--continue-at', 'continuedl', '-', '0')
191 cmd += self._valueless_option('--silent', 'noprogress')
192 cmd += self._valueless_option('--verbose', 'verbose')
193 cmd += self._option('--limit-rate', 'ratelimit')
194 retry = self._option('--retry', 'retries')
195 if len(retry) == 2:
196 if retry[1] in ('inf', 'infinite'):
197 retry[1] = '2147483647'
198 cmd += retry
199 cmd += self._option('--max-filesize', 'max_filesize')
200 cmd += self._option('--interface', 'source_address')
201 cmd += self._option('--proxy', 'proxy')
202 cmd += self._valueless_option('--insecure', 'nocheckcertificate')
203 cmd += self._configuration_args()
204 cmd += ['--', info_dict['url']]
205 return cmd
206
207 def _call_downloader(self, tmpfilename, info_dict):
208 cmd = [encodeArgument(a) for a in self._make_cmd(tmpfilename, info_dict)]
209
210 self._debug_cmd(cmd)
211
212 # curl writes the progress to stderr so don't capture it.
213 p = Popen(cmd)
214 p.communicate_or_kill()
215 return p.returncode
216
217
218 class AxelFD(ExternalFD):
219 AVAILABLE_OPT = '-V'
220
221 def _make_cmd(self, tmpfilename, info_dict):
222 cmd = [self.exe, '-o', tmpfilename]
223 if info_dict.get('http_headers') is not None:
224 for key, val in info_dict['http_headers'].items():
225 cmd += ['-H', f'{key}: {val}']
226 cmd += self._configuration_args()
227 cmd += ['--', info_dict['url']]
228 return cmd
229
230
231 class WgetFD(ExternalFD):
232 AVAILABLE_OPT = '--version'
233
234 def _make_cmd(self, tmpfilename, info_dict):
235 cmd = [self.exe, '-O', tmpfilename, '-nv', '--no-cookies', '--compression=auto']
236 if info_dict.get('http_headers') is not None:
237 for key, val in info_dict['http_headers'].items():
238 cmd += ['--header', f'{key}: {val}']
239 cmd += self._option('--limit-rate', 'ratelimit')
240 retry = self._option('--tries', 'retries')
241 if len(retry) == 2:
242 if retry[1] in ('inf', 'infinite'):
243 retry[1] = '0'
244 cmd += retry
245 cmd += self._option('--bind-address', 'source_address')
246 proxy = self.params.get('proxy')
247 if proxy:
248 for var in ('http_proxy', 'https_proxy'):
249 cmd += ['--execute', f'{var}={proxy}']
250 cmd += self._valueless_option('--no-check-certificate', 'nocheckcertificate')
251 cmd += self._configuration_args()
252 cmd += ['--', info_dict['url']]
253 return cmd
254
255
256 class Aria2cFD(ExternalFD):
257 AVAILABLE_OPT = '-v'
258 SUPPORTED_PROTOCOLS = ('http', 'https', 'ftp', 'ftps', 'dash_frag_urls', 'm3u8_frag_urls')
259
260 @staticmethod
261 def supports_manifest(manifest):
262 UNSUPPORTED_FEATURES = [
263 r'#EXT-X-BYTERANGE', # playlists composed of byte ranges of media files [1]
264 # 1. https://tools.ietf.org/html/draft-pantos-http-live-streaming-17#section-4.3.2.2
265 ]
266 check_results = (not re.search(feature, manifest) for feature in UNSUPPORTED_FEATURES)
267 return all(check_results)
268
269 def _make_cmd(self, tmpfilename, info_dict):
270 cmd = [self.exe, '-c',
271 '--console-log-level=warn', '--summary-interval=0', '--download-result=hide',
272 '--http-accept-gzip=true', '--file-allocation=none', '-x16', '-j16', '-s16']
273 if 'fragments' in info_dict:
274 cmd += ['--allow-overwrite=true', '--allow-piece-length-change=true']
275 else:
276 cmd += ['--min-split-size', '1M']
277
278 if info_dict.get('http_headers') is not None:
279 for key, val in info_dict['http_headers'].items():
280 cmd += ['--header', f'{key}: {val}']
281 cmd += self._option('--max-overall-download-limit', 'ratelimit')
282 cmd += self._option('--interface', 'source_address')
283 cmd += self._option('--all-proxy', 'proxy')
284 cmd += self._bool_option('--check-certificate', 'nocheckcertificate', 'false', 'true', '=')
285 cmd += self._bool_option('--remote-time', 'updatetime', 'true', 'false', '=')
286 cmd += self._bool_option('--show-console-readout', 'noprogress', 'false', 'true', '=')
287 cmd += self._configuration_args()
288
289 # aria2c strips out spaces from the beginning/end of filenames and paths.
290 # We work around this issue by adding a "./" to the beginning of the
291 # filename and relative path, and adding a "/" at the end of the path.
292 # See: https://github.com/yt-dlp/yt-dlp/issues/276
293 # https://github.com/ytdl-org/youtube-dl/issues/20312
294 # https://github.com/aria2/aria2/issues/1373
295 dn = os.path.dirname(tmpfilename)
296 if dn:
297 if not os.path.isabs(dn):
298 dn = f'.{os.path.sep}{dn}'
299 cmd += ['--dir', dn + os.path.sep]
300 if 'fragments' not in info_dict:
301 cmd += ['--out', f'.{os.path.sep}{os.path.basename(tmpfilename)}']
302 cmd += ['--auto-file-renaming=false']
303
304 if 'fragments' in info_dict:
305 cmd += ['--file-allocation=none', '--uri-selector=inorder']
306 url_list_file = '%s.frag.urls' % tmpfilename
307 url_list = []
308 for frag_index, fragment in enumerate(info_dict['fragments']):
309 fragment_filename = '%s-Frag%d' % (os.path.basename(tmpfilename), frag_index)
310 url_list.append('%s\n\tout=%s' % (fragment['url'], fragment_filename))
311 stream, _ = self.sanitize_open(url_list_file, 'wb')
312 stream.write('\n'.join(url_list).encode())
313 stream.close()
314 cmd += ['-i', url_list_file]
315 else:
316 cmd += ['--', info_dict['url']]
317 return cmd
318
319
320 class HttpieFD(ExternalFD):
321 AVAILABLE_OPT = '--version'
322 EXE_NAME = 'http'
323
324 def _make_cmd(self, tmpfilename, info_dict):
325 cmd = ['http', '--download', '--output', tmpfilename, info_dict['url']]
326
327 if info_dict.get('http_headers') is not None:
328 for key, val in info_dict['http_headers'].items():
329 cmd += [f'{key}:{val}']
330 return cmd
331
332
333 class FFmpegFD(ExternalFD):
334 SUPPORTED_PROTOCOLS = ('http', 'https', 'ftp', 'ftps', 'm3u8', 'm3u8_native', 'rtsp', 'rtmp', 'rtmp_ffmpeg', 'mms', 'http_dash_segments')
335 SUPPORTED_FEATURES = (Features.TO_STDOUT, Features.MULTIPLE_FORMATS)
336
337 @classmethod
338 def available(cls, path=None):
339 # TODO: Fix path for ffmpeg
340 # Fixme: This may be wrong when --ffmpeg-location is used
341 return FFmpegPostProcessor().available
342
343 def on_process_started(self, proc, stdin):
344 """ Override this in subclasses """
345 pass
346
347 @classmethod
348 def can_merge_formats(cls, info_dict, params):
349 return (
350 info_dict.get('requested_formats')
351 and info_dict.get('protocol')
352 and not params.get('allow_unplayable_formats')
353 and 'no-direct-merge' not in params.get('compat_opts', [])
354 and cls.can_download(info_dict))
355
356 def _call_downloader(self, tmpfilename, info_dict):
357 urls = [f['url'] for f in info_dict.get('requested_formats', [])] or [info_dict['url']]
358 ffpp = FFmpegPostProcessor(downloader=self)
359 if not ffpp.available:
360 self.report_error('m3u8 download detected but ffmpeg could not be found. Please install')
361 return False
362 ffpp.check_version()
363
364 args = [ffpp.executable, '-y']
365
366 for log_level in ('quiet', 'verbose'):
367 if self.params.get(log_level, False):
368 args += ['-loglevel', log_level]
369 break
370 if not self.params.get('verbose'):
371 args += ['-hide_banner']
372
373 args += traverse_obj(info_dict, ('downloader_options', 'ffmpeg_args'), default=[])
374
375 # These exists only for compatibility. Extractors should use
376 # info_dict['downloader_options']['ffmpeg_args'] instead
377 args += info_dict.get('_ffmpeg_args') or []
378 seekable = info_dict.get('_seekable')
379 if seekable is not None:
380 # setting -seekable prevents ffmpeg from guessing if the server
381 # supports seeking(by adding the header `Range: bytes=0-`), which
382 # can cause problems in some cases
383 # https://github.com/ytdl-org/youtube-dl/issues/11800#issuecomment-275037127
384 # http://trac.ffmpeg.org/ticket/6125#comment:10
385 args += ['-seekable', '1' if seekable else '0']
386
387 # start_time = info_dict.get('start_time') or 0
388 # if start_time:
389 # args += ['-ss', str(start_time)]
390 # end_time = info_dict.get('end_time')
391 # if end_time:
392 # args += ['-t', str(end_time - start_time)]
393
394 http_headers = None
395 if info_dict.get('http_headers'):
396 youtubedl_headers = handle_youtubedl_headers(info_dict['http_headers'])
397 http_headers = [
398 # Trailing \r\n after each HTTP header is important to prevent warning from ffmpeg/avconv:
399 # [http @ 00000000003d2fa0] No trailing CRLF found in HTTP header.
400 '-headers',
401 ''.join(f'{key}: {val}\r\n' for key, val in youtubedl_headers.items())
402 ]
403
404 env = None
405 proxy = self.params.get('proxy')
406 if proxy:
407 if not re.match(r'^[\da-zA-Z]+://', proxy):
408 proxy = 'http://%s' % proxy
409
410 if proxy.startswith('socks'):
411 self.report_warning(
412 '%s does not support SOCKS proxies. Downloading is likely to fail. '
413 'Consider adding --hls-prefer-native to your command.' % self.get_basename())
414
415 # Since December 2015 ffmpeg supports -http_proxy option (see
416 # http://git.videolan.org/?p=ffmpeg.git;a=commit;h=b4eb1f29ebddd60c41a2eb39f5af701e38e0d3fd)
417 # We could switch to the following code if we are able to detect version properly
418 # args += ['-http_proxy', proxy]
419 env = os.environ.copy()
420 compat_setenv('HTTP_PROXY', proxy, env=env)
421 compat_setenv('http_proxy', proxy, env=env)
422
423 protocol = info_dict.get('protocol')
424
425 if protocol == 'rtmp':
426 player_url = info_dict.get('player_url')
427 page_url = info_dict.get('page_url')
428 app = info_dict.get('app')
429 play_path = info_dict.get('play_path')
430 tc_url = info_dict.get('tc_url')
431 flash_version = info_dict.get('flash_version')
432 live = info_dict.get('rtmp_live', False)
433 conn = info_dict.get('rtmp_conn')
434 if player_url is not None:
435 args += ['-rtmp_swfverify', player_url]
436 if page_url is not None:
437 args += ['-rtmp_pageurl', page_url]
438 if app is not None:
439 args += ['-rtmp_app', app]
440 if play_path is not None:
441 args += ['-rtmp_playpath', play_path]
442 if tc_url is not None:
443 args += ['-rtmp_tcurl', tc_url]
444 if flash_version is not None:
445 args += ['-rtmp_flashver', flash_version]
446 if live:
447 args += ['-rtmp_live', 'live']
448 if isinstance(conn, list):
449 for entry in conn:
450 args += ['-rtmp_conn', entry]
451 elif isinstance(conn, str):
452 args += ['-rtmp_conn', conn]
453
454 for i, url in enumerate(urls):
455 # We need to specify headers for each http input stream
456 # otherwise, it will only be applied to the first.
457 # https://github.com/yt-dlp/yt-dlp/issues/2696
458 if http_headers is not None and re.match(r'^https?://', url):
459 args += http_headers
460 args += self._configuration_args((f'_i{i + 1}', '_i')) + ['-i', url]
461
462 args += ['-c', 'copy']
463 if info_dict.get('requested_formats') or protocol == 'http_dash_segments':
464 for (i, fmt) in enumerate(info_dict.get('requested_formats') or [info_dict]):
465 stream_number = fmt.get('manifest_stream_number', 0)
466 args.extend(['-map', f'{i}:{stream_number}'])
467
468 if self.params.get('test', False):
469 args += ['-fs', str(self._TEST_FILE_SIZE)]
470
471 ext = info_dict['ext']
472 if protocol in ('m3u8', 'm3u8_native'):
473 use_mpegts = (tmpfilename == '-') or self.params.get('hls_use_mpegts')
474 if use_mpegts is None:
475 use_mpegts = info_dict.get('is_live')
476 if use_mpegts:
477 args += ['-f', 'mpegts']
478 else:
479 args += ['-f', 'mp4']
480 if (ffpp.basename == 'ffmpeg' and ffpp._features.get('needs_adtstoasc')) and (not info_dict.get('acodec') or info_dict['acodec'].split('.')[0] in ('aac', 'mp4a')):
481 args += ['-bsf:a', 'aac_adtstoasc']
482 elif protocol == 'rtmp':
483 args += ['-f', 'flv']
484 elif ext == 'mp4' and tmpfilename == '-':
485 args += ['-f', 'mpegts']
486 elif ext == 'unknown_video':
487 ext = determine_ext(remove_end(tmpfilename, '.part'))
488 if ext == 'unknown_video':
489 self.report_warning(
490 'The video format is unknown and cannot be downloaded by ffmpeg. '
491 'Explicitly set the extension in the filename to attempt download in that format')
492 else:
493 self.report_warning(f'The video format is unknown. Trying to download as {ext} according to the filename')
494 args += ['-f', EXT_TO_OUT_FORMATS.get(ext, ext)]
495 else:
496 args += ['-f', EXT_TO_OUT_FORMATS.get(ext, ext)]
497
498 args += self._configuration_args(('_o1', '_o', ''))
499
500 args = [encodeArgument(opt) for opt in args]
501 args.append(encodeFilename(ffpp._ffmpeg_filename_argument(tmpfilename), True))
502 self._debug_cmd(args)
503
504 proc = Popen(args, stdin=subprocess.PIPE, env=env)
505 if url in ('-', 'pipe:'):
506 self.on_process_started(proc, proc.stdin)
507 try:
508 retval = proc.wait()
509 except BaseException as e:
510 # subprocces.run would send the SIGKILL signal to ffmpeg and the
511 # mp4 file couldn't be played, but if we ask ffmpeg to quit it
512 # produces a file that is playable (this is mostly useful for live
513 # streams). Note that Windows is not affected and produces playable
514 # files (see https://github.com/ytdl-org/youtube-dl/issues/8300).
515 if isinstance(e, KeyboardInterrupt) and sys.platform != 'win32' and url not in ('-', 'pipe:'):
516 proc.communicate_or_kill(b'q')
517 else:
518 proc.kill()
519 proc.wait()
520 raise
521 return retval
522
523
524 class AVconvFD(FFmpegFD):
525 pass
526
527
528 _BY_NAME = {
529 klass.get_basename(): klass
530 for name, klass in globals().items()
531 if name.endswith('FD') and name not in ('ExternalFD', 'FragmentFD')
532 }
533
534 _BY_EXE = {klass.EXE_NAME: klass for klass in _BY_NAME.values()}
535
536
537 def list_external_downloaders():
538 return sorted(_BY_NAME.keys())
539
540
541 def get_external_downloader(external_downloader):
542 """ Given the name of the executable, see whether we support the given
543 downloader . """
544 # Drop .exe extension on Windows
545 bn = os.path.splitext(os.path.basename(external_downloader))[0]
546 return _BY_NAME.get(bn, _BY_EXE.get(bn))