File size: 2,431 Bytes
820320a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# AudioSquish: A User-Friendly Audio Compression Tool

# Import necessary libraries
import gradio as gr
import numpy as np
import os
import subprocess

# Define the function to compress audio using FFmpeg
def compress_audio(input_file, bitrate, quality):
    # Define the output file name
    output_file = os.path.splitext(input_file)[0] + "_compressed.mp3"
    
    # Construct the FFmpeg command
    command = [
        "ffmpeg",
        "-i", input_file,
        "-b:a", f"{bitrate}k",
        "-q:a", str(quality),
        output_file
    ]
    
    # Run the FFmpeg command
    subprocess.run(command, check=True)
    
    # Return the path to the compressed audio file
    return output_file

# Define the Gradio app
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="cyan")) as demo:
    # Create a title and description
    gr.Markdown(
        """
        # AudioSquish
        Compress your audio files with ease and style! 🎵
        """
    )
    
    # Create a file input component for uploading audio files
    input_audio = gr.Audio(label="Upload Audio File", type="filepath", sources=["upload", "microphone"])
    
    # Create sliders for bitrate and quality adjustments
    bitrate_slider = gr.Slider(32, 320, value=128, step=8, label="Bitrate (kbps)")
    quality_slider = gr.Slider(0, 10, value=5, step=1, label="Quality (0-10)")
    
    # Create a button to trigger the compression
    compress_button = gr.Button("Compress Audio", variant="primary")
    
    # Create an output component to display the compressed audio
    output_audio = gr.Audio(label="Compressed Audio", type="filepath", autoplay=True)
    
    # Define the event listener for the compress button
    compress_button.click(
        fn=compress_audio,
        inputs=[input_audio, bitrate_slider, quality_slider],
        outputs=output_audio
    )
    
    # Add some playful styling
    css = """
    .gradio-container {
        background-color: #f0f8ff;
        font-family: 'Comic Sans MS', 'Comic Sans', cursive;
    }
    .gr-button {
        background-color: #6495ed;
        color: white;
        border-radius: 20px;
    }
    .gr-button:hover {
        background-color: #4682b4;
    }
    .gr-input {
        border-radius: 10px;
    }
    .gr-slider {
        accent-color: #6495ed;
    }
    """
    
    # Apply the custom CSS
    demo.css = css

# Launch the Gradio app
demo.launch(show_error=True)