|
import gradio as gr |
|
import unidecode |
|
from unidecode import unidecode |
|
import yt_dlp |
|
import os |
|
|
|
|
|
def cleanString(string): |
|
cleanString = unidecode(string) |
|
cleanString = re.sub('\W+','_', cleanString) |
|
return cleanString.lower() |
|
|
|
def download_audio(url): |
|
path_to_folder_audio_mp3 = "./audio/" |
|
ydl_opts = { |
|
|
|
'format': 'm4a/bestaudio/best', |
|
|
|
'outtmpl': f'{path_to_folder_audio_mp3}%(title)s', |
|
'postprocessors': [{ |
|
'key': 'FFmpegExtractAudio', |
|
'preferredcodec': 'mp3', |
|
|
|
}] |
|
} |
|
with yt_dlp.YoutubeDL(ydl_opts) as ydl: |
|
info_dict = ydl.extract_info(url, download=True) |
|
video_title = info_dict['title'] |
|
|
|
|
|
|
|
local_link = video_title + ".mp3" |
|
new_local_link = cleanString(video_title) + ".mp3" |
|
for filename in os.listdir(path_to_folder_audio_mp3): |
|
if cleanString(local_link) == cleanString(filename): |
|
os.rename(os.path.join(path_to_folder_audio_mp3, filename),os.path.join(path_to_folder_audio_mp3, new_local_link)) |
|
|
|
return path_to_folder_audio_mp3 + new_local_link |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
iface = gr.Interface(fn=download_audio, |
|
inputs=gr.Textbox(label="YouTube Video URL"), |
|
outputs=gr.File(label="Output Audio"), |
|
allow_flagging="never" |
|
) |
|
iface.launch(debug=True) |
|
|