A feature-rich command-line audio/video downloader
Add `--preset-alias` option (#12839)
sepro88eb1e7a9a27
diff --git a/yt_dlp/options.py b/yt_dlp/options.pyindex 1742cbdfaf..76d401ceaa 100644--- a/yt_dlp/options.py+++ b/yt_dlp/options.py@@ -150,6 +150,15 @@ class _YoutubeDLHelpFormatter(optparse.IndentedHelpFormatter):return opts+_PRESET_ALIASES = {+ 'mp3': ['-f', 'ba[acodec^=mp3]/ba/b', '-x', '--audio-format', 'mp3'],+ 'aac': ['-f', 'ba[acodec^=aac]/ba[acodec^=mp4a.40.]/ba/b', '-x', '--audio-format', 'aac'],+ 'mp4': ['--merge-output-format', 'mp4', '--remux-video', 'mp4', '-S', 'vcodec:h264,lang,quality,res,fps,hdr:12,acodec:aac'],+ 'mkv': ['--merge-output-format', 'mkv', '--remux-video', 'mkv'],+ 'sleep': ['--sleep-subtitles', '5', '--sleep-requests', '0.75', '--sleep-interval', '10', '--max-sleep-interval', '20'],+}++class _YoutubeDLOptionParser(optparse.OptionParser):# optparse is deprecated since Python 3.2. So assume a stable interface even for private methodsALIAS_DEST = '_triggered_aliases'@@ -215,6 +224,22 @@ class _YoutubeDLOptionParser(optparse.OptionParser):return e.possibilities[0]raise+ def format_option_help(self, formatter=None):+ assert formatter, 'Formatter can not be None'+ formatted_help = super().format_option_help(formatter=formatter)+ formatter.indent()+ heading = formatter.format_heading('Preset Aliases')+ formatter.indent()+ result = []+ for name, args in _PRESET_ALIASES.items():+ option = optparse.Option('-t', help=shlex.join(args))+ formatter.option_strings[option] = f'-t {name}'+ result.append(formatter.format_option(option))+ formatter.dedent()+ formatter.dedent()+ help_lines = '\n'.join(result)+ return f'{formatted_help}\n{heading}{help_lines}'+def create_parser():def _list_from_options_callback(option, opt_str, value, parser, append=True, delim=',', process=str.strip):@@ -317,6 +342,13 @@ def create_parser():parser.rargs[:0] = shlex.split(opts if value is None else opts.format(*map(shlex.quote, value)))+ def _preset_alias_callback(option, opt_str, value, parser):+ if not value:+ return+ if value not in _PRESET_ALIASES:+ raise optparse.OptionValueError(f'Unknown preset alias: {value}')+ parser.rargs[:0] = _PRESET_ALIASES[value]+general = optparse.OptionGroup(parser, 'General Options')general.add_option('-h', '--help', dest='print_help', action='store_true',@@ -519,6 +551,15 @@ def create_parser():'Alias options can trigger more aliases; so be careful to avoid defining recursive options. 'f'As a safety measure, each alias may be triggered a maximum of {_YoutubeDLOptionParser.ALIAS_TRIGGER_LIMIT} times. ''This option can be used multiple times'))+ general.add_option(+ '-t', '--preset-alias',+ metavar='PRESET', dest='_', type='str',+ action='callback', callback=_preset_alias_callback,+ help=(+ 'Applies a predefined set of options. e.g. --preset-alias mp3. '+ f'The following presets are available: {", ".join(_PRESET_ALIASES)}. '+ 'See the "Preset Aliases" section at the end for more info. '+ 'This option can be used multiple times'))network = optparse.OptionGroup(parser, 'Network Options')network.add_option(