Changing the speed of audio sounds easy. In ffmpeg there are two different ways, and they give different results. One keeps the pitch. The other changes it, like a tape running fast.
Keep the pitch: atempo
atempo changes the tempo and leaves the pitch alone. This is what you want for a lecture or a podcast you want to hear at 1.5x.
ffmpeg -i lecture.mp3 -af "atempo=1.5" lecture_fast.mp3
There is one catch. A single atempo only accepts values from 0.5 to 2.0. For 4x you chain filters:
ffmpeg -i lecture.mp3 -af "atempo=2.0,atempo=2.0" lecture_4x.mp3
A small function builds the chain for any value:
def atempo_chain(speed):
filters = []
while speed > 2.0:
filters.append("atempo=2.0")
speed /= 2.0
while speed < 0.5:
filters.append("atempo=0.5")
speed /= 0.5
filters.append(f"atempo={speed}")
return ",".join(filters)
atempo_chain(3.0) # atempo=2.0,atempo=1.5
atempo_chain(0.25) # atempo=0.5,atempo=0.5
Change the pitch too: asetrate
If you want the chipmunk or slow-tape effect, change the sample rate and resample back. The audio is played as if it had a different rate, so speed and pitch move together.
ffmpeg -i song.mp3 -af "asetrate=44100*1.25,aresample=44100" song_up.mp3
You need the real sample rate of the file. Do not assume 44100. Ask ffprobe:
ffprobe -v error -select_streams a:0 \
-show_entries stream=sample_rate -of csv=p=0 song.mp3
Many phone recordings are 48000. Using the wrong rate in asetrate shifts the pitch by an unwanted amount. In my code I fall back to 44100 only when ffprobe fails or gives a value outside 8000 to 192000.
Re-encode after filtering
Filters work on decoded audio, so you cannot use -c:a copy. Pick the encoder from the format: libmp3lame for mp3, aac for m4a, libvorbis for ogg, flac for flac. Speech sped up and re-encoded to mp3 loses a little quality each time, so keep the original.
Which to choose
| Goal | Filter |
|---|---|
| Faster lecture, same voice | atempo |
| Slower speech to transcribe | atempo |
| Tape or vinyl effect | asetrate |
Past about 2x, speech gets hard to follow even with pitch kept. Try 1.5x first.
Without the command line
The speed changer on MPY3JUICES runs this ffmpeg logic. You pick a multiplier between 0.25x and 4x with a slider. The tool keeps the pitch, using the atempo chain above. The server code also has the asetrate path, but the page does not expose it.
Top comments (0)