Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -9,11 +9,6 @@ gr.themes.Soft()
|
|
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 sanitize_filename(filename):
|
13 |
-
"""Sanitasi nama file dengan mengganti karakter khusus dan spasi."""
|
14 |
-
filename = re.sub(r'[^a-zA-Z0-9_.-]', '_', filename) # Ganti karakter yang tidak valid dengan '_'
|
15 |
-
return filename
|
16 |
-
|
17 |
def convert_audio(audio_file, target_format):
|
18 |
try:
|
19 |
# Pastikan ffmpeg sudah terinstal
|
@@ -23,30 +18,30 @@ def convert_audio(audio_file, target_format):
|
|
23 |
temp_dir = tempfile.mkdtemp()
|
24 |
|
25 |
# Sanitasi nama file dan simpan audio yang diunggah ke direktori sementara
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
|
30 |
with open(audio_path, "wb") as f:
|
31 |
f.write(audio_file.read()) # Menyimpan file audio yang diunggah
|
32 |
|
33 |
-
|
|
|
34 |
ffmpeg.input(audio_path).output(output_file).run() # Mengonversi audio ke format yang diinginkan
|
35 |
|
36 |
return output_file
|
37 |
except ffmpeg.Error as e:
|
38 |
# Tangkap kesalahan ffmpeg dan tampilkan pesan error lebih rinci
|
39 |
-
|
40 |
except Exception as e:
|
41 |
# Tangkap kesalahan lain dan tampilkan pesan error lebih rinci
|
42 |
-
|
43 |
-
return null
|
44 |
|
45 |
# Antarmuka Gradio dengan tema kustom untuk menyembunyikan footer
|
46 |
interface = gr.Interface(
|
47 |
fn=convert_audio,
|
48 |
inputs=[
|
49 |
-
gr.File(label="Upload Audio File", type="filepath", file_types=[f'.{format}' for format in audio_formats]),
|
50 |
gr.Dropdown(label="Select Target Format", choices=audio_formats)
|
51 |
],
|
52 |
outputs=gr.File(label="Converted Audio File"),
|
|
|
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
|
|
|
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.name)
|
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 |
with open(audio_path, "wb") as f:
|
26 |
f.write(audio_file.read()) # Menyimpan file audio yang diunggah
|
27 |
|
28 |
+
# Tentukan output file berdasarkan format target
|
29 |
+
output_file = f"{base_name}_converted.{target_format.lower()}"
|
30 |
ffmpeg.input(audio_path).output(output_file).run() # Mengonversi audio ke format yang diinginkan
|
31 |
|
32 |
return output_file
|
33 |
except ffmpeg.Error as e:
|
34 |
# Tangkap kesalahan ffmpeg dan tampilkan pesan error lebih rinci
|
35 |
+
return f"FFMPEG Error: {e.stderr.decode()}"
|
36 |
except Exception as e:
|
37 |
# Tangkap kesalahan lain dan tampilkan pesan error lebih rinci
|
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="filepath", file_types=[f'.{format.lower()}' for format in audio_formats]),
|
45 |
gr.Dropdown(label="Select Target Format", choices=audio_formats)
|
46 |
],
|
47 |
outputs=gr.File(label="Converted Audio File"),
|