Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,50 +1,4 @@
|
|
1 |
-
|
2 |
-
import yt_dlp
|
3 |
-
import os
|
4 |
-
from pydub import AudioSegment
|
5 |
-
import re
|
6 |
-
|
7 |
-
os.makedirs("downloads", exist_ok=True)
|
8 |
-
|
9 |
-
def sanitize_filename(filename):
|
10 |
-
"""Sanitize the filename by removing or replacing special characters."""
|
11 |
-
return re.sub(r'[^a-zA-Z0-9_-]', '_', filename)
|
12 |
-
|
13 |
-
def process_youtube_or_audio(url, recorded_audio):
|
14 |
-
"""Process YouTube URL or recorded audio to create ringtones."""
|
15 |
-
try:
|
16 |
-
filename = None
|
17 |
-
song_name = None
|
18 |
-
|
19 |
-
|
20 |
-
if url:
|
21 |
-
ydl_opts = {
|
22 |
-
'format': 'bestaudio/best',
|
23 |
-
'outtmpl': 'downloads/%(id)s.%(ext)s',
|
24 |
-
'cookiefile': 'cookies.txt' #cookies.txt
|
25 |
-
}
|
26 |
-
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
27 |
-
info = ydl.extract_info(url, download=True)
|
28 |
-
filename = os.path.join('downloads', f"{info['id']}.webm")
|
29 |
-
song_name = sanitize_filename(info['title'])
|
30 |
-
|
31 |
-
|
32 |
-
elif recorded_audio:
|
33 |
-
filename = recorded_audio
|
34 |
-
song_name = "recorded_audio"
|
35 |
-
|
36 |
-
if not filename or not os.path.exists(filename):
|
37 |
-
return None, None
|
38 |
-
|
39 |
-
|
40 |
-
mp3_filename = f"downloads/{song_name}.mp3"
|
41 |
-
if not os.path.exists(mp3_filename):
|
42 |
-
audio = AudioSegment.from_file(filename)
|
43 |
-
audio.export(mp3_filename, format="mp3")
|
44 |
-
|
45 |
-
|
46 |
-
ringtone_filename_m4r = f"downloads/{song_name}.m4r"
|
47 |
-
if not os.path.exists(ringtone_filename_m4r):
|
48 |
ringtone_audio = AudioSegment.from_file(mp3_filename)[:20000] # 20 seconds
|
49 |
ringtone_audio.export(ringtone_filename_m4r, format="mp4")
|
50 |
|
|
|
1 |
+
m4r):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
ringtone_audio = AudioSegment.from_file(mp3_filename)[:20000] # 20 seconds
|
3 |
ringtone_audio.export(ringtone_filename_m4r, format="mp4")
|
4 |
|