diff --git a/src/engine/base.py b/src/engine/base.py index 14ccd0d1..0ecf6698 100644 --- a/src/engine/base.py +++ b/src/engine/base.py @@ -143,7 +143,7 @@ def get_cache_fileid(self): return self._redis.get_send_cache(unique) @abstractmethod - def _setup_formats(self): + def _setup_formats(self) -> list | None: pass @abstractmethod diff --git a/src/engine/direct.py b/src/engine/direct.py index 4d7757e8..e641a269 100644 --- a/src/engine/direct.py +++ b/src/engine/direct.py @@ -21,7 +21,7 @@ class DirectDownloader(BaseDownloader): def start(self): pass - def _setup_formats(self): + def _setup_formats(self) -> list | None: pass def _requests_download(self): diff --git a/src/engine/generic.py b/src/engine/generic.py index 6c4801d5..1902b5a6 100644 --- a/src/engine/generic.py +++ b/src/engine/generic.py @@ -3,6 +3,9 @@ # ytdlbot - generic.py +from pathlib import Path + +import yt_dlp from base import BaseDownloader from pyrogram import types @@ -11,7 +14,7 @@ class YoutubeDownload(BaseDownloader): - def _setup_formats(self): + def _setup_formats(self) -> list | None: download = get_download_settings(self._user_id) formats = [] # "high", "medium", "low", "audio", "custom" @@ -33,13 +36,21 @@ def _setup_formats(self): if temp_row: markup.append(temp_row) self._bot_msg.__edit_text("Choose the format", reply_markup=types.InlineKeyboardMarkup(markup)) - return + return None if download == "audio": # download audio only formats.append("bestaudio") elif download == "high": - # download high quality video - formats.append("best") + # default config + formats.extend( + [ + # webm , vp9 and av01 are not streamable on telegram, so we'll extract only mp4 + "bestvideo[ext=mp4][vcodec!*=av01][vcodec!*=vp09]+bestaudio[ext=m4a]/bestvideo+bestaudio", + "bestvideo[vcodec^=avc]+bestaudio[acodec^=mp4a]/best[vcodec^=avc]/best", + None, + ] + ) + elif download == "medium": # download medium quality video formats.append("medium") @@ -49,10 +60,34 @@ def _setup_formats(self): return formats def _download(self, formats): - pass + output = Path(self._tempdir, "%(title).70s.%(ext)s").as_posix() + + ydl_opts = { + "progress_hooks": [lambda d: self.download_hook(d)], + "outtmpl": output, + "restrictfilenames": False, + "quiet": True, + } + + if self._url.startswith("https://drive.google.com"): + # Always use the `source` format for Google Drive URLs. + formats = ["source"] + formats + + files = None + for f in formats: + ydl_opts["format"] = f + with yt_dlp.YoutubeDL(ydl_opts) as ydl: + ydl.download([self._url]) + files = list(Path(self._tempdir).glob("*")) + break + + return files def start(self, formats=None): - if formats is None: - formats = self._setup_formats() - self._download(formats) + # user can choose format by clicking on the button(custom config) + default_formats = self._setup_formats() + if formats is not None: + # formats according to user choice + default_formats = formats + self._setup_formats() + self._download(default_formats) self._upload() diff --git a/src/engine/helper.py b/src/engine/helper.py index 25ee57c1..0ea80a73 100644 --- a/src/engine/helper.py +++ b/src/engine/helper.py @@ -33,47 +33,6 @@ from utils import shorten_url, sizeof_fmt -def ytdl_download(url: str, tempdir, bm, formats: list = None) -> list: - output = pathlib.Path(tempdir, "%(title).70s.%(ext)s").as_posix() - default_formats = [ - # webm , vp9 and av01 are not streamable on telegram, so we'll extract only mp4 - "bestvideo[ext=mp4][vcodec!*=av01][vcodec!*=vp09]+bestaudio[ext=m4a]/bestvideo+bestaudio", - "bestvideo[vcodec^=avc]+bestaudio[acodec^=mp4a]/best[vcodec^=avc]/best", - None, - ] - if formats: - default_formats = formats + default_formats - - ydl_opts = { - "progress_hooks": [lambda d: download_hook(d, bm)], - "outtmpl": output, - "restrictfilenames": False, - "quiet": True, - } - 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", - ] - - if url.startswith("https://drive.google.com"): - # Always use the `source` format for Google Drive URLs. - default_formats = ["source"] - - video_paths = None - for f in default_formats: - ydl_opts["format"] = f - with yt_dlp.YoutubeDL(ydl_opts) as ydl: - ydl.download([url]) - video_paths = list(pathlib.Path(tempdir).glob("*")) - break - - return video_paths - - def debounce(wait_seconds): """ Thread-safe debounce decorator for functions that take a message with chat.id and msg.id attributes. diff --git a/src/engine/instagram.py b/src/engine/instagram.py index d64a9559..3921a7f2 100644 --- a/src/engine/instagram.py +++ b/src/engine/instagram.py @@ -40,7 +40,7 @@ def extract_code(self): return None - def _setup_formats(self): + def _setup_formats(self) -> list | None: pass def _download(self, formats):