| import yt_dlp | |
| import os | |
| import uuid | |
| def download_youtube_video(url: str, output_dir: str = "/tmp"): | |
| """Downloads the YouTube video and returns the local file path.""" | |
| video_id = str(uuid.uuid4())[:8] | |
| output_path = os.path.join(output_dir, f"{video_id}.mp4") | |
| ydl_opts = { | |
| "format": "best[ext=mp4]/best", | |
| "outtmpl": output_path, | |
| "quiet": True, | |
| "noplaylist": True, | |
| } | |
| with yt_dlp.YoutubeDL(ydl_opts) as ydl: | |
| ydl.download([url]) | |
| return output_path | |