__init__.py 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  1. import sys
  2. if sys.version_info < (3, 8):
  3. raise ImportError(
  4. f'You are using an unsupported version of Python. Only Python versions 3.8 and above are supported by yt-dlp') # noqa: F541
  5. __license__ = 'The Unlicense'
  6. import collections
  7. import getpass
  8. import itertools
  9. import optparse
  10. import os
  11. import re
  12. import traceback
  13. from .compat import compat_os_name
  14. from .cookies import SUPPORTED_BROWSERS, SUPPORTED_KEYRINGS
  15. from .downloader.external import get_external_downloader
  16. from .extractor import list_extractor_classes
  17. from .extractor.adobepass import MSO_INFO
  18. from .networking.impersonate import ImpersonateTarget
  19. from .options import parseOpts
  20. from .postprocessor import (
  21. FFmpegExtractAudioPP,
  22. FFmpegMergerPP,
  23. FFmpegPostProcessor,
  24. FFmpegSubtitlesConvertorPP,
  25. FFmpegThumbnailsConvertorPP,
  26. FFmpegVideoConvertorPP,
  27. FFmpegVideoRemuxerPP,
  28. MetadataFromFieldPP,
  29. MetadataParserPP,
  30. )
  31. from .update import Updater
  32. from .utils import (
  33. NO_DEFAULT,
  34. POSTPROCESS_WHEN,
  35. DateRange,
  36. DownloadCancelled,
  37. DownloadError,
  38. FormatSorter,
  39. GeoUtils,
  40. PlaylistEntries,
  41. SameFileError,
  42. decodeOption,
  43. download_range_func,
  44. expand_path,
  45. float_or_none,
  46. format_field,
  47. int_or_none,
  48. join_nonempty,
  49. match_filter_func,
  50. parse_bytes,
  51. parse_duration,
  52. preferredencoding,
  53. read_batch_urls,
  54. read_stdin,
  55. render_table,
  56. setproctitle,
  57. shell_quote,
  58. traverse_obj,
  59. variadic,
  60. write_string,
  61. )
  62. from .utils.networking import std_headers
  63. from .utils._utils import _UnsafeExtensionError
  64. from .YoutubeDL import YoutubeDL
  65. _IN_CLI = False
  66. def _exit(status=0, *args):
  67. for msg in args:
  68. sys.stderr.write(msg)
  69. raise SystemExit(status)
  70. def get_urls(urls, batchfile, verbose):
  71. """
  72. @param verbose -1: quiet, 0: normal, 1: verbose
  73. """
  74. batch_urls = []
  75. if batchfile is not None:
  76. try:
  77. batch_urls = read_batch_urls(
  78. read_stdin(None if verbose == -1 else 'URLs') if batchfile == '-'
  79. else open(expand_path(batchfile), encoding='utf-8', errors='ignore'))
  80. if verbose == 1:
  81. write_string('[debug] Batch file urls: ' + repr(batch_urls) + '\n')
  82. except OSError:
  83. _exit(f'ERROR: batch file {batchfile} could not be read')
  84. _enc = preferredencoding()
  85. return [
  86. url.strip().decode(_enc, 'ignore') if isinstance(url, bytes) else url.strip()
  87. for url in batch_urls + urls]
  88. def print_extractor_information(opts, urls):
  89. out = ''
  90. if opts.list_extractors:
  91. # Importing GenericIE is currently slow since it imports YoutubeIE
  92. from .extractor.generic import GenericIE
  93. urls = dict.fromkeys(urls, False)
  94. for ie in list_extractor_classes(opts.age_limit):
  95. out += ie.IE_NAME + (' (CURRENTLY BROKEN)' if not ie.working() else '') + '\n'
  96. if ie == GenericIE:
  97. matched_urls = [url for url, matched in urls.items() if not matched]
  98. else:
  99. matched_urls = tuple(filter(ie.suitable, urls.keys()))
  100. urls.update(dict.fromkeys(matched_urls, True))
  101. out += ''.join(f' {url}\n' for url in matched_urls)
  102. elif opts.list_extractor_descriptions:
  103. _SEARCHES = ('cute kittens', 'slithering pythons', 'falling cat', 'angry poodle', 'purple fish', 'running tortoise', 'sleeping bunny', 'burping cow')
  104. out = '\n'.join(
  105. ie.description(markdown=False, search_examples=_SEARCHES)
  106. for ie in list_extractor_classes(opts.age_limit) if ie.working() and ie.IE_DESC is not False)
  107. elif opts.ap_list_mso:
  108. out = 'Supported TV Providers:\n{}\n'.format(render_table(
  109. ['mso', 'mso name'],
  110. [[mso_id, mso_info['name']] for mso_id, mso_info in MSO_INFO.items()]))
  111. else:
  112. return False
  113. write_string(out, out=sys.stdout)
  114. return True
  115. def set_compat_opts(opts):
  116. def _unused_compat_opt(name):
  117. if name not in opts.compat_opts:
  118. return False
  119. opts.compat_opts.discard(name)
  120. opts.compat_opts.update([f'*{name}'])
  121. return True
  122. def set_default_compat(compat_name, opt_name, default=True, remove_compat=True):
  123. attr = getattr(opts, opt_name)
  124. if compat_name in opts.compat_opts:
  125. if attr is None:
  126. setattr(opts, opt_name, not default)
  127. return True
  128. else:
  129. if remove_compat:
  130. _unused_compat_opt(compat_name)
  131. return False
  132. elif attr is None:
  133. setattr(opts, opt_name, default)
  134. return None
  135. set_default_compat('abort-on-error', 'ignoreerrors', 'only_download')
  136. set_default_compat('no-playlist-metafiles', 'allow_playlist_files')
  137. set_default_compat('no-clean-infojson', 'clean_infojson')
  138. if 'no-attach-info-json' in opts.compat_opts:
  139. if opts.embed_infojson:
  140. _unused_compat_opt('no-attach-info-json')
  141. else:
  142. opts.embed_infojson = False
  143. if 'format-sort' in opts.compat_opts:
  144. opts.format_sort.extend(FormatSorter.ytdl_default)
  145. _video_multistreams_set = set_default_compat('multistreams', 'allow_multiple_video_streams', False, remove_compat=False)
  146. _audio_multistreams_set = set_default_compat('multistreams', 'allow_multiple_audio_streams', False, remove_compat=False)
  147. if _video_multistreams_set is False and _audio_multistreams_set is False:
  148. _unused_compat_opt('multistreams')
  149. if 'filename' in opts.compat_opts:
  150. if opts.outtmpl.get('default') is None:
  151. opts.outtmpl.update({'default': '%(title)s-%(id)s.%(ext)s'})
  152. else:
  153. _unused_compat_opt('filename')
  154. def validate_options(opts):
  155. def validate(cndn, name, value=None, msg=None):
  156. if cndn:
  157. return True
  158. raise ValueError((msg or 'invalid {name} "{value}" given').format(name=name, value=value))
  159. def validate_in(name, value, items, msg=None):
  160. return validate(value is None or value in items, name, value, msg)
  161. def validate_regex(name, value, regex):
  162. return validate(value is None or re.match(regex, value), name, value)
  163. def validate_positive(name, value, strict=False):
  164. return validate(value is None or value > 0 or (not strict and value == 0),
  165. name, value, '{name} "{value}" must be positive' + ('' if strict else ' or 0'))
  166. def validate_minmax(min_val, max_val, min_name, max_name=None):
  167. if max_val is None or min_val is None or max_val >= min_val:
  168. return
  169. if not max_name:
  170. min_name, max_name = f'min {min_name}', f'max {min_name}'
  171. raise ValueError(f'{max_name} "{max_val}" must be must be greater than or equal to {min_name} "{min_val}"')
  172. # Usernames and passwords
  173. validate(sum(map(bool, (opts.usenetrc, opts.netrc_cmd, opts.username))) <= 1, '.netrc',
  174. msg='{name}, netrc command and username/password are mutually exclusive options')
  175. validate(opts.password is None or opts.username is not None, 'account username', msg='{name} missing')
  176. validate(opts.ap_password is None or opts.ap_username is not None,
  177. 'TV Provider account username', msg='{name} missing')
  178. validate_in('TV Provider', opts.ap_mso, MSO_INFO,
  179. 'Unsupported {name} "{value}", use --ap-list-mso to get a list of supported TV Providers')
  180. # Numbers
  181. validate_positive('autonumber start', opts.autonumber_start)
  182. validate_positive('autonumber size', opts.autonumber_size, True)
  183. validate_positive('concurrent fragments', opts.concurrent_fragment_downloads, True)
  184. validate_positive('playlist start', opts.playliststart, True)
  185. if opts.playlistend != -1:
  186. validate_minmax(opts.playliststart, opts.playlistend, 'playlist start', 'playlist end')
  187. # Time ranges
  188. validate_positive('subtitles sleep interval', opts.sleep_interval_subtitles)
  189. validate_positive('requests sleep interval', opts.sleep_interval_requests)
  190. validate_positive('sleep interval', opts.sleep_interval)
  191. validate_positive('max sleep interval', opts.max_sleep_interval)
  192. if opts.sleep_interval is None:
  193. validate(
  194. opts.max_sleep_interval is None, 'min sleep interval',
  195. msg='{name} must be specified; use --min-sleep-interval')
  196. elif opts.max_sleep_interval is None:
  197. opts.max_sleep_interval = opts.sleep_interval
  198. else:
  199. validate_minmax(opts.sleep_interval, opts.max_sleep_interval, 'sleep interval')
  200. if opts.wait_for_video is not None:
  201. min_wait, max_wait, *_ = map(parse_duration, [*opts.wait_for_video.split('-', 1), None])
  202. validate(min_wait is not None and not (max_wait is None and '-' in opts.wait_for_video),
  203. 'time range to wait for video', opts.wait_for_video)
  204. validate_minmax(min_wait, max_wait, 'time range to wait for video')
  205. opts.wait_for_video = (min_wait, max_wait)
  206. # Format sort
  207. for f in opts.format_sort:
  208. validate_regex('format sorting', f, FormatSorter.regex)
  209. # Postprocessor formats
  210. validate_regex('merge output format', opts.merge_output_format,
  211. r'({0})(/({0}))*'.format('|'.join(map(re.escape, FFmpegMergerPP.SUPPORTED_EXTS))))
  212. validate_regex('audio format', opts.audioformat, FFmpegExtractAudioPP.FORMAT_RE)
  213. validate_in('subtitle format', opts.convertsubtitles, FFmpegSubtitlesConvertorPP.SUPPORTED_EXTS)
  214. validate_regex('thumbnail format', opts.convertthumbnails, FFmpegThumbnailsConvertorPP.FORMAT_RE)
  215. validate_regex('recode video format', opts.recodevideo, FFmpegVideoConvertorPP.FORMAT_RE)
  216. validate_regex('remux video format', opts.remuxvideo, FFmpegVideoRemuxerPP.FORMAT_RE)
  217. if opts.audioquality:
  218. opts.audioquality = opts.audioquality.strip('k').strip('K')
  219. # int_or_none prevents inf, nan
  220. validate_positive('audio quality', int_or_none(float_or_none(opts.audioquality), default=0))
  221. # Retries
  222. def parse_retries(name, value):
  223. if value is None:
  224. return None
  225. elif value in ('inf', 'infinite'):
  226. return float('inf')
  227. try:
  228. return int(value)
  229. except (TypeError, ValueError):
  230. validate(False, f'{name} retry count', value)
  231. opts.retries = parse_retries('download', opts.retries)
  232. opts.fragment_retries = parse_retries('fragment', opts.fragment_retries)
  233. opts.extractor_retries = parse_retries('extractor', opts.extractor_retries)
  234. opts.file_access_retries = parse_retries('file access', opts.file_access_retries)
  235. # Retry sleep function
  236. def parse_sleep_func(expr):
  237. NUMBER_RE = r'\d+(?:\.\d+)?'
  238. op, start, limit, step, *_ = (*tuple(re.fullmatch(
  239. rf'(?:(linear|exp)=)?({NUMBER_RE})(?::({NUMBER_RE})?)?(?::({NUMBER_RE}))?',
  240. expr.strip()).groups()), None, None)
  241. if op == 'exp':
  242. return lambda n: min(float(start) * (float(step or 2) ** n), float(limit or 'inf'))
  243. else:
  244. default_step = start if op or limit else 0
  245. return lambda n: min(float(start) + float(step or default_step) * n, float(limit or 'inf'))
  246. for key, expr in opts.retry_sleep.items():
  247. if not expr:
  248. del opts.retry_sleep[key]
  249. continue
  250. try:
  251. opts.retry_sleep[key] = parse_sleep_func(expr)
  252. except AttributeError:
  253. raise ValueError(f'invalid {key} retry sleep expression {expr!r}')
  254. # Bytes
  255. def validate_bytes(name, value):
  256. if value is None:
  257. return None
  258. numeric_limit = parse_bytes(value)
  259. validate(numeric_limit is not None, 'rate limit', value)
  260. return numeric_limit
  261. opts.ratelimit = validate_bytes('rate limit', opts.ratelimit)
  262. opts.throttledratelimit = validate_bytes('throttled rate limit', opts.throttledratelimit)
  263. opts.min_filesize = validate_bytes('min filesize', opts.min_filesize)
  264. opts.max_filesize = validate_bytes('max filesize', opts.max_filesize)
  265. opts.buffersize = validate_bytes('buffer size', opts.buffersize)
  266. opts.http_chunk_size = validate_bytes('http chunk size', opts.http_chunk_size)
  267. # Output templates
  268. def validate_outtmpl(tmpl, msg):
  269. err = YoutubeDL.validate_outtmpl(tmpl)
  270. if err:
  271. raise ValueError(f'invalid {msg} "{tmpl}": {err}')
  272. for k, tmpl in opts.outtmpl.items():
  273. validate_outtmpl(tmpl, f'{k} output template')
  274. for type_, tmpl_list in opts.forceprint.items():
  275. for tmpl in tmpl_list:
  276. validate_outtmpl(tmpl, f'{type_} print template')
  277. for type_, tmpl_list in opts.print_to_file.items():
  278. for tmpl, file in tmpl_list:
  279. validate_outtmpl(tmpl, f'{type_} print to file template')
  280. validate_outtmpl(file, f'{type_} print to file filename')
  281. validate_outtmpl(opts.sponsorblock_chapter_title, 'SponsorBlock chapter title')
  282. for k, tmpl in opts.progress_template.items():
  283. k = f'{k[:-6]} console title' if '-title' in k else f'{k} progress'
  284. validate_outtmpl(tmpl, f'{k} template')
  285. outtmpl_default = opts.outtmpl.get('default')
  286. if outtmpl_default == '':
  287. opts.skip_download = None
  288. del opts.outtmpl['default']
  289. def parse_chapters(name, value, advanced=False):
  290. parse_timestamp = lambda x: float('inf') if x in ('inf', 'infinite') else parse_duration(x)
  291. TIMESTAMP_RE = r'''(?x)(?:
  292. (?P<start_sign>-?)(?P<start>[^-]+)
  293. )?\s*-\s*(?:
  294. (?P<end_sign>-?)(?P<end>[^-]+)
  295. )?'''
  296. chapters, ranges, from_url = [], [], False
  297. for regex in value or []:
  298. if advanced and regex == '*from-url':
  299. from_url = True
  300. continue
  301. elif not regex.startswith('*'):
  302. try:
  303. chapters.append(re.compile(regex))
  304. except re.error as err:
  305. raise ValueError(f'invalid {name} regex "{regex}" - {err}')
  306. continue
  307. for range_ in map(str.strip, regex[1:].split(',')):
  308. mobj = range_ != '-' and re.fullmatch(TIMESTAMP_RE, range_)
  309. dur = mobj and [parse_timestamp(mobj.group('start') or '0'), parse_timestamp(mobj.group('end') or 'inf')]
  310. signs = mobj and (mobj.group('start_sign'), mobj.group('end_sign'))
  311. err = None
  312. if None in (dur or [None]):
  313. err = 'Must be of the form "*start-end"'
  314. elif not advanced and any(signs):
  315. err = 'Negative timestamps are not allowed'
  316. else:
  317. dur[0] *= -1 if signs[0] else 1
  318. dur[1] *= -1 if signs[1] else 1
  319. if dur[1] == float('-inf'):
  320. err = '"-inf" is not a valid end'
  321. if err:
  322. raise ValueError(f'invalid {name} time range "{regex}". {err}')
  323. ranges.append(dur)
  324. return chapters, ranges, from_url
  325. opts.remove_chapters, opts.remove_ranges, _ = parse_chapters('--remove-chapters', opts.remove_chapters)
  326. opts.download_ranges = download_range_func(*parse_chapters('--download-sections', opts.download_ranges, True))
  327. # Cookies from browser
  328. if opts.cookiesfrombrowser:
  329. container = None
  330. mobj = re.fullmatch(r'''(?x)
  331. (?P<name>[^+:]+)
  332. (?:\s*\+\s*(?P<keyring>[^:]+))?
  333. (?:\s*:\s*(?!:)(?P<profile>.+?))?
  334. (?:\s*::\s*(?P<container>.+))?
  335. ''', opts.cookiesfrombrowser)
  336. if mobj is None:
  337. raise ValueError(f'invalid cookies from browser arguments: {opts.cookiesfrombrowser}')
  338. browser_name, keyring, profile, container = mobj.group('name', 'keyring', 'profile', 'container')
  339. browser_name = browser_name.lower()
  340. if browser_name not in SUPPORTED_BROWSERS:
  341. raise ValueError(f'unsupported browser specified for cookies: "{browser_name}". '
  342. f'Supported browsers are: {", ".join(sorted(SUPPORTED_BROWSERS))}')
  343. if keyring is not None:
  344. keyring = keyring.upper()
  345. if keyring not in SUPPORTED_KEYRINGS:
  346. raise ValueError(f'unsupported keyring specified for cookies: "{keyring}". '
  347. f'Supported keyrings are: {", ".join(sorted(SUPPORTED_KEYRINGS))}')
  348. opts.cookiesfrombrowser = (browser_name, profile, keyring, container)
  349. if opts.impersonate is not None:
  350. opts.impersonate = ImpersonateTarget.from_str(opts.impersonate.lower())
  351. # MetadataParser
  352. def metadataparser_actions(f):
  353. if isinstance(f, str):
  354. cmd = f'--parse-metadata {shell_quote(f)}'
  355. try:
  356. actions = [MetadataFromFieldPP.to_action(f)]
  357. except Exception as err:
  358. raise ValueError(f'{cmd} is invalid; {err}')
  359. else:
  360. cmd = f'--replace-in-metadata {shell_quote(f)}'
  361. actions = ((MetadataParserPP.Actions.REPLACE, x, *f[1:]) for x in f[0].split(','))
  362. for action in actions:
  363. try:
  364. MetadataParserPP.validate_action(*action)
  365. except Exception as err:
  366. raise ValueError(f'{cmd} is invalid; {err}')
  367. yield action
  368. if opts.metafromtitle is not None:
  369. opts.parse_metadata.setdefault('pre_process', []).append(f'title:{opts.metafromtitle}')
  370. opts.parse_metadata = {
  371. k: list(itertools.chain(*map(metadataparser_actions, v)))
  372. for k, v in opts.parse_metadata.items()
  373. }
  374. # Other options
  375. if opts.playlist_items is not None:
  376. try:
  377. tuple(PlaylistEntries.parse_playlist_items(opts.playlist_items))
  378. except Exception as err:
  379. raise ValueError(f'Invalid playlist-items {opts.playlist_items!r}: {err}')
  380. opts.geo_bypass_country, opts.geo_bypass_ip_block = None, None
  381. if opts.geo_bypass.lower() not in ('default', 'never'):
  382. try:
  383. GeoUtils.random_ipv4(opts.geo_bypass)
  384. except Exception:
  385. raise ValueError(f'Unsupported --xff "{opts.geo_bypass}"')
  386. if len(opts.geo_bypass) == 2:
  387. opts.geo_bypass_country = opts.geo_bypass
  388. else:
  389. opts.geo_bypass_ip_block = opts.geo_bypass
  390. opts.geo_bypass = opts.geo_bypass.lower() != 'never'
  391. opts.match_filter = match_filter_func(opts.match_filter, opts.breaking_match_filter)
  392. if opts.download_archive is not None:
  393. opts.download_archive = expand_path(opts.download_archive)
  394. if opts.ffmpeg_location is not None:
  395. opts.ffmpeg_location = expand_path(opts.ffmpeg_location)
  396. if opts.user_agent is not None:
  397. opts.headers.setdefault('User-Agent', opts.user_agent)
  398. if opts.referer is not None:
  399. opts.headers.setdefault('Referer', opts.referer)
  400. if opts.no_sponsorblock:
  401. opts.sponsorblock_mark = opts.sponsorblock_remove = set()
  402. default_downloader = None
  403. for proto, path in opts.external_downloader.items():
  404. if path == 'native':
  405. continue
  406. ed = get_external_downloader(path)
  407. if ed is None:
  408. raise ValueError(
  409. f'No such {format_field(proto, None, "%s ", ignore="default")}external downloader "{path}"')
  410. elif ed and proto == 'default':
  411. default_downloader = ed.get_basename()
  412. for policy in opts.color.values():
  413. if policy not in ('always', 'auto', 'no_color', 'never'):
  414. raise ValueError(f'"{policy}" is not a valid color policy')
  415. warnings, deprecation_warnings = [], []
  416. # Common mistake: -f best
  417. if opts.format == 'best':
  418. warnings.append('.\n '.join((
  419. '"-f best" selects the best pre-merged format which is often not the best option',
  420. 'To let yt-dlp download and merge the best available formats, simply do not pass any format selection',
  421. 'If you know what you are doing and want only the best pre-merged format, use "-f b" instead to suppress this warning')))
  422. # --(postprocessor/downloader)-args without name
  423. def report_args_compat(name, value, key1, key2=None, where=None):
  424. if key1 in value and key2 not in value:
  425. warnings.append(f'{name.title()} arguments given without specifying name. '
  426. f'The arguments will be given to {where or f"all {name}s"}')
  427. return True
  428. return False
  429. if report_args_compat('external downloader', opts.external_downloader_args,
  430. 'default', where=default_downloader) and default_downloader:
  431. # Compat with youtube-dl's behavior. See https://github.com/ytdl-org/youtube-dl/commit/49c5293014bc11ec8c009856cd63cffa6296c1e1
  432. opts.external_downloader_args.setdefault(default_downloader, opts.external_downloader_args.pop('default'))
  433. if report_args_compat('post-processor', opts.postprocessor_args, 'default-compat', 'default'):
  434. opts.postprocessor_args['default'] = opts.postprocessor_args.pop('default-compat')
  435. opts.postprocessor_args.setdefault('sponskrub', [])
  436. def report_conflict(arg1, opt1, arg2='--allow-unplayable-formats', opt2='allow_unplayable_formats',
  437. val1=NO_DEFAULT, val2=NO_DEFAULT, default=False):
  438. if val2 is NO_DEFAULT:
  439. val2 = getattr(opts, opt2)
  440. if not val2:
  441. return
  442. if val1 is NO_DEFAULT:
  443. val1 = getattr(opts, opt1)
  444. if val1:
  445. warnings.append(f'{arg1} is ignored since {arg2} was given')
  446. setattr(opts, opt1, default)
  447. # Conflicting options
  448. report_conflict('--playlist-reverse', 'playlist_reverse', '--playlist-random', 'playlist_random')
  449. report_conflict('--playlist-reverse', 'playlist_reverse', '--lazy-playlist', 'lazy_playlist')
  450. report_conflict('--playlist-random', 'playlist_random', '--lazy-playlist', 'lazy_playlist')
  451. report_conflict('--dateafter', 'dateafter', '--date', 'date', default=None)
  452. report_conflict('--datebefore', 'datebefore', '--date', 'date', default=None)
  453. report_conflict('--exec-before-download', 'exec_before_dl_cmd',
  454. '"--exec before_dl:"', 'exec_cmd', val2=opts.exec_cmd.get('before_dl'))
  455. report_conflict('--id', 'useid', '--output', 'outtmpl', val2=opts.outtmpl.get('default'))
  456. report_conflict('--remux-video', 'remuxvideo', '--recode-video', 'recodevideo')
  457. report_conflict('--sponskrub', 'sponskrub', '--remove-chapters', 'remove_chapters')
  458. report_conflict('--sponskrub', 'sponskrub', '--sponsorblock-mark', 'sponsorblock_mark')
  459. report_conflict('--sponskrub', 'sponskrub', '--sponsorblock-remove', 'sponsorblock_remove')
  460. report_conflict('--sponskrub-cut', 'sponskrub_cut', '--split-chapter', 'split_chapters',
  461. val1=opts.sponskrub and opts.sponskrub_cut)
  462. # Conflicts with --allow-unplayable-formats
  463. report_conflict('--embed-metadata', 'addmetadata')
  464. report_conflict('--embed-chapters', 'addchapters')
  465. report_conflict('--embed-info-json', 'embed_infojson')
  466. report_conflict('--embed-subs', 'embedsubtitles')
  467. report_conflict('--embed-thumbnail', 'embedthumbnail')
  468. report_conflict('--extract-audio', 'extractaudio')
  469. report_conflict('--fixup', 'fixup', val1=opts.fixup not in (None, 'never', 'ignore'), default='never')
  470. report_conflict('--recode-video', 'recodevideo')
  471. report_conflict('--remove-chapters', 'remove_chapters', default=[])
  472. report_conflict('--remux-video', 'remuxvideo')
  473. report_conflict('--sponskrub', 'sponskrub')
  474. report_conflict('--sponsorblock-remove', 'sponsorblock_remove', default=set())
  475. report_conflict('--xattrs', 'xattrs')
  476. # Fully deprecated options
  477. def report_deprecation(val, old, new=None):
  478. if not val:
  479. return
  480. deprecation_warnings.append(
  481. f'{old} is deprecated and may be removed in a future version. Use {new} instead' if new
  482. else f'{old} is deprecated and may not work as expected')
  483. report_deprecation(opts.sponskrub, '--sponskrub', '--sponsorblock-mark or --sponsorblock-remove')
  484. report_deprecation(not opts.prefer_ffmpeg, '--prefer-avconv', 'ffmpeg')
  485. # report_deprecation(opts.include_ads, '--include-ads') # We may re-implement this in future
  486. # report_deprecation(opts.call_home, '--call-home') # We may re-implement this in future
  487. # report_deprecation(opts.writeannotations, '--write-annotations') # It's just that no website has it
  488. # Dependent options
  489. opts.date = DateRange.day(opts.date) if opts.date else DateRange(opts.dateafter, opts.datebefore)
  490. if opts.exec_before_dl_cmd:
  491. opts.exec_cmd['before_dl'] = opts.exec_before_dl_cmd
  492. if opts.useid: # --id is not deprecated in youtube-dl
  493. opts.outtmpl['default'] = '%(id)s.%(ext)s'
  494. if opts.overwrites: # --force-overwrites implies --no-continue
  495. opts.continue_dl = False
  496. if (opts.addmetadata or opts.sponsorblock_mark) and opts.addchapters is None:
  497. # Add chapters when adding metadata or marking sponsors
  498. opts.addchapters = True
  499. if opts.extractaudio and not opts.keepvideo and opts.format is None:
  500. # Do not unnecessarily download audio
  501. opts.format = 'bestaudio/best'
  502. if opts.getcomments and opts.writeinfojson is None and not opts.embed_infojson:
  503. # If JSON is not printed anywhere, but comments are requested, save it to file
  504. if not opts.dumpjson or opts.print_json or opts.dump_single_json:
  505. opts.writeinfojson = True
  506. if opts.allsubtitles and not (opts.embedsubtitles or opts.writeautomaticsub):
  507. # --all-sub automatically sets --write-sub if --write-auto-sub is not given
  508. opts.writesubtitles = True
  509. if opts.addmetadata and opts.embed_infojson is None:
  510. # If embedding metadata and infojson is present, embed it
  511. opts.embed_infojson = 'if_exists'
  512. # Ask for passwords
  513. if opts.username is not None and opts.password is None:
  514. opts.password = getpass.getpass('Type account password and press [Return]: ')
  515. if opts.ap_username is not None and opts.ap_password is None:
  516. opts.ap_password = getpass.getpass('Type TV provider account password and press [Return]: ')
  517. # compat option changes global state destructively; only allow from cli
  518. if 'allow-unsafe-ext' in opts.compat_opts:
  519. warnings.append(
  520. 'Using allow-unsafe-ext opens you up to potential attacks. '
  521. 'Use with great care!')
  522. _UnsafeExtensionError.sanitize_extension = lambda x, prepend=False: x
  523. return warnings, deprecation_warnings
  524. def get_postprocessors(opts):
  525. yield from opts.add_postprocessors
  526. for when, actions in opts.parse_metadata.items():
  527. yield {
  528. 'key': 'MetadataParser',
  529. 'actions': actions,
  530. 'when': when,
  531. }
  532. sponsorblock_query = opts.sponsorblock_mark | opts.sponsorblock_remove
  533. if sponsorblock_query:
  534. yield {
  535. 'key': 'SponsorBlock',
  536. 'categories': sponsorblock_query,
  537. 'api': opts.sponsorblock_api,
  538. 'when': 'after_filter',
  539. }
  540. if opts.convertsubtitles:
  541. yield {
  542. 'key': 'FFmpegSubtitlesConvertor',
  543. 'format': opts.convertsubtitles,
  544. 'when': 'before_dl',
  545. }
  546. if opts.convertthumbnails:
  547. yield {
  548. 'key': 'FFmpegThumbnailsConvertor',
  549. 'format': opts.convertthumbnails,
  550. 'when': 'before_dl',
  551. }
  552. if opts.extractaudio:
  553. yield {
  554. 'key': 'FFmpegExtractAudio',
  555. 'preferredcodec': opts.audioformat,
  556. 'preferredquality': opts.audioquality,
  557. 'nopostoverwrites': opts.nopostoverwrites,
  558. }
  559. if opts.remuxvideo:
  560. yield {
  561. 'key': 'FFmpegVideoRemuxer',
  562. 'preferedformat': opts.remuxvideo,
  563. }
  564. if opts.recodevideo:
  565. yield {
  566. 'key': 'FFmpegVideoConvertor',
  567. 'preferedformat': opts.recodevideo,
  568. }
  569. # If ModifyChapters is going to remove chapters, subtitles must already be in the container.
  570. if opts.embedsubtitles:
  571. keep_subs = 'no-keep-subs' not in opts.compat_opts
  572. yield {
  573. 'key': 'FFmpegEmbedSubtitle',
  574. # already_have_subtitle = True prevents the file from being deleted after embedding
  575. 'already_have_subtitle': opts.writesubtitles and keep_subs,
  576. }
  577. if not opts.writeautomaticsub and keep_subs:
  578. opts.writesubtitles = True
  579. # ModifyChapters must run before FFmpegMetadataPP
  580. if opts.remove_chapters or sponsorblock_query:
  581. yield {
  582. 'key': 'ModifyChapters',
  583. 'remove_chapters_patterns': opts.remove_chapters,
  584. 'remove_sponsor_segments': opts.sponsorblock_remove,
  585. 'remove_ranges': opts.remove_ranges,
  586. 'sponsorblock_chapter_title': opts.sponsorblock_chapter_title,
  587. 'force_keyframes': opts.force_keyframes_at_cuts,
  588. }
  589. # FFmpegMetadataPP should be run after FFmpegVideoConvertorPP and
  590. # FFmpegExtractAudioPP as containers before conversion may not support
  591. # metadata (3gp, webm, etc.)
  592. # By default ffmpeg preserves metadata applicable for both
  593. # source and target containers. From this point the container won't change,
  594. # so metadata can be added here.
  595. if opts.addmetadata or opts.addchapters or opts.embed_infojson:
  596. yield {
  597. 'key': 'FFmpegMetadata',
  598. 'add_chapters': opts.addchapters,
  599. 'add_metadata': opts.addmetadata,
  600. 'add_infojson': opts.embed_infojson,
  601. }
  602. # Deprecated
  603. # This should be above EmbedThumbnail since sponskrub removes the thumbnail attachment
  604. # but must be below EmbedSubtitle and FFmpegMetadata
  605. # See https://github.com/yt-dlp/yt-dlp/issues/204 , https://github.com/faissaloo/SponSkrub/issues/29
  606. # If opts.sponskrub is None, sponskrub is used, but it silently fails if the executable can't be found
  607. if opts.sponskrub is not False:
  608. yield {
  609. 'key': 'SponSkrub',
  610. 'path': opts.sponskrub_path,
  611. 'args': opts.sponskrub_args,
  612. 'cut': opts.sponskrub_cut,
  613. 'force': opts.sponskrub_force,
  614. 'ignoreerror': opts.sponskrub is None,
  615. '_from_cli': True,
  616. }
  617. if opts.embedthumbnail:
  618. yield {
  619. 'key': 'EmbedThumbnail',
  620. # already_have_thumbnail = True prevents the file from being deleted after embedding
  621. 'already_have_thumbnail': opts.writethumbnail,
  622. }
  623. if not opts.writethumbnail:
  624. opts.writethumbnail = True
  625. opts.outtmpl['pl_thumbnail'] = ''
  626. if opts.split_chapters:
  627. yield {
  628. 'key': 'FFmpegSplitChapters',
  629. 'force_keyframes': opts.force_keyframes_at_cuts,
  630. }
  631. # XAttrMetadataPP should be run after post-processors that may change file contents
  632. if opts.xattrs:
  633. yield {'key': 'XAttrMetadata'}
  634. if opts.concat_playlist != 'never':
  635. yield {
  636. 'key': 'FFmpegConcat',
  637. 'only_multi_video': opts.concat_playlist != 'always',
  638. 'when': 'playlist',
  639. }
  640. # Exec must be the last PP of each category
  641. for when, exec_cmd in opts.exec_cmd.items():
  642. yield {
  643. 'key': 'Exec',
  644. 'exec_cmd': exec_cmd,
  645. 'when': when,
  646. }
  647. ParsedOptions = collections.namedtuple('ParsedOptions', ('parser', 'options', 'urls', 'ydl_opts'))
  648. def parse_options(argv=None):
  649. """@returns ParsedOptions(parser, opts, urls, ydl_opts)"""
  650. parser, opts, urls = parseOpts(argv)
  651. urls = get_urls(urls, opts.batchfile, -1 if opts.quiet and not opts.verbose else opts.verbose)
  652. set_compat_opts(opts)
  653. try:
  654. warnings, deprecation_warnings = validate_options(opts)
  655. except ValueError as err:
  656. parser.error(f'{err}\n')
  657. postprocessors = list(get_postprocessors(opts))
  658. print_only = bool(opts.forceprint) and all(k not in opts.forceprint for k in POSTPROCESS_WHEN[3:])
  659. any_getting = any(getattr(opts, k) for k in (
  660. 'dumpjson', 'dump_single_json', 'getdescription', 'getduration', 'getfilename',
  661. 'getformat', 'getid', 'getthumbnail', 'gettitle', 'geturl',
  662. ))
  663. if opts.quiet is None:
  664. opts.quiet = any_getting or opts.print_json or bool(opts.forceprint)
  665. playlist_pps = [pp for pp in postprocessors if pp.get('when') == 'playlist']
  666. write_playlist_infojson = (opts.writeinfojson and not opts.clean_infojson
  667. and opts.allow_playlist_files and opts.outtmpl.get('pl_infojson') != '')
  668. if not any((
  669. opts.extract_flat,
  670. opts.dump_single_json,
  671. opts.forceprint.get('playlist'),
  672. opts.print_to_file.get('playlist'),
  673. write_playlist_infojson,
  674. )):
  675. if not playlist_pps:
  676. opts.extract_flat = 'discard'
  677. elif playlist_pps == [{'key': 'FFmpegConcat', 'only_multi_video': True, 'when': 'playlist'}]:
  678. opts.extract_flat = 'discard_in_playlist'
  679. final_ext = (
  680. opts.recodevideo if opts.recodevideo in FFmpegVideoConvertorPP.SUPPORTED_EXTS
  681. else opts.remuxvideo if opts.remuxvideo in FFmpegVideoRemuxerPP.SUPPORTED_EXTS
  682. else opts.audioformat if (opts.extractaudio and opts.audioformat in FFmpegExtractAudioPP.SUPPORTED_EXTS)
  683. else None)
  684. return ParsedOptions(parser, opts, urls, {
  685. 'usenetrc': opts.usenetrc,
  686. 'netrc_location': opts.netrc_location,
  687. 'netrc_cmd': opts.netrc_cmd,
  688. 'username': opts.username,
  689. 'password': opts.password,
  690. 'twofactor': opts.twofactor,
  691. 'videopassword': opts.videopassword,
  692. 'ap_mso': opts.ap_mso,
  693. 'ap_username': opts.ap_username,
  694. 'ap_password': opts.ap_password,
  695. 'client_certificate': opts.client_certificate,
  696. 'client_certificate_key': opts.client_certificate_key,
  697. 'client_certificate_password': opts.client_certificate_password,
  698. 'quiet': opts.quiet,
  699. 'no_warnings': opts.no_warnings,
  700. 'forceurl': opts.geturl,
  701. 'forcetitle': opts.gettitle,
  702. 'forceid': opts.getid,
  703. 'forcethumbnail': opts.getthumbnail,
  704. 'forcedescription': opts.getdescription,
  705. 'forceduration': opts.getduration,
  706. 'forcefilename': opts.getfilename,
  707. 'forceformat': opts.getformat,
  708. 'forceprint': opts.forceprint,
  709. 'print_to_file': opts.print_to_file,
  710. 'forcejson': opts.dumpjson or opts.print_json,
  711. 'dump_single_json': opts.dump_single_json,
  712. 'force_write_download_archive': opts.force_write_download_archive,
  713. 'simulate': (print_only or any_getting or None) if opts.simulate is None else opts.simulate,
  714. 'skip_download': opts.skip_download,
  715. 'format': opts.format,
  716. 'allow_unplayable_formats': opts.allow_unplayable_formats,
  717. 'ignore_no_formats_error': opts.ignore_no_formats_error,
  718. 'format_sort': opts.format_sort,
  719. 'format_sort_force': opts.format_sort_force,
  720. 'allow_multiple_video_streams': opts.allow_multiple_video_streams,
  721. 'allow_multiple_audio_streams': opts.allow_multiple_audio_streams,
  722. 'check_formats': opts.check_formats,
  723. 'listformats': opts.listformats,
  724. 'listformats_table': opts.listformats_table,
  725. 'outtmpl': opts.outtmpl,
  726. 'outtmpl_na_placeholder': opts.outtmpl_na_placeholder,
  727. 'paths': opts.paths,
  728. 'autonumber_size': opts.autonumber_size,
  729. 'autonumber_start': opts.autonumber_start,
  730. 'restrictfilenames': opts.restrictfilenames,
  731. 'windowsfilenames': opts.windowsfilenames,
  732. 'ignoreerrors': opts.ignoreerrors,
  733. 'force_generic_extractor': opts.force_generic_extractor,
  734. 'allowed_extractors': opts.allowed_extractors or ['default'],
  735. 'ratelimit': opts.ratelimit,
  736. 'throttledratelimit': opts.throttledratelimit,
  737. 'overwrites': opts.overwrites,
  738. 'retries': opts.retries,
  739. 'file_access_retries': opts.file_access_retries,
  740. 'fragment_retries': opts.fragment_retries,
  741. 'extractor_retries': opts.extractor_retries,
  742. 'retry_sleep_functions': opts.retry_sleep,
  743. 'skip_unavailable_fragments': opts.skip_unavailable_fragments,
  744. 'keep_fragments': opts.keep_fragments,
  745. 'concurrent_fragment_downloads': opts.concurrent_fragment_downloads,
  746. 'buffersize': opts.buffersize,
  747. 'noresizebuffer': opts.noresizebuffer,
  748. 'http_chunk_size': opts.http_chunk_size,
  749. 'continuedl': opts.continue_dl,
  750. 'noprogress': opts.quiet if opts.noprogress is None else opts.noprogress,
  751. 'progress_with_newline': opts.progress_with_newline,
  752. 'progress_template': opts.progress_template,
  753. 'progress_delta': opts.progress_delta,
  754. 'playliststart': opts.playliststart,
  755. 'playlistend': opts.playlistend,
  756. 'playlistreverse': opts.playlist_reverse,
  757. 'playlistrandom': opts.playlist_random,
  758. 'lazy_playlist': opts.lazy_playlist,
  759. 'noplaylist': opts.noplaylist,
  760. 'logtostderr': opts.outtmpl.get('default') == '-',
  761. 'consoletitle': opts.consoletitle,
  762. 'nopart': opts.nopart,
  763. 'updatetime': opts.updatetime,
  764. 'writedescription': opts.writedescription,
  765. 'writeannotations': opts.writeannotations,
  766. 'writeinfojson': opts.writeinfojson,
  767. 'allow_playlist_files': opts.allow_playlist_files,
  768. 'clean_infojson': opts.clean_infojson,
  769. 'getcomments': opts.getcomments,
  770. 'writethumbnail': opts.writethumbnail is True,
  771. 'write_all_thumbnails': opts.writethumbnail == 'all',
  772. 'writelink': opts.writelink,
  773. 'writeurllink': opts.writeurllink,
  774. 'writewebloclink': opts.writewebloclink,
  775. 'writedesktoplink': opts.writedesktoplink,
  776. 'writesubtitles': opts.writesubtitles,
  777. 'writeautomaticsub': opts.writeautomaticsub,
  778. 'allsubtitles': opts.allsubtitles,
  779. 'listsubtitles': opts.listsubtitles,
  780. 'subtitlesformat': opts.subtitlesformat,
  781. 'subtitleslangs': opts.subtitleslangs,
  782. 'matchtitle': decodeOption(opts.matchtitle),
  783. 'rejecttitle': decodeOption(opts.rejecttitle),
  784. 'max_downloads': opts.max_downloads,
  785. 'prefer_free_formats': opts.prefer_free_formats,
  786. 'trim_file_name': opts.trim_file_name,
  787. 'verbose': opts.verbose,
  788. 'dump_intermediate_pages': opts.dump_intermediate_pages,
  789. 'write_pages': opts.write_pages,
  790. 'load_pages': opts.load_pages,
  791. 'test': opts.test,
  792. 'keepvideo': opts.keepvideo,
  793. 'min_filesize': opts.min_filesize,
  794. 'max_filesize': opts.max_filesize,
  795. 'min_views': opts.min_views,
  796. 'max_views': opts.max_views,
  797. 'daterange': opts.date,
  798. 'cachedir': opts.cachedir,
  799. 'youtube_print_sig_code': opts.youtube_print_sig_code,
  800. 'age_limit': opts.age_limit,
  801. 'download_archive': opts.download_archive,
  802. 'break_on_existing': opts.break_on_existing,
  803. 'break_on_reject': opts.break_on_reject,
  804. 'break_per_url': opts.break_per_url,
  805. 'skip_playlist_after_errors': opts.skip_playlist_after_errors,
  806. 'cookiefile': opts.cookiefile,
  807. 'cookiesfrombrowser': opts.cookiesfrombrowser,
  808. 'legacyserverconnect': opts.legacy_server_connect,
  809. 'nocheckcertificate': opts.no_check_certificate,
  810. 'prefer_insecure': opts.prefer_insecure,
  811. 'enable_file_urls': opts.enable_file_urls,
  812. 'http_headers': opts.headers,
  813. 'proxy': opts.proxy,
  814. 'socket_timeout': opts.socket_timeout,
  815. 'bidi_workaround': opts.bidi_workaround,
  816. 'debug_printtraffic': opts.debug_printtraffic,
  817. 'prefer_ffmpeg': opts.prefer_ffmpeg,
  818. 'include_ads': opts.include_ads,
  819. 'default_search': opts.default_search,
  820. 'dynamic_mpd': opts.dynamic_mpd,
  821. 'extractor_args': opts.extractor_args,
  822. 'youtube_include_dash_manifest': opts.youtube_include_dash_manifest,
  823. 'youtube_include_hls_manifest': opts.youtube_include_hls_manifest,
  824. 'encoding': opts.encoding,
  825. 'extract_flat': opts.extract_flat,
  826. 'live_from_start': opts.live_from_start,
  827. 'wait_for_video': opts.wait_for_video,
  828. 'mark_watched': opts.mark_watched,
  829. 'merge_output_format': opts.merge_output_format,
  830. 'final_ext': final_ext,
  831. 'postprocessors': postprocessors,
  832. 'fixup': opts.fixup,
  833. 'source_address': opts.source_address,
  834. 'impersonate': opts.impersonate,
  835. 'call_home': opts.call_home,
  836. 'sleep_interval_requests': opts.sleep_interval_requests,
  837. 'sleep_interval': opts.sleep_interval,
  838. 'max_sleep_interval': opts.max_sleep_interval,
  839. 'sleep_interval_subtitles': opts.sleep_interval_subtitles,
  840. 'external_downloader': opts.external_downloader,
  841. 'download_ranges': opts.download_ranges,
  842. 'force_keyframes_at_cuts': opts.force_keyframes_at_cuts,
  843. 'list_thumbnails': opts.list_thumbnails,
  844. 'playlist_items': opts.playlist_items,
  845. 'xattr_set_filesize': opts.xattr_set_filesize,
  846. 'match_filter': opts.match_filter,
  847. 'color': opts.color,
  848. 'ffmpeg_location': opts.ffmpeg_location,
  849. 'hls_prefer_native': opts.hls_prefer_native,
  850. 'hls_use_mpegts': opts.hls_use_mpegts,
  851. 'hls_split_discontinuity': opts.hls_split_discontinuity,
  852. 'external_downloader_args': opts.external_downloader_args,
  853. 'postprocessor_args': opts.postprocessor_args,
  854. 'cn_verification_proxy': opts.cn_verification_proxy,
  855. 'geo_verification_proxy': opts.geo_verification_proxy,
  856. 'geo_bypass': opts.geo_bypass,
  857. 'geo_bypass_country': opts.geo_bypass_country,
  858. 'geo_bypass_ip_block': opts.geo_bypass_ip_block,
  859. '_warnings': warnings,
  860. '_deprecation_warnings': deprecation_warnings,
  861. 'compat_opts': opts.compat_opts,
  862. })
  863. def _real_main(argv=None):
  864. setproctitle('yt-dlp')
  865. parser, opts, all_urls, ydl_opts = parse_options(argv)
  866. # Dump user agent
  867. if opts.dump_user_agent:
  868. ua = traverse_obj(opts.headers, 'User-Agent', casesense=False, default=std_headers['User-Agent'])
  869. write_string(f'{ua}\n', out=sys.stdout)
  870. return
  871. if print_extractor_information(opts, all_urls):
  872. return
  873. # We may need ffmpeg_location without having access to the YoutubeDL instance
  874. # See https://github.com/yt-dlp/yt-dlp/issues/2191
  875. if opts.ffmpeg_location:
  876. FFmpegPostProcessor._ffmpeg_location.set(opts.ffmpeg_location)
  877. with YoutubeDL(ydl_opts) as ydl:
  878. pre_process = opts.update_self or opts.rm_cachedir
  879. actual_use = all_urls or opts.load_info_filename
  880. if opts.rm_cachedir:
  881. ydl.cache.remove()
  882. try:
  883. updater = Updater(ydl, opts.update_self)
  884. if opts.update_self and updater.update() and actual_use:
  885. if updater.cmd:
  886. return updater.restart()
  887. # This code is reachable only for zip variant in py < 3.10
  888. # It makes sense to exit here, but the old behavior is to continue
  889. ydl.report_warning('Restart yt-dlp to use the updated version')
  890. # return 100, 'ERROR: The program must exit for the update to complete'
  891. except Exception:
  892. traceback.print_exc()
  893. ydl._download_retcode = 100
  894. if opts.list_impersonate_targets:
  895. known_targets = [
  896. # List of simplified targets we know are supported,
  897. # to help users know what dependencies may be required.
  898. (ImpersonateTarget('chrome'), 'curl_cffi'),
  899. (ImpersonateTarget('edge'), 'curl_cffi'),
  900. (ImpersonateTarget('safari'), 'curl_cffi'),
  901. ]
  902. available_targets = ydl._get_available_impersonate_targets()
  903. def make_row(target, handler):
  904. return [
  905. join_nonempty(target.client.title(), target.version, delim='-') or '-',
  906. join_nonempty((target.os or '').title(), target.os_version, delim='-') or '-',
  907. handler,
  908. ]
  909. rows = [make_row(target, handler) for target, handler in available_targets]
  910. for known_target, known_handler in known_targets:
  911. if not any(
  912. known_target in target and handler == known_handler
  913. for target, handler in available_targets
  914. ):
  915. rows.append([
  916. ydl._format_out(text, ydl.Styles.SUPPRESS)
  917. for text in make_row(known_target, f'{known_handler} (not available)')
  918. ])
  919. ydl.to_screen('[info] Available impersonate targets')
  920. ydl.to_stdout(render_table(['Client', 'OS', 'Source'], rows, extra_gap=2, delim='-'))
  921. return
  922. if not actual_use:
  923. if pre_process:
  924. return ydl._download_retcode
  925. args = sys.argv[1:] if argv is None else argv
  926. ydl.warn_if_short_id(args)
  927. # Show a useful error message and wait for keypress if not launched from shell on Windows
  928. if not args and compat_os_name == 'nt' and getattr(sys, 'frozen', False):
  929. import ctypes.wintypes
  930. import msvcrt
  931. kernel32 = ctypes.WinDLL('Kernel32')
  932. buffer = (1 * ctypes.wintypes.DWORD)()
  933. attached_processes = kernel32.GetConsoleProcessList(buffer, 1)
  934. # If we only have a single process attached, then the executable was double clicked
  935. # When using `pyinstaller` with `--onefile`, two processes get attached
  936. is_onefile = hasattr(sys, '_MEIPASS') and os.path.basename(sys._MEIPASS).startswith('_MEI')
  937. if attached_processes == 1 or is_onefile and attached_processes == 2:
  938. print(parser._generate_error_message(
  939. 'Do not double-click the executable, instead call it from a command line.\n'
  940. 'Please read the README for further information on how to use yt-dlp: '
  941. 'https://github.com/yt-dlp/yt-dlp#readme'))
  942. msvcrt.getch()
  943. _exit(2)
  944. parser.error(
  945. 'You must provide at least one URL.\n'
  946. 'Type yt-dlp --help to see a list of all options.')
  947. parser.destroy()
  948. try:
  949. if opts.load_info_filename is not None:
  950. if all_urls:
  951. ydl.report_warning('URLs are ignored due to --load-info-json')
  952. return ydl.download_with_info_file(expand_path(opts.load_info_filename))
  953. else:
  954. return ydl.download(all_urls)
  955. except DownloadCancelled:
  956. ydl.to_screen('Aborting remaining downloads')
  957. return 101
  958. def main(argv=None):
  959. global _IN_CLI
  960. _IN_CLI = True
  961. try:
  962. _exit(*variadic(_real_main(argv)))
  963. except DownloadError:
  964. _exit(1)
  965. except SameFileError as e:
  966. _exit(f'ERROR: {e}')
  967. except KeyboardInterrupt:
  968. _exit('\nERROR: Interrupted by user')
  969. except BrokenPipeError as e:
  970. # https://docs.python.org/3/library/signal.html#note-on-sigpipe
  971. devnull = os.open(os.devnull, os.O_WRONLY)
  972. os.dup2(devnull, sys.stdout.fileno())
  973. _exit(f'\nERROR: {e}')
  974. except optparse.OptParseError as e:
  975. _exit(2, f'\n{e}')
  976. from .extractor import gen_extractors, list_extractors
  977. __all__ = [
  978. 'main',
  979. 'YoutubeDL',
  980. 'parse_options',
  981. 'gen_extractors',
  982. 'list_extractors',
  983. ]