3

Could I use ffmpeg to make one video which takes different segments from different videos?

1.webm, 2.webm, 3.webm, 4.webm 

Those are my 4 video files. I want the 10 seconds from each video. The segments are specified by me.

ffmpeg -i 1.webm -1 2.webm -i 3.webm -i 4.webm -ss 00:00:00 -t 10 -ss 00:00:10  \
-t 10 -ss 00:00:20 -t 10 -ss 00:00:30 -t 10;

The above is not a real command that works. Could I get something like this programatically?

1
  • might late to the party but newer version of ffmpeg has an advanced filter called concatdec_select can do all your requirements in a single step superuser.com/a/1020455/15252
    – est
    Jan 5, 2016 at 14:38

1 Answer 1

2

You cannot do this in a single command. However, assuming you run on Linux and have Bash (or similar ones) as your shell, all it takes is two commands. Please note that this only works if the videos all have the exact same video and audio codecs and time base (fps, etc.).

1. Create a list of videos and start times

You first have to make an edit list containing the following. Let's call it videos:

1.webm 00:00:00
2.webm 00:00:10
3.webm 00:00:20

Note that this does not support video names with spaces.

2. Split the videos

Now we'll create a temporary folder to hold our smaller videos. Let's call it tmp. Then you can split up your files like so:

while read video time; do ffmpeg -i $video -ss $time -t 10 -c copy tmp/$video; done < videos

Here, -t 10 means we'll cut 10 seconds from the video, starting at the timestamp defined by -ss. You can also use -to instead of -t to have it cut until a particular point.

If your input videos all have different video and audio codecs, you need to re-encode the videos at this point. For this you should replace the -c copy part with the specific video and audio codec settings, e.g. -c:v libx264 -c:a libfaac for H.264 / AAC video and audio.

3. Concatenate the videos

Now, using the concat protocol we can merge the files back together:

ffmpeg -f concat -i <(for f in tmp/*.webm; do echo "file '$f'"; done) -c copy output.webm

This is all you need. It works by dynamically creating a concatenation file on the fly. Otherwise you'd have to create it manually according to the concat documentation.

Finally you can clean up your tmp folder.

9
  • I just noticed that the concat'd videos don't play. They play on the media player on ubuntu but fail to play on vlc on the same machine. Any reason you could think of why this is happening?
    – Pavan K
    Aug 19, 2013 at 12:40
  • It can happen. If the original videos have slightly different encoding parameters, the concatenation might result in an unplayable file. Can you try exchanging the -c copy in the last command to -c:v libvpx -b:v 1M -c:a libvorbis and see if that produces a playable file?
    – slhck
    Aug 19, 2013 at 12:52
  • But by specifying -b:v it will change the bitrate which I would like to not touch. let me try with the other options
    – Pavan K
    Aug 19, 2013 at 12:56
  • Well, with that command you'll re-encode everything anyway instead of copying the streams, so 1MBit/s bitrate was just an example for debugging purposes.
    – slhck
    Aug 19, 2013 at 12:57
  • Oh If I just want to dump the streams without modifying any params, can concat do this?
    – Pavan K
    Aug 19, 2013 at 13:33

You must log in to answer this question.

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