Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import ffmpeg
|
| 3 |
+
import tempfile
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
def convert_to_mp3(file):
|
| 7 |
+
input_file = file.name
|
| 8 |
+
# Create a temporary directory to store the output file
|
| 9 |
+
with tempfile.TemporaryDirectory() as tmpdir:
|
| 10 |
+
output_file = os.path.join(tmpdir, "output.mp3")
|
| 11 |
+
|
| 12 |
+
# Use ffmpeg to extract audio and convert it to mp3
|
| 13 |
+
try:
|
| 14 |
+
ffmpeg.input(input_file).output(output_file).run(overwrite_output=True)
|
| 15 |
+
except FileNotFoundError as e:
|
| 16 |
+
return str(e)
|
| 17 |
+
|
| 18 |
+
# Return the path to the output file
|
| 19 |
+
return output_file
|
| 20 |
+
|
| 21 |
+
app = gr.Interface(
|
| 22 |
+
fn=convert_to_mp3,
|
| 23 |
+
inputs=gr.File(label="Upload Audio/Video File"),
|
| 24 |
+
outputs=gr.File(label="Download MP3 File"),
|
| 25 |
+
title="Convert Audio/Video to MP3",
|
| 26 |
+
description="Upload an audio or video file and convert its audio to MP3 format."
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
app.launch()
|