Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhanced download functionality for yt-dlp unsupported links #363

Merged
merged 6 commits into from
Apr 8, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 11 additions & 30 deletions ytdlbot/sp_downloader.py
BennyThink marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import yt_dlp as ytdl

from config import (
ENABLE_ARIA2,
IPv6,
)

Expand Down Expand Up @@ -55,41 +54,23 @@ def sp_ytdl_download(url: str, tempdir: str):
"outtmpl": output,
"restrictfilenames": False,
"quiet": True,
"format": None,
}
if ENABLE_ARIA2:
ydl_opts["external_downloader"] = "aria2c"
ydl_opts["external_downloader_args"] = [
"--min-split-size=1M",
"--max-connection-per-server=16",
"--max-concurrent-downloads=16",
"--split=16",
]
formats = [
# webm , vp9 and av01 are not streamable on telegram, so we'll extract mp4 and not av01 codec
"bestvideo[ext=mp4][vcodec!*=av01][vcodec!*=vp09]+bestaudio[ext=m4a]/bestvideo+bestaudio",
"bestvideo[vcodec^=avc]+bestaudio[acodec^=mp4a]/best[vcodec^=avc]/best",
None,
]

address = ["::", "0.0.0.0"] if IPv6 else [None]
error = None
video_paths = None
for format_ in formats:
ydl_opts["format"] = format_
for addr in address:
# IPv6 goes first in each format
ydl_opts["source_address"] = addr
try:
logging.info("Downloading for %s with format %s", url, format_)
with ytdl.YoutubeDL(ydl_opts) as ydl:
ydl.download([url])
video_paths = list(pathlib.Path(tempdir).glob("*"))
break
except Exception:
error = traceback.format_exc()
logging.error("Download failed for %s - %s, try another way", format_, url)
if error is None:
for addr in address:
ydl_opts["source_address"] = addr
try:
logging.info("Downloading %s", url)
with ytdl.YoutubeDL(ydl_opts) as ydl:
ydl.download([url])
video_paths = list(pathlib.Path(tempdir).glob("*"))
break
except Exception:
error = traceback.format_exc()
logging.error("Download failed for %s - %s", url)

if not video_paths:
raise Exception(error)
Expand Down