sudo-soldier commited on
Commit
6ed23fa
·
verified ·
1 Parent(s): 885477c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +2 -11
app.py CHANGED
@@ -5,16 +5,13 @@ from pydub import AudioSegment
5
  import re
6
  import subprocess
7
 
8
- # Create downloads directory safely
9
  if not os.path.exists("downloads"):
10
  os.makedirs("downloads")
11
 
12
  def sanitize_filename(filename):
13
- """Sanitize the filename by removing or replacing special characters."""
14
  return re.sub(r'[^a-zA-Z0-9_-]', '_', filename)
15
 
16
  def process_youtube_or_audio(url, recorded_audio):
17
- """Process YouTube URL or recorded audio to create ringtones."""
18
  try:
19
  filename = None
20
  song_name = None
@@ -42,15 +39,12 @@ def process_youtube_or_audio(url, recorded_audio):
42
  audio = AudioSegment.from_file(filename)
43
  audio.export(mp3_filename, format="mp3")
44
 
45
- # Use ffmpeg to convert mp3 to m4a
46
  m4a_filename = f"downloads/{song_name}.m4a"
47
  if not os.path.exists(m4a_filename):
48
  subprocess.run(['ffmpeg', '-i', mp3_filename, '-vn', '-acodec', 'aac', '-b:a', '192k', m4a_filename])
49
 
50
- # Trim the first 20 seconds
51
  m4r_filename = f"downloads/{song_name}.m4r"
52
  if not os.path.exists(m4r_filename):
53
- # Rename the .m4a to .m4r
54
  os.rename(m4a_filename, m4r_filename)
55
 
56
  return mp3_filename, m4r_filename
@@ -59,7 +53,6 @@ def process_youtube_or_audio(url, recorded_audio):
59
  print(f"Error: {e}")
60
  return None, None
61
 
62
- # Gradio UI with Improved Layout
63
  with gr.Blocks(css="""
64
  body { font-family: Arial, sans-serif; text-align: center; }
65
  .light-btn { background-color: #f0f0f0; color: #333; border: 2px solid #ccc; padding: 10px 20px; font-size: 16px; cursor: pointer; }
@@ -74,13 +67,12 @@ with gr.Blocks(css="""
74
  with gr.Row():
75
  with gr.Column(scale=1, min_width=250):
76
  gr.HTML('<label><i class="fa fa-link"></i>YouTube URL https://</label>')
77
- youtube_url = gr.Textbox(placeholder="Paste link here...")
78
 
79
- with gr.Column(scale=1, min_width=250):
80
  gr.HTML('<label><i class="fa fa-microphone"></i>Record Audio</label>')
81
  audio_record = gr.Audio(sources=["microphone"], type="filepath", show_label=False)
82
 
83
-
84
  with gr.Row():
85
  process_button = gr.Button("🎵 Create Ringtones", elem_classes="light-btn")
86
 
@@ -98,4 +90,3 @@ with gr.Blocks(css="""
98
  interface.launch(share=True)
99
 
100
 
101
-
 
5
  import re
6
  import subprocess
7
 
 
8
  if not os.path.exists("downloads"):
9
  os.makedirs("downloads")
10
 
11
  def sanitize_filename(filename):
 
12
  return re.sub(r'[^a-zA-Z0-9_-]', '_', filename)
13
 
14
  def process_youtube_or_audio(url, recorded_audio):
 
15
  try:
16
  filename = None
17
  song_name = None
 
39
  audio = AudioSegment.from_file(filename)
40
  audio.export(mp3_filename, format="mp3")
41
 
 
42
  m4a_filename = f"downloads/{song_name}.m4a"
43
  if not os.path.exists(m4a_filename):
44
  subprocess.run(['ffmpeg', '-i', mp3_filename, '-vn', '-acodec', 'aac', '-b:a', '192k', m4a_filename])
45
 
 
46
  m4r_filename = f"downloads/{song_name}.m4r"
47
  if not os.path.exists(m4r_filename):
 
48
  os.rename(m4a_filename, m4r_filename)
49
 
50
  return mp3_filename, m4r_filename
 
53
  print(f"Error: {e}")
54
  return None, None
55
 
 
56
  with gr.Blocks(css="""
57
  body { font-family: Arial, sans-serif; text-align: center; }
58
  .light-btn { background-color: #f0f0f0; color: #333; border: 2px solid #ccc; padding: 10px 20px; font-size: 16px; cursor: pointer; }
 
67
  with gr.Row():
68
  with gr.Column(scale=1, min_width=250):
69
  gr.HTML('<label><i class="fa fa-link"></i>YouTube URL https://</label>')
70
+ youtube_url = gr.Textbox(placeholder="Paste link here...", show_label=False)
71
 
72
+ with gr.Column(scale=1, min_width=250):
73
  gr.HTML('<label><i class="fa fa-microphone"></i>Record Audio</label>')
74
  audio_record = gr.Audio(sources=["microphone"], type="filepath", show_label=False)
75
 
 
76
  with gr.Row():
77
  process_button = gr.Button("🎵 Create Ringtones", elem_classes="light-btn")
78
 
 
90
  interface.launch(share=True)
91
 
92