Upload 2 files
Browse files- app.py +44 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import tempfile
|
| 4 |
+
import subprocess
|
| 5 |
+
|
| 6 |
+
def convert_webm_to_mp3(webm_file):
|
| 7 |
+
# Create a temporary file for the output .mp3
|
| 8 |
+
with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as tmp_mp3:
|
| 9 |
+
tmp_mp3_path = tmp_mp3.name
|
| 10 |
+
|
| 11 |
+
# Run ffmpeg command directly for conversion with error handling
|
| 12 |
+
ffmpeg_command = [
|
| 13 |
+
"ffmpeg", "-y", # Overwrite output file if exists
|
| 14 |
+
"-i", webm_file, # Input file
|
| 15 |
+
"-vn", # No video
|
| 16 |
+
"-acodec", "libmp3lame", # Use the mp3 codec
|
| 17 |
+
"-q:a", "2", # Set quality to ensure good compression
|
| 18 |
+
tmp_mp3_path # Output file path
|
| 19 |
+
]
|
| 20 |
+
|
| 21 |
+
# Run the ffmpeg command and capture any errors
|
| 22 |
+
process = subprocess.run(ffmpeg_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
| 23 |
+
|
| 24 |
+
# Check if there was an error during the conversion
|
| 25 |
+
if process.returncode != 0:
|
| 26 |
+
# Log or print the error for debugging
|
| 27 |
+
print("Error in conversion:", process.stderr.decode())
|
| 28 |
+
return "Error in conversion. Please check the file format and try again."
|
| 29 |
+
|
| 30 |
+
# Return the path to the .mp3 file if successful
|
| 31 |
+
return tmp_mp3_path
|
| 32 |
+
|
| 33 |
+
# Gradio interface
|
| 34 |
+
iface = gr.Interface(
|
| 35 |
+
fn=convert_webm_to_mp3,
|
| 36 |
+
inputs=gr.File(type="filepath", label="Upload .webm file"),
|
| 37 |
+
outputs=gr.File(label="Converted .mp3 file"),
|
| 38 |
+
title="WebM to MP3 Converter",
|
| 39 |
+
description="Upload a .webm audio file, and this tool will convert it to .mp3 format."
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
# Launch the interface
|
| 43 |
+
if __name__ == "__main__":
|
| 44 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
gradio
|
| 3 |
+
pydub
|
| 4 |
+
ffmpeg-python
|