diff --git a/app/transcript.py b/app/transcript.py index 10f9693..0b686e9 100644 --- a/app/transcript.py +++ b/app/transcript.py @@ -1,6 +1,12 @@ +import os +import shutil +import tempfile + import httpx import yt_dlp +COOKIES_SRC = "/app/cookies.txt" + def extract_video_id(url: str) -> str: """YouTube URL에서 video ID 추출.""" @@ -20,13 +26,22 @@ def fetch_transcript(video_id: str) -> str: "writeautomaticsub": True, "subtitleslangs": ["ko", "en"], "subtitlesformat": "json3", + "format": "worst", "quiet": True, "no_warnings": True, - "cookiefile": "/app/cookies.txt", } - with yt_dlp.YoutubeDL(ydl_opts) as ydl: - info = ydl.extract_info(url, download=False) + if os.path.isfile(COOKIES_SRC): + tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".txt") + shutil.copy2(COOKIES_SRC, tmp.name) + ydl_opts["cookiefile"] = tmp.name + + try: + with yt_dlp.YoutubeDL(ydl_opts) as ydl: + info = ydl.extract_info(url, download=False) + finally: + if "cookiefile" in ydl_opts: + os.unlink(ydl_opts["cookiefile"]) subs = info.get("automatic_captions", {}) lang = "ko" if "ko" in subs else "en" if "en" in subs else None