Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +43 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from pydub import AudioSegment
|
3 |
+
|
4 |
+
def compress_audio(input_audio_path, compression_rate=128):
|
5 |
+
"""
|
6 |
+
Compress an audio file.
|
7 |
+
|
8 |
+
Args:
|
9 |
+
input_audio_path: Path to the input audio file.
|
10 |
+
compression_rate: Desired bitrate for compression (in kbps).
|
11 |
+
|
12 |
+
Returns:
|
13 |
+
Compressed audio file path.
|
14 |
+
"""
|
15 |
+
try:
|
16 |
+
# Load the audio file using pydub
|
17 |
+
audio = AudioSegment.from_file(input_audio_path)
|
18 |
+
|
19 |
+
# Temporary output path
|
20 |
+
output_path = "compressed_audio.mp3"
|
21 |
+
|
22 |
+
# Export the compressed audio
|
23 |
+
audio.export(output_path, format="mp3", bitrate=f"{compression_rate}k")
|
24 |
+
|
25 |
+
return output_path
|
26 |
+
except Exception as e:
|
27 |
+
return str(e)
|
28 |
+
|
29 |
+
# Create the Gradio app
|
30 |
+
interface = gr.Interface(
|
31 |
+
fn=compress_audio,
|
32 |
+
inputs=[
|
33 |
+
gr.Audio(label="Upload Audio File", type="filepath"),
|
34 |
+
gr.Slider(32, 320, step=16, label="Compression Rate (kbps)", value=128),
|
35 |
+
],
|
36 |
+
outputs=gr.File(label="Download Compressed Audio"),
|
37 |
+
title="Audio Compressor",
|
38 |
+
description="Upload an audio file and select the compression rate to reduce the file size.",
|
39 |
+
allow_flagging="never",
|
40 |
+
)
|
41 |
+
|
42 |
+
# Launch the app
|
43 |
+
interface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
pydub
|