FFmpeg Select Filter Not Working for Multiple Clip Extraction
In this article, we will discuss the issue of using the select filter in FFmpeg for extracting multiple clips from a video based on time constraints, as described in the following StackOverflow question: Cut multiple parts of a video with ffmpeg.
Background
FFmpeg is a powerful, open-source multimedia framework that can handle a wide variety of video and audio formats. It includes a number of filters that can be used to manipulate media streams in various ways, including the select filter, which allows you to extract specific segments of a media stream based on various criteria.
The Problem
The user in the StackOverflow question is trying to extract multiple clips from a video using the select filter, based on time constraints. However, they are experiencing issues with the filter not working as expected. Specifically, the output video only contains the last clip specified in the command, and not all of the clips as expected.
Possible Solutions
There are a few possible solutions to this problem. One option is to use the concat filter instead of the select filter. The concat filter allows you to concatenate multiple media streams together, and it can be used to extract multiple clips from a single video by specifying the input clips as separate streams. Here is an example command that uses the concat filter to extract two clips from a video:
ffmpeg -i input.mp4 -filter\_complex "[0:v]select='between(t,0,5)',setpts=N/TB[v0]; \
[0:v]select='between(t,10,15)',setpts=N/TB[v1]; \
[0:a]aselect='between(t,0,5)',asetpts=N/TB[a0]; \
[0:a]aselect='between(t,10,15)',asetpts=N/TB[a1]; \
[v0][a0][v1][a1]concat=n=2:v=1:a=1[outv][outa]" \
-map "[outv]" -map "[outa]" output.mp4
In this command, we are using the select filter to extract two clips from the input video, one from 0 to 5 seconds and the other from 10 to 15 seconds. We are then using the concat filter to concatenate these two clips together into a single output video. Note that we are also using the asetpts and setpts filters to adjust the timestamps of the input clips so that they can be concatenated correctly.
Another option is to use the -ss and -t options in FFmpeg to extract the clips. These options allow you to specify the start time and duration of the clip that you want to extract, and they can be used multiple times in the same command to extract multiple clips. Here is an example command that uses the -ss and -t options to extract two clips from a video:
ffmpeg -i input.mp4 -ss 0 -t 5 -c copy clip1.mp4 \
ffmpeg -i input.mp4 -ss 10 -t 5 -c copy clip2.mp4
In this command, we are using the -ss option to specify the start time of each clip, and the -t option to specify the duration of each clip. We are then using the -c copy option to copy the video and audio streams from the input video to the output clips, without re-encoding them. This can be much faster than using the select or concat filters, especially for large videos.
In this article, we have discussed the issue of using the select filter in FFmpeg to extract multiple clips from a video based on time constraints. We have provided two possible solutions to this problem, using the concat filter and the -ss and -t options. By using these techniques, you can extract multiple clips from a video with FFmpeg and avoid the issues that can arise when using the select filter for this purpose.
References
- FFmpeg
- select filter documentation
- concat filter documentation
- Time duration syntax