Extract a Cropped Frame as PNG Using FFmpeg

Extracting a sub-image from a video involves seeking to a specific timestamp, defining a rectangular crop area, and exporting that single frame as a PNG image. FFmpeg handles this efficiently using its built-in video filter engine, combining seeking parameters with the crop filter to isolate the desired portion of a video frame without re-encoding the entire file.

The Basic Command

To extract a cropped sub-image and save it as a PNG, use the following command syntax:

ffmpeg -ss 00:01:30 -i input.mp4 -vf "crop=width:height:x:y" -vframes 1 output.png

Parameter Breakdown

  • -ss 00:01:30: The timestamp from which to extract the frame (format: HH:MM:SS or total seconds like 90). Placing this before -i enables fast seeking.
  • -i input.mp4: The path to your source video file.
  • -vf "crop=width:height:x:y": The video filter applied to cut out the sub-image:
    • width: The width of the extracted sub-image in pixels.
    • height: The height of the extracted sub-image in pixels.
    • x: The horizontal coordinate (from the top-left corner) where the crop begins.
    • y: The vertical coordinate (from the top-left corner) where the crop begins.
  • -vframes 1: Instructs FFmpeg to export only a single frame.
  • output.png: The target file name. Specifying the .png extension automatically sets the output format to PNG.

Practical Example

To extract a 640x480 pixel sub-image starting 100 pixels from the left and 200 pixels from the top at the 45-second mark, run:

ffmpeg -ss 00:00:45 -i input.mp4 -vf "crop=640:480:100:200" -vframes 1 cropped_frame.png

Centering the Sub-Image

To crop a 500x500 square directly from the center of the video frame, you can use FFmpeg's built-in variables rather than calculating coordinates manually:

ffmpeg -ss 00:00:15 -i input.mp4 -vf "crop=500:500:(in_w-500)/2:(in_h-500)/2" -vframes 1 center_crop.png

In this command, in_w represents the input video's width and in_h represents the input video's height, automatically centering the crop box.