Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,36 +2,14 @@ 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 only output formats
|
| 25 |
-
return 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,
|
| 33 |
input_extension = get_file_extension(file)
|
| 34 |
-
output_file = file.replace(input_extension, f'.{
|
| 35 |
|
| 36 |
# FFmpeg command to convert the audio file
|
| 37 |
command = f'ffmpeg -i "{file}" "{output_file}"'
|
|
@@ -40,30 +18,21 @@ def convert_audio(file, output_format):
|
|
| 40 |
return output_file
|
| 41 |
|
| 42 |
# Gradio Interface
|
| 43 |
-
def interface(file,
|
| 44 |
-
output_file = convert_audio(file,
|
| 45 |
return output_file
|
| 46 |
|
| 47 |
-
# Function to reload output formats
|
| 48 |
-
def reload_formats():
|
| 49 |
-
return gr.Dropdown.update(choices=get_supported_formats())
|
| 50 |
-
|
| 51 |
-
# Initial supported formats
|
| 52 |
-
output_formats = get_supported_formats()
|
| 53 |
-
|
| 54 |
# Building the Gradio interface
|
| 55 |
with gr.Blocks() as app:
|
| 56 |
-
gr.Markdown("## Audio Converter using FFmpeg
|
| 57 |
with gr.Row():
|
| 58 |
input_file = gr.File(label="Upload Audio File", type="filepath")
|
| 59 |
-
|
| 60 |
output_file = gr.File(label="Converted Audio File")
|
| 61 |
|
| 62 |
convert_button = gr.Button("Convert")
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
convert_button.click(fn=interface, inputs=[input_file, output_format], outputs=output_file)
|
| 66 |
-
reload_button.click(fn=reload_formats, inputs=None, outputs=output_format)
|
| 67 |
|
| 68 |
# Launch the app
|
| 69 |
app.launch()
|
|
|
|
| 2 |
import os
|
| 3 |
import subprocess
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
# Function to get the file extension
|
| 6 |
def get_file_extension(file_path):
|
| 7 |
return os.path.splitext(file_path)[1].lower()
|
| 8 |
|
| 9 |
# Function to convert the audio file using ffmpeg
|
| 10 |
+
def convert_audio(file, output_extension):
|
| 11 |
input_extension = get_file_extension(file)
|
| 12 |
+
output_file = file.replace(input_extension, f'.{output_extension}')
|
| 13 |
|
| 14 |
# FFmpeg command to convert the audio file
|
| 15 |
command = f'ffmpeg -i "{file}" "{output_file}"'
|
|
|
|
| 18 |
return output_file
|
| 19 |
|
| 20 |
# Gradio Interface
|
| 21 |
+
def interface(file, output_extension):
|
| 22 |
+
output_file = convert_audio(file, output_extension)
|
| 23 |
return output_file
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
# Building the Gradio interface
|
| 26 |
with gr.Blocks() as app:
|
| 27 |
+
gr.Markdown("## Audio Converter using FFmpeg")
|
| 28 |
with gr.Row():
|
| 29 |
input_file = gr.File(label="Upload Audio File", type="filepath")
|
| 30 |
+
output_extension = gr.Textbox(label="Enter Output File Extension (e.g., mp3, wav, m4a, etc.)")
|
| 31 |
output_file = gr.File(label="Converted Audio File")
|
| 32 |
|
| 33 |
convert_button = gr.Button("Convert")
|
| 34 |
+
|
| 35 |
+
convert_button.click(fn=interface, inputs=[input_file, output_extension], outputs=output_file)
|
|
|
|
|
|
|
| 36 |
|
| 37 |
# Launch the app
|
| 38 |
app.launch()
|