4

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.

5
  • possible duplicate of How can I remove multiple segments from a video using FFmpeg? Commented Feb 27, 2014 at 7:58
  • NO, this is not a duplicate. Its really an extension. How to get the audio as well as the video.
    – BruceR
    Commented Feb 27, 2014 at 8:33
  • 2
    Please 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.
    – Rajib
    Commented Feb 27, 2014 at 9:05
  • I 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.
    – BruceR
    Commented Mar 1, 2014 at 0:59
  • The 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.
    – Rajib
    Commented Mar 2, 2014 at 7:28

1 Answer 1

7

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
4
  • Thank 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
    – BruceR
    Commented Mar 2, 2014 at 22:55
  • If this solves your problem you might want to accept the answer by using the check mark.
    – Rajib
    Commented Mar 3, 2014 at 2:41
  • 1
    Superb! Thanks Rajib! This was the easiest code that I found for ffmpeg related answers. It's clearly explained.
    – Mugen
    Commented Aug 18, 2018 at 5:12
  • Excellent descriptions and a new way to join pieces of video using a single command. I'll definitely be using it. Commented Dec 16, 2020 at 17:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .