|
|
import gradio as gr |
|
|
import ffmpeg |
|
|
import os |
|
|
import tempfile |
|
|
import re |
|
|
|
|
|
gr.themes.Soft() |
|
|
|
|
|
|
|
|
audio_formats = sorted(['MP3', 'WAV', 'AAC', 'FLAC', 'OGG', 'M4A', 'ALAC', 'WMA', 'AIFF', 'OPUS', 'APE', 'CAF', 'PCM', 'DTS', 'TTA', 'AMR', 'MID', 'SPX', 'WV', 'RA', 'TAK']) |
|
|
|
|
|
def convert_audio(audio_file, target_format): |
|
|
try: |
|
|
|
|
|
ffmpeg.probe(audio_file) |
|
|
|
|
|
|
|
|
temp_dir = tempfile.mkdtemp() |
|
|
|
|
|
|
|
|
file_name = os.path.basename(audio_file) |
|
|
audio_path = os.path.join(temp_dir, file_name) |
|
|
base_name = os.path.splitext(file_name)[0] |
|
|
|
|
|
|
|
|
with open(audio_path, "wb") as f: |
|
|
with open(audio_file, 'rb') as input_file: |
|
|
f.write(input_file.read()) |
|
|
|
|
|
|
|
|
output_file = f"flowly_ai_audio_converter_{base_name}.{target_format.lower()}" |
|
|
ffmpeg.input(audio_path).output(output_file).run() |
|
|
|
|
|
return output_file |
|
|
except ffmpeg.Error as e: |
|
|
|
|
|
return f"FFMPEG Error: {e.stderr.decode()}" |
|
|
except Exception as e: |
|
|
|
|
|
return f"Error: {e}" |
|
|
|
|
|
|
|
|
interface = gr.Interface( |
|
|
fn=convert_audio, |
|
|
inputs=[ |
|
|
gr.File(label="Upload Audio File", type="filepath", file_types=[f'.{format.lower()}' for format in audio_formats]), |
|
|
gr.Dropdown(label="Select Target Format", choices=audio_formats) |
|
|
], |
|
|
outputs=gr.File(label="Converted Audio File"), |
|
|
title="Audio Format Converter", |
|
|
description="Upload audio and choose a target format.", |
|
|
css="footer {visibility: hidden}" |
|
|
) |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
interface.launch() |
|
|
|