sudo-soldier commited on
Commit
581993c
·
verified ·
1 Parent(s): 8f7f666

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -17
app.py CHANGED
@@ -13,12 +13,12 @@ def sanitize_filename(filename):
13
  """Sanitize filenames to avoid special characters."""
14
  return re.sub(r'[^a-zA-Z0-9_-]', '_', filename)
15
 
16
- def process_youtube_or_audio(url, recorded_audio, start_time, end_time):
17
  try:
18
  filename = None
19
  song_name = None
20
 
21
- # If URL is provided, download the YouTube audio
22
  if url:
23
  ydl_opts = {
24
  'format': 'bestaudio/best',
@@ -30,34 +30,34 @@ def process_youtube_or_audio(url, recorded_audio, start_time, end_time):
30
  filename = os.path.join('downloads', f"{info['id']}.webm")
31
  song_name = sanitize_filename(info['title'])
32
 
33
- # If an audio file is uploaded, use that instead
34
- elif recorded_audio:
35
- filename = recorded_audio.name # Use the uploaded file's name
36
- song_name = "recorded_audio"
37
 
38
  if not filename or not os.path.exists(filename):
39
  return None, None
40
 
41
- # Process the audio file (trim according to start and end time)
42
  audio = AudioSegment.from_file(filename)
43
-
44
  start_time_ms = start_time * 1000
45
  end_time_ms = end_time * 1000
46
-
47
  start_time_ms = max(0, min(start_time_ms, len(audio)))
48
  end_time_ms = max(start_time_ms, min(end_time_ms, len(audio)))
49
 
50
  trimmed_audio = audio[start_time_ms:end_time_ms]
51
 
52
- # Export the audio to MP3 and M4R formats
53
  mp3_filename = f"downloads/{song_name}.mp3"
54
  trimmed_audio.export(mp3_filename, format="mp3")
55
 
 
56
  m4a_filename = f"downloads/{song_name}.m4a"
57
  subprocess.run([
58
  'ffmpeg', '-i', mp3_filename, '-vn', '-acodec', 'aac', '-b:a', '192k', m4a_filename
59
  ], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
60
 
 
61
  m4r_filename = f"downloads/{song_name}.m4r"
62
  os.rename(m4a_filename, m4r_filename)
63
 
@@ -68,7 +68,6 @@ def process_youtube_or_audio(url, recorded_audio, start_time, end_time):
68
  return None, None
69
 
70
 
71
- # Gradio Interface
72
  with gr.Blocks(css="""
73
  body { font-family: Arial, sans-serif; text-align: center; }
74
  .light-btn {
@@ -85,7 +84,7 @@ with gr.Blocks(css="""
85
  gr.HTML("""
86
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
87
  <h1><i class="fas fa-music"></i>&nbsp;PYTR</h1>
88
- <p>Python YouTube Ringtones. Enter a YouTube URL or record audio to create ringtones.</p>
89
  <p>
90
  <a href="https://ringtones.JesseJesse.xyz" target="_blank">Ringtones</a>&nbsp;&nbsp;&nbsp;
91
  <a href="https://pub-c1de1cb456e74d6bbbee111ba9e6c757.r2.dev/iphone.png" target="_blank">iPhone xfers</a>&nbsp;&nbsp;&nbsp;
@@ -99,8 +98,8 @@ with gr.Blocks(css="""
99
  youtube_url = gr.Textbox(placeholder="Enter the URL here...", show_label=False)
100
 
101
  with gr.Column(scale=1, min_width=250):
102
- gr.HTML('<label><i class="fas fa-file-audio"></i>&nbsp;Upload Audio</label>')
103
- audio_upload = gr.File(label="Upload Audio", type="file", show_label=False)
104
 
105
  with gr.Row():
106
  gr.HTML("<h3>Trim Audio (Optional)</h3>")
@@ -128,6 +127,3 @@ with gr.Blocks(css="""
128
  interface.launch(share=True)
129
 
130
 
131
-
132
-
133
-
 
13
  """Sanitize filenames to avoid special characters."""
14
  return re.sub(r'[^a-zA-Z0-9_-]', '_', filename)
15
 
16
+ def process_youtube_or_audio(url, uploaded_audio, start_time, end_time):
17
  try:
18
  filename = None
19
  song_name = None
20
 
21
+ #URL
22
  if url:
23
  ydl_opts = {
24
  'format': 'bestaudio/best',
 
30
  filename = os.path.join('downloads', f"{info['id']}.webm")
31
  song_name = sanitize_filename(info['title'])
32
 
33
+ #audio file
34
+ elif uploaded_audio:
35
+ filename = uploaded_audio
36
+ song_name = "uploaded_audio"
37
 
38
  if not filename or not os.path.exists(filename):
39
  return None, None
40
 
41
+ #trim
42
  audio = AudioSegment.from_file(filename)
 
43
  start_time_ms = start_time * 1000
44
  end_time_ms = end_time * 1000
 
45
  start_time_ms = max(0, min(start_time_ms, len(audio)))
46
  end_time_ms = max(start_time_ms, min(end_time_ms, len(audio)))
47
 
48
  trimmed_audio = audio[start_time_ms:end_time_ms]
49
 
50
+ #Export
51
  mp3_filename = f"downloads/{song_name}.mp3"
52
  trimmed_audio.export(mp3_filename, format="mp3")
53
 
54
+ #m4r
55
  m4a_filename = f"downloads/{song_name}.m4a"
56
  subprocess.run([
57
  'ffmpeg', '-i', mp3_filename, '-vn', '-acodec', 'aac', '-b:a', '192k', m4a_filename
58
  ], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
59
 
60
+
61
  m4r_filename = f"downloads/{song_name}.m4r"
62
  os.rename(m4a_filename, m4r_filename)
63
 
 
68
  return None, None
69
 
70
 
 
71
  with gr.Blocks(css="""
72
  body { font-family: Arial, sans-serif; text-align: center; }
73
  .light-btn {
 
84
  gr.HTML("""
85
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
86
  <h1><i class="fas fa-music"></i>&nbsp;PYTR</h1>
87
+ <p>Python YouTube Ringtones. Enter a YouTube URL or upload audio to create ringtones.</p>
88
  <p>
89
  <a href="https://ringtones.JesseJesse.xyz" target="_blank">Ringtones</a>&nbsp;&nbsp;&nbsp;
90
  <a href="https://pub-c1de1cb456e74d6bbbee111ba9e6c757.r2.dev/iphone.png" target="_blank">iPhone xfers</a>&nbsp;&nbsp;&nbsp;
 
98
  youtube_url = gr.Textbox(placeholder="Enter the URL here...", show_label=False)
99
 
100
  with gr.Column(scale=1, min_width=250):
101
+ gr.HTML('<label><i class="fas fa-upload"></i>&nbsp;Upload Audio</label>')
102
+ audio_upload = gr.File(label="Upload Audio", type="filepath", show_label=False)
103
 
104
  with gr.Row():
105
  gr.HTML("<h3>Trim Audio (Optional)</h3>")
 
127
  interface.launch(share=True)
128
 
129