Spaces:
Running
Running
pierreguillou
commited on
Commit
•
9b1d24e
1
Parent(s):
b0d4dca
Update app.py
Browse files
app.py
CHANGED
@@ -3,12 +3,25 @@ import re, unidecode
|
|
3 |
from unidecode import unidecode
|
4 |
import yt_dlp
|
5 |
import os
|
|
|
|
|
6 |
|
7 |
# no space, punctuation, accent in lower string
|
8 |
def cleanString(string):
|
9 |
cleanString = unidecode(string)
|
10 |
cleanString = re.sub('\W+','_', cleanString)
|
11 |
-
return cleanString.lower
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
def download_audio(url):
|
14 |
path_to_folder_audio_mp3 = "./audio/"
|
@@ -31,12 +44,20 @@ def download_audio(url):
|
|
31 |
if cleanString(local_link) == cleanString(filename):
|
32 |
os.rename(os.path.join(path_to_folder_audio_mp3, filename),os.path.join(path_to_folder_audio_mp3, new_local_link))
|
33 |
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
# Gradio interface
|
37 |
iface = gr.Interface(fn=download_audio,
|
38 |
inputs=gr.Textbox(label="YouTube Video URL"),
|
39 |
-
outputs=
|
|
|
|
|
|
|
40 |
allow_flagging="never"
|
41 |
)
|
42 |
iface.launch()
|
|
|
3 |
from unidecode import unidecode
|
4 |
import yt_dlp
|
5 |
import os
|
6 |
+
import pydub
|
7 |
+
import numpy as np
|
8 |
|
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 |
+
def read_audio(f, normalized=False):
|
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 |
path_to_folder_audio_mp3 = "./audio/"
|
|
|
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 |
+
with open(file_path, "r") as f:
|
50 |
+
audio =
|
51 |
+
|
52 |
+
return file_path, read_audio(file_path)
|
53 |
|
54 |
# Gradio interface
|
55 |
iface = gr.Interface(fn=download_audio,
|
56 |
inputs=gr.Textbox(label="YouTube Video URL"),
|
57 |
+
outputs=[
|
58 |
+
gr.File(label="Output Audio File"),
|
59 |
+
gr.Audio(label="Play Audio", show_download_button=False, format="mp3"),
|
60 |
+
],
|
61 |
allow_flagging="never"
|
62 |
)
|
63 |
iface.launch()
|