mirror of
https://github.com/yt-dlp/yt-dlp.git
synced 2025-05-19 17:35:43 -05:00
Add --preset-alias
option (#12839)
Authored by: seproDev, Grub4K Co-authored-by: Simon Sawicki <contact@grub4k.xyz>
This commit is contained in:
parent
f5a37ea40e
commit
88eb1e7a9a
1 changed files with 41 additions and 0 deletions
|
@ -150,6 +150,15 @@ def format_option_strings(option):
|
||||||
return opts
|
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):
|
class _YoutubeDLOptionParser(optparse.OptionParser):
|
||||||
# optparse is deprecated since Python 3.2. So assume a stable interface even for private methods
|
# optparse is deprecated since Python 3.2. So assume a stable interface even for private methods
|
||||||
ALIAS_DEST = '_triggered_aliases'
|
ALIAS_DEST = '_triggered_aliases'
|
||||||
|
@ -215,6 +224,22 @@ def _match_long_opt(self, opt):
|
||||||
return e.possibilities[0]
|
return e.possibilities[0]
|
||||||
raise
|
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 create_parser():
|
||||||
def _list_from_options_callback(option, opt_str, value, parser, append=True, delim=',', process=str.strip):
|
def _list_from_options_callback(option, opt_str, value, parser, append=True, delim=',', process=str.strip):
|
||||||
|
@ -317,6 +342,13 @@ def _alias_callback(option, opt_str, value, parser, opts, nargs):
|
||||||
parser.rargs[:0] = shlex.split(
|
parser.rargs[:0] = shlex.split(
|
||||||
opts if value is None else opts.format(*map(shlex.quote, value)))
|
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 = optparse.OptionGroup(parser, 'General Options')
|
||||||
general.add_option(
|
general.add_option(
|
||||||
'-h', '--help', dest='print_help', action='store_true',
|
'-h', '--help', dest='print_help', action='store_true',
|
||||||
|
@ -519,6 +551,15 @@ def _alias_callback(option, opt_str, value, parser, opts, nargs):
|
||||||
'Alias options can trigger more aliases; so be careful to avoid defining recursive options. '
|
'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. '
|
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'))
|
'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 = optparse.OptionGroup(parser, 'Network Options')
|
||||||
network.add_option(
|
network.add_option(
|
||||||
|
|
Loading…
Reference in a new issue