sudo-soldier commited on
Commit
ccea871
·
verified ·
1 Parent(s): 147c80a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -55
app.py CHANGED
@@ -5,19 +5,17 @@ import re
5
  import subprocess
6
  from pydub import AudioSegment
7
 
8
-
9
  os.makedirs("downloads", exist_ok=True)
10
 
11
  def sanitize_filename(filename):
12
  """Sanitize filenames by replacing special characters."""
13
  return re.sub(r'[^a-zA-Z0-9_-]', '_', filename)
14
 
15
- def process_youtube_or_audio(url, recorded_audio, start_time, end_time):
16
  """Downloads or processes audio, trims it, and exports ringtones."""
17
  try:
18
  filename, song_name = None, None
19
 
20
-
21
  if url:
22
  ydl_opts = {
23
  'format': 'bestaudio/best',
@@ -28,31 +26,24 @@ def process_youtube_or_audio(url, recorded_audio, start_time, end_time):
28
  info = ydl.extract_info(url, download=True)
29
  filename = ydl.prepare_filename(info)
30
  song_name = sanitize_filename(info['title'])
31
-
32
-
33
- elif recorded_audio and isinstance(recorded_audio, dict):
34
- filename = recorded_audio["name"]
35
- song_name = "recorded_audio"
36
-
37
-
38
  if not filename or not os.path.exists(filename):
39
  return None, None
40
 
41
-
42
  audio = AudioSegment.from_file(filename)
43
  start_ms, end_ms = int(start_time * 1000), int(end_time * 1000)
44
-
45
-
46
  start_ms = max(0, min(start_ms, len(audio)))
47
  end_ms = max(start_ms, min(end_ms, len(audio)))
48
 
49
  trimmed_audio = audio[start_ms:end_ms]
50
 
51
-
52
  mp3_filename = f"downloads/{song_name}.mp3"
53
  trimmed_audio.export(mp3_filename, format="mp3")
54
 
55
-
56
  m4a_filename = f"downloads/{song_name}.m4a"
57
  result = subprocess.run([
58
  'ffmpeg', '-y', '-i', mp3_filename, '-vn', '-acodec', 'aac', '-b:a', '192k', m4a_filename
@@ -60,9 +51,8 @@ def process_youtube_or_audio(url, recorded_audio, start_time, end_time):
60
 
61
  if result.returncode != 0 or not os.path.exists(m4a_filename):
62
  print("FFmpeg Error:", result.stderr)
63
- return mp3_filename, None #
64
 
65
-
66
  m4r_filename = f"downloads/{song_name}.m4r"
67
  os.rename(m4a_filename, m4r_filename)
68
 
@@ -72,53 +62,28 @@ def process_youtube_or_audio(url, recorded_audio, start_time, end_time):
72
  print(f"Error: {e}")
73
  return None, None
74
 
75
-
76
- with gr.Blocks(css="""
77
- body { font-family: Arial, sans-serif; text-align: center; }
78
- .light-btn { background-color: #ADD8E6; color: #333; border: 2px solid #ccc; padding: 10px 20px; font-size: 16px; cursor: pointer; }
79
- .light-btn:hover { background-color: #87CEFA; }
80
- """) as interface:
81
-
82
  gr.HTML("""
83
- <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
84
  <h1><i class="fas fa-music"></i>&nbsp;PYTR - Python YouTube Ringtones</h1>
85
- <p>Enter a YouTube URL or record audio to create ringtones.</p>
86
- <p>
87
- <a href="https://ringtones.JesseJesse.xyz" target="_blank">Ringtones API</a>&nbsp;&nbsp;&nbsp;
88
- <a href="https://pub-c1de1cb456e74d6bbbee111ba9e6c757.r2.dev/iphone.png" target="_blank">iPhone Transfers</a>&nbsp;&nbsp;&nbsp;
89
- <a href="https://youtube.com" target="_blank">YouTube</a>
90
- </p>
91
  """)
92
-
93
  with gr.Row():
94
- with gr.Column(scale=1, min_width=250):
95
- gr.HTML('<label><i class="fas fa-link"></i>&nbsp;YouTube URL</label>')
96
- youtube_url = gr.Textbox(placeholder="Enter the URL here...", show_label=False)
97
-
98
- with gr.Column(scale=1, min_width=250):
99
- gr.HTML('<label><i class="fas fa-microphone"></i>&nbsp;Record Audio</label>')
100
- audio_record = gr.Audio(sources=["microphone"], type="filepath", show_label=False)
101
-
102
  gr.HTML("<h3>Trim Audio</h3>")
103
-
104
  with gr.Row():
105
  start_time = gr.Slider(0, 20, value=0, label="Start Time (seconds)")
106
  end_time = gr.Slider(1, 20, value=20, label="End Time (seconds)")
107
-
108
- process_button = gr.Button("Create Ringtones", elem_classes="light-btn")
109
-
110
  with gr.Row():
111
- with gr.Column(scale=1, min_width=250):
112
- gr.HTML('<label>Android Ringtone</label>')
113
- mp3_download = gr.File(label="Android", interactive=False)
114
- gr.Textbox(label="Install", value="Copy the MP3 file to your ringtones folder", lines=1, interactive=False)
115
-
116
- with gr.Column(scale=1, min_width=250):
117
- gr.HTML('<label>iPhone Ringtone</label>')
118
- iphone_ringtone = gr.File(label="Apple", interactive=False)
119
- gr.Textbox(label="Install", value="Open GarageBand on your iPhone, import the .m4r file, and set it as a ringtone.", lines=1, interactive=False)
120
-
121
- process_button.click(process_youtube_or_audio, inputs=[youtube_url, audio_record, start_time, end_time], outputs=[mp3_download, iphone_ringtone])
122
 
123
  interface.launch(share=True)
124
 
 
5
  import subprocess
6
  from pydub import AudioSegment
7
 
 
8
  os.makedirs("downloads", exist_ok=True)
9
 
10
  def sanitize_filename(filename):
11
  """Sanitize filenames by replacing special characters."""
12
  return re.sub(r'[^a-zA-Z0-9_-]', '_', filename)
13
 
14
+ def process_youtube_or_audio(url, uploaded_audio, start_time, end_time):
15
  """Downloads or processes audio, trims it, and exports ringtones."""
16
  try:
17
  filename, song_name = None, None
18
 
 
19
  if url:
20
  ydl_opts = {
21
  'format': 'bestaudio/best',
 
26
  info = ydl.extract_info(url, download=True)
27
  filename = ydl.prepare_filename(info)
28
  song_name = sanitize_filename(info['title'])
29
+
30
+ elif uploaded_audio is not None:
31
+ filename = uploaded_audio
32
+ song_name = sanitize_filename(os.path.splitext(os.path.basename(uploaded_audio))[0])
33
+
 
 
34
  if not filename or not os.path.exists(filename):
35
  return None, None
36
 
 
37
  audio = AudioSegment.from_file(filename)
38
  start_ms, end_ms = int(start_time * 1000), int(end_time * 1000)
 
 
39
  start_ms = max(0, min(start_ms, len(audio)))
40
  end_ms = max(start_ms, min(end_ms, len(audio)))
41
 
42
  trimmed_audio = audio[start_ms:end_ms]
43
 
 
44
  mp3_filename = f"downloads/{song_name}.mp3"
45
  trimmed_audio.export(mp3_filename, format="mp3")
46
 
 
47
  m4a_filename = f"downloads/{song_name}.m4a"
48
  result = subprocess.run([
49
  'ffmpeg', '-y', '-i', mp3_filename, '-vn', '-acodec', 'aac', '-b:a', '192k', m4a_filename
 
51
 
52
  if result.returncode != 0 or not os.path.exists(m4a_filename):
53
  print("FFmpeg Error:", result.stderr)
54
+ return mp3_filename, None
55
 
 
56
  m4r_filename = f"downloads/{song_name}.m4r"
57
  os.rename(m4a_filename, m4r_filename)
58
 
 
62
  print(f"Error: {e}")
63
  return None, None
64
 
65
+ with gr.Blocks() as interface:
 
 
 
 
 
 
66
  gr.HTML("""
 
67
  <h1><i class="fas fa-music"></i>&nbsp;PYTR - Python YouTube Ringtones</h1>
68
+ <p>Enter a YouTube URL or upload an audio file to create ringtones.</p>
 
 
 
 
 
69
  """)
70
+
71
  with gr.Row():
72
+ youtube_url = gr.Textbox(placeholder="Enter the URL here...", label="YouTube URL")
73
+ uploaded_audio = gr.File(label="Upload Audio File", type="file")
74
+
 
 
 
 
 
75
  gr.HTML("<h3>Trim Audio</h3>")
 
76
  with gr.Row():
77
  start_time = gr.Slider(0, 20, value=0, label="Start Time (seconds)")
78
  end_time = gr.Slider(1, 20, value=20, label="End Time (seconds)")
79
+
80
+ process_button = gr.Button("Create Ringtones")
81
+
82
  with gr.Row():
83
+ mp3_download = gr.File(label="Android Ringtone")
84
+ iphone_ringtone = gr.File(label="iPhone Ringtone")
85
+
86
+ process_button.click(process_youtube_or_audio, inputs=[youtube_url, uploaded_audio, start_time, end_time], outputs=[mp3_download, iphone_ringtone])
 
 
 
 
 
 
 
87
 
88
  interface.launch(share=True)
89