aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Shnitman <alexta69@gmail.com>2021-11-20 10:12:08 +0200
committerAlex Shnitman <alexta69@gmail.com>2021-11-20 10:12:08 +0200
commitf52bea74d335cc84b9d08ebe485e620a8309b2a3 (patch)
treef140c3c5ad806c197f1abcf259ad2db9bcc1323b
parentMerge branch 'master' of https://github.com/alexta69/metube into mp3-support (diff)
downloadmetube-f52bea74d335cc84b9d08ebe485e620a8309b2a3.tar.gz
metube-f52bea74d335cc84b9d08ebe485e620a8309b2a3.tar.bz2
metube-f52bea74d335cc84b9d08ebe485e620a8309b2a3.zip
simplified format handling
-rw-r--r--app/dl_formats.py54
-rw-r--r--app/ytdl.py2
2 files changed, 19 insertions, 37 deletions
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)
bgstack15