Spaces:
Runtime error
Runtime error
| import yt_dlp | |
| import os | |
| def download_audio(url, output_format="mp3"): | |
| """ | |
| Downloads audio from a given URL and converts it to the selected format. | |
| """ | |
| os.makedirs("downloads", exist_ok=True) | |
| ydl_opts = { | |
| 'format': 'bestaudio/best', | |
| 'outtmpl': os.path.join("downloads", '%(title)s.%(ext)s'), | |
| 'postprocessors': [{ | |
| 'key': 'FFmpegExtractAudio', | |
| 'preferredcodec': output_format, | |
| 'preferredquality': '192', | |
| }], | |
| 'noplaylist': True, | |
| 'quiet': True, | |
| 'no_warnings': True | |
| } | |
| with yt_dlp.YoutubeDL(ydl_opts) as ydl: | |
| info = ydl.extract_info(url, download=True) | |
| filename = os.path.splitext(ydl.prepare_filename(info))[0] + f".{output_format}" | |
| return os.path.abspath(filename), info.get('title', 'Unknown title') | |