FFmpeg batch merge videos within a folder
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.
Related videos on Youtube
![Dr. Snail](https://i.stack.imgur.com/bkQ6l.jpg?s=256&g=1)
Dr. Snail
Updated on September 18, 2022Comments
-
Dr. Snail almost 2 years
I have a folder
c:\myfolder\
containing a variable amount ofmp4
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
intoc:\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 stepffmpeg.exe -f concat -i mylist.txt -c copy output.mp4
Question: Is there a way to do this in one step?
-
LotPings about 6 yearsA 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 countingfor /l
to build the list. -
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 over 4 yearsSee also trac.ffmpeg.org/wiki/Concatenate