mp3towav / app.py
MK-316's picture
Update app.py
1c8cb08 verified
import gradio as gr
from pydub import AudioSegment
import io
import tempfile
import os
def convert_to_wav(file_path):
# Extract the original filename without its extension
original_filename = os.path.splitext(os.path.basename(file_path))[0]
# Create a new filename with a .wav extension
new_filename = f"{original_filename}.wav"
# Define the full path for the new file in the temporary directory
temp_dir = tempfile.gettempdir()
new_file_path = os.path.join(temp_dir, new_filename)
sound = AudioSegment.from_mp3(file_path)
# Export the converted sound to the new file path
sound.export(new_file_path, format="wav")
return new_file_path
app = gr.Interface(
fn=convert_to_wav,
inputs=gr.Audio(type="filepath", label="Upload MP3 File"),
outputs=gr.File(label="Download WAV File", type="filepath"),
title="MP3 to WAV Converter",
description="Upload an MP3 file and convert it to WAV format. The downloaded file will retain the original file name with a .wav extension."
)
if __name__ == "__main__":
app.launch()