ytdl-sub/src/ytdl_sub/plugins/format.py
Jesse Bannon a48efdc84c
[FEATURE] format plugin to set yt-dlp format easier (#714)
Makes setting yt-dlp's `format` field easier by giving it its own documented plugin.

Old:
```
my_format_preset:
  ytdl_options:
    format: "best"
```

New:
```
my_format_preset:
  format: "best"
```

The old method will still work, so do not worry about updating configs ASAP. However, the option is available to save a few lines 😉
2023-09-13 00:02:19 -07:00

44 lines
1.1 KiB
Python

from typing import Dict
from typing import Optional
from ytdl_sub.config.plugin import Plugin
from ytdl_sub.config.preset_options import OptionsValidator
from ytdl_sub.validators.validators import StringValidator
class FormatOptions(OptionsValidator):
"""
Set ``--format`` to pass into yt-dlp to download a specific format quality.
Uses the same syntax as yt-dlp.
Usage:
.. code-block:: yaml
presets:
my_example_preset:
format: "(bv*[height<=1080]+bestaudio/best[height<=1080])"
"""
def __init__(self, name, value):
super().__init__(name, value)
self._format = StringValidator(name=name, value=value).value
@property
def format(self) -> str:
"""
yt-dlp format, uses same syntax as yt-dlp.
"""
return self._format
class FormatPlugin(Plugin[FormatOptions]):
plugin_options_type = FormatOptions
def ytdl_options(self) -> Optional[Dict]:
"""
Returns
-------
yt-dlp format
"""
return {"format": self.plugin_options.format}