日本語

How to Speed Up or Slow Down a Video with setpts and atempo in FFmpeg

​​FFmpeg​audio,command line,video

To speed up or slow down a video in FFmpeg, use two filters with the same factor: setpts for the video and atempo for the audio. This works well for shortening a lecture or tutorial recording to 2x or 1.5x, or for making a slow-motion version.

In my test environment, a 10-second video sped up to 2x became about 5 seconds for both video and audio, and the 440 Hz tone stayed at 440 Hz. I also tested what happens when only the video is sped up and when the atempo range is exceeded.

Note

The test environment was Ubuntu 24.04.5, FFmpeg 6.1.1 (Ubuntu package 6.1.1-3ubuntu5), on a virtual machine with 4 CPU cores. The source video was a 10-second MP4 made from FFmpeg's test video (640×360, 30fps, with each frame showing its original frame number) and a 440 Hz sine wave (48 kHz, mono). The video was encoded with libx264 and the audio with AAC at 128 kbps. I did not test how speech or music sounds, variable frame rate video, or smoothing frames with minterpolate or similar methods.

Command for 2x speed

This command doubles the speed of both the video and the audio.

bash
ffmpeg -i src.mp4 -vf "setpts=0.5*PTS" -af "atempo=2.0" \
  -c:v libx264 -pix_fmt yuv420p -c:a aac -b:a 128k x2.mp4

setpts rewrites the presentation timestamp (PTS) of each video frame using an expression. 0.5*PTS means "display each frame at half its original time," which doubles the video speed. The FFmpeg filters documentation also gives setpts=0.5*PTS as an example of speeding up and setpts=2.0*PTS as an example of slowing down.

atempo changes the tempo (speed) of the audio. You write the factor directly. Note that the numbers are inverse: the video uses 0.5 because it halves the timestamps, while the audio uses 2.0 because it doubles the speed.

Here is the ffprobe output for the result.

text
r_frame_rate=30/1|avg_frame_rate=30/1|duration=5.066667|nb_read_frames=152
sample_rate=48000|duration=5.011000
duration=5.066667

The first line is the video, the second is the audio, and the third is the overall file duration. The original video had 300 video frames, and both video and audio were 10.000 seconds long. At 2x, the video became 5.067 seconds and the audio 5.011 seconds, and the frame rate stayed at 30 fps.

Writing the same thing with -filter_complex gave the same result.

bash
ffmpeg -i src.mp4 -filter_complex "[0:v]setpts=0.5*PTS[v];[0:a]atempo=2.0[a]" \
  -map "[v]" -map "[a]" -c:v libx264 -pix_fmt yuv420p -c:a aac -b:a 128k x2.mp4

Filters require re-encoding both video and audio. When I combined -c:v copy with the video filter, the command stopped with the following error. Combining the audio filter with -c:a copy produced the same error.

text
[vost#0:0/copy @ 0x...] Filtergraph 'setpts=0.5*PTS' was specified, but codec copy was selected. Filtering and streamcopy cannot be used together.

(The 0x... part is a memory address that changes on each run. The same applies to the error below.)

How to write each factor and the results

I tested different factors on the same 10-second video. The lengths are the values measured with ffprobe.

Speed Video filter Audio filter Video length Audio length Frame count
Original none none 10.000 s 10.000 s 300
4x setpts=0.25*PTS atempo=4.0 2.567 s 2.516 s 77
2x setpts=0.5*PTS atempo=2.0 5.067 s 5.011 s 152
1.5x setpts=PTS/1.5 atempo=1.5 6.733 s 6.675 s 202
0.5x setpts=2.0*PTS atempo=0.5 19.967 s 19.988 s 599
0.25x setpts=4.0*PTS atempo=0.5,atempo=0.5 39.967 s 39.956 s 1199

You can also write the 1.5x factor as a division, setpts=PTS/1.5, which avoids writing a repeating decimal such as 0.6667.

For every factor, the video length was within 0.1 seconds of the calculated value. For the three speed-ups, the video was 0.05 to 0.06 seconds longer than the audio.

Chaining atempo to slow down below 0.5x

The factor range for atempo is 0.5 to 100. When I tried to make a 0.25x version with atempo=0.25, the command stopped with the following error.

text
[Parsed_atempo_0 @ 0x...] Value 0.250000 for parameter 'tempo' out of range [0.5 - 100]
Error applying option 'tempo' to filter 'atempo': Numerical result out of range

Chaining two filters, as in atempo=0.5,atempo=0.5, gives 0.5 × 0.5 = 0.25x. The 0.25x row in the table is the result of this chain.

To speed up, you can write a single filter, such as atempo=4.0. However, the documentation says that when the factor is greater than 2, some audio samples are dropped rather than mixed, and that if this is a concern, you can chain atempo filters to reach the same factor. In my test, both atempo=4.0 and atempo=2.0,atempo=2.0 produced audio about 2.51 seconds long.

The pitch does not change

atempo changes only the speed and keeps the pitch. I exported the audio and measured the strongest frequency.

Audio Strongest frequency
Original 440.0 Hz
atempo=2.0 (2x) 440.0 Hz
atempo=0.5 (0.5x) 440.0 Hz
atempo=4.0 (4x) 440.2 Hz
asetrate=96000,aresample=48000 (2x) 880.0 Hz

The last row is a 2x version made with asetrate for comparison. asetrate changes only the sample rate value without changing the audio data. The documentation says it changes both speed and pitch. When I treated 48000 Hz audio as 96000 Hz and then resampled it back to 48000 Hz with aresample, the audio played at 2x and the pitch rose by one octave to 880 Hz. Use this method when you want to change the pitch along with the speed.

Speeding up only the video leaves the audio at its original length

If you specify only setpts without -af, the audio is written at its original speed.

bash
ffmpeg -i src.mp4 -vf "setpts=0.5*PTS" -c:v libx264 -pix_fmt yuv420p -c:a aac -b:a 128k x2-video-only.mp4
text
r_frame_rate=30/1|avg_frame_rate=30/1|duration=5.066667|nb_read_frames=152
sample_rate=48000|duration=10.005000
duration=10.005000

The video is 5.067 seconds, but the audio and the overall file are longer than 10 seconds. To make a video with no audio, add -an to remove the audio track. With -an, the result was a video-only MP4 of 5.067 seconds.

Frames are dropped, and slow motion repeats frames

I checked which original frames remain when the speed changes. I compared the MD5 hash of each frame's image with the original video and listed which original frame each output frame came from. To compare without writing an MP4, I used -fps_mode cfr -r to fix the frame rate, as the MP4 encoding does. The frame counts matched the MP4s above.

Speed and output Frame count First frames (original numbers) Last frames (original numbers)
2x, 30 fps 152 0, 1, 2, 3, 4, 6, 8, 10, 12 … … 292, 294, 296, 298
2x, 60 fps 301 0, 0, 1, 2, 3, 4, 5, 6 … … 296, 297, 298, 299
0.5x, 30 fps 599 0, 1, 1, 2, 2, 3, 3, 4 … … 297, 298, 298, 299, 299

At 2x with the frame rate kept at 30 fps, apart from the first few frames, only every other original frame was used. At 0.5x, apart from the first frame, each frame was shown twice. FFmpeg does not generate new frames here, so in the 0.5x video the picture changes only 15 times per second.

The following image shows the same moment after playback starts, cut from each of the three videos. The number on the screen is the original frame number.

Frames at playback times 1.5 s, 2.5 s, 3.5 s, and 4.5 s. The original video shows frames 45, 75, 105, and 135. The 2x video shows frames 86, 146, 206, and 266. The 0.5x video shows frames 23, 38, 53, and 68
Original video frame numbers shown at the same playback time

At 2x, the video advances about 60 original frames per second of playback. At 0.5x, it advances only 15 frames per second.

To keep every original frame at 2x, double the output frame rate. With -r 60, the MP4 was 60 fps and had 301 frames. With the same frame matching, all 300 original frames, 0 to 299, were used, with frame 0 repeated twice.

bash
ffmpeg -i src.mp4 -vf "setpts=0.5*PTS" -af "atempo=2.0" -r 60 \
  -c:v libx264 -pix_fmt yuv420p -c:a aac -b:a 128k x2-60fps.mp4
text
r_frame_rate=60/1|avg_frame_rate=60/1|duration=5.016667|nb_read_frames=301
sample_rate=48000|duration=5.011000
duration=5.016667

The video length was 5.017 seconds. This is closer to the audio length of 5.011 seconds than the 5.067 seconds at 30 fps.

​Posted by 86