File size: 516 Bytes
56225c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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