sudo-soldier commited on
Commit
dd81a22
·
verified ·
1 Parent(s): a58d3c7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -7
app.py CHANGED
@@ -3,6 +3,7 @@ import gradio as gr
3
  import yt_dlp
4
  from pydub import AudioSegment
5
  import re
 
6
 
7
  # Create downloads directory safely
8
  if not os.path.exists("downloads"):
@@ -41,14 +42,18 @@ def process_youtube_or_audio(url, recorded_audio):
41
  audio = AudioSegment.from_file(filename)
42
  audio.export(mp3_filename, format="mp3")
43
 
44
- ringtone_filename_m4r = f"downloads/{song_name}.m4r"
45
- if not os.path.exists(ringtone_filename_m4r):
46
- ringtone_audio = AudioSegment.from_file(mp3_filename)
47
- # Trimming to 20 seconds (20000 milliseconds)
48
- ringtone_audio = ringtone_audio[:20000] # 20 seconds
49
- ringtone_audio.export(ringtone_filename_m4r, format="aac") # Export as AAC for M4R
50
 
51
- return mp3_filename, ringtone_filename_m4r
 
 
 
 
 
 
52
 
53
  except Exception as e:
54
  print(f"Error: {e}")
@@ -92,3 +97,4 @@ with gr.Blocks(css="""
92
  interface.launch(share=True)
93
 
94
 
 
 
3
  import yt_dlp
4
  from pydub import AudioSegment
5
  import re
6
+ import subprocess
7
 
8
  # Create downloads directory safely
9
  if not os.path.exists("downloads"):
 
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
57
 
58
  except Exception as e:
59
  print(f"Error: {e}")
 
97
  interface.launch(share=True)
98
 
99
 
100
+