I have a bunch of directories with video files that I would like to merge into larger videos. The logic is that the filenames have date time stamps in them that can be used to figure out which should be merged together. Each existing file is no more than 3 minutes long and then another file is created. If the filenames appear to only be 3 minutes apart, I want to merge those files. If the next file is more than 3 minutes, start a new file. I strip a portion of the filename to get the date time part and try to run comparisons on that but when time crosses over the hour, the calculation I'm using doesn't work.
My files look like this:
20230425175113_002143A-Front.MP4
20230425175413_002146A-Front.MP4
20230425175714_002149A-Front.MP4
20230425180014_002152A-Front.MP4
20230425180313_002155A-Front.MP4
20230425180614_002158A-Front.MP4
20230425193016_002161A-Front.MP4
20230425175114_002145B-Middle.MP4
20230425175414_002148B-Middle.MP4
20230425175714_002151B-Middle.MP4
20230425180014_002154B-Middle.MP4
20230425180314_002157B-Middle.MP4
20230425180614_002160B-Middle.MP4
20230425193017_002162B-Middle.MP4
20230425175114_002144C-Rear.MP4
20230425175414_002147C-Rear.MP4
20230425175714_002150C-Rear.MP4
20230425180014_002153C-Rear.MP4
20230425180314_002156C-Rear.MP4
20230425180614_002159C-Rear.MP4
20230425193018_002163C-Rear.MP4
I'm trying to write a code to merge files but only if they are between 2:59 and 3:01 minutes of each other. The first 6 "Front" videos should be merged. The first 6 Middle should be merged. The first 6 Rear should be merged.
I started with this code but since these are time strings and not just numbers, this doesn't work:
#!/bin/bash
locations=( Front Middle Rear )
for location in "${locations[@]}"
do
newfile=$(ls *$location.MP4 | head -n 1 | cut -f1 -d'_')
curfile=$newfile
for i in $(find . -type f -name "*$location.MP4" | sort);do
ifile=$(echo $i | cut -f1 -d'_' | sed 's/\.\///')
if [[ $ifile -eq "$curfile" || $ifile -eq "$curhigh" || $ifile -eq "$curlow" ]]
then
cat "$i" >> "$newfile-$location.mp4"
curfile=$ifile
curhigh=$ifile
curlow=$ifile
else
cat "$i" >> "$newfile-$location.mp4"
curfile=$ifile
curhigh=$ifile
curlow=$ifile
newfile=$ifile
fi
let curhigh+=301
let curfile+=300
let curlow+=299
done
done
It does work for the obvious relations, the first 3 but the forth is 3 minutes from the previous but because it's in time it isn't written to calculate time. It would be best if I can do a plus/minus 3 minute comparison. What I really would need is to get the filename get length of the video, add that to the file name and see if there is a new file with that timestamp to merge, else start a new merge file. But that is much more complex.