I have a directory of AVI files that I would like to combine into a single AVI file. These AVI files come from a dash cam and have two video tracks -- a front camera and a rear camera.

I tried to use the suggestion here to combine the videos. Specifically:

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

This works great for the first (front) video track but completely removes the second (rear) video track.

How can I concatenate these files and retain both video tracks? Alternatively, is there a way I can specify the video track to concatenate?

1 Answer 1


Basically what you need to do is demultiplex each file, then concatenate the resulting files. There is a way to do this with the concat filter, but it requires re-encoding everything as it goes.

So you need to do two operations. First, demultiplex. (Note this is untested with ffmpeg, as I don't have any video files to test with, but should theoretically work.)

I=0;J=1;for f in $PWD/*.avi;do ffmpeg -i "$f" -map 0:v:0 -codec copy output$I.avi -map 0:v:1 -codec copy output$J.avi && I=$(($I+1)) && J=$(($J+1));done

Then you can use the concat demuxer as usual:

ffmpeg -f concat -i <(for f in $PWD/*.avi;do echo "file '$f'";done) -c copy output.avi
  • This worked great. The only change I made is: I=0;J=1;for f in $PWD/*.avi;do ffmpeg -i "$f" -map 0:v:0 -codec copy front$I.avi -map 0:v:1 -codec copy back$J.avi && I=$(($I+1)) && J=$(($J+1));done and then a corresponding filename change to the concatenation command. I'd edit, but I don't have the necessary reputation. Thanks!
    – Erik
    Apr 26, 2015 at 17:47
  • 1
    If you do that, you can lose J=1; and J=$(($J+1)); and change back$J.avi to back$I.avi - with different output filenames, the number can be the same for both.
    – tubedogg
    Apr 26, 2015 at 17:50
  • Right. Nice simplification.
    – Erik
    Apr 26, 2015 at 17:51

You must log in to answer this question.

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