Spaces:
Sleeping
Sleeping
from yt_dlp import YoutubeDL | |
import uuid | |
def download_youtube(url:str): | |
output_audio_folder = f"./cached/audio" | |
# file_path = output_audio_folder + '/%(title)s.%(ext)s' | |
file_path = output_audio_folder + f'/{str(uuid.uuid4())}' | |
# f"{file_path}.%(ext)s" | |
# url = "https://www.youtube.com/watch?v=WtpPolBLRN0" | |
yt = YoutubeDL(params={'postprocessors': [{ # Post-process to convert to MP3 | |
'key': 'FFmpegExtractAudio', | |
'preferredcodec': 'mp3', # Convert to mp3 | |
'preferredquality': '0', # '0' means best quality, auto-determined by source | |
}], | |
'outtmpl': f"{file_path}.%(ext)s", | |
}) | |
with yt as ydl: | |
ydl.download(url) | |
return f"{file_path}.mp3" | |