AudioSqueeze / app.py
Gerry
initial commit
820320a verified
# 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)