Automate tasks with Python

Question:

I am looking for a way to automate tasks in external programs with Python.

I have large audio files in AAC format. I need to convert them to mp3, and then amplify them (avoiding the distortion).

I wrote a program with the pydub library that works great with small files, but my files are too large (longer than 2hs or 200mb) and I run out of memory (because that lib store the full files in RAM, I think). I can’t split the file in chunks because I could not merge them again for the previous reason, and I need the file in one piece.

So, I would like to write a program that open another program to convert the file to mp3 (mediahuman audio converter) and then, amplify the converted file with another program (WavePad audio editor) but i don’t know if is this possible.

In the present, I’m doing that manually, but that takes a long time of waiting and requires less than 10 clicks (spread throughout the process), which is tedious.

I leave the program I wrote. I transcribed it to remove some functions that are not relevant and are not related to this process, plus I translated the comments, variables and other things into English, so it may have some errors but the original program works well:

import glob
import os
from pydub import AudioSegment

#convert to mp3 128 bits
sound = AudioSegment.from_file("input-file.aac")
sound.export("output-file.mp3", format="mp3", bitrate="128k")

#sound.max_dBFS shows how far below the limit the highest sample is (in dB)
sound = AudioSegment.from_file("output.mp3", format="mp3")
max_gain_without_distortion = -1 * sound.max_dBFS

#increase volume by "max_gain_without_distortion" dB
from pydub.playback import play
song = AudioSegment.from_mp3("output-file.mp3")
louder_song = song + max_gain_without_distortion

#save louder song
louder_song.export("output.mp3", format='mp3')

PC specifications: ///
OS: windows 10 pro 64 bits ///
RAM: 4gb ///
CPU: dualcore 3ghz ///
PYTHON VERSION: 3.7.1 ///
Pydub version: v0.23.1-0-g46782a9 ///
ffmpeg/avlib version: "Build: ffmpeg-20190219-ff03418-win32-static" ///

Asked By: agb147

||

Answers:

As agreed in comments, as a solution I am going to propose using a command line tool: FFmpeg. Here’s the command you need:

ffmpeg -i input-file.aac -b:v 128k -filter:a loudnorm output.mp3

using loudnorm. You can also apply gain directly as explained in the docs, but one should expect inferior results. Normalization can be done in number of ways, I suggest reading this post.

By combining it with e.g. find . -name '*.wav' -type f you can easily find and convert all files in a directory tree.

If you’re bent on using Python, you can check Python bindings. Basics:

import ffmpeg
ffmpeg.input('stereo.aac').output('mono.mp3').run()

Initially I was going to propose using sox: Sound eXchange, the Swiss Army knife of audio manipulation. It’s not Python, though has Python bindings: pysox. However, it turned out it does not support aac format (still has dozens of other formats). I thought it could be interesting to mention it anyway, as one could convert first to more popular format with ffmpeg and pipe results to sox. The latter has many more options for modification of audio stream.

Convert wav to mp3 and resample to 128kbit:

sox -r 128k input-file.wav output-file.mp3

The OP asks to “increase volume by max_gain_without_distortion dB” and for this we can use either gain or norm as explained in docs:

sox -r 128k input-file.wav output-file.mp3 gain −n -3

After docs, The −n option normalises the audio to 0dB FSD; it is often used in conjunction with a negative gain-dB to the effect that the audio is normalised to a given level below 0dB.

sox −−norm -r 128k input-file.wav output-file.mp3

Answered By: Lukasz Tracewski
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.