Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 = [
|
11 |
+
'mp3', 'wav', 'aac', 'flac', 'ogg', 'm4a', 'alac', 'wma', 'aiff', 'opus',
|
12 |
+
'ape', 'caf', 'pcm', 'dts', 'tta', 'amr', 'mid', 'spx', 'wv', 'ra', 'tak'
|
13 |
+
]
|
14 |
+
|
15 |
+
def sanitize_filename(filename):
|
16 |
+
"""Sanitasi nama file dengan mengganti karakter khusus dan spasi."""
|
17 |
+
filename = re.sub(r'[^a-zA-Z0-9_.-]', '_', filename) # Ganti karakter yang tidak valid dengan '_'
|
18 |
+
return filename
|
19 |
+
|
20 |
+
def convert_audio(audio_file, target_format):
|
21 |
+
try:
|
22 |
+
# Membuat direktori sementara untuk file yang diunggah
|
23 |
+
temp_dir = tempfile.mkdtemp()
|
24 |
+
|
25 |
+
# Sanitasi nama file dan simpan audio yang diunggah ke direktori sementara
|
26 |
+
base_name = os.path.splitext(os.path.basename(audio_file.name))[0]
|
27 |
+
sanitized_base_name = sanitize_filename(base_name)
|
28 |
+
audio_path = os.path.join(temp_dir, f"{sanitized_base_name}.mp3") # Menyimpan sebagai mp3 secara default
|
29 |
+
|
30 |
+
with open(audio_path, "wb") as f:
|
31 |
+
f.write(audio_file.read()) # Menyimpan file audio yang diunggah
|
32 |
+
|
33 |
+
output_file = f"{sanitized_base_name}_converted.{target_format.lower()}"
|
34 |
+
ffmpeg.input(audio_path).output(output_file).run() # Mengonversi audio ke format yang diinginkan
|
35 |
+
|
36 |
+
return output_file
|
37 |
+
except Exception as e:
|
38 |
+
return f"Error: {e}"
|
39 |
+
|
40 |
+
# Antarmuka Gradio dengan tema kustom untuk menyembunyikan footer
|
41 |
+
interface = gr.Interface(
|
42 |
+
fn=convert_audio,
|
43 |
+
inputs=[
|
44 |
+
gr.File(label="Upload Audio File", type="file", file_types=['.mp3', '.wav', '.aac', '.flac', '.ogg', '.m4a', '.alac', '.wma', '.aiff', '.opus']),
|
45 |
+
gr.Dropdown(label="Select Target Format", choices=audio_formats)
|
46 |
+
],
|
47 |
+
outputs=gr.File(label="Converted Audio File"),
|
48 |
+
title="Audio Format Converter",
|
49 |
+
description="Upload an audio file and select a target format for conversion. Supports multiple formats.",
|
50 |
+
css="footer {visibility: hidden}"
|
51 |
+
)
|
52 |
+
|
53 |
+
# Jalankan aplikasi
|
54 |
+
if __name__ == "__main__":
|
55 |
+
interface.launch()
|