From 1474df04e71317a1aee463895d864e12e296514b Mon Sep 17 00:00:00 2001 From: Benex254 Date: Mon, 5 Aug 2024 09:47:03 +0300 Subject: [PATCH] feat: add yt-dlp format option --- fastanime/Utility/downloader/downloader.py | 3 ++- fastanime/cli/__init__.py | 9 +++++++++ fastanime/cli/commands/download.py | 6 +++++- fastanime/cli/config.py | 5 +++++ fastanime/cli/interfaces/anilist_interfaces.py | 2 +- 5 files changed, 22 insertions(+), 3 deletions(-) diff --git a/fastanime/Utility/downloader/downloader.py b/fastanime/Utility/downloader/downloader.py index c54c1ec..4abc854 100644 --- a/fastanime/Utility/downloader/downloader.py +++ b/fastanime/Utility/downloader/downloader.py @@ -49,7 +49,7 @@ class YtDLPDownloader: self._thread.start() # Function to download the file - def _download_file(self, url: str, download_dir, title, silent): + def _download_file(self, url: str, download_dir, title, silent, vid_format="best"): anime_title = sanitize_filename(title[0]) episode_title = sanitize_filename(title[1]) ydl_opts = { @@ -59,6 +59,7 @@ class YtDLPDownloader: ], # Progress hook "silent": silent, "verbose": False, + "format": vid_format, } with yt_dlp.YoutubeDL(ydl_opts) as ydl: diff --git a/fastanime/cli/__init__.py b/fastanime/cli/__init__.py index 2c156b3..de24e58 100644 --- a/fastanime/cli/__init__.py +++ b/fastanime/cli/__init__.py @@ -46,6 +46,12 @@ signal.signal(signal.SIGINT, handle_exit) type=click.Choice(SERVERS_AVAILABLE, case_sensitive=False), help="Server of choice", ) +@click.option( + "-f", + "--format", + type=str, + help="yt-dlp format to use", +) @click.option( "-c/-no-c", "--continue/--no-continue", @@ -91,6 +97,7 @@ signal.signal(signal.SIGINT, handle_exit) def run_cli( ctx: click.Context, server, + format, continue_, translation_type, quality, @@ -106,6 +113,8 @@ def run_cli( ctx.obj = Config() if server: ctx.obj.server = server + if format: + ctx.obj.format = format if ctx.get_parameter_source("continue_") == click.core.ParameterSource.COMMANDLINE: ctx.obj.continue_from_history = continue_ if quality: diff --git a/fastanime/cli/commands/download.py b/fastanime/cli/commands/download.py index c6c4092..71e2a1a 100644 --- a/fastanime/cli/commands/download.py +++ b/fastanime/cli/commands/download.py @@ -80,7 +80,11 @@ def download(config: Config, anime_title, episode_range): ] link = max(links, key=lambda x: x[0])[1] downloader._download_file( - link, download_dir, (anime["title"], streams[0]["episode_title"]), True + link, + download_dir, + (anime["title"], streams[0]["episode_title"]), + True, + config.format, ) except Exception as e: print(e) diff --git a/fastanime/cli/config.py b/fastanime/cli/config.py index ad1bc73..b87fe43 100644 --- a/fastanime/cli/config.py +++ b/fastanime/cli/config.py @@ -28,6 +28,7 @@ class Config(object): "preferred_language": "english", "use_fzf": "False", "preview": "False", + "format": "bestvideo[height<=1080]+bestaudio/best", } ) self.configparser.add_section("stream") @@ -49,6 +50,7 @@ class Config(object): self.auto_select = self.get_auto_select() self.quality = self.get_quality() self.server = self.get_server() + self.format = self.get_format() self.preferred_language = self.get_preferred_language() # ---- setup user data ------ @@ -105,6 +107,9 @@ class Config(object): def get_server(self): return self.configparser.get("stream", "server") + def get_format(self): + return self.configparser.get("stream", "format") + def update_config(self, section: str, key: str, value: str): self.configparser.set(section, key, value) with open(USER_CONFIG_PATH, "w") as config: diff --git a/fastanime/cli/interfaces/anilist_interfaces.py b/fastanime/cli/interfaces/anilist_interfaces.py index 5f87d9c..7380131 100644 --- a/fastanime/cli/interfaces/anilist_interfaces.py +++ b/fastanime/cli/interfaces/anilist_interfaces.py @@ -384,7 +384,7 @@ def anilist_options(config, anilist_config: QueryDict): if trailer := selected_anime.get("trailer"): trailer_url = "https://youtube.com/watch?v=" + trailer["id"] print("[bold magenta]Watching Trailer of:[/]", selected_anime_title) - mpv(trailer_url, selected_anime_title) + mpv(trailer_url, selected_anime_title, f"--ytdl-format={config.format}") anilist_options(config, anilist_config) else: print("no trailer available :confused:")