File size: 1,767 Bytes
2c84fff
 
 
30d2e4e
2c84fff
 
 
 
 
 
30d2e4e
2c84fff
 
 
 
 
 
 
 
30d2e4e
 
 
2c84fff
 
30d2e4e
2c84fff
30d2e4e
2c84fff
 
 
323fa8a
 
 
 
 
 
 
 
 
 
 
 
 
 
2c84fff
 
 
323fa8a
2c84fff
30d2e4e
2c84fff
 
 
30d2e4e
323fa8a
2c84fff
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import gradio as gr
from pydub import AudioSegment

def compress_audio(input_audio_path, compression_rate=128, output_name="compressed_audio.mp3"):
    """
    Compress an audio file.
    
    Args:
        input_audio_path: Path to the input audio file.
        compression_rate: Desired bitrate for compression (in kbps).
        output_name: Desired name for the compressed file.
        
    Returns:
        Compressed audio file path.
    """
    try:
        # Load the audio file using pydub
        audio = AudioSegment.from_file(input_audio_path)
        
        # Ensure output name has the .mp3 extension
        if not output_name.endswith(".mp3"):
            output_name += ".mp3"
        
        # Export the compressed audio
        audio.export(output_name, format="mp3", bitrate=f"{compression_rate}k")
        
        return output_name
    except Exception as e:
        return str(e)

# Custom CSS for better mobile layout
custom_css = """
#interface-container {
    max-width: 100%;
    margin: auto;
    padding: 10px;
    font-family: Arial, sans-serif;
}
.audio {
    max-width: 100%;
}
"""

# Create the Gradio app with improved layout
interface = gr.Interface(
    fn=compress_audio,
    inputs=[
        gr.Audio(label="Upload Audio File", type="filepath", elem_id="audio-input"),
        gr.Slider(32, 320, step=16, label="Compression Rate (kbps)", value=128),
        gr.Textbox(label="Output File Name", placeholder="compressed_audio.mp3"),
    ],
    outputs=gr.File(label="Download Compressed Audio"),
    title="Audio Compressor",
    description="Upload an audio file, set the compression rate, and specify a name for the compressed file.",
    css=custom_css,
    allow_flagging="never",
)

# Launch the app
interface.launch()