diff --git a/app/dl_formats.py b/app/dl_formats.py new file mode 100644 index 0000000..1f27ee9 --- /dev/null +++ b/app/dl_formats.py @@ -0,0 +1,62 @@ +def get_format(format: str, quality: str) -> str: + """ + Returns format for download + + Args: + format (str): format selected + quality (str): quality selected + + Raises: + Exception: unknown quality, unknown format + + Returns: + dl_format: Formatted download string + """ + format = format or "best" + + if format.startswith("custom:"): + return format[7:] + + if format == "mp3": + # Audio quality needs to be set post-download, set in opts + return "bestaudio/best" + + if format in ("mp4", "any"): + if quality == "audio": + return "bestaudio/best" + + # video {res} {vfmt} + audio {afmt} {res} {vfmt} + vfmt, afmt = ("[ext=mp4]", "[ext=m4a]") if format == "mp4" else ("", "") + vres = f"[height<={quality}]" if quality != "best" else "" + vcombo = vres + vfmt + + return f"bestvideo{vcombo}+bestaudio{afmt}/best{vcombo}" + + raise Exception(f"Unkown format {format}") + + +def get_opts(format: str, quality: str, ytdl_opts: dict) -> dict: + """ + Returns extra download options + Mostly postprocessing options + + Args: + format (str): format selected + quality (str): quality of format selected (needed for some formats) + ytdl_opts (dict): current options selected + + Returns: + ytdl_opts: Extra options + """ + if "postprocessors" not in ytdl_opts: + ytdl_opts["postprocessors"] = [] + + if format == "mp3": + extra_args = {} + if quality != "best": + extra_args = {"preferredquality": quality} + ytdl_opts["postprocessors"].append( + {"key": "FFmpegExtractAudio", "preferredcodec": "mp3", **extra_args}, + ) + + return ytdl_opts diff --git a/app/ytdl.py b/app/ytdl.py index a14855d..e0267d7 100644 --- a/app/ytdl.py +++ b/app/ytdl.py @@ -4,6 +4,7 @@ from collections import OrderedDict import asyncio import multiprocessing import logging +from dl_formats import get_format, get_opts log = logging.getLogger('ytdl') @@ -36,21 +37,8 @@ class Download: def __init__(self, download_dir, output_template, quality, format, ytdl_opts, info): self.download_dir = download_dir self.output_template = output_template - vfmt, afmt = '', '' - if format == 'mp4': - vfmt, afmt = '[ext=mp4]', '[ext=m4a]' - if quality == 'best': - self.format = f'bestvideo{vfmt}+bestaudio{afmt}/best{vfmt}' - elif quality in ('1440p', '1080p', '720p', '480p'): - res = quality[:-1] - self.format = f'bestvideo[height<={res}]{vfmt}+bestaudio{afmt}/best[height<={res}]{vfmt}' - elif quality == 'audio': - self.format = f'bestaudio{afmt}' - elif quality.startswith('custom:'): - self.format = quality[7:] - else: - raise Exception(f'unknown quality {quality}') - self.ytdl_opts = ytdl_opts + self.format = get_format(format, quality) + self.ytdl_opts = get_opts(format, quality, ytdl_opts) self.info = info self.canceled = False self.tmpfilename = None @@ -171,7 +159,7 @@ class DownloadQueue: elif etype == 'video' or etype.startswith('url') and 'id' in entry and 'title' in entry: if entry['id'] not in self.queue: dl = DownloadInfo(entry['id'], entry['title'], entry.get('webpage_url') or entry['url'], quality, format) - dldirectory = self.config.DOWNLOAD_DIR if quality != 'audio' else self.config.AUDIO_DOWNLOAD_DIR + dldirectory = self.config.DOWNLOAD_DIR if (quality != 'audio' and format != 'mp3') else self.config.AUDIO_DOWNLOAD_DIR self.queue[entry['id']] = Download(dldirectory, self.config.OUTPUT_TEMPLATE, quality, format, self.config.YTDL_OPTIONS, dl) self.event.set() await self.notifier.added(dl) diff --git a/ui/src/app/app.component.html b/ui/src/app/app.component.html index 20a78b4..039aab6 100644 --- a/ui/src/app/app.component.html +++ b/ui/src/app/app.component.html @@ -26,7 +26,7 @@