Concatenating AVI files with multiple tracks

videovideo editing

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?

Best Answer

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