test_python / libs /transformer /youtube_download.py
minhpng's picture
add gradio client
3c36fb5
raw
history blame contribute delete
769 Bytes
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"