日本語

Making a Timelapse Video from a Long Video with FFmpeg

​​FFmpeg​command line,video

To make a timelapse from a long video with FFmpeg, thin out the frames with the select filter so that you keep one frame every 60 frames, then reset the display timestamps with setpts. This is useful when you want to turn a long recording from a fixed camera, or footage of work in progress, into a fast-forwarded video of a few tens of seconds.

In my test environment, a 30-minute video sped up 60 times became a timelapse of exactly 900 frames and 30.000 seconds. Using -skip_frame nokey, which reads only keyframes, I made the same video in 11.8 seconds. With select, it took 64.1 seconds.

Note

The test environment was Ubuntu 24.04.5, FFmpeg 6.1.1 (Ubuntu package 6.1.1-3ubuntu5), and a virtual machine with 4 CPU cores. The source video was a 30-minute MP4 (1280×720, 30 fps, H.264, a keyframe every 2 seconds, AAC audio) made from FFmpeg's test pattern testsrc2, with the original time drawn large on each frame. All timed encodes used libx264 with -preset veryfast (the test that used only the first 120 seconds used -preset ultrafast). I did not test videos from cameras or smartphones, videos with a variable frame rate, or GPU encoding.

Deciding how often to use a frame

With a timelapse, the thing you decide is how many frames of the original video you use one frame out of (for example, one frame every 60 frames).

Divide the length of the original video by the length you want in the output to get the speed factor. This time I wanted 30 minutes (1800 seconds) to become 30 seconds, so 1800 ÷ 30 gives 60x. If both the source and the output are 30 fps, keeping one frame every 60 frames gives 60x speed. In terms of the original video, that is one frame every 2 seconds.

Keeping one frame every 60 frames with select

This command makes a 60x timelapse.

bash
ffmpeg -i src.mp4 -vf "select='not(mod(n,60))',setpts=N/30/TB" -r 30 -an \
  -c:v libx264 -preset veryfast -pix_fmt yuv420p timelapse.mp4

The select filter keeps only the frames for which the expression is non-zero. n is the frame number (counting from 0), so not(mod(n,60)) means "keep only the frames whose number divided by 60 leaves a remainder of 0." The official documentation has an example that keeps one frame every 100 frames: select='not(mod(n\,100))'.

The frames you keep still carry their original timestamps. So use the setpts filter to reassign the timestamps. N is the number of the frame that reaches setpts, and TB is the time base, so N/30/TB means "advance 1/30 second for each frame."

Here is the result of checking the output with ffprobe.

text
stream|r_frame_rate=30/1|avg_frame_rate=30/1|nb_read_frames=900
format|duration=30.000000

It is 30 fps, 900 frames, and 30.000 seconds. I read the original time I had drawn on each frame. The first frame shows 00:00:00.000, the next 00:00:02.000, then 00:00:04.000, advancing 2 seconds at a time. The 900th and last frame shows 00:29:58.000.

The first 9 seconds of the timelapse. The elapsed time of the output and the time in the original video appear in the band at the top (the band is in Japanese).
Each 0.1 second of output advances the original video by 6 seconds (GIF of the first 9 seconds, thinned to 10 fps)

Writing framestep=60,setpts=N/30/TB with the framestep filter also keeps one frame every 60 frames. In my test environment, the output was byte-for-byte identical to the file made with select.

Speeding up with setpts alone makes the first few frames bunch together

You can also get a 60x speed-up with setpts=PTS/60 alone, the same way you would speed up a normal video.

bash
ffmpeg -i src.mp4 -vf "setpts=PTS/60" -an \
  -c:v libx264 -preset veryfast -pix_fmt yuv420p timelapse-setpts.mp4

With this method, 903 of the 54,000 frames ended up in the output, and the length was 30.100 seconds. This differs slightly from the select result.

The difference shows up in which frames were kept. The first three frames were three consecutive frames from the original video, at 0.000, 0.033, and 0.067 seconds. From the fourth frame on, they were spaced 2 seconds apart, at 1.833 and 3.833 seconds.

Side-by-side image of the original timestamps shown in the first five frames of the output, for setpts alone and for select with setpts. The labels are in Japanese. Left: setpts=PTS/60 alone. Right: select and setpts.
Left: setpts=PTS/60 alone. Right: select and setpts. With setpts alone, the first three frames show nearly the same time

At 60x speed, one output frame covers 2 seconds of original footage. The first three frames look almost frozen for only 0.1 seconds, but if you want to control which frames are used, specifying frame numbers with select is more reliable. Adding -r 30 did not change the output file compared with leaving it out.

Removing the audio with -an

For a timelapse, you usually do not need audio. The -an in the commands above tells FFmpeg not to include any audio in the output.

I made a video from the first 120 seconds of the original without -an (with AAC audio). The video stream was 2.000 seconds long, but the audio stayed at 120.000 seconds, so the whole file was 120 seconds long.

text
stream|codec_type=video|duration=2.000000
stream|codec_type=audio|duration=120.000000
format|duration=120.000000

Setting the interval in seconds: add fps=30 at the end

If you want to set the interval in seconds, such as "one frame every 2 seconds," rather than as a frame count, you can use the fps filter. However, writing fps=1/2,setpts=N/30/TB the same way as the select version produced a broken video.

bash
# A version that did not work
ffmpeg -i src.mp4 -vf "fps=1/2,setpts=N/30/TB" -r 30 -an \
  -c:v libx264 -preset veryfast -pix_fmt yuv420p timelapse-fps.mp4

The output had 902 frames, but the first 40 frames were all the same frame, at 00:00:00.967. The last frame was at 00:28:04.967, so the last two minutes or so of the original video were missing.

Checking with the showinfo filter, I found that the time base after fps=1/2 was 2 seconds.

text
config in time_base: 2/1, frame_rate: 1/2

I also checked the timestamps after the following setpts=N/30/TB with showinfo. Frames 0 to 59 all had timestamp 0, and from frame 60 on, the timestamp was 2 seconds. Because the timestamps were quantized to 2-second units, the video did not advance 1/30 second per frame.

When I changed the time base to 1/30 second and added fps=30 at the end, I got a video of 900 frames and 30.000 seconds.

bash
ffmpeg -i src.mp4 -vf "fps=1/2,settb=1/30,setpts=N,fps=30" -an \
  -c:v libx264 -preset veryfast -pix_fmt yuv420p timelapse-fps.mp4

The original times of the frames were 00:00:00.967, 00:00:02.967, and 00:00:04.967, spaced 2 seconds apart, and the last frame was 00:29:58.967. Compared with the select version, the frames used are offset by 0.967 seconds.

I also tried fps=1/2,settb=1/30,setpts=N with -r 30 to get 30 fps, but without the final fps=30. On the first 120 seconds of the original video, a section that gives 60 frames with select gave 119 frames. If you specify the interval in seconds, check the frame count with ffprobe before you use it.

Reading only keyframes is more than 5 times faster

All the methods so far decode all 54,000 frames of the 30-minute video, then keep only 900 of them. Each one took more than a minute.

If the keyframe interval of the original video matches the interval you want, you can read only the keyframes. Setting the decoder's skip_frame option to nokey discards every frame that is not a keyframe. FFmpeg options apply to the file that comes after them, so to apply it to the input video, write it before -i.

bash
ffmpeg -skip_frame nokey -i src.mp4 -vf "setpts=N/30/TB" -r 30 -an \
  -c:v libx264 -preset veryfast -pix_fmt yuv420p timelapse-key.mp4

The source video has a keyframe every 2 seconds, so this keeps one frame every 60 frames. The output has 900 frames and 30.000 seconds, and it was byte-for-byte identical to the file made with select. It took 11.8 seconds. The output is the same but the time dropped to about a fifth, which shows that with the other methods most of the time was spent on decoding.

Bar chart of the time to make a 60x timelapse from the 30-minute video. Only -skip_frame nokey finished in 11.8 seconds; the others took 62 to 70 seconds.
Processing time per method (the real value from the time command)
Method Time Frames
setpts=PTS/60 69.6 seconds 903
select='not(mod(n,60))' 64.1 seconds 900
framestep=60 66.8 seconds 900
fps=1/2,settb=1/30,setpts=N,fps=30 62.4 seconds 900
-skip_frame nokey 11.8 seconds 900

However, with this method the keyframe interval determines the speed factor. When I ran the same command on a 5-minute video with a keyframe every 250 frames, I got 36 frames and 1.200 seconds, which is a 250x timelapse.

You can check the keyframe interval in advance with ffprobe.

bash
ffprobe -v error -select_streams v -skip_frame nokey \
  -show_entries frame=pts_time -of csv=p=0 src.mp4

For the 30-minute video, this printed 900 timestamps at 2-second intervals: 0, 2, 4, 6, and so on. If the keyframe interval does not match the interval you want, or if the intervals are irregular, use select even though it takes longer.

​Posted by 86