]> jfr.im git - yt-dlp.git/blame - yt_dlp/downloader/external.py
[cleanup, utils] Split into submodules (#7090)
[yt-dlp.git] / yt_dlp / downloader / external.py
CommitLineData
c487cf00 1import enum
8c53322c 2import json
222516d9 3import os.path
f0298f65 4import re
222516d9 5import subprocess
12b84ac8 6import sys
f0298f65 7import time
8c53322c 8import uuid
5219cb3e 9
1009f67c 10from .fragment import FragmentFD
14f25df2 11from ..compat import functools
f8271158 12from ..postprocessor.ffmpeg import EXT_TO_OUT_FORMATS, FFmpegPostProcessor
222516d9 13from ..utils import (
f8271158 14 Popen,
be5c1ae8 15 RetryManager,
f8271158 16 _configuration_args,
17 check_executable,
28787f16 18 classproperty,
f8271158 19 cli_bool_option,
1195a38f
S
20 cli_option,
21 cli_valueless_option,
af6793f8 22 determine_ext,
74f8654a 23 encodeArgument,
f8271158 24 encodeFilename,
8c53322c 25 find_available_port,
12b84ac8 26 handle_youtubedl_headers,
af6793f8 27 remove_end,
8c53322c 28 sanitized_Request,
0a5a191a 29 traverse_obj,
222516d9
PH
30)
31
32
c487cf00 33class Features(enum.Enum):
34 TO_STDOUT = enum.auto()
35 MULTIPLE_FORMATS = enum.auto()
36
37
1009f67c 38class ExternalFD(FragmentFD):
5219cb3e 39 SUPPORTED_PROTOCOLS = ('http', 'https', 'ftp', 'ftps')
c487cf00 40 SUPPORTED_FEATURES = ()
f0c9fb96 41 _CAPTURE_STDERR = True
5219cb3e 42
222516d9
PH
43 def real_download(self, filename, info_dict):
44 self.report_destination(filename)
45 tmpfilename = self.temp_name(filename)
46
e7db6759 47 try:
f0298f65 48 started = time.time()
e7db6759
S
49 retval = self._call_downloader(tmpfilename, info_dict)
50 except KeyboardInterrupt:
51 if not info_dict.get('is_live'):
52 raise
53 # Live stream downloading cancellation should be considered as
54 # correct and expected termination thus all postprocessing
55 # should take place
56 retval = 0
57 self.to_screen('[%s] Interrupted by user' % self.get_basename())
58
222516d9 59 if retval == 0:
f0298f65
S
60 status = {
61 'filename': filename,
62 'status': 'finished',
63 'elapsed': time.time() - started,
64 }
65 if filename != '-':
80aa2460 66 fsize = os.path.getsize(encodeFilename(tmpfilename))
80aa2460 67 self.try_rename(tmpfilename, filename)
f0298f65 68 status.update({
80aa2460
JH
69 'downloaded_bytes': fsize,
70 'total_bytes': fsize,
80aa2460 71 })
3ba7740d 72 self._hook_progress(status, info_dict)
222516d9
PH
73 return True
74 else:
75 self.to_stderr('\n')
76 self.report_error('%s exited with code %d' % (
77 self.get_basename(), retval))
78 return False
79
80 @classmethod
81 def get_basename(cls):
82 return cls.__name__[:-2].lower()
83
28787f16 84 @classproperty
85 def EXE_NAME(cls):
86 return cls.get_basename()
87
2762dbb1 88 @functools.cached_property
222516d9 89 def exe(self):
28787f16 90 return self.EXE_NAME
222516d9 91
99cbe98c 92 @classmethod
7f7de7f9 93 def available(cls, path=None):
28787f16 94 path = check_executable(
95 cls.EXE_NAME if path in (None, cls.get_basename()) else path,
96 [cls.AVAILABLE_OPT])
97 if not path:
98 return False
99 cls.exe = path
100 return path
99cbe98c 101
222516d9
PH
102 @classmethod
103 def supports(cls, info_dict):
c487cf00 104 return all((
105 not info_dict.get('to_stdout') or Features.TO_STDOUT in cls.SUPPORTED_FEATURES,
106 '+' not in info_dict['protocol'] or Features.MULTIPLE_FORMATS in cls.SUPPORTED_FEATURES,
7e68567e 107 not traverse_obj(info_dict, ('hls_aes', ...), 'extra_param_to_segment_url'),
c487cf00 108 all(proto in cls.SUPPORTED_PROTOCOLS for proto in info_dict['protocol'].split('+')),
109 ))
222516d9 110
2cb99ebb 111 @classmethod
7f7de7f9 112 def can_download(cls, info_dict, path=None):
113 return cls.available(path) and cls.supports(info_dict)
2cb99ebb 114
bf812ef7 115 def _option(self, command_option, param):
1195a38f 116 return cli_option(self.params, command_option, param)
bf812ef7 117
266b0ad6 118 def _bool_option(self, command_option, param, true_value='true', false_value='false', separator=None):
1195a38f 119 return cli_bool_option(self.params, command_option, param, true_value, false_value, separator)
266b0ad6 120
dc534b67 121 def _valueless_option(self, command_option, param, expected_value=True):
1195a38f 122 return cli_valueless_option(self.params, command_option, param, expected_value)
f30c2e8e 123
330690a2 124 def _configuration_args(self, keys=None, *args, **kwargs):
125 return _configuration_args(
28787f16 126 self.get_basename(), self.params.get('external_downloader_args'), self.EXE_NAME,
330690a2 127 keys, *args, **kwargs)
c75f0b36 128
222516d9
PH
129 def _call_downloader(self, tmpfilename, info_dict):
130 """ Either overwrite this or implement _make_cmd """
74f8654a 131 cmd = [encodeArgument(a) for a in self._make_cmd(tmpfilename, info_dict)]
222516d9 132
74f8654a 133 self._debug_cmd(cmd)
222516d9 134
fc5c8b64 135 if 'fragments' not in info_dict:
8c53322c 136 _, stderr, returncode = self._call_process(cmd, info_dict)
f0c9fb96 137 if returncode and stderr:
138 self.to_stderr(stderr)
139 return returncode
fc5c8b64 140
fc5c8b64 141 skip_unavailable_fragments = self.params.get('skip_unavailable_fragments', True)
142
be5c1ae8 143 retry_manager = RetryManager(self.params.get('fragment_retries'), self.report_retry,
144 frag_index=None, fatal=not skip_unavailable_fragments)
145 for retry in retry_manager:
8c53322c 146 _, stderr, returncode = self._call_process(cmd, info_dict)
f0c9fb96 147 if not returncode:
fc5c8b64 148 break
149 # TODO: Decide whether to retry based on error code
150 # https://aria2.github.io/manual/en/html/aria2c.html#exit-status
f0c9fb96 151 if stderr:
152 self.to_stderr(stderr)
be5c1ae8 153 retry.error = Exception()
154 continue
155 if not skip_unavailable_fragments and retry_manager.error:
156 return -1
fc5c8b64 157
158 decrypt_fragment = self.decrypter(info_dict)
205a0654 159 dest, _ = self.sanitize_open(tmpfilename, 'wb')
fc5c8b64 160 for frag_index, fragment in enumerate(info_dict['fragments']):
161 fragment_filename = '%s-Frag%d' % (tmpfilename, frag_index)
162 try:
205a0654 163 src, _ = self.sanitize_open(fragment_filename, 'rb')
86e5f3ed 164 except OSError as err:
fc5c8b64 165 if skip_unavailable_fragments and frag_index > 1:
b4b855eb 166 self.report_skip_fragment(frag_index, err)
fc5c8b64 167 continue
b4b855eb 168 self.report_error(f'Unable to open fragment {frag_index}; {err}')
fc5c8b64 169 return -1
170 dest.write(decrypt_fragment(fragment, src.read()))
171 src.close()
172 if not self.params.get('keep_fragments', False):
45806d44 173 self.try_remove(encodeFilename(fragment_filename))
fc5c8b64 174 dest.close()
45806d44 175 self.try_remove(encodeFilename('%s.frag.urls' % tmpfilename))
fc5c8b64 176 return 0
222516d9 177
8c53322c 178 def _call_process(self, cmd, info_dict):
66aeaac9 179 return Popen.run(cmd, text=True, stderr=subprocess.PIPE if self._CAPTURE_STDERR else None)
8c53322c 180
222516d9 181
384b6202 182class CurlFD(ExternalFD):
91ee320b 183 AVAILABLE_OPT = '-V'
f0c9fb96 184 _CAPTURE_STDERR = False # curl writes the progress to stderr
99cbe98c 185
384b6202 186 def _make_cmd(self, tmpfilename, info_dict):
af14914b 187 cmd = [self.exe, '--location', '-o', tmpfilename, '--compressed']
002ea8fe 188 if info_dict.get('http_headers') is not None:
189 for key, val in info_dict['http_headers'].items():
86e5f3ed 190 cmd += ['--header', f'{key}: {val}']
002ea8fe 191
98e698f1
RA
192 cmd += self._bool_option('--continue-at', 'continuedl', '-', '0')
193 cmd += self._valueless_option('--silent', 'noprogress')
194 cmd += self._valueless_option('--verbose', 'verbose')
195 cmd += self._option('--limit-rate', 'ratelimit')
37b239b3
S
196 retry = self._option('--retry', 'retries')
197 if len(retry) == 2:
198 if retry[1] in ('inf', 'infinite'):
199 retry[1] = '2147483647'
200 cmd += retry
98e698f1 201 cmd += self._option('--max-filesize', 'max_filesize')
9f3da138 202 cmd += self._option('--interface', 'source_address')
e7a8c303 203 cmd += self._option('--proxy', 'proxy')
dc534b67 204 cmd += self._valueless_option('--insecure', 'nocheckcertificate')
c75f0b36 205 cmd += self._configuration_args()
384b6202
PH
206 cmd += ['--', info_dict['url']]
207 return cmd
208
209
e0ac5214 210class AxelFD(ExternalFD):
91ee320b 211 AVAILABLE_OPT = '-V'
99cbe98c 212
e0ac5214 213 def _make_cmd(self, tmpfilename, info_dict):
214 cmd = [self.exe, '-o', tmpfilename]
002ea8fe 215 if info_dict.get('http_headers') is not None:
216 for key, val in info_dict['http_headers'].items():
86e5f3ed 217 cmd += ['-H', f'{key}: {val}']
e0ac5214 218 cmd += self._configuration_args()
219 cmd += ['--', info_dict['url']]
220 return cmd
221
222
222516d9 223class WgetFD(ExternalFD):
91ee320b 224 AVAILABLE_OPT = '--version'
99cbe98c 225
222516d9 226 def _make_cmd(self, tmpfilename, info_dict):
af14914b 227 cmd = [self.exe, '-O', tmpfilename, '-nv', '--no-cookies', '--compression=auto']
002ea8fe 228 if info_dict.get('http_headers') is not None:
229 for key, val in info_dict['http_headers'].items():
86e5f3ed 230 cmd += ['--header', f'{key}: {val}']
8c80603f
S
231 cmd += self._option('--limit-rate', 'ratelimit')
232 retry = self._option('--tries', 'retries')
233 if len(retry) == 2:
234 if retry[1] in ('inf', 'infinite'):
235 retry[1] = '0'
236 cmd += retry
9f3da138 237 cmd += self._option('--bind-address', 'source_address')
8a23db95 238 proxy = self.params.get('proxy')
239 if proxy:
240 for var in ('http_proxy', 'https_proxy'):
86e5f3ed 241 cmd += ['--execute', f'{var}={proxy}']
dc534b67 242 cmd += self._valueless_option('--no-check-certificate', 'nocheckcertificate')
c75f0b36 243 cmd += self._configuration_args()
222516d9
PH
244 cmd += ['--', info_dict['url']]
245 return cmd
246
247
384b6202 248class Aria2cFD(ExternalFD):
91ee320b 249 AVAILABLE_OPT = '-v'
52a8a1e1 250 SUPPORTED_PROTOCOLS = ('http', 'https', 'ftp', 'ftps', 'dash_frag_urls', 'm3u8_frag_urls')
99cbe98c 251
0a473f2f 252 @staticmethod
253 def supports_manifest(manifest):
254 UNSUPPORTED_FEATURES = [
255 r'#EXT-X-BYTERANGE', # playlists composed of byte ranges of media files [1]
256 # 1. https://tools.ietf.org/html/draft-pantos-http-live-streaming-17#section-4.3.2.2
257 ]
258 check_results = (not re.search(feature, manifest) for feature in UNSUPPORTED_FEATURES)
259 return all(check_results)
260
af7a5eef 261 @staticmethod
262 def _aria2c_filename(fn):
263 return fn if os.path.isabs(fn) else f'.{os.path.sep}{fn}'
264
8c53322c 265 def _call_downloader(self, tmpfilename, info_dict):
ad68b16a 266 # FIXME: Disabled due to https://github.com/yt-dlp/yt-dlp/issues/5931
267 if False and 'no-external-downloader-progress' not in self.params.get('compat_opts', []):
8c53322c
L
268 info_dict['__rpc'] = {
269 'port': find_available_port() or 19190,
270 'secret': str(uuid.uuid4()),
271 }
272 return super()._call_downloader(tmpfilename, info_dict)
273
384b6202 274 def _make_cmd(self, tmpfilename, info_dict):
2b3bf01c 275 cmd = [self.exe, '-c',
276 '--console-log-level=warn', '--summary-interval=0', '--download-result=hide',
dcd55f76 277 '--http-accept-gzip=true', '--file-allocation=none', '-x16', '-j16', '-s16']
2b3bf01c 278 if 'fragments' in info_dict:
279 cmd += ['--allow-overwrite=true', '--allow-piece-length-change=true']
ff0f78e1 280 else:
281 cmd += ['--min-split-size', '1M']
2b3bf01c 282
002ea8fe 283 if info_dict.get('http_headers') is not None:
284 for key, val in info_dict['http_headers'].items():
86e5f3ed 285 cmd += ['--header', f'{key}: {val}']
691d5823 286 cmd += self._option('--max-overall-download-limit', 'ratelimit')
9f3da138 287 cmd += self._option('--interface', 'source_address')
bf812ef7 288 cmd += self._option('--all-proxy', 'proxy')
266b0ad6 289 cmd += self._bool_option('--check-certificate', 'nocheckcertificate', 'false', 'true', '=')
71f47617 290 cmd += self._bool_option('--remote-time', 'updatetime', 'true', 'false', '=')
f44afb54 291 cmd += self._bool_option('--show-console-readout', 'noprogress', 'false', 'true', '=')
2b3bf01c 292 cmd += self._configuration_args()
293
8c53322c
L
294 if '__rpc' in info_dict:
295 cmd += [
296 '--enable-rpc',
297 f'--rpc-listen-port={info_dict["__rpc"]["port"]}',
298 f'--rpc-secret={info_dict["__rpc"]["secret"]}']
299
eb55bad5 300 # aria2c strips out spaces from the beginning/end of filenames and paths.
301 # We work around this issue by adding a "./" to the beginning of the
302 # filename and relative path, and adding a "/" at the end of the path.
303 # See: https://github.com/yt-dlp/yt-dlp/issues/276
304 # https://github.com/ytdl-org/youtube-dl/issues/20312
305 # https://github.com/aria2/aria2/issues/1373
2b3bf01c 306 dn = os.path.dirname(tmpfilename)
307 if dn:
af7a5eef 308 cmd += ['--dir', self._aria2c_filename(dn) + os.path.sep]
2b3bf01c 309 if 'fragments' not in info_dict:
af7a5eef 310 cmd += ['--out', self._aria2c_filename(os.path.basename(tmpfilename))]
5219cb3e 311 cmd += ['--auto-file-renaming=false']
2b3bf01c 312
d7009caa 313 if 'fragments' in info_dict:
fe845284 314 cmd += ['--file-allocation=none', '--uri-selector=inorder']
5219cb3e 315 url_list_file = '%s.frag.urls' % tmpfilename
316 url_list = []
fe845284 317 for frag_index, fragment in enumerate(info_dict['fragments']):
318 fragment_filename = '%s-Frag%d' % (os.path.basename(tmpfilename), frag_index)
af7a5eef 319 url_list.append('%s\n\tout=%s' % (fragment['url'], self._aria2c_filename(fragment_filename)))
205a0654 320 stream, _ = self.sanitize_open(url_list_file, 'wb')
0f06bcd7 321 stream.write('\n'.join(url_list).encode())
539d158c 322 stream.close()
af7a5eef 323 cmd += ['-i', self._aria2c_filename(url_list_file)]
5219cb3e 324 else:
325 cmd += ['--', info_dict['url']]
384b6202
PH
326 return cmd
327
8c53322c
L
328 def aria2c_rpc(self, rpc_port, rpc_secret, method, params=()):
329 # Does not actually need to be UUID, just unique
330 sanitycheck = str(uuid.uuid4())
331 d = json.dumps({
332 'jsonrpc': '2.0',
333 'id': sanitycheck,
334 'method': method,
335 'params': [f'token:{rpc_secret}', *params],
336 }).encode('utf-8')
337 request = sanitized_Request(
338 f'http://localhost:{rpc_port}/jsonrpc',
339 data=d, headers={
340 'Content-Type': 'application/json',
341 'Content-Length': f'{len(d)}',
342 'Ytdl-request-proxy': '__noproxy__',
343 })
344 with self.ydl.urlopen(request) as r:
345 resp = json.load(r)
346 assert resp.get('id') == sanitycheck, 'Something went wrong with RPC server'
347 return resp['result']
348
349 def _call_process(self, cmd, info_dict):
350 if '__rpc' not in info_dict:
351 return super()._call_process(cmd, info_dict)
352
353 send_rpc = functools.partial(self.aria2c_rpc, info_dict['__rpc']['port'], info_dict['__rpc']['secret'])
354 started = time.time()
355
356 fragmented = 'fragments' in info_dict
357 frag_count = len(info_dict['fragments']) if fragmented else 1
358 status = {
359 'filename': info_dict.get('_filename'),
360 'status': 'downloading',
361 'elapsed': 0,
362 'downloaded_bytes': 0,
363 'fragment_count': frag_count if fragmented else None,
364 'fragment_index': 0 if fragmented else None,
365 }
366 self._hook_progress(status, info_dict)
367
368 def get_stat(key, *obj, average=False):
369 val = tuple(filter(None, map(float, traverse_obj(obj, (..., ..., key))))) or [0]
370 return sum(val) / (len(val) if average else 1)
371
372 with Popen(cmd, text=True, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE) as p:
373 # Add a small sleep so that RPC client can receive response,
374 # or the connection stalls infinitely
375 time.sleep(0.2)
376 retval = p.poll()
377 while retval is None:
378 # We don't use tellStatus as we won't know the GID without reading stdout
379 # Ref: https://aria2.github.io/manual/en/html/aria2c.html#aria2.tellActive
380 active = send_rpc('aria2.tellActive')
381 completed = send_rpc('aria2.tellStopped', [0, frag_count])
382
383 downloaded = get_stat('totalLength', completed) + get_stat('completedLength', active)
384 speed = get_stat('downloadSpeed', active)
385 total = frag_count * get_stat('totalLength', active, completed, average=True)
386 if total < downloaded:
387 total = None
388
389 status.update({
390 'downloaded_bytes': int(downloaded),
391 'speed': speed,
392 'total_bytes': None if fragmented else total,
393 'total_bytes_estimate': total,
394 'eta': (total - downloaded) / (speed or 1),
395 'fragment_index': min(frag_count, len(completed) + 1) if fragmented else None,
396 'elapsed': time.time() - started
397 })
398 self._hook_progress(status, info_dict)
399
400 if not active and len(completed) >= frag_count:
401 send_rpc('aria2.shutdown')
402 retval = p.wait()
403 break
404
405 time.sleep(0.1)
406 retval = p.poll()
407
408 return '', p.stderr.read(), retval
409
906e2f0e
JMF
410
411class HttpieFD(ExternalFD):
52a8a1e1 412 AVAILABLE_OPT = '--version'
28787f16 413 EXE_NAME = 'http'
99cbe98c 414
906e2f0e
JMF
415 def _make_cmd(self, tmpfilename, info_dict):
416 cmd = ['http', '--download', '--output', tmpfilename, info_dict['url']]
002ea8fe 417
418 if info_dict.get('http_headers') is not None:
419 for key, val in info_dict['http_headers'].items():
86e5f3ed 420 cmd += [f'{key}:{val}']
906e2f0e
JMF
421 return cmd
422
12b84ac8 423
424class FFmpegFD(ExternalFD):
6251555f 425 SUPPORTED_PROTOCOLS = ('http', 'https', 'ftp', 'ftps', 'm3u8', 'm3u8_native', 'rtsp', 'rtmp', 'rtmp_ffmpeg', 'mms', 'http_dash_segments')
c487cf00 426 SUPPORTED_FEATURES = (Features.TO_STDOUT, Features.MULTIPLE_FORMATS)
12b84ac8 427
99cbe98c 428 @classmethod
52a8a1e1 429 def available(cls, path=None):
430 # TODO: Fix path for ffmpeg
dbf5416a 431 # Fixme: This may be wrong when --ffmpeg-location is used
99cbe98c 432 return FFmpegPostProcessor().available
433
e36d50c5 434 def on_process_started(self, proc, stdin):
435 """ Override this in subclasses """
436 pass
437
dbf5416a 438 @classmethod
d5fe04f5 439 def can_merge_formats(cls, info_dict, params):
dbf5416a 440 return (
441 info_dict.get('requested_formats')
442 and info_dict.get('protocol')
443 and not params.get('allow_unplayable_formats')
444 and 'no-direct-merge' not in params.get('compat_opts', [])
445 and cls.can_download(info_dict))
446
12b84ac8 447 def _call_downloader(self, tmpfilename, info_dict):
12b84ac8 448 ffpp = FFmpegPostProcessor(downloader=self)
77dea16a 449 if not ffpp.available:
e3b771a8 450 self.report_error('m3u8 download detected but ffmpeg could not be found. Please install')
77dea16a 451 return False
12b84ac8 452 ffpp.check_version()
453
454 args = [ffpp.executable, '-y']
455
a609e61a
S
456 for log_level in ('quiet', 'verbose'):
457 if self.params.get(log_level, False):
458 args += ['-loglevel', log_level]
459 break
2ec1759f 460 if not self.params.get('verbose'):
461 args += ['-hide_banner']
a609e61a 462
0a5a191a 463 args += traverse_obj(info_dict, ('downloader_options', 'ffmpeg_args'), default=[])
bb36a55c 464
0a5a191a 465 # These exists only for compatibility. Extractors should use
466 # info_dict['downloader_options']['ffmpeg_args'] instead
1d485a1a 467 args += info_dict.get('_ffmpeg_args') or []
36fce548
RA
468 seekable = info_dict.get('_seekable')
469 if seekable is not None:
470 # setting -seekable prevents ffmpeg from guessing if the server
471 # supports seeking(by adding the header `Range: bytes=0-`), which
472 # can cause problems in some cases
067aa17e 473 # https://github.com/ytdl-org/youtube-dl/issues/11800#issuecomment-275037127
36fce548
RA
474 # http://trac.ffmpeg.org/ticket/6125#comment:10
475 args += ['-seekable', '1' if seekable else '0']
476
e62d9c5c
S
477 env = None
478 proxy = self.params.get('proxy')
479 if proxy:
480 if not re.match(r'^[\da-zA-Z]+://', proxy):
481 proxy = 'http://%s' % proxy
20bad91d
YCH
482
483 if proxy.startswith('socks'):
484 self.report_warning(
6c9b71bc
YCH
485 '%s does not support SOCKS proxies. Downloading is likely to fail. '
486 'Consider adding --hls-prefer-native to your command.' % self.get_basename())
20bad91d 487
e62d9c5c
S
488 # Since December 2015 ffmpeg supports -http_proxy option (see
489 # http://git.videolan.org/?p=ffmpeg.git;a=commit;h=b4eb1f29ebddd60c41a2eb39f5af701e38e0d3fd)
490 # We could switch to the following code if we are able to detect version properly
491 # args += ['-http_proxy', proxy]
492 env = os.environ.copy()
ac668111 493 env['HTTP_PROXY'] = proxy
494 env['http_proxy'] = proxy
e62d9c5c 495
4230c489 496 protocol = info_dict.get('protocol')
497
498 if protocol == 'rtmp':
499 player_url = info_dict.get('player_url')
500 page_url = info_dict.get('page_url')
501 app = info_dict.get('app')
502 play_path = info_dict.get('play_path')
503 tc_url = info_dict.get('tc_url')
504 flash_version = info_dict.get('flash_version')
505 live = info_dict.get('rtmp_live', False)
d7d86fdd 506 conn = info_dict.get('rtmp_conn')
4230c489 507 if player_url is not None:
508 args += ['-rtmp_swfverify', player_url]
509 if page_url is not None:
510 args += ['-rtmp_pageurl', page_url]
511 if app is not None:
512 args += ['-rtmp_app', app]
513 if play_path is not None:
514 args += ['-rtmp_playpath', play_path]
515 if tc_url is not None:
516 args += ['-rtmp_tcurl', tc_url]
517 if flash_version is not None:
518 args += ['-rtmp_flashver', flash_version]
519 if live:
520 args += ['-rtmp_live', 'live']
d7d86fdd
RA
521 if isinstance(conn, list):
522 for entry in conn:
523 args += ['-rtmp_conn', entry]
c487cf00 524 elif isinstance(conn, str):
d7d86fdd 525 args += ['-rtmp_conn', conn]
4230c489 526
5ec1b6b7 527 start_time, end_time = info_dict.get('section_start') or 0, info_dict.get('section_end')
528
3cf50fa8 529 selected_formats = info_dict.get('requested_formats') or [info_dict]
530 for i, fmt in enumerate(selected_formats):
531 if fmt.get('http_headers') and re.match(r'^https?://', fmt['url']):
532 headers_dict = handle_youtubedl_headers(fmt['http_headers'])
533 # Trailing \r\n after each HTTP header is important to prevent warning from ffmpeg/avconv:
534 # [http @ 00000000003d2fa0] No trailing CRLF found in HTTP header.
535 args.extend(['-headers', ''.join(f'{key}: {val}\r\n' for key, val in headers_dict.items())])
536
5ec1b6b7 537 if start_time:
538 args += ['-ss', str(start_time)]
539 if end_time:
540 args += ['-t', str(end_time - start_time)]
541
3cf50fa8 542 args += self._configuration_args((f'_i{i + 1}', '_i')) + ['-i', fmt['url']]
6b6c16ca 543
5ec1b6b7 544 if not (start_time or end_time) or not self.params.get('force_keyframes_at_cuts'):
545 args += ['-c', 'copy']
546
6251555f 547 if info_dict.get('requested_formats') or protocol == 'http_dash_segments':
3cf50fa8 548 for i, fmt in enumerate(selected_formats):
6251555f 549 stream_number = fmt.get('manifest_stream_number', 0)
234416e4 550 args.extend(['-map', f'{i}:{stream_number}'])
6d0fe752
JH
551
552 if self.params.get('test', False):
c487cf00 553 args += ['-fs', str(self._TEST_FILE_SIZE)]
6d0fe752 554
e5611e8e 555 ext = info_dict['ext']
f5436c5d 556 if protocol in ('m3u8', 'm3u8_native'):
9bd20204 557 use_mpegts = (tmpfilename == '-') or self.params.get('hls_use_mpegts')
558 if use_mpegts is None:
559 use_mpegts = info_dict.get('is_live')
560 if use_mpegts:
12b84ac8 561 args += ['-f', 'mpegts']
562 else:
8bdc1494 563 args += ['-f', 'mp4']
8913ef74 564 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')):
8bdc1494 565 args += ['-bsf:a', 'aac_adtstoasc']
4230c489 566 elif protocol == 'rtmp':
567 args += ['-f', 'flv']
e5611e8e 568 elif ext == 'mp4' and tmpfilename == '-':
569 args += ['-f', 'mpegts']
af6793f8 570 elif ext == 'unknown_video':
571 ext = determine_ext(remove_end(tmpfilename, '.part'))
572 if ext == 'unknown_video':
573 self.report_warning(
574 'The video format is unknown and cannot be downloaded by ffmpeg. '
575 'Explicitly set the extension in the filename to attempt download in that format')
576 else:
577 self.report_warning(f'The video format is unknown. Trying to download as {ext} according to the filename')
578 args += ['-f', EXT_TO_OUT_FORMATS.get(ext, ext)]
12b84ac8 579 else:
e5611e8e 580 args += ['-f', EXT_TO_OUT_FORMATS.get(ext, ext)]
12b84ac8 581
6251555f 582 args += self._configuration_args(('_o1', '_o', ''))
330690a2 583
12b84ac8 584 args = [encodeArgument(opt) for opt in args]
d868f43c 585 args.append(encodeFilename(ffpp._ffmpeg_filename_argument(tmpfilename), True))
12b84ac8 586 self._debug_cmd(args)
587
3cf50fa8 588 piped = any(fmt['url'] in ('-', 'pipe:') for fmt in selected_formats)
f0c9fb96 589 with Popen(args, stdin=subprocess.PIPE, env=env) as proc:
3cf50fa8 590 if piped:
f0c9fb96 591 self.on_process_started(proc, proc.stdin)
592 try:
593 retval = proc.wait()
594 except BaseException as e:
595 # subprocces.run would send the SIGKILL signal to ffmpeg and the
596 # mp4 file couldn't be played, but if we ask ffmpeg to quit it
597 # produces a file that is playable (this is mostly useful for live
598 # streams). Note that Windows is not affected and produces playable
599 # files (see https://github.com/ytdl-org/youtube-dl/issues/8300).
3cf50fa8 600 if isinstance(e, KeyboardInterrupt) and sys.platform != 'win32' and not piped:
f0c9fb96 601 proc.communicate_or_kill(b'q')
602 else:
603 proc.kill(timeout=None)
604 raise
605 return retval
12b84ac8 606
607
608class AVconvFD(FFmpegFD):
609 pass
610
582be358 611
28787f16 612_BY_NAME = {
613 klass.get_basename(): klass
222516d9 614 for name, klass in globals().items()
1009f67c 615 if name.endswith('FD') and name not in ('ExternalFD', 'FragmentFD')
28787f16 616}
617
222516d9
PH
618
619def list_external_downloaders():
620 return sorted(_BY_NAME.keys())
621
622
623def get_external_downloader(external_downloader):
e1eabd7b 624 """ Given the name of the executable, see whether we support the given downloader """
6c4d20cd 625 bn = os.path.splitext(os.path.basename(external_downloader))[0]
e1eabd7b 626 return _BY_NAME.get(bn) or next((
627 klass for klass in _BY_NAME.values() if klass.EXE_NAME in bn
628 ), None)