日本語

Change Audio Pitch Without Changing Speed with FFmpeg

​​FFmpeg​audio,command line

To change only the pitch of audio in FFmpeg without changing its length, you can use the rubberband filter, or use asetrate to change the pitch and then atempo to restore the speed. These methods are useful when you want to transpose a song up or down by a few semitones for singing or instrument practice, or when you want to change only the pitch of a voice in a video.

In my test environment, both methods turned a 440Hz tone raised by three semitones into about 523 to 524Hz, and the change in length was within 0.003 seconds. I also measured the frequencies to compare how the two methods differ in their results.

Note

The test environment was Ubuntu 24.04.5, FFmpeg 6.1.1 (Ubuntu package 6.1.1-3ubuntu5, built with --enable-librubberband), librubberband 3.3.0, and a virtual machine with 4 CPU cores. The source audio was a 440Hz sine wave generated with FFmpeg's sine, and a chord overlaying 440, 554.37, and 659.26Hz (both at 48kHz, mono, 10 seconds). I found the frequencies by running an FFT with NumPy in Python on a 4-second section of the audio starting at the 1-second mark. I did not test how this sounds with human voices or instrument recordings, or the effect of rubberband's formant=preserved (a setting that keeps the formants, which determine the timbre of a voice).

Specify the pitch as a ratio of 2^(n/12) for n semitones

With either method, you specify the pitch as a ratio. Raising the pitch by one semitone multiplies the frequency by 2 to the power of 1/12 (about 1.059463). Raising it by 12 semitones (one octave) doubles the frequency. To lower the pitch, make n negative.

Change Ratio Where 440Hz goes (calculated)
One semitone up 1.059463 466.2Hz
Three semitones up 1.189207 523.3Hz
One octave up 2 880.0Hz
Five semitones down 0.749154 329.6Hz
One octave down 0.5 220.0Hz

Change only the pitch with the rubberband filter

rubberband is a filter that changes tempo and pitch using the librubberband library (the rubberband section of the FFmpeg documentation). You set the pitch as a ratio with pitch. The command to raise the pitch by three semitones is below.

bash
ffmpeg -i sine440.wav -af "rubberband=pitch=1.189207" out.wav

These are the results when I tried different ratios. The lengths are the values measured with ffprobe.

pitch Length Measured frequency
Original audio 10.000000 seconds 440.0Hz
1.059463 (one semitone up) 10.000000 seconds 465.8Hz
1.189207 (three semitones up) 10.000000 seconds 524.2Hz
2 (one octave up) 10.000000 seconds 879.9Hz
0.749154 (five semitones down) 10.000000 seconds 329.7Hz
0.5 (one octave down) 10.000000 seconds 219.0Hz

The length stayed at exactly 10 seconds in every case. The differences from the calculated values were all under 1.1Hz. The largest was 219.0Hz for one octave down.

Instead of a number, pitch also accepts an expression. The two examples below both gave 524.2Hz. Write the comma inside pow as \,. If you write pow(2,3/12) as is, the comma is read as a filter separator and the command stops with the error No such filter: '3/12)'.

bash
ffmpeg -i sine440.wav -af "rubberband=pitch=pow(2\,3/12)" out.wav
ffmpeg -i sine440.wav -af "rubberband=pitch=2^(3/12)" out.wav

rubberband is only available if FFmpeg was built with --enable-librubberband. You can check whether your FFmpeg has it by listing the filters. On the Ubuntu package in my test environment, this showed:

bash
ffmpeg -hide_banner -filters | grep rubberband
text
 ..C rubberband        A->A       Apply time-stretching and pitch-shifting.

Change only the pitch with asetrate and atempo

Even an FFmpeg build without rubberband can do the same thing by chaining three standard filters.

bash
ffmpeg -i sine440.wav -af "asetrate=57082,aresample=48000,atempo=0.840896" out.wav

The role of each filter is as follows.

  • asetrate=57082: Leaves the audio data as it is and only rewrites the sample rate from 48000Hz to 57082Hz. This is the same as playing it back faster, so both the pitch and the speed become 1.189207 times higher.
  • aresample=48000: Converts the sample rate back to 48000Hz.
  • atempo=0.840896: Restores the speed by a factor of 1/1.189207 while keeping the pitch.

The asetrate documentation says that this filter changes both speed and pitch (the asetrate section of the FFmpeg documentation). When I used only asetrate and aresample without atempo, the pitch became 523.3Hz, but the length shrank to 8.408958 seconds.

The value for asetrate is "original sample rate × ratio," and the value for atempo is "1 ÷ ratio." The asetrate value is treated as an integer (ffmpeg -h filter=asetrate shows <int>). That's why I wrote 57082, which is 48000 × 1.189207 = 57081.9 rounded to the nearest integer. Writing 57081.9 did not cause an error, and it produced audio of the same length as 57082.

Change Filter Length Measured frequency
Three semitones up asetrate=57082,aresample=48000,atempo=0.840896 9.997271 seconds 523.2Hz
One octave up asetrate=96000,aresample=48000,atempo=0.5 9.979500 seconds 880.1Hz
Five semitones down asetrate=35959,aresample=48000,atempo=1.334840 10.004896 seconds 329.6Hz

The frequencies were within 0.1Hz of the calculated values. The length deviated from 10 seconds by at most 0.021 seconds.

The atempo ratio must be between 0.5 and 100 (the atempo section of the FFmpeg documentation). When I tried to raise the pitch by two octaves with atempo=0.25, the command stopped with the error out of range [0.5 - 100]. Chaining two atempo filters gives 0.5 × 0.5 = 0.25 times.

bash
ffmpeg -i sine440.wav -af "asetrate=192000,aresample=48000,atempo=0.5,atempo=0.5" out.wav

With this command, the result was 1760.3Hz and 9.953333 seconds.

For 44.1kHz audio, calculate from 44100

The asetrate value must be calculated from the sample rate of the source audio. When I used the 48000Hz value (57082) on 44100Hz audio, the pitch became 569.5Hz instead of 523.3Hz for three semitones up. The length also shrank to 9.184771 seconds.

When I recalculated with 44100 × 1.189207 = 52444 and also set aresample to 44100, the result was 523.3Hz and 9.997052 seconds.

bash
ffmpeg -i sine440-44k.wav -af "asetrate=52444,aresample=44100,atempo=0.840896" out.wav

You can check the sample rate of the source audio with ffprobe.

bash
ffprobe -v error -select_streams a:0 -show_entries stream=sample_rate -of csv=p=0 sine440-44k.wav

The two methods produce different frequency components

I raised a chord of 440, 554.37, and 659.26Hz by three semitones with each method and compared the frequency distributions.

Frequency distribution of the chord. The original audio has three narrow peaks. rubberband shows skirts spreading around the three peaks. The asetrate and atempo combination shows small peaks scattered around the three peaks (labels in the image are in Japanese; from top: original audio, rubberband, asetrate and atempo combination)
Frequency distribution of the chord raised by three semitones (from top: original audio, rubberband, asetrate and atempo combination)

The three strongest frequencies were all close to the calculated values (523.3, 659.3, and 784.0Hz).

Method Three strongest frequencies Length Components outside the three frequencies (±5Hz)
Original chord 440.0, 554.4, 659.3Hz 10.000000 seconds 0.00%
rubberband=pitch=1.189207 523.4, 659.2, 784.0Hz 10.000000 seconds 4.94%
asetrate + aresample + atempo 523.5, 658.7, 784.5Hz 9.994958 seconds 0.08%

The rightmost column is the share of the total signal power that fell outside the ±5Hz range of the three target frequencies. With rubberband, skirts spread around the peaks and almost 5% of the power ended up outside them. With the asetrate and atempo combination, a few small peaks appeared around the main peaks, but only 0.08% of the power was outside them.

This difference was measured on audio made by simply overlaying sine waves.

rubberband also has pitchq, which selects the method used for pitch shifting (speed, quality, or consistency). The default is speed. When I tried quality and consistency on the same chord, the share outside the peaks was 7.63%, which is more than the default's 4.94%.

Processing time

I raised a 60-second chord (48kHz, stereo) by three semitones and measured the processing time three times for each filter.

Filter Run 1 Run 2 Run 3
rubberband=pitch=1.189207 2.61 seconds 2.63 seconds 2.78 seconds
rubberband=pitch=1.189207:pitchq=quality 4.09 seconds 3.98 seconds 4.18 seconds
asetrate=57082,aresample=48000,atempo=0.840896 0.33 seconds 0.47 seconds 0.37 seconds

rubberband took 5.6 to 7.9 times as long as the asetrate and atempo combination. With pitchq=quality, it took about 1.5 times longer still.

Change only the pitch of a video's audio

For a video, copy the video stream as is with -c:v copy, and apply the filter only to the audio. Audio that has been filtered must be re-encoded. When I combined it with -c:a copy, the command stopped with the error Filtering and streamcopy cannot be used together.

bash
ffmpeg -i src.mp4 -c:v copy -af "rubberband=pitch=1.189207" -c:a aac -b:a 128k out.mp4

I tried this on a 10-second MP4 (H.264 video and 440Hz AAC audio). The audio came out at 523.6Hz. The MD5 of the video data matched the original video, so the video was unchanged.

text
codec_name=h264|duration=10.000000
codec_name=aac|duration=10.005000

The top line is the video and the bottom line is the audio durations. The original video had both video and audio at 10.000000 seconds. The audio is 0.005 seconds longer, but the video and audio lengths match within 0.01 seconds.

​Posted by 86