How can I remove multiple segments from a video while keeping the audio?
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.
Solution 1:
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