sudo-soldier commited on
Commit
a148336
·
verified ·
1 Parent(s): ebd001b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -6
app.py CHANGED
@@ -1,4 +1,46 @@
1
- m4r):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  ringtone_audio = AudioSegment.from_file(mp3_filename)[:20000] # 20 seconds
3
  ringtone_audio.export(ringtone_filename_m4r, format="mp4")
4
 
@@ -8,22 +50,27 @@ m4r):
8
  print(f"Error: {e}")
9
  return None, None
10
 
11
- # Gradio UI
12
  with gr.Blocks() as interface:
13
  gr.HTML("""
14
- <h1>Python YouTube Ringtones</h1>
 
15
  <p>Insert a URL to create ringtones or record your own audio.</p>
16
  """)
17
 
18
  with gr.Row():
19
- youtube_url = gr.Textbox(label="Enter YouTube URL", placeholder="Paste the URL here...")
20
- audio_record = gr.Audio(sources=["microphone"], type="filepath", label="Record Audio")
 
 
 
 
21
 
22
  with gr.Row():
23
  mp3_download = gr.File(label="Download Android Ringtone")
24
  iphone_ringtone = gr.File(label="Download iPhone Ringtone")
25
 
26
- process_button = gr.Button("Create Ringtones")
27
  process_button.click(process_youtube_or_audio, inputs=[youtube_url, audio_record], outputs=[mp3_download, iphone_ringtone])
28
 
29
  interface.launch(share=True)
 
1
+ import gradio as gr
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
+ if url:
20
+ ydl_opts = {
21
+ 'format': 'bestaudio/best',
22
+ 'outtmpl': 'downloads/%(id)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 = os.path.join('downloads', f"{info['id']}.webm")
28
+ song_name = sanitize_filename(info['title'])
29
+
30
+ elif recorded_audio:
31
+ filename = recorded_audio
32
+ song_name = "recorded_audio"
33
+
34
+ if not filename or not os.path.exists(filename):
35
+ return None, None
36
+
37
+ mp3_filename = f"downloads/{song_name}.mp3"
38
+ if not os.path.exists(mp3_filename):
39
+ audio = AudioSegment.from_file(filename)
40
+ audio.export(mp3_filename, format="mp3")
41
+
42
+ ringtone_filename_m4r = f"downloads/{song_name}.m4r"
43
+ if not os.path.exists(ringtone_filename_m4r):
44
  ringtone_audio = AudioSegment.from_file(mp3_filename)[:20000] # 20 seconds
45
  ringtone_audio.export(ringtone_filename_m4r, format="mp4")
46
 
 
50
  print(f"Error: {e}")
51
  return None, None
52
 
53
+ # Gradio UI with FontAwesome
54
  with gr.Blocks() as interface:
55
  gr.HTML("""
56
+ <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
57
+ <h1><i class="fa fa-music"></i> Python YouTube Ringtones</h1>
58
  <p>Insert a URL to create ringtones or record your own audio.</p>
59
  """)
60
 
61
  with gr.Row():
62
+ gr.HTML('<p><i class="fa fa-link"></i> Enter YouTube URL:</p>')
63
+ youtube_url = gr.Textbox(placeholder="Paste the URL here...")
64
+
65
+ with gr.Row():
66
+ gr.HTML('<p><i class="fa fa-microphone"></i> Record Audio:</p>')
67
+ audio_record = gr.Audio(sources=["microphone"], type="filepath")
68
 
69
  with gr.Row():
70
  mp3_download = gr.File(label="Download Android Ringtone")
71
  iphone_ringtone = gr.File(label="Download iPhone Ringtone")
72
 
73
+ process_button = gr.Button("🎵 Create Ringtones")
74
  process_button.click(process_youtube_or_audio, inputs=[youtube_url, audio_record], outputs=[mp3_download, iphone_ringtone])
75
 
76
  interface.launch(share=True)