The
select filter in FFmpeg does not automatically handle keyframe alignment when extracting multiple disjointed segments, often resulting in corrupted output or black frames during concatenation. To successfully extract and join specific clips, you must ensure the input streams are properly segmented before merging them with the
concat demuxer or filter. This requires precise timestamp management and often intermediate file handling to avoid presentation timestamp (PTS) errors that break playback.
Understanding the Select Filter Limitations
When users attempt to use the
select filter to pick out specific time ranges from a single video stream, they frequently encounter issues because the filter operates on a per-frame basis without considering container-level boundaries. The
select expression evaluates each frame's PTS (Presentation Timestamp), but if the selected frames do not align with keyframes (I-frames) in the source codec, the decoder may struggle to reconstruct the image data correctly.
This is particularly problematic when extracting non-contiguous segments. For instance, selecting frames from 0:10-0:20 and then 0:30-0:40 creates two distinct logical streams within one processing pipeline. When these are later concatenated, the PTS values may reset incorrectly or fail to increment continuously, leading to playback glitches. Unlike simple trimming with
-ss and
-t, which can leverage keyframe seeking for speed,
select processes every frame sequentially unless paired with complex filtering strategies.
For more context on how timestamps behave during extraction, see
Trimming Start of Video Generates Negative Presentation Timestamps with FFmpeg. Understanding PTS behavior is crucial when manipulating frame selection logic.
Why Concatenation Fails with Select Output
The core issue arises when trying to merge outputs from multiple
select operations into a single video file. FFmpeg’s
concat filter expects inputs to have synchronized and continuous timestamps. If you generate separate temporary files using
select for different clip ranges, those files will start at PTS 0 by default. However, if you attempt to concat them directly in a single command line without proper formatting, the second segment might inherit timestamp offsets from the first, causing audio desync or video stuttering.
Furthermore, many codecs require keyframes at regular intervals. If your selected segments end just before a keyframe, the decoder may need reference frames from outside the selected range to decode the final frames. Since
select discards those references, the resulting segment becomes undecodable or displays visual artifacts. This is why simply chaining
select filters often fails for robust video editing workflows.
If you are experiencing broader concatenation issues, such as broadcast file errors, refer to
Troubleshooting FFmpeg Concat Error with Broadcast Files for advanced troubleshooting steps related to container formats.
Step-by-Step Solution: Segmented Extraction
To reliably extract multiple clips and combine them, follow this workflow:
- Extract Each Segment Individually: Use the
trim filter combined with setpts to create clean, independent video files for each clip. This ensures each file starts at PTS 0 and contains necessary keyframes.
- Force Keyframes: Add
-force_key_frames expr:gte(t,n_forced*2) or similar options if your codec requires strict keyframe alignment in the output segments.
- Create a Concat File List: Generate a text file listing the full paths of the extracted segments in order, using the format
file 'segment1.mp4'.
- Merge Using Concat Demuxer: Use the
-f concat option to join the files seamlessly.
Here is an example command structure for extracting two clips and merging them:
# Step 1: Extract first clip (00:00:10 to 00:00:20)
ffmpeg -i input.mp4 -vf "trim=start=10:end=20,setpts=PTS-STARTPTS" -an clip1.mp4
# Step 2: Extract second clip (00:00:30 to 00:00:40)
ffmpeg -i input.mp4 -vf "trim=start=30:end=40,setpts=PTS-STARTPTS" -an clip2.mp4
# Step 3: Create concat_list.txt
echo "file 'clip1.mp4'" > concat_list.txt
echo "file 'clip2.mp4'" >> concat_list.txt
# Step 4: Merge clips
ffmpeg -f concat -safe 0 -i concat_list.txt -c copy output.mp4
Note that we used
-an to remove audio temporarily if audio sync is also problematic. If you need to preserve audio, apply the same
trim and
setpts logic to the audio stream using
-af "atrim=start=10:end=20,asetpts=PTS-STARTPTS".
Alternative: Single-Pass Filtering with Complex Graphs
While the segmented approach is most reliable, advanced users may attempt a single-pass solution using the
concat filter within the video filter graph. This requires generating internal streams for each segment and concatenating them virtually before encoding. However, this method is highly sensitive to encoder settings and often produces larger files or slower processing times due to the lack of hardware acceleration support for complex filter chains.
For cases where you are extracting specific frames rather than time ranges, consider using the PTS-based extraction methods detailed in
Extracting Specific Frame Index using FFmpeg's Timestamp (PTS), which offers more precision for static image sequences.
Frequently Asked Questions
Why do I get black frames after my selected clip?
This usually happens because the decoder lacks the preceding keyframe needed to render the first P or B frames in your selection. You must ensure your trim points align with keyframes or force keyframe insertion during encoding to resolve decoding dependencies.
Can I use select instead of trim for better accuracy?
No,
trim is generally preferred for time-based clipping because it respects container boundaries and keyframe structures more effectively than
select, which operates purely on frame evaluation and can miss critical header information.
How do I fix negative PTS errors during concatenation?
Negative PTS errors occur when timestamps are not reset relative to the start of each segment. Always apply
setpts=PTS-STARTPTS to video and
asetpts=PTS-STARTPTS to audio after trimming to normalize timestamps to zero.
Is it possible to concat videos with different resolutions?
Standard MP4/MOV containers do not support changing resolution mid-stream. You must resize all clips to a uniform resolution using the
scale filter before concatenating, or use a wrapper format like MKV that supports stream switching, though player compatibility varies.
What causes the "Invalid data found" error in concat?
This error typically indicates that one of the input files in your concat list is corrupted, empty, or not in a compatible format. Verify that each segment plays independently in a media player before attempting to merge them.