Spaces:
Sleeping
Sleeping
pierreguillou
commited on
Commit
•
f14a1c1
1
Parent(s):
10a0c3e
Update app.py
Browse files
app.py
CHANGED
@@ -9,22 +9,15 @@ import numpy as np
|
|
9 |
# no space, punctuation, accent in lower string
|
10 |
def cleanString(string):
|
11 |
cleanString = unidecode(string)
|
12 |
-
cleanString = re.sub('\W+','_', cleanString)
|
|
|
|
|
13 |
return cleanString.lower()
|
14 |
|
15 |
-
|
16 |
-
"""MP3 to numpy array"""
|
17 |
-
a = pydub.AudioSegment.from_mp3(f)
|
18 |
-
y = np.array(a.get_array_of_samples())
|
19 |
-
if a.channels == 2:
|
20 |
-
y = y.reshape((-1, 2))
|
21 |
-
if normalized:
|
22 |
-
return a.frame_rate, np.float32(y) / 2**15
|
23 |
-
else:
|
24 |
-
return a.frame_rate, y
|
25 |
-
|
26 |
def download_audio(url):
|
27 |
-
|
|
|
28 |
ydl_opts = {
|
29 |
'format': 'm4a/bestaudio/best',
|
30 |
'outtmpl': f'{path_to_folder_audio_mp3}%(title)s',
|
@@ -33,6 +26,7 @@ def download_audio(url):
|
|
33 |
'preferredcodec': 'mp3',
|
34 |
}]
|
35 |
}
|
|
|
36 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
37 |
info_dict = ydl.extract_info(url, download=True)
|
38 |
video_title = info_dict['title']
|
@@ -42,21 +36,22 @@ def download_audio(url):
|
|
42 |
new_local_link = cleanString(video_title) + ".mp3"
|
43 |
for filename in os.listdir(path_to_folder_audio_mp3):
|
44 |
if cleanString(local_link) == cleanString(filename):
|
45 |
-
os.rename(os.path.join(path_to_folder_audio_mp3, filename),os.path.join(path_to_folder_audio_mp3, new_local_link))
|
46 |
|
|
|
47 |
file_path = path_to_folder_audio_mp3 + new_local_link
|
48 |
|
49 |
-
return file_path,
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
|
|
9 |
# no space, punctuation, accent in lower string
|
10 |
def cleanString(string):
|
11 |
cleanString = unidecode(string)
|
12 |
+
# cleanString = re.sub('\W+','_', cleanString)
|
13 |
+
cleanString = re.sub(r'[^\w\s]','',cleanString)
|
14 |
+
cleanString = cleanString.replace(" ", "_")
|
15 |
return cleanString.lower()
|
16 |
|
17 |
+
# from YouTube url to audio file path and sample rate + numpy array
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
def download_audio(url):
|
19 |
+
|
20 |
+
path_to_folder_audio_mp3 = "./"
|
21 |
ydl_opts = {
|
22 |
'format': 'm4a/bestaudio/best',
|
23 |
'outtmpl': f'{path_to_folder_audio_mp3}%(title)s',
|
|
|
26 |
'preferredcodec': 'mp3',
|
27 |
}]
|
28 |
}
|
29 |
+
|
30 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
31 |
info_dict = ydl.extract_info(url, download=True)
|
32 |
video_title = info_dict['title']
|
|
|
36 |
new_local_link = cleanString(video_title) + ".mp3"
|
37 |
for filename in os.listdir(path_to_folder_audio_mp3):
|
38 |
if cleanString(local_link) == cleanString(filename):
|
39 |
+
os.rename(os.path.join(path_to_folder_audio_mp3, filename),os.path.join(path_to_folder_audio_mp3, new_local_link))
|
40 |
|
41 |
+
# get audio file path
|
42 |
file_path = path_to_folder_audio_mp3 + new_local_link
|
43 |
|
44 |
+
return file_path, file_path
|
45 |
+
|
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 |
+
demo.queue().launch(debug = False)
|