Search code examples
bashubuntupython-3.xaudiomp3

Compress/expand sound from Mp3 files in Linux server/terminal?


In a python script on my Ubuntu server, i need to process many incoming mp3 audio files with sound compressor. E.g. a tool that reduces difference between the most quiet and most loud sound levels.

Is that possible? I believe i don't need to write a program for this.

If so, what utility i should use and how do i configure it?


Solution

  • FFmpeg has a filter for compressing or expanding the audio’s dynamic range called compand. You can exec it from within your Python script. Check the documentation for usage examples.

    Random example (for audio with whisper and explosion parts):

    ffmpeg -i input.mp3 -af "compand=0|0:1|1:-90/-900|-70/-70|-30/-9|0/-3:6:0:0:0" output.mp3
    

    meaning: attack 0, decay 1, points -90/900, -70/-70, -30/-9. 0/-3, soft-knee 6, gain 0, volume 0, delay 0

    To install it you can sudo apt-get install ffmpeg or compile it from source to get the latest features.