sudo-soldier commited on
Commit
19487f1
·
verified ·
1 Parent(s): cfea90d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -19
app.py CHANGED
@@ -4,76 +4,73 @@ import os
4
  from pydub import AudioSegment
5
  import re
6
 
7
-
8
  os.makedirs("downloads", exist_ok=True)
9
 
10
  def sanitize_filename(filename):
11
  """Sanitize the filename by removing or replacing special characters."""
12
  return re.sub(r'[^a-zA-Z0-9_-]', '_', filename)
13
 
14
- def process_youtube_url(url, uploaded_file):
15
- """Process the YouTube URL or uploaded file to create ringtones."""
16
  try:
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:
29
  info = ydl.extract_info(url, download=True)
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>
64
- <p>Insert a URL to create ringtones or Upload an MP3 to convert.</p>
65
  """)
66
 
67
  with gr.Row():
68
  youtube_url = gr.Textbox(label="Enter YouTube URL", placeholder="Paste the URL here...")
69
- mp3_upload = gr.File(label="Upload MP3 (Optional)")
70
 
71
  with gr.Row():
72
  mp3_download = gr.File(label="Download Android Ringtone")
73
  iphone_ringtone = gr.File(label="Download iPhone Ringtone")
74
 
75
  process_button = gr.Button("Create Ringtones")
76
- process_button.click(process_youtube_url, inputs=[youtube_url, mp3_upload], outputs=[mp3_download, iphone_ringtone])
77
 
78
  interface.launch(share=True)
79
 
@@ -93,3 +90,4 @@ interface.launch(share=True)
93
 
94
 
95
 
 
 
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
+ # Download from YouTube if URL is provided
20
  if url:
21
  ydl_opts = {
22
  'format': 'bestaudio/best',
23
  'outtmpl': 'downloads/%(id)s.%(ext)s',
24
+ 'cookiefile': 'cookies.txt' # Use cookies for authentication if needed
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
+ # If audio is recorded, use it
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
+ # Convert to MP3
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
+ # Convert to iPhone ringtone (M4R, 20 sec clip)
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
 
 
51
  return mp3_filename, ringtone_filename_m4r
52
 
53
  except Exception as e:
54
  print(f"Error: {e}")
55
  return None, None
56
 
57
+ # Gradio UI
58
  with gr.Blocks() as interface:
59
  gr.HTML("""
60
  <h1>Python YouTube Ringtones</h1>
61
+ <p>Insert a URL to create ringtones or record your own audio.</p>
62
  """)
63
 
64
  with gr.Row():
65
  youtube_url = gr.Textbox(label="Enter YouTube URL", placeholder="Paste the URL here...")
66
+ audio_record = gr.Audio(sources=["microphone"], type="filepath", label="Record Audio")
67
 
68
  with gr.Row():
69
  mp3_download = gr.File(label="Download Android Ringtone")
70
  iphone_ringtone = gr.File(label="Download iPhone Ringtone")
71
 
72
  process_button = gr.Button("Create Ringtones")
73
+ process_button.click(process_youtube_or_audio, inputs=[youtube_url, audio_record], outputs=[mp3_download, iphone_ringtone])
74
 
75
  interface.launch(share=True)
76
 
 
90
 
91
 
92
 
93
+