external.py 27 KB

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