File size: 1,541 Bytes
e2d0bc7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

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()