Spaces:
Running
Running
dennisvdang
commited on
Commit
·
0eb1a5b
1
Parent(s):
e6301c4
Update audio extraction libraries
Browse files
app.py
CHANGED
@@ -11,6 +11,7 @@ from functools import reduce
|
|
11 |
from sklearn.preprocessing import StandardScaler
|
12 |
from matplotlib import pyplot as plt
|
13 |
import tempfile
|
|
|
14 |
|
15 |
# Constants
|
16 |
SR = 12000
|
@@ -26,6 +27,7 @@ tf.get_logger().setLevel('ERROR')
|
|
26 |
|
27 |
def extract_audio(url):
|
28 |
try:
|
|
|
29 |
yt = YouTube(url)
|
30 |
video_title = yt.title
|
31 |
audio_stream = yt.streams.filter(only_audio=True).first()
|
@@ -42,7 +44,28 @@ def extract_audio(url):
|
|
42 |
st.error("No audio stream found")
|
43 |
return None, None, None
|
44 |
except Exception as e:
|
45 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
return None, None, None
|
47 |
|
48 |
|
|
|
11 |
from sklearn.preprocessing import StandardScaler
|
12 |
from matplotlib import pyplot as plt
|
13 |
import tempfile
|
14 |
+
import yt_dlp
|
15 |
|
16 |
# Constants
|
17 |
SR = 12000
|
|
|
27 |
|
28 |
def extract_audio(url):
|
29 |
try:
|
30 |
+
# Attempt to use pytube
|
31 |
yt = YouTube(url)
|
32 |
video_title = yt.title
|
33 |
audio_stream = yt.streams.filter(only_audio=True).first()
|
|
|
44 |
st.error("No audio stream found")
|
45 |
return None, None, None
|
46 |
except Exception as e:
|
47 |
+
st.warning(f"pytube error: {e}. Falling back to yt-dlp.")
|
48 |
+
return extract_audio_with_ytdlp(url)
|
49 |
+
|
50 |
+
def extract_audio_with_ytdlp(url):
|
51 |
+
try:
|
52 |
+
ydl_opts = {
|
53 |
+
'format': 'bestaudio/best',
|
54 |
+
'postprocessors': [{
|
55 |
+
'key': 'FFmpegExtractAudio',
|
56 |
+
'preferredcodec': 'mp3',
|
57 |
+
'preferredquality': '192',
|
58 |
+
}],
|
59 |
+
'outtmpl': '%(title)s.%(ext)s',
|
60 |
+
'paths': {'home': tempfile.mkdtemp()}
|
61 |
+
}
|
62 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
63 |
+
info_dict = ydl.extract_info(url, download=True)
|
64 |
+
video_title = info_dict.get('title', None)
|
65 |
+
audio_file = os.path.join(ydl_opts['paths']['home'], f"{video_title}.mp3")
|
66 |
+
return audio_file, video_title, ydl_opts['paths']['home']
|
67 |
+
except Exception as e:
|
68 |
+
st.error(f"An error occurred with yt-dlp: {e}")
|
69 |
return None, None, None
|
70 |
|
71 |
|