Score:1

How do you compare time date strings not actual dates and times?

au flag

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.

Score:1
us flag

Use gnu date or similar to parse them into integer timestamps.

ts=$( date -ud "${i:0:4}-${i:4:2}-${i:6:2} ${i:8:2}:${i:10:2}:${i:12:2}" '+%s' )

Proceed from there checking that the difference is approximately 180 seconds.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.