metube/app/dl_formats.py
2021-11-20 10:12:08 +02:00

62 lines
1.7 KiB
Python

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