import yt_dlp from pathlib import Path DOWNLOAD_DIR = Path("/data/audio") DOWNLOAD_DIR.mkdir(exist_ok=True) def get_video_title(video_url): ydl_opts = { "quiet": True, "skip_download": True, } with yt_dlp.YoutubeDL(ydl_opts) as ydl: info = ydl.extract_info(video_url, download=False) return info.get("title", "New podcast") def download_audio(video_url): ydl_opts = { "format": "bestaudio/best", "outtmpl": "/data/audio/%(title)s.%(ext)s", "postprocessors": [{ "key": "FFmpegExtractAudio", "preferredcodec": "mp3", "preferredquality": "192", }], "noplaylist": True, "quiet": True, } with yt_dlp.YoutubeDL(ydl_opts) as ydl: info = ydl.extract_info(video_url, download=True) filename = ydl.prepare_filename(info) mp3_file = Path(filename).with_suffix(".mp3") return mp3_file, info.get("title", "audio")