__init__.py 43 KB

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