Skip to content

Commit

Permalink
feat: add yt-dlp format option
Browse files Browse the repository at this point in the history
  • Loading branch information
Benex254 committed Jul 25, 2024
1 parent b619a11 commit 7831973
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 3 deletions.
3 changes: 2 additions & 1 deletion fastanime/Utility/downloader/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def __init__(self):
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 = {
Expand All @@ -59,6 +59,7 @@ def _download_file(self, url: str, download_dir, title, silent):
], # Progress hook
"silent": silent,
"verbose": False,
"format": vid_format,
}

with yt_dlp.YoutubeDL(ydl_opts) as ydl:
Expand Down
9 changes: 9 additions & 0 deletions fastanime/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ def handle_exit(signum, frame):
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",
Expand Down Expand Up @@ -91,6 +97,7 @@ def handle_exit(signum, frame):
def run_cli(
ctx: click.Context,
server,
format,
continue_,
translation_type,
quality,
Expand All @@ -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:
Expand Down
6 changes: 5 additions & 1 deletion fastanime/cli/commands/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions fastanime/cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def load_config(self):
"preferred_language": "english",
"use_fzf": "False",
"preview": "False",
"format": "bestvideo[height<=1080]+bestaudio/best",
}
)
self.configparser.add_section("stream")
Expand All @@ -49,6 +50,7 @@ def load_config(self):
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 ------
Expand Down Expand Up @@ -105,6 +107,9 @@ def get_quality(self):
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:
Expand Down
2 changes: 1 addition & 1 deletion fastanime/cli/interfaces/anilist_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ def _watch_trailer(config: 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:")
Expand Down

0 comments on commit 7831973

Please sign in to comment.