How can I remove multiple segments from a video while keeping the audio?
Here I am assuming that your original file is in mp4
container, but the method should work for other containers as well.
One step method- (slightly more involved) Also see the answer given by ptQa in the referenced thread. But here is the same with audio added:
ffmpeg -i inputfile.mp4 \
-filter_complex "[0:v]trim=start=10:end=16,setpts=PTS-STARTPTS[a];
[0:v]trim=start=20:end=26,setpts=PTS-STARTPTS[b];
[0:a]atrim=start=10:end=16,asetpts=PTS-STARTPTS[c];
[0:a]atrim=start=20:end=26,asetpts=PTS-STARTPTS[d];
[a][c][b][d]concat=n=2:v=1:a=1[e][f]" \
-map '[e]' -map '[f]' -strict -2 outputfile.mp4
This adds the audio stream and neccesary pads to get audio.
Provided for completeness:
The Easy Way: You could use the 2 step process which you are already aware of:
First Cut and extract the 2 pieces you want out of the full length video.
Second Join these 2 pieces together.
This should be the easy way out.
To cut a piece from say the third second to ninth second:
ffmpeg -i input.mp4 -ss 00:00:03 -to 00:00:09 -c:v copy -c:a copy part1.mp4
You can repeat the process for the second part. If it is from 10th second to 16th second:
ffmpeg -i input.mp4 -ss 00:00:10 -to 00:00:16 -c:v copy -c:a copy part2.mp4
Also, see this thread for more details.
Now you have 2 files which you can join. You can "concatenate" in at least 3 different ways. The way to do it without re-encoding is to do it through the demuxer option. Create a text file named (say) joinlist.txt with this content:
file 'part1.mp4'
file 'part2.mp4'
To join the two files place the 2 movies and text (joinlist.txt) in the same folder and use this command:
ffmpeg -f concat -i joinlist.txt -c copy joinedfile.mp4
Related videos on Youtube
BruceR
Updated on September 18, 2022Comments
-
BruceR over 1 year
I have tried the method described in this answer. It works well, except that the audio is missing. Can anyone suggest how to get both the video and audio into the output?
Example:
I have a 2min video shot on holiday, of a kangaroo. There are 2 sections of it (both about 6secs long) I want to extract (including the audio) into a single, short piece with just these 2 cute bits.
I know I can extract two separate bits, and join them, but the article mentioned above allowed me to do exactly what I wanted, but THERE IS NO AUDIO when you follow that method. I would like to use a similar single call of FFmpeg.
-
Outdated Computer Tech about 10 yearspossible duplicate of How can I remove multiple segments from a video using FFmpeg?
-
BruceR about 10 yearsNO, this is not a duplicate. Its really an extension. How to get the audio as well as the video.
-
Rajib about 10 yearsPlease clarify: do you want to get one section from a longer file, and this smaller section should have both audio and video? Or is it something else? Please edit your question and add an example case to illustrate.
-
BruceR about 10 yearsI have edited the original, as requested. I find it odd that some people point to my question as a duplicate, when I mentioned the referenced article in my question, and ask for help extending the functionality. That shows a lack of research by the respondents, not me.
-
Rajib about 10 yearsThe original answer by ptQa in the referenced thread did not include the audio stream. See the answer below to see the added audio stream and pads. The 2 step process is given only for matter of completeness.
-
-
BruceR about 10 yearsThank You, Rajib, that is exactly what I was looking for. I had tried a number of ways of selecting audio, but defining the 2 video bits and the 2 audio bits as output suitable for the next step eluded my various attempts (guesses!) at the syntax. Once again Thank You
-
Rajib about 10 yearsIf this solves your problem you might want to accept the answer by using the check mark.
-
Mugen almost 6 yearsSuperb! Thanks Rajib! This was the easiest code that I found for ffmpeg related answers. It's clearly explained.
-
bballdave025 over 3 yearsExcellent descriptions and a new way to join pieces of video using a single command. I'll definitely be using it.