Upload 2 files
Browse files- app.py +57 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import ffmpeg
|
| 3 |
+
import os
|
| 4 |
+
import tempfile
|
| 5 |
+
import re
|
| 6 |
+
|
| 7 |
+
gr.themes.Soft()
|
| 8 |
+
|
| 9 |
+
# Daftar format audio yang didukung
|
| 10 |
+
audio_formats = sorted(['MP3', 'WAV', 'AAC', 'FLAC', 'OGG', 'M4A', 'ALAC', 'WMA', 'AIFF', 'OPUS', 'APE', 'CAF', 'PCM', 'DTS', 'TTA', 'AMR', 'MID', 'SPX', 'WV', 'RA', 'TAK'])
|
| 11 |
+
|
| 12 |
+
def convert_audio(audio_file, target_format):
|
| 13 |
+
try:
|
| 14 |
+
# Pastikan ffmpeg sudah terinstal
|
| 15 |
+
ffmpeg.probe(audio_file) # Menggunakan 'audio_file' yang berisi path file
|
| 16 |
+
|
| 17 |
+
# Membuat direktori sementara untuk file yang diunggah
|
| 18 |
+
temp_dir = tempfile.mkdtemp()
|
| 19 |
+
|
| 20 |
+
# Sanitasi nama file dan simpan audio yang diunggah ke direktori sementara
|
| 21 |
+
file_name = os.path.basename(audio_file)
|
| 22 |
+
audio_path = os.path.join(temp_dir, file_name) # Menyimpan sebagai mp3 secara default
|
| 23 |
+
base_name = os.path.splitext(file_name)[0]
|
| 24 |
+
|
| 25 |
+
# Menyimpan file audio yang diunggah ke direktori sementara
|
| 26 |
+
with open(audio_path, "wb") as f:
|
| 27 |
+
with open(audio_file, 'rb') as input_file:
|
| 28 |
+
f.write(input_file.read()) # Menyimpan file audio yang diunggah
|
| 29 |
+
|
| 30 |
+
# Tentukan output file berdasarkan format target
|
| 31 |
+
output_file = f"flowly_ai_audio_converter_{base_name}.{target_format.lower()}"
|
| 32 |
+
ffmpeg.input(audio_path).output(output_file).run() # Mengonversi audio ke format yang diinginkan
|
| 33 |
+
|
| 34 |
+
return output_file
|
| 35 |
+
except ffmpeg.Error as e:
|
| 36 |
+
# Tangkap kesalahan ffmpeg dan tampilkan pesan error lebih rinci
|
| 37 |
+
return f"FFMPEG Error: {e.stderr.decode()}"
|
| 38 |
+
except Exception as e:
|
| 39 |
+
# Tangkap kesalahan lain dan tampilkan pesan error lebih rinci
|
| 40 |
+
return f"Error: {e}"
|
| 41 |
+
|
| 42 |
+
# Antarmuka Gradio dengan tema kustom untuk menyembunyikan footer
|
| 43 |
+
interface = gr.Interface(
|
| 44 |
+
fn=convert_audio,
|
| 45 |
+
inputs=[
|
| 46 |
+
gr.File(label="Upload Audio File", type="filepath", file_types=[f'.{format.lower()}' for format in audio_formats]),
|
| 47 |
+
gr.Dropdown(label="Select Target Format", choices=audio_formats)
|
| 48 |
+
],
|
| 49 |
+
outputs=gr.File(label="Converted Audio File"),
|
| 50 |
+
title="Audio Format Converter",
|
| 51 |
+
description="Upload audio and choose a target format.",
|
| 52 |
+
css="footer {visibility: hidden}"
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
# Jalankan aplikasi
|
| 56 |
+
if __name__ == "__main__":
|
| 57 |
+
interface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
ffmpeg-python
|