35

I'm unable to merge two avi videos together. google is full of below examples:

cat file1.avi file2.avi file3.avi > video_draft.avi
after appending the data together using cat above, you need to re-index the draft movie like this:

mencoder video_draft.avi -o video_final.avi -forceidx -ovc copy -oac copy
Now you're video_final.avi file will be right to go.

but it doesn't work for me, the first video is converted and that's it.

teslasimus
  • 1,238
  • 5
  • 15
  • 23
  • [FFmpeg wiki: How to concatenate (join, merge) media files](http://ffmpeg.org/trac/ffmpeg/wiki/How%20to%20concatenate%20(join,%20merge)%20media%20files) – blahdiblah Mar 03 '13 at 23:24

3 Answers3

75

You should look into the concat demux and concat protocol that was added in ffmpeg 1.1. Assuming the codecs are the same you create a file (example mylist.txt):

file '/path/here/file1.avi'
file '/path/here/file2.avi'
file '/path/here/file3.avi'

Then pass that file to ffmpeg

ffmpeg -f concat -i mylist.txt -c copy video_draft.avi

You can use this command to make the list:

ls *.avi | while read each; do echo "file '$each'" >> mylist.txt; done

The linked page has more advanced examples for dealing with issue like different codecs/formats.

Theodore R. Smith
  • 21,848
  • 12
  • 65
  • 91
cwgem
  • 2,739
  • 19
  • 15
  • 1
    can you please say me how to setup ffmpeg in my android project – Nirav Ranpara Apr 19 '13 at 07:50
  • 3
    This procedure gives to me the following error:`Unknown input or output format: concat` – Zac Dec 11 '13 at 19:24
  • 2
    update ffmpeg to recent version to avoid `Unknown input or output format: concat` – vearutop Mar 23 '14 at 15:52
  • 1
    And if you use `avconv` b/c `ffmpeg` is not provided by your Linux distro, see [this answer](http://stackoverflow.com/a/19314483) on how to avoid the `Unknown input or output format: concat` error. – tanius Mar 01 '15 at 23:58
22

You can use this single command line:

ffmpeg -i "concat:input1.avi|input2.avi|input3.avi" -c copy output.avi
Omer Lerinman
  • 321
  • 2
  • 3
-2

you need to add -safe 0 for recent versions of ffmpeg, and it needs to be before the file list.

ffmpeg -f concat -safe 0 -i mylist.txt ...
lbutlr
  • 414
  • 6
  • 18
  • 1
    1) `-safe 0` is only needed in certain situations. See the [concat demuxer documentation](https://ffmpeg.org/ffmpeg-formats.html#concat-1) for details. 2) You have a typo in your example: change `-I` to `-i`. – llogan Dec 06 '18 at 19:34