Spaces:
Running
Running
pierreguillou
commited on
Commit
•
f52579e
1
Parent(s):
f14a1c1
Update app.py
Browse files
app.py
CHANGED
@@ -1,57 +1,58 @@
|
|
1 |
import gradio as gr
|
2 |
-
import re
|
3 |
-
|
4 |
import yt_dlp
|
5 |
import os
|
6 |
-
import pydub
|
7 |
-
import numpy as np
|
8 |
|
9 |
-
#
|
10 |
def cleanString(string):
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
return cleanString.lower()
|
16 |
|
17 |
-
#
|
18 |
def download_audio(url):
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
46 |
with gr.Blocks() as demo:
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import re
|
3 |
+
import unidecode
|
4 |
import yt_dlp
|
5 |
import os
|
|
|
|
|
6 |
|
7 |
+
# Function to clean the string for file naming
|
8 |
def cleanString(string):
|
9 |
+
clean_string = unidecode.unidecode(string)
|
10 |
+
clean_string = re.sub(r'[^\w\s]', '', clean_string)
|
11 |
+
clean_string = clean_string.replace(" ", "_")
|
12 |
+
return clean_string.lower()
|
|
|
13 |
|
14 |
+
# Function to download audio from YouTube
|
15 |
def download_audio(url):
|
16 |
+
path_to_folder_audio_mp3 = "./"
|
17 |
+
ydl_opts = {
|
18 |
+
'format': 'bestaudio/best',
|
19 |
+
'outtmpl': f'{path_to_folder_audio_mp3}%(title)s.%(ext)s',
|
20 |
+
'postprocessors': [{
|
21 |
+
'key': 'FFmpegExtractAudio',
|
22 |
+
'preferredcodec': 'mp3',
|
23 |
+
'preferredquality': '192',
|
24 |
+
}],
|
25 |
+
'noplaylist': True, # Avoid downloading playlists
|
26 |
+
}
|
27 |
+
|
28 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
29 |
+
info_dict = ydl.extract_info(url, download=True)
|
30 |
+
video_title = info_dict['title']
|
31 |
+
|
32 |
+
# Clean the title for the filename
|
33 |
+
new_local_link = cleanString(video_title) + ".mp3"
|
34 |
+
original_file_path = os.path.join(path_to_folder_audio_mp3, f"{video_title}.mp3")
|
35 |
+
|
36 |
+
# Rename the audio file if it exists
|
37 |
+
if os.path.exists(original_file_path):
|
38 |
+
os.rename(original_file_path, os.path.join(path_to_folder_audio_mp3, new_local_link))
|
39 |
+
|
40 |
+
# Get the final file path
|
41 |
+
file_path = os.path.join(path_to_folder_audio_mp3, new_local_link)
|
42 |
+
|
43 |
+
return file_path, file_path
|
44 |
+
|
45 |
+
# Gradio interface
|
46 |
with gr.Blocks() as demo:
|
47 |
+
gr.Markdown("<h1><center>Free YouTube URL Video-to-Audio</center></h1>")
|
48 |
+
gr.Markdown("<center>Enter the link of any YouTube video to generate its mp3 audio file.</center>")
|
49 |
+
|
50 |
+
input_text_url = gr.Textbox(placeholder='YouTube video URL', label='YouTube URL')
|
51 |
+
result_button_audio = gr.Button('Get Audio File')
|
52 |
+
output_audio_file = gr.File(label='MP3 Audio File')
|
53 |
+
output_audio_play = gr.Audio(type="filepath", label="Listen to Audio")
|
54 |
+
|
55 |
+
result_button_audio.click(download_audio, inputs=input_text_url, outputs=[output_audio_file, output_audio_play])
|
56 |
+
|
57 |
+
# Launch the Gradio app
|
58 |
+
demo.queue().launch(debug=True)
|