Changing Audio Pitch With FFMPEG

Posted by

A few months ago, I had a job that required me to do audio file manipulation. I had to make the pitch in a file higher or lower. So, I did some research and come across FFMPEG. FFMPEG is a software that allows users to do audio manipulation.

Musical note is related to frequency. For example, C4 or middle C has frequency of 261.626 Hz, Dâ™­4 or C#4 has frequency of 277.183 Hz, D4 has frequency of 293.665 Hz. So, how do we calculate the next note?

To calculate the frequency of the next or previous note, we can use this formula:

F2 = F1 * 2 ^ ( n / 12 )

Symbol F1 is the frequency of the original note, while n is the number of semitone between note 1 and 2. Semitone is the interval between C and C# or D and D#, etc. We can use this calculation in FFMPEG to change the sample rate of the audio file. Changing the sample rate will cause the pitch to go up or down.

To change the sample rate in FFMPEG, we can use asetrate filter. The filter will change the sample rate of the audio file. Changing the sample rate will also affect the tempo. Increasing sample rate will make the tempo faster, while decreasing sample rate will make the tempo slower. So, we’ll also use atempo filter to change the tempo back. So, the command to do all these will be:

ffmpeg -i "<input_file>" -filter_complex "asetrate=<sample_rate>*2^(<n>/12),atempo=1/2^(<n>/12)" "<output_file>"

Replace the necessary parameters, and you’ll be good to go. For example, if I want to decrease the pitch by 3 semitones and the audio file’s sample rate is 48000 Hz, I can run this command:

ffmpeg -i "input.mp3" -filter_complex "asetrate=48000*2^(-3/12),atempo=1/2^(-3/12)" "output.mp3"

Now, the output.mp3 file will be 3 semitones lower than input.mp3 file. That’s it!

If you have any questions, feel free to ask me below.

Leave a Reply

Your email address will not be published. Required fields are marked *