How to calculate the Ratio of video file size and video duration of each file in a directory and subdirectories

I want to make a BASH script in Linux.
So far I have the following

#!/bin/bash
for file in $(find "/home/drive1/videos" -type f -name '*.mp4' -o -name '*.avi' -o -name '*.mov'); do
  size=$(stat -c%s "$file")
  duration=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$file")
  ratio=$(echo "scale=2; $size / $duration" | bc)
  echo "$file: size=$size, duration=$duration, ratio=$ratio"
done

But this gives a error

stat: cannot statx ‘/home/drive1/videos/file’: No such file or directory
/home/drive1/videos/file: No such file or directory
(standard_in) 1: syntax error
/home/drive1/videos/file: size=, duration=, ratio=

However if I only run find

find . -type f -name '*.mp4' -o -name '*.avi' -o -name '*.mov'

it shows the file(s):

./file.mov

ffprobe works as well, as it shows the duration of the videofile in seconds using

ffprobe -v quiet -of csv=p=0 -show_entries format=duration file.mov 

40.707333

What am I doing wrong, any ideas?
Thanks for your help in advance!

Cheers

Asked By: dave999

||

Please modify your expression to add parentheses, to be sure you target files with the right extension.

find . -type f '(' -name '*.mp4' -o -name '*.avi' -o -name '*.mov' ')'
Answered By: EchoMike444

The problem is not the find, but the for-loop and how that handles white-space in filenames. I find while-loops quite handy for this:

find "/home/drive1/videos" -type f -name '*.mp4' -o -name '*.avi' -o -name '*.mov' | while read -r file
do 
    size=$(stat -c%s "$file")
    duration=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$file")
    ratio=$(echo "scale=2; $size / $duration" | bc)
    echo "$file: size=$size, duration=$duration, ratio=$ratio"
done
Answered By: tink
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.