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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -11
app.py CHANGED
@@ -5,7 +5,6 @@ from pydub import AudioSegment
5
  import re
6
  import subprocess
7
 
8
-
9
  if not os.path.exists("downloads"):
10
  os.makedirs("downloads")
11
 
@@ -18,8 +17,13 @@ def process_youtube_or_audio(url, uploaded_audio, start_time, end_time):
18
  filename = None
19
  song_name = None
20
 
21
- #URL
 
 
 
 
22
  if url:
 
23
  ydl_opts = {
24
  'format': 'bestaudio/best',
25
  'outtmpl': 'downloads/%(id)s.%(ext)s',
@@ -30,15 +34,17 @@ def process_youtube_or_audio(url, uploaded_audio, start_time, end_time):
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
@@ -47,24 +53,26 @@ def process_youtube_or_audio(url, uploaded_audio, start_time, end_time):
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
 
 
 
64
  return os.path.abspath(mp3_filename), os.path.abspath(m4r_filename)
65
 
66
  except Exception as e:
67
- print(f"Error: {e}")
68
  return None, None
69
 
70
 
@@ -125,5 +133,3 @@ with gr.Blocks(css="""
125
  process_button.click(process_youtube_or_audio, inputs=[youtube_url, audio_upload, start_time, end_time], outputs=[mp3_download, iphone_ringtone])
126
 
127
  interface.launch(share=True)
128
-
129
-
 
5
  import re
6
  import subprocess
7
 
 
8
  if not os.path.exists("downloads"):
9
  os.makedirs("downloads")
10
 
 
17
  filename = None
18
  song_name = None
19
 
20
+ print(f"URL: {url}") # Debug: Check URL input
21
+ print(f"Uploaded audio: {uploaded_audio}") # Debug: Check if audio is uploaded
22
+ print(f"Start time: {start_time}, End time: {end_time}") # Debug: Check times
23
+
24
+ # Process YouTube URL
25
  if url:
26
+ print("Processing YouTube URL...")
27
  ydl_opts = {
28
  'format': 'bestaudio/best',
29
  'outtmpl': 'downloads/%(id)s.%(ext)s',
 
34
  filename = os.path.join('downloads', f"{info['id']}.webm")
35
  song_name = sanitize_filename(info['title'])
36
 
37
+ # Process uploaded audio file
38
  elif uploaded_audio:
39
+ print("Processing uploaded audio...")
40
+ filename = uploaded_audio.name
41
  song_name = "uploaded_audio"
42
 
43
  if not filename or not os.path.exists(filename):
44
+ print(f"Error: No valid file found at {filename}") # Debug
45
  return None, None
46
 
47
+ # Load audio and trim it
48
  audio = AudioSegment.from_file(filename)
49
  start_time_ms = start_time * 1000
50
  end_time_ms = end_time * 1000
 
53
 
54
  trimmed_audio = audio[start_time_ms:end_time_ms]
55
 
56
+ # Export trimmed audio
57
  mp3_filename = f"downloads/{song_name}.mp3"
58
  trimmed_audio.export(mp3_filename, format="mp3")
59
 
60
+ # Convert to m4r format for iPhone
61
  m4a_filename = f"downloads/{song_name}.m4a"
62
  subprocess.run([
63
  'ffmpeg', '-i', mp3_filename, '-vn', '-acodec', 'aac', '-b:a', '192k', m4a_filename
64
  ], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
65
 
66
+ # Rename to m4r format for iPhone ringtones
67
  m4r_filename = f"downloads/{song_name}.m4r"
68
  os.rename(m4a_filename, m4r_filename)
69
 
70
+ print(f"Files saved: {mp3_filename}, {m4r_filename}") # Debug
71
+
72
  return os.path.abspath(mp3_filename), os.path.abspath(m4r_filename)
73
 
74
  except Exception as e:
75
+ print(f"Error: {e}") # Debug: Print the error
76
  return None, None
77
 
78
 
 
133
  process_button.click(process_youtube_or_audio, inputs=[youtube_url, audio_upload, start_time, end_time], outputs=[mp3_download, iphone_ringtone])
134
 
135
  interface.launch(share=True)