Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import subprocess
|
| 4 |
+
|
| 5 |
+
# Function to get supported audio formats using ffmpeg
|
| 6 |
+
def get_supported_formats():
|
| 7 |
+
result = subprocess.run(['ffmpeg', '-formats'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
| 8 |
+
lines = result.stdout.splitlines()
|
| 9 |
+
|
| 10 |
+
# Parse the output and collect format codes for input (Demuxing) and output (Muxing)
|
| 11 |
+
input_formats = []
|
| 12 |
+
output_formats = []
|
| 13 |
+
|
| 14 |
+
for line in lines:
|
| 15 |
+
if line.startswith(" D") or line.startswith(".E") or line.startswith("DE"): # D = Demux (Input), E = Mux (Output)
|
| 16 |
+
parts = line.split()
|
| 17 |
+
if len(parts) > 1:
|
| 18 |
+
format_code = parts[1]
|
| 19 |
+
if line.startswith(" D"): # Input format
|
| 20 |
+
input_formats.append(format_code)
|
| 21 |
+
if line.startswith(".E") or line.startswith("DE"): # Output format
|
| 22 |
+
output_formats.append(format_code)
|
| 23 |
+
|
| 24 |
+
# Return a dictionary of input and output formats
|
| 25 |
+
return input_formats, output_formats
|
| 26 |
+
|
| 27 |
+
# Function to get the file extension
|
| 28 |
+
def get_file_extension(file_path):
|
| 29 |
+
return os.path.splitext(file_path)[1].lower()
|
| 30 |
+
|
| 31 |
+
# Function to convert the audio file using ffmpeg
|
| 32 |
+
def convert_audio(file, output_format):
|
| 33 |
+
input_extension = get_file_extension(file.name)
|
| 34 |
+
output_file = file.name.replace(input_extension, f'.{output_format}')
|
| 35 |
+
|
| 36 |
+
# FFmpeg command to convert the audio file
|
| 37 |
+
command = f'ffmpeg -i "{file.name}" "{output_file}"'
|
| 38 |
+
subprocess.run(command, shell=True)
|
| 39 |
+
|
| 40 |
+
return output_file
|
| 41 |
+
|
| 42 |
+
# Gradio Interface
|
| 43 |
+
def interface(file, output_format):
|
| 44 |
+
output_file = convert_audio(file, output_format)
|
| 45 |
+
return output_file
|
| 46 |
+
|
| 47 |
+
# Dynamically generate the list of supported formats
|
| 48 |
+
input_formats, output_formats = get_supported_formats()
|
| 49 |
+
|
| 50 |
+
# Building the Gradio interface
|
| 51 |
+
with gr.Blocks() as app:
|
| 52 |
+
gr.Markdown("## Audio Converter using FFmpeg - Full Format Support")
|
| 53 |
+
with gr.Row():
|
| 54 |
+
input_file = gr.File(label="Upload Audio File", type="file")
|
| 55 |
+
output_format = gr.Dropdown(choices=output_formats, label="Select Output Format")
|
| 56 |
+
output_file = gr.File(label="Converted Audio File")
|
| 57 |
+
|
| 58 |
+
convert_button = gr.Button("Convert")
|
| 59 |
+
|
| 60 |
+
convert_button.click(fn=interface, inputs=[input_file, output_format], outputs=output_file)
|
| 61 |
+
|
| 62 |
+
# Launch the app
|
| 63 |
+
app.launch()
|