Commit d01d358

benny-dou <60535774+benny-dou@users.noreply.github.com>
2025-02-27 18:02:36
feat(ytdlp): support limit max download file size
1 parent 673b698
Changed files (2)
src
src/preview/ytdlp.py
@@ -17,7 +17,7 @@ from pyrogram.types import Message
 from yt_dlp import YoutubeDL
 from yt_dlp.utils import DownloadError, ExtractorError, YoutubeDLError
 
-from config import API, CAPTION_LENGTH, DB, DOWNLOAD_DIR, MAX_FILE_BYTES, PROVIDER, PROXY, TID, TOKEN, YTDLP_RE_ENCODING_MAX_FILE_BYTES, cache
+from config import API, CAPTION_LENGTH, DB, DOWNLOAD_DIR, MAX_FILE_BYTES, PROVIDER, PROXY, TID, TOKEN, YTDLP_DOWNLOAD_MAX_FILE_BYTES, YTDLP_RE_ENCODING_MAX_FILE_BYTES, cache
 from database import get_db
 from messages.database import copy_messages_from_db, save_messages
 from messages.preprocess import preprocess_media
@@ -375,6 +375,10 @@ def download_video_info(url: str, ydl_opts: dict, json_path: str | Path) -> dict
             if info["duration"]:
                 summary += f"\n🕒时长: {readable_time(info['duration'])}"
             info["summary"] = summary.strip()
+            media_size = int(info.get("video_size", 0)) + int(info.get("audio_size", 0))
+            if media_size > YTDLP_DOWNLOAD_MAX_FILE_BYTES:
+                info["ytdlp_error"] = f"{summary.strip()}\n**⚠️视频文件过大: {readable_size(media_size)}**\n**⚠️机器硬盘限制: {readable_size(YTDLP_DOWNLOAD_MAX_FILE_BYTES)}**"
+
     except Exception as e:
         logger.error(f"Failed to download video info: {e}")
         info = {"ytdlp_error": str(e)}
src/config.py
@@ -23,8 +23,10 @@ MAX_MESSAGE_RETRIEVED = int(os.getenv("MAX_MESSAGE_RETRIEVED", "5000"))  # Maxim
 MAX_MESSAGE_SUMMARY = int(os.getenv("MAX_MESSAGE_SUMMARY", "5000"))  # Maximum number of messages to summay
 READING_SPEED = int(os.getenv("READING_SPEED", "300"))  # words per minute
 DAILY_MESSAGES = os.getenv("DAILY_MESSAGES", "{}")  # Useful for daily checkin for some services. Should be a json string: '{"chat-1": "msg-1", "chat-2": "msg-2"}'
-# For ytdlp downloaded video, re-encoding to H264 format. This set the max file size for re-encoding. 0 means no limit
-YTDLP_RE_ENCODING_MAX_FILE_BYTES = int(os.getenv("YTDLP_RE_ENCODING_MAX_FILE_BYTES", "0"))
+# For ytdlp downloaded video, re-encoding to H264 format. This set the max file size for re-encoding. Default: 1PB
+YTDLP_RE_ENCODING_MAX_FILE_BYTES = int(os.getenv("YTDLP_RE_ENCODING_MAX_FILE_BYTES", "1125899906842624"))
+# ytdlp max allowed file bytes. Default: 1PB (Set this if the VPS disk space is limited)
+YTDLP_DOWNLOAD_MAX_FILE_BYTES = int(os.getenv("YTDLP_DOWNLOAD_MAX_FILE_BYTES", "1125899906842624"))
 
 
 class ENABLE:  # see fine-grained permission in `src/permission.py`