Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save c0d3d-net/3859b2d0556ee3e86c833000f379855e to your computer and use it in GitHub Desktop.

Select an option

Save c0d3d-net/3859b2d0556ee3e86c833000f379855e to your computer and use it in GitHub Desktop.
Use FFmpeg to merge a directory of AVI files by appending each file name to a text file

FFmpeg: merge a folder of AVI files with names that include order number

You can merge a series of .avi files with ffmpeg using the concat filter. However, the concat filter requires that all inputs have the same streams (same codecs, same time base, etc.).

Here is an example of how you can do it (each video file should end in ..-01.avi, ..-02.avi, etc..):

Create the text file using echo

First, create a file that contains the list of all your .avi files. You can do this manually, but if your files are named in a sequence like 01.avi, 02.avi, 03.avi, and so on, you can generate this list automatically using a bash command. Here's how you can do it:

for f in *.avi; do echo "file '$f'" >> mylist.txt; done

This will create a file mylist.txt that contains a list of your .avi files in this format:

file '01.avi'
file '02.avi'
file '03.avi'
...

Pass the text file to an FFmpeg command

Then, you can use ffmpeg to concatenate the .avi files listed in mylist.txt:

ffmpeg -f concat -safe 0 -i mylist.txt -c copy output.avi

This command tells ffmpeg to use the concat demuxer (-f concat), to read the input files from mylist.txt (-i mylist.txt), and to copy the codec data without transcoding (-c copy). The result will be written to output.avi.

Please note that this method only works if all the .avi files have the same format and codec. If they differ, you will have to transcode them to a common format/codec before concatenating.

Also, the -safe 0 option is not an instruction to ignore safety, but is required when the file paths are relative, which is likely the case if you're using the command above to generate mylist.txt.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment