From b3a589f1a9d4f84e180623cb32cb826579f52553 Mon Sep 17 00:00:00 2001 From: asuyou Date: Mon, 25 Oct 2021 17:15:09 +0100 Subject: Added simple MP3 support --- app/ytdl.py | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'app') diff --git a/app/ytdl.py b/app/ytdl.py index f9084e3..0400877 100644 --- a/app/ytdl.py +++ b/app/ytdl.py @@ -39,6 +39,14 @@ class Download: vfmt, afmt = '', '' if format == 'mp4': vfmt, afmt = '[ext=mp4]', '[ext=m4a]' + elif format == 'mp3': + afmt = '/best' + ytdl_opts["writethumbnail"] = True + ytdl_opts["postprocessors"] = [ + {"key": "FFmpegExtractAudio", "preferredcodec": "mp3"}, + {"key": "EmbedThumbnail"}, + ] + if quality == 'best': self.format = f'bestvideo{vfmt}+bestaudio{afmt}/best{vfmt}' elif quality in ('1440p', '1080p', '720p', '480p'): -- cgit From d0518142599d326bac12a66e0173eb0e2da2c66d Mon Sep 17 00:00:00 2001 From: asuyou Date: Thu, 28 Oct 2021 11:19:17 +0100 Subject: Added quality choice based on format --- app/dl_formats.py | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ app/ytdl.py | 26 ++--------------- 2 files changed, 88 insertions(+), 23 deletions(-) create mode 100644 app/dl_formats.py (limited to 'app') diff --git a/app/dl_formats.py b/app/dl_formats.py new file mode 100644 index 0000000..184f9f9 --- /dev/null +++ b/app/dl_formats.py @@ -0,0 +1,85 @@ +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 + """ + audio_fmt = "" + video_fmt = "" + final_fmt = "" + + if format.startswith("custom: "): + final_fmt = format[7:] + elif format == "any": + final_fmt = "bv*+ba/b" + elif format == "mp3": + audio_fmt = _get_audio_fmt(quality) + elif format == "mp4": + audio_fmt = "ba/b" + video_fmt = _get_video_fmt(quality) + else: + raise Exception(f"Unknown format {format}") + + if not final_fmt: + final_fmt = video_fmt + audio_fmt + + return final_fmt + + +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}, + ) + + elif format == "mp4": + ytdl_opts["merge_output_format"] = "mp4" + + return ytdl_opts + + +def _get_audio_fmt(quality: str) -> str: + if quality == "best" or quality in ("128", "192", "320"): + audio_fmt = "ba/b" + # Audio quality needs to be set post-download, set in opts + else: + raise Exception(f"Unknown quality {quality}") + + return audio_fmt + + +def _get_video_fmt(quality: str) -> str: + if quality == "best": + video_fmt = "bv*+" + elif quality in ("1440", "1080", "720", "480"): + video_fmt = f"bv[height<={quality}]+" + else: + raise Exception(f"Unknown quality {quality}") + + return video_fmt diff --git a/app/ytdl.py b/app/ytdl.py index 0400877..63c3786 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,29 +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]' - elif format == 'mp3': - afmt = '/best' - ytdl_opts["writethumbnail"] = True - ytdl_opts["postprocessors"] = [ - {"key": "FFmpegExtractAudio", "preferredcodec": "mp3"}, - {"key": "EmbedThumbnail"}, - ] - - 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 -- cgit From 9e82aa1976eeca20d643073596cdca0196695617 Mon Sep 17 00:00:00 2001 From: asuyou Date: Sat, 30 Oct 2021 18:06:56 +0100 Subject: Fixed "custom:" download format --- app/dl_formats.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app') diff --git a/app/dl_formats.py b/app/dl_formats.py index 184f9f9..f1b3881 100644 --- a/app/dl_formats.py +++ b/app/dl_formats.py @@ -16,7 +16,7 @@ def get_format(format: str, quality: str) -> str: video_fmt = "" final_fmt = "" - if format.startswith("custom: "): + if format.startswith("custom:"): final_fmt = format[7:] elif format == "any": final_fmt = "bv*+ba/b" -- cgit From 63ec5c37afc99453125b855559c8913811217bdf Mon Sep 17 00:00:00 2001 From: asuyou Date: Fri, 5 Nov 2021 23:46:40 +0000 Subject: Changed back to original format download --- app/dl_formats.py | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) (limited to 'app') diff --git a/app/dl_formats.py b/app/dl_formats.py index f1b3881..84be68e 100644 --- a/app/dl_formats.py +++ b/app/dl_formats.py @@ -12,24 +12,12 @@ def get_format(format: str, quality: str) -> str: Returns: dl_format: Formatted download string """ - audio_fmt = "" - video_fmt = "" final_fmt = "" if format.startswith("custom:"): final_fmt = format[7:] - elif format == "any": - final_fmt = "bv*+ba/b" - elif format == "mp3": - audio_fmt = _get_audio_fmt(quality) - elif format == "mp4": - audio_fmt = "ba/b" - video_fmt = _get_video_fmt(quality) else: - raise Exception(f"Unknown format {format}") - - if not final_fmt: - final_fmt = video_fmt + audio_fmt + final_fmt = _get_final_fmt(format, quality) return final_fmt @@ -58,15 +46,12 @@ def get_opts(format: str, quality: str, ytdl_opts: dict) -> dict: {"key": "FFmpegExtractAudio", "preferredcodec": "mp3", **extra_args}, ) - elif format == "mp4": - ytdl_opts["merge_output_format"] = "mp4" - return ytdl_opts def _get_audio_fmt(quality: str) -> str: if quality == "best" or quality in ("128", "192", "320"): - audio_fmt = "ba/b" + audio_fmt = "bestaudio/best" # Audio quality needs to be set post-download, set in opts else: raise Exception(f"Unknown quality {quality}") @@ -74,12 +59,29 @@ def _get_audio_fmt(quality: str) -> str: return audio_fmt -def _get_video_fmt(quality: str) -> str: +def _get_video_res(quality: str) -> str: if quality == "best": - video_fmt = "bv*+" + video_fmt = "" elif quality in ("1440", "1080", "720", "480"): - video_fmt = f"bv[height<={quality}]+" + video_fmt = f"[height<={quality}]" else: raise Exception(f"Unknown quality {quality}") return video_fmt + + +def _get_final_fmt(format: str, quality: str) -> str: + vfmt, afmt, vres = "", "", "" + + if format == "mp4": + # video {res} {vfmt} + audio {afmt} {res} {vfmt} + vfmt, afmt = "[ext=mp4]", "[ext=m4a]" + vres = _get_video_res(quality) + combo = vres + vfmt + final_fmt = f"bestvideo{combo}+bestaudio{afmt}/best{combo}" + elif format == "mp3": + final_fmt = _get_audio_fmt(quality) + else: + raise Exception(f"Unkown format {format}") + + return final_fmt -- cgit From eadf8239e968d3ffd2e1da344ba9d8bc8fd78a1c Mon Sep 17 00:00:00 2001 From: asuyou Date: Sun, 14 Nov 2021 23:17:07 +0000 Subject: Changed to "any" to work like original (backend) --- app/dl_formats.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'app') diff --git a/app/dl_formats.py b/app/dl_formats.py index 84be68e..b47ae5c 100644 --- a/app/dl_formats.py +++ b/app/dl_formats.py @@ -60,7 +60,7 @@ def _get_audio_fmt(quality: str) -> str: def _get_video_res(quality: str) -> str: - if quality == "best": + if quality in ("best", "audio"): video_fmt = "" elif quality in ("1440", "1080", "720", "480"): video_fmt = f"[height<={quality}]" @@ -73,12 +73,17 @@ def _get_video_res(quality: str) -> str: def _get_final_fmt(format: str, quality: str) -> str: vfmt, afmt, vres = "", "", "" - if format == "mp4": + if format in ("mp4", "any"): # video {res} {vfmt} + audio {afmt} {res} {vfmt} - vfmt, afmt = "[ext=mp4]", "[ext=m4a]" - vres = _get_video_res(quality) - combo = vres + vfmt - final_fmt = f"bestvideo{combo}+bestaudio{afmt}/best{combo}" + if format == "mp4": + vfmt, afmt = "[ext=mp4]", "[ext=m4a]" + + if quality == "audio": + final_fmt = "bestaudio/best" + else: + vres = _get_video_res(quality) + combo = vres + vfmt + final_fmt = f"bestvideo{combo}+bestaudio{afmt}/best{combo}" elif format == "mp3": final_fmt = _get_audio_fmt(quality) else: -- cgit From 69746826f8765dc61c2b4ba82bc4ccbe664bc933 Mon Sep 17 00:00:00 2001 From: asuyou Date: Fri, 19 Nov 2021 20:37:55 +0000 Subject: dl_formats options are now inline --- app/dl_formats.py | 34 +++++++++++----------------------- 1 file changed, 11 insertions(+), 23 deletions(-) (limited to 'app') diff --git a/app/dl_formats.py b/app/dl_formats.py index b47ae5c..d0af344 100644 --- a/app/dl_formats.py +++ b/app/dl_formats.py @@ -49,27 +49,6 @@ def get_opts(format: str, quality: str, ytdl_opts: dict) -> dict: return ytdl_opts -def _get_audio_fmt(quality: str) -> str: - if quality == "best" or quality in ("128", "192", "320"): - audio_fmt = "bestaudio/best" - # Audio quality needs to be set post-download, set in opts - else: - raise Exception(f"Unknown quality {quality}") - - return audio_fmt - - -def _get_video_res(quality: str) -> str: - if quality in ("best", "audio"): - video_fmt = "" - elif quality in ("1440", "1080", "720", "480"): - video_fmt = f"[height<={quality}]" - else: - raise Exception(f"Unknown quality {quality}") - - return video_fmt - - def _get_final_fmt(format: str, quality: str) -> str: vfmt, afmt, vres = "", "", "" @@ -81,11 +60,20 @@ def _get_final_fmt(format: str, quality: str) -> str: if quality == "audio": final_fmt = "bestaudio/best" else: - vres = _get_video_res(quality) + if quality in ("best", "audio"): + vres = "" + elif quality in ("1440", "1080", "720", "480"): + vres = f"[height<={quality}]" + else: + raise Exception(f"Unknown quality {quality}") combo = vres + vfmt final_fmt = f"bestvideo{combo}+bestaudio{afmt}/best{combo}" elif format == "mp3": - final_fmt = _get_audio_fmt(quality) + if quality == "best" or quality in ("128", "192", "320"): + final_fmt = "bestaudio/best" + # Audio quality needs to be set post-download, set in opts + else: + raise Exception(f"Unknown quality {quality}") else: raise Exception(f"Unkown format {format}") -- cgit From f52bea74d335cc84b9d08ebe485e620a8309b2a3 Mon Sep 17 00:00:00 2001 From: Alex Shnitman Date: Sat, 20 Nov 2021 10:12:08 +0200 Subject: simplified format handling --- app/dl_formats.py | 54 ++++++++++++++++++------------------------------------ app/ytdl.py | 2 +- 2 files changed, 19 insertions(+), 37 deletions(-) (limited to 'app') diff --git a/app/dl_formats.py b/app/dl_formats.py index d0af344..1f27ee9 100644 --- a/app/dl_formats.py +++ b/app/dl_formats.py @@ -12,14 +12,27 @@ def get_format(format: str, quality: str) -> str: Returns: dl_format: Formatted download string """ - final_fmt = "" + format = format or "best" if format.startswith("custom:"): - final_fmt = format[7:] - else: - final_fmt = _get_final_fmt(format, quality) + return format[7:] - return final_fmt + 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: @@ -47,34 +60,3 @@ def get_opts(format: str, quality: str, ytdl_opts: dict) -> dict: ) return ytdl_opts - - -def _get_final_fmt(format: str, quality: str) -> str: - vfmt, afmt, vres = "", "", "" - - if format in ("mp4", "any"): - # video {res} {vfmt} + audio {afmt} {res} {vfmt} - if format == "mp4": - vfmt, afmt = "[ext=mp4]", "[ext=m4a]" - - if quality == "audio": - final_fmt = "bestaudio/best" - else: - if quality in ("best", "audio"): - vres = "" - elif quality in ("1440", "1080", "720", "480"): - vres = f"[height<={quality}]" - else: - raise Exception(f"Unknown quality {quality}") - combo = vres + vfmt - final_fmt = f"bestvideo{combo}+bestaudio{afmt}/best{combo}" - elif format == "mp3": - if quality == "best" or quality in ("128", "192", "320"): - final_fmt = "bestaudio/best" - # Audio quality needs to be set post-download, set in opts - else: - raise Exception(f"Unknown quality {quality}") - else: - raise Exception(f"Unkown format {format}") - - return final_fmt diff --git a/app/ytdl.py b/app/ytdl.py index 688744b..e0267d7 100644 --- a/app/ytdl.py +++ b/app/ytdl.py @@ -159,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) -- cgit