I like to create a Bash script to concatenate MP4 files using FFmpeg, in batches of 5 files at a time for a directory with 100 MP4 files, so that afterwards there would be 20 files like:
001_005.mp4, 006_010.mp4, and so on...
instead of just 1 file consisting of all 100 files.
Contents of mylist.txt:
file 001.mp4
file 002.mp4
file 003.mp4
file 004.mp4
file 005.mp4
............
file 099.mp4
file 100.mp4
Though I've found a command that works just fine (from this StackOverflow thread), it would create only 1 file consisting all 100 files.
#!/bin/bash
cd /home/admn/Downloads/MP4_Files;
# Create mylist.txt:
for f in *.mp4
do
echo "file $f" >> mylist.txt
done;
# Concatenate files:
ffmpeg -f concat -safe 0 -i mylist.txt -c copy output.mp4;
So, how do I modify the ffmpeg command so that it concat in batches of 5 files at a time.
All the files have exact same resolution (1080p), audio, and video codecs.
OS: Ubuntu MATE 21.04
ffmpeg version: 4.3.2-0+deb11u1ubuntu1
I think the following script will work.
echo
from the line withffmpeg
to make it do its thing.Check that the content of the temporary files
xaa' ...
xat` matches the names (and content) of the output files.Another solution is to use nested loops.
As long as there are files remaining, the inner loop uses
${@:1:5}
to take the next slice of (up to) 5 files.Assuming
mylist.txt
exists in your current working directory, you can do something like this,