Export Frames in a Time Range Using FFmpeg

Extracting specific video frames within a defined timeframe is a common task in video processing. This guide demonstrates how to use FFmpeg's powerful select filter to target a precise time range and export those matching frames as image files.

To extract frames within a specific time range, you use the select video filter with the between evaluation function. The filter checks the presentation timestamp (t) of each frame and only exports the frames that fall within your start and end times (measured in seconds).

The Command Syntax

Here is the standard command to extract frames between the 10-second mark and the 15-second mark of a video:

ffmpeg -i input.mp4 -vf "select='between(t,10,15)'" -vsync vfr output_%04d.png

Parameter Breakdown

  • -i input.mp4: Specifies the path to your input video file.
  • -vf: Introduces the video filtergraph.
  • select='between(t,10,15)': This is the core filter.
    • t represents the timestamp of the current frame in seconds.
    • between(t, 10, 15) tells FFmpeg to only select frames where the timestamp is greater than or equal to 10 and less than or equal to 15.
  • -vsync vfr: (Variable Frame Rate) Tells FFmpeg to drop the unselected frames. Without this parameter, FFmpeg may duplicate frames to maintain the original video's constant frame rate, resulting in thousands of duplicate images. Note: In newer FFmpeg versions, you can also use -fps_mode vfr.
  • output_%04d.png: The naming pattern for the output images. %04d is a placeholder that formats the frame numbers sequentially with leading zeros (e.g., output_0001.png, output_0002.png).

Advanced: Selecting Frames by Frame Index

If you prefer to export frames based on frame numbers rather than timestamps, you can use n (the frame index) instead of t:

ffmpeg -i input.mp4 -vf "select='between(n,300,450)'" -vsync vfr output_%04d.png

This command exports frames starting from frame index 300 up to frame index 450.