Skip to content

A powerful Python script to reduce audio file sizes using various compression techniques.

License

Notifications You must be signed in to change notification settings

Danztee/audio-compressor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Audio File Size Reducer

A powerful Python script to reduce audio file sizes using various compression techniques.

Features

  • Multiple Format Support: Convert to MP3, AAC, OGG, or WAV
  • Adjustable Bitrate: Control compression quality (64k, 128k, 192k, etc.)
  • Sample Rate Control: Resample to lower frequencies for smaller files
  • Mono Conversion: Convert stereo to mono for 50% size reduction
  • Audio Trimming: Cut audio to specific time ranges
  • Batch Processing: Process entire directories at once
  • Progress Reporting: Shows compression ratios and file sizes

Installation

1. Clone or Download the Project

git clone https://github.com/Danztee/audio-compressor.git
cd audio-compressor

2. Create Virtual Environment (Recommended)

python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

3. Install Python Dependencies

pip install -r requirements.txt

4. Install FFmpeg (Required)

The script uses FFmpeg for audio processing. Install it based on your operating system:

macOS:

brew install ffmpeg

Ubuntu/Debian:

sudo apt-get update
sudo apt-get install ffmpeg

Windows: Download from https://ffmpeg.org/download.html

Quick Start

# Activate virtual environment
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Test with a sample file
python audio_compressor.py your_audio_file.wav

# Get help
python audio_compressor.py --help

Usage

Basic Usage

# Compress a single file
python audio_compressor.py input.wav

# Specify output file
python audio_compressor.py input.wav -o compressed.mp3

# High compression (low quality, small size)
python audio_compressor.py input.wav -b 64k -r 16000 -c 1

# Batch process all audio files in a directory
python audio_compressor.py /path/to/audio/files/ --batch

Command Line Options

Option Description Default
input Input audio file or directory Required
-o, --output Output file or directory Auto-generated
-f, --format Output format (mp3, aac, ogg, wav) mp3
-b, --bitrate Audio bitrate (e.g., "64k", "128k") 128k
-r, --sample-rate Sample rate in Hz 22050
-c, --channels Number of channels (1=mono, 2=stereo) 2
--start-time Start time in milliseconds None
--end-time End time in milliseconds None
--batch Process all files in directory False

Examples

1. Maximum Compression (Smallest File Size)

python audio_compressor.py song.wav -f mp3 -b 64k -r 16000 -c 1
  • Converts to MP3 with 64kbps bitrate
  • Reduces sample rate to 16kHz
  • Converts to mono
  • Results in ~90% size reduction

2. Balanced Compression (Good Quality, Reasonable Size)

python audio_compressor.py song.wav -f mp3 -b 128k -r 22050 -c 2
  • Converts to MP3 with 128kbps bitrate
  • Keeps 22kHz sample rate
  • Maintains stereo
  • Results in ~70% size reduction

3. High Quality Compression (Minimal Quality Loss)

python audio_compressor.py song.wav -f mp3 -b 192k -r 44100 -c 2
  • Converts to MP3 with 192kbps bitrate
  • Keeps 44.1kHz sample rate
  • Maintains stereo
  • Results in ~50% size reduction

4. Trim Audio and Compress

python audio_compressor.py song.wav --start-time 30000 --end-time 120000 -b 128k
  • Trims audio from 30 seconds to 2 minutes
  • Compresses with 128kbps bitrate

5. Batch Process Directory

python audio_compressor.py /path/to/music/ --batch -f mp3 -b 128k
  • Processes all audio files in the directory
  • Converts all to MP3 with 128kbps

Compression Guidelines

Bitrate Recommendations

  • 64k: Very small files, low quality (podcasts, voice recordings)
  • 128k: Good balance, acceptable quality for most music
  • 192k: High quality, minimal quality loss
  • 320k: Near CD quality

Sample Rate Recommendations

  • 16kHz: Voice recordings, very small files
  • 22kHz: Good for most compressed audio
  • 44.1kHz: CD quality, minimal compression

Format Comparison

  • MP3: Most compatible, good compression
  • AAC: Better quality than MP3 at same bitrate
  • OGG: Open source, good compression
  • WAV: Uncompressed, largest files

Troubleshooting

Common Issues

  1. "ffmpeg not found"

    • Install FFmpeg using the instructions above
  2. "pydub is required"

    • Run: pip install pydub
  3. "Error processing file"

    • Check if the input file is corrupted
    • Ensure the file format is supported
    • Try a different output format

Supported Input Formats

  • MP3, WAV, FLAC, AAC, OGG, M4A, WMA

Supported Output Formats

  • MP3, AAC, OGG, WAV

Performance Tips

  1. For maximum compression: Use mono (1 channel), low bitrate (64k), low sample rate (16kHz)
  2. For best quality: Use stereo (2 channels), high bitrate (192k+), high sample rate (44.1kHz)
  3. For batch processing: Use the --batch flag to process multiple files efficiently
  4. For voice recordings: Mono + 64k + 16kHz gives excellent compression with minimal quality loss

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

Contributions are welcome! Feel free to:

  • Report bugs
  • Suggest new features
  • Submit pull requests
  • Improve documentation

Acknowledgments

  • Built with pydub for audio processing
  • Uses FFmpeg for audio codec support

About

A powerful Python script to reduce audio file sizes using various compression techniques.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published