FFmpeg batch merge videos within a folder

8,042

For those who use bash (Linux/Mac), here is the script to generate the list of all files in a folder and then merge it all into one video file:

#!/bin/bash
for filename in pieces/*.mp4; do
  echo "file $filename" >> concat-list.txt
done

ffmpeg -f concat -i concat-list.txt merged.mp4

echo "Concatenated videos list:"
cat concat-list.txt
rm concat-list.txt

It takes all mp4 files from pieces/ folder and concatenates the videos into merged.mp4 file.

Files list is generated in alphabetical order, so if you want the videos to be in a particular order, name them like 01-murder-scene.mp4, 02-office-scene.mp4 etc.

Share:
8,042

Related videos on Youtube

Dr. Snail
Author by

Dr. Snail

Updated on September 18, 2022

Comments

  • Dr. Snail
    Dr. Snail almost 2 years

    I have a folder c:\myfolder\ containing a variable amount of mp4 files with same resolution / codec.

    Now I need a .bat file to merge all videos into one.

    e.g. c:\myfolder\1.mp4, c:\myfolder\2.mp4, c:\myfolder\3.mp4 into c:\myfolder\output.mp4

    I've found a way to do this by creating a .txt file first which contains all input videos and to do in another step

    ffmpeg.exe -f concat -i mylist.txt -c copy output.mp4
    

    Question: Is there a way to do this in one step?

    • LotPings
      LotPings about 6 years
      A batch file could produce the .cue file for you, but I see a sorting problem if there are more than 9 files as 10.mp4 will follow 1.mp4 unless you use a counting for /l to build the list.
    • Dr. Snail
      Dr. Snail about 6 years
      @LotPings there is no sorting problem since the files have sortable timestamps - could you write your code into a answer?
  • slhck
    slhck over 4 years