Search code examples
bashmacosshellvideosubshell

Bash Shell Calculating Sum of All Video durations inside a folder in MAC OS


I used to get my result in windows by just searching *.mp4 and select all files. The sum of duration would show in side panels details. I want to find the same things inside MAC recursively. This is the script I wrote in bash. Tell me what I am doing wrong?

#!/bin/bash
sum=0
find .  -type f -name "*.mp4" | while read line; do
    duration=`mdls -name kMDItemDurationSeconds "$line" | cut -d "=" -f 2`
    sum=$(echo "$duration + $sum"|bc)
all=$sum
done
echo $all

Solution

  • #!/bin/bash
    sum=0
    while read line; do
        duration=$(mdls -name kMDItemDurationSeconds "$line" | cut -d "=" -f 2)
        sum=$(echo "$duration+$sum"|bc)
    done <<< "$(find .  -type f -name "*.mp4")"
    h=$(bc <<< "$sum/3600")
    m=$(bc <<< "($sum%3600)/60")
    s=$(bc <<< "$sum%60")
    printf "%02d:%02d:%05.2f\n" $h $m $s
    

    My solution, not perfect yet.