MK-316 commited on
Commit
1c8cb08
·
verified ·
1 Parent(s): 9ee473d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -5
app.py CHANGED
@@ -5,18 +5,25 @@ import tempfile
5
  import os
6
 
7
  def convert_to_wav(file_path):
 
 
 
 
 
 
 
 
8
  sound = AudioSegment.from_mp3(file_path)
9
- # Create a temporary file to save the WAV file
10
- temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.wav')
11
- sound.export(temp_file.name, format="wav")
12
- return temp_file.name
13
 
14
  app = gr.Interface(
15
  fn=convert_to_wav,
16
  inputs=gr.Audio(type="filepath", label="Upload MP3 File"),
17
  outputs=gr.File(label="Download WAV File", type="filepath"),
18
  title="MP3 to WAV Converter",
19
- description="Upload an MP3 file and convert it to WAV format."
20
  )
21
 
22
  if __name__ == "__main__":
 
5
  import os
6
 
7
  def convert_to_wav(file_path):
8
+ # Extract the original filename without its extension
9
+ original_filename = os.path.splitext(os.path.basename(file_path))[0]
10
+ # Create a new filename with a .wav extension
11
+ new_filename = f"{original_filename}.wav"
12
+ # Define the full path for the new file in the temporary directory
13
+ temp_dir = tempfile.gettempdir()
14
+ new_file_path = os.path.join(temp_dir, new_filename)
15
+
16
  sound = AudioSegment.from_mp3(file_path)
17
+ # Export the converted sound to the new file path
18
+ sound.export(new_file_path, format="wav")
19
+ return new_file_path
 
20
 
21
  app = gr.Interface(
22
  fn=convert_to_wav,
23
  inputs=gr.Audio(type="filepath", label="Upload MP3 File"),
24
  outputs=gr.File(label="Download WAV File", type="filepath"),
25
  title="MP3 to WAV Converter",
26
+ description="Upload an MP3 file and convert it to WAV format. The downloaded file will retain the original file name with a .wav extension."
27
  )
28
 
29
  if __name__ == "__main__":