import gradio as gr import tempfile import subprocess def convert_webm_to_mp3(webm_file): # Create a temporary file for the output .mp3 with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as tmp_mp3: tmp_mp3_path = tmp_mp3.name # Run ffmpeg command directly for conversion with error handling ffmpeg_command = [ "ffmpeg", "-y", # Overwrite output file if exists "-i", webm_file, # Input file "-vn", # No video "-acodec", "libmp3lame", # Use the mp3 codec "-q:a", "2", # Set quality to ensure good compression tmp_mp3_path # Output file path ] # Run the ffmpeg command and capture any errors process = subprocess.run(ffmpeg_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) # Check if there was an error during the conversion if process.returncode != 0: # Log or print the error for debugging print("Error in conversion:", process.stderr.decode()) return "Error in conversion. Please check the file format and try again." # Return the path to the .mp3 file if successful return tmp_mp3_path # Gradio interface iface = gr.Interface( fn=convert_webm_to_mp3, inputs=gr.File(type="filepath", label="Upload .webm file"), outputs=gr.File(label="Converted .mp3 file"), title="WebM to MP3 Converter", description="Upload a .webm audio file, and this tool will convert it to .mp3 format." ) # Launch the interface if __name__ == "__main__": iface.launch()