Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer
    
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 '13 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 '13 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 '13 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 '13 at 12:57
    
Oh If I just want to dump the streams without modifying any params, can concat do this? –  Pavan K Aug 19 '13 at 13:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.