Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,7 @@ import gradio as gr
|
|
4 |
import re
|
5 |
from pydub import AudioSegment
|
6 |
|
7 |
-
|
8 |
os.makedirs("downloads", exist_ok=True)
|
9 |
|
10 |
def sanitize_filename(filename):
|
@@ -20,14 +20,14 @@ def process_youtube_or_audio(url, uploaded_audio, start_time, end_time):
|
|
20 |
ydl_opts = {
|
21 |
'format': 'm4a/bestaudio',
|
22 |
'outtmpl': 'downloads/%(title)s.%(ext)s',
|
23 |
-
'cookiefile': 'cookies.txt' #
|
24 |
}
|
25 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
26 |
info = ydl.extract_info(url, download=True)
|
27 |
filename = ydl.prepare_filename(info)
|
28 |
song_name = sanitize_filename(info['title'])
|
29 |
|
30 |
-
|
31 |
if not filename.endswith(".m4a"):
|
32 |
possible_file = f"downloads/{song_name}.m4a"
|
33 |
if os.path.exists(possible_file):
|
@@ -39,12 +39,12 @@ def process_youtube_or_audio(url, uploaded_audio, start_time, end_time):
|
|
39 |
filename = uploaded_audio
|
40 |
song_name = sanitize_filename(os.path.splitext(os.path.basename(uploaded_audio))[0])
|
41 |
|
42 |
-
|
43 |
if not filename or not os.path.exists(filename):
|
44 |
print(f"File not found: {filename}")
|
45 |
return None, None
|
46 |
|
47 |
-
|
48 |
audio = AudioSegment.from_file(filename)
|
49 |
start_ms, end_ms = int(start_time * 1000), int(end_time * 1000)
|
50 |
start_ms = max(0, min(start_ms, len(audio)))
|
@@ -52,11 +52,11 @@ def process_youtube_or_audio(url, uploaded_audio, start_time, end_time):
|
|
52 |
|
53 |
trimmed_audio = audio[start_ms:end_ms]
|
54 |
|
55 |
-
|
56 |
mp3_filename = f"downloads/{song_name}.mp3"
|
57 |
trimmed_audio.export(mp3_filename, format="mp3")
|
58 |
|
59 |
-
|
60 |
m4r_filename = f"downloads/{song_name}.m4r"
|
61 |
trimmed_audio.export(m4r_filename, format="m4r")
|
62 |
|
|
|
4 |
import re
|
5 |
from pydub import AudioSegment
|
6 |
|
7 |
+
|
8 |
os.makedirs("downloads", exist_ok=True)
|
9 |
|
10 |
def sanitize_filename(filename):
|
|
|
20 |
ydl_opts = {
|
21 |
'format': 'm4a/bestaudio',
|
22 |
'outtmpl': 'downloads/%(title)s.%(ext)s',
|
23 |
+
'cookiefile': 'cookies.txt' #yt
|
24 |
}
|
25 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
26 |
info = ydl.extract_info(url, download=True)
|
27 |
filename = ydl.prepare_filename(info)
|
28 |
song_name = sanitize_filename(info['title'])
|
29 |
|
30 |
+
|
31 |
if not filename.endswith(".m4a"):
|
32 |
possible_file = f"downloads/{song_name}.m4a"
|
33 |
if os.path.exists(possible_file):
|
|
|
39 |
filename = uploaded_audio
|
40 |
song_name = sanitize_filename(os.path.splitext(os.path.basename(uploaded_audio))[0])
|
41 |
|
42 |
+
|
43 |
if not filename or not os.path.exists(filename):
|
44 |
print(f"File not found: {filename}")
|
45 |
return None, None
|
46 |
|
47 |
+
|
48 |
audio = AudioSegment.from_file(filename)
|
49 |
start_ms, end_ms = int(start_time * 1000), int(end_time * 1000)
|
50 |
start_ms = max(0, min(start_ms, len(audio)))
|
|
|
52 |
|
53 |
trimmed_audio = audio[start_ms:end_ms]
|
54 |
|
55 |
+
|
56 |
mp3_filename = f"downloads/{song_name}.mp3"
|
57 |
trimmed_audio.export(mp3_filename, format="mp3")
|
58 |
|
59 |
+
|
60 |
m4r_filename = f"downloads/{song_name}.m4r"
|
61 |
trimmed_audio.export(m4r_filename, format="m4r")
|
62 |
|