sudo-soldier commited on
Commit
cfea90d
·
verified ·
1 Parent(s): 4bb038f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -9
app.py CHANGED
@@ -4,7 +4,7 @@ import os
4
  from pydub import AudioSegment
5
  import re
6
 
7
- # Create downloads directory if it doesn't exist
8
  os.makedirs("downloads", exist_ok=True)
9
 
10
  def sanitize_filename(filename):
@@ -17,12 +17,12 @@ def process_youtube_url(url, uploaded_file):
17
  filename = None
18
  song_name = None
19
 
20
- # Download the song if a URL is provided
21
  if url:
22
  ydl_opts = {
23
  'format': 'bestaudio/best',
24
  'outtmpl': 'downloads/%(id)s.%(ext)s',
25
- 'cookiefile': 'cookies.txt' # Optional: use cookies for authentication
26
  }
27
 
28
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
@@ -30,35 +30,34 @@ def process_youtube_url(url, uploaded_file):
30
  filename = os.path.join('downloads', f"{info['id']}.webm")
31
  song_name = sanitize_filename(info['title'])
32
 
33
- # Use uploaded file if URL is not provided
34
  elif uploaded_file:
35
  filename = uploaded_file.name
36
  song_name = sanitize_filename(os.path.splitext(uploaded_file.name)[0])
37
 
38
- # Ensure file exists
39
  if not filename or not os.path.exists(filename):
40
  return None, None, None
41
 
42
- # Convert to MP3 if not already converted
43
  mp3_filename = f"downloads/{song_name}.mp3"
44
  if not os.path.exists(mp3_filename):
45
  audio = AudioSegment.from_file(filename)
46
  audio.export(mp3_filename, format="mp3")
47
 
48
- # Convert to M4R (iPhone ringtone format), limiting length to 20 seconds
49
  ringtone_filename_m4r = f"downloads/{song_name}.m4r"
50
  if not os.path.exists(ringtone_filename_m4r):
51
  ringtone_audio = AudioSegment.from_file(mp3_filename)[:20000] # 20 seconds
52
  ringtone_audio.export(ringtone_filename_m4r, format="mp4")
53
 
54
- # Return file names for download
55
  return mp3_filename, ringtone_filename_m4r
56
 
57
  except Exception as e:
58
  print(f"Error: {e}")
59
  return None, None
60
 
61
- # Gradio UI
62
  with gr.Blocks() as interface:
63
  gr.HTML("""
64
  <h1>Python YouTube Ringtones</h1>
 
4
  from pydub import AudioSegment
5
  import re
6
 
7
+
8
  os.makedirs("downloads", exist_ok=True)
9
 
10
  def sanitize_filename(filename):
 
17
  filename = None
18
  song_name = None
19
 
20
+
21
  if url:
22
  ydl_opts = {
23
  'format': 'bestaudio/best',
24
  'outtmpl': 'downloads/%(id)s.%(ext)s',
25
+ 'cookiefile': 'cookies.txt' #cookies
26
  }
27
 
28
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
 
30
  filename = os.path.join('downloads', f"{info['id']}.webm")
31
  song_name = sanitize_filename(info['title'])
32
 
33
+
34
  elif uploaded_file:
35
  filename = uploaded_file.name
36
  song_name = sanitize_filename(os.path.splitext(uploaded_file.name)[0])
37
 
38
+
39
  if not filename or not os.path.exists(filename):
40
  return None, None, None
41
 
 
42
  mp3_filename = f"downloads/{song_name}.mp3"
43
  if not os.path.exists(mp3_filename):
44
  audio = AudioSegment.from_file(filename)
45
  audio.export(mp3_filename, format="mp3")
46
 
47
+
48
  ringtone_filename_m4r = f"downloads/{song_name}.m4r"
49
  if not os.path.exists(ringtone_filename_m4r):
50
  ringtone_audio = AudioSegment.from_file(mp3_filename)[:20000] # 20 seconds
51
  ringtone_audio.export(ringtone_filename_m4r, format="mp4")
52
 
53
+
54
  return mp3_filename, ringtone_filename_m4r
55
 
56
  except Exception as e:
57
  print(f"Error: {e}")
58
  return None, None
59
 
60
+ #UI
61
  with gr.Blocks() as interface:
62
  gr.HTML("""
63
  <h1>Python YouTube Ringtones</h1>