FFmpeg Cut Video and Re-encode First Frame

This article explains how to use FFmpeg to cut a specific segment of a video while re-encoding the stream to ensure the first frame of your output is a fully rendered keyframe. When cutting videos without re-encoding, the output often starts with a black screen or visual artifacts because the cut begins on a non-keyframe (B-frame or P-frame). By re-encoding the video during the cut, FFmpeg automatically forces the very first frame of the new file to become an IDR/I-frame (keyframe), ensuring smooth playback from the very first second.

The Standard Command to Cut and Re-encode

To cut a video and force FFmpeg to re-encode the stream (which automatically creates a proper keyframe at the start of the cut), use the following command structure:

ffmpeg -ss 00:01:20 -i input.mp4 -to 00:01:50 -c:v libx264 -c:a aac output.mp4

Parameter Breakdown:

  • -ss 00:01:20: Specifies the start time of the cut (1 minute and 20 seconds). Placing this before the input file (-i) enables fast seeking.
  • -i input.mp4: Defines your source video file.
  • -to 00:01:50: Specifies the stop time of the cut (1 minute and 50 seconds). Alternatively, you can use -t 00:00:30 to define a duration of 30 seconds.
  • -c:v libx264: Re-encodes the video stream using the H.264 codec. This re-encoding process automatically turns the first frame of your cut into a keyframe.
  • -c:a aac: Re-encodes the audio stream to AAC to maintain compatibility.

Frame-Accurate Cutting

If you place the -ss parameter before the -i parameter, FFmpeg seeks to the closest keyframe before your timestamp, which can sometimes lead to slightly inaccurate cuts. To achieve frame-accurate cutting where the first frame of the output is exactly the timestamp you requested, place the -ss parameter after the input file:

ffmpeg -i input.mp4 -ss 00:01:20 -to 00:01:50 -c:v libx264 -c:a aac output.mp4

This method decodes the input from the beginning and starts the output precisely at the requested timestamp, re-encoding the start point into a perfect keyframe.

Extracting and Re-encoding Just the First Frame as an Image

If your goal is to cut a video and extract only its very first frame as a high-quality, re-encoded image (for a thumbnail or poster image), use this command:

ffmpeg -ss 00:01:20 -i input.mp4 -vframes 1 -q:v 2 output.jpg

Parameter Breakdown:

  • -vframes 1: Instructs FFmpeg to limit the video output to exactly one frame.
  • -q:v 2: Controls the image quality (scale of 1-31, where lower means better quality).