Why does this bash script not work as an automator service

automatorbashfinderscriptterminal

I know very little about bash scripting or Terminal commands, this is all very new to me so please bear with me.


Original script

I would like to use a script I found on littlefield.info as an automator service. This is the original script:

for file in *.flv *.mov *.mp4
  do mediainfo "--Inform=Video;%Duration%" $file
done | awk '{total+=$0}END{total=total/1000}END{print strftime("%H:%M:%S",total,1)}'

Explained by the creator:

  1. Cycle through every video file in a folder,
  2. Passing it to the mediainfo program to extract the duration in milliseconds,
  3. Passing that result to the awk program to add up all the numbers to give us a total,
  4. Passing that to awk again to divide that number by 1000 to give the number of seconds,
  5. Passing that that to the strftime function to display the result in Hours:Minutes:Seconds format.

Modified script

I installed the mediainfo CLI and started experimenting. Apparently strftime is not available on mac, so I had to look up another way on how to convert the seconds to readable output. I cobbled together the following script (probably horribly inefficient, but hey.. it works):

# the cd is needed for automator
cd "$1"

i=$(for file in *.flv *.mov *.mp4
        do mediainfo "--Inform=Video;%Duration%" $file
    done | awk '{total+=$0}END{print total}')

((i/=1000, sec=i%60, i/=60, min=i%60, hrs=i/60))
timestamp=$(printf "%dh%02dm%02ds" $hrs $min $sec)
echo -e "Total duration of video files in this folder:\n\n$timestamp" > _total_duration.txt

It works perfectly fine in Terminal. But running it as a service from Finder gives the following output:

Total duration of video files in this folder:

0h00m00s

awk variable problem

The duration shouldn't be 0. I started troubleshooting, and reduced the script to the following:

for file in *.flv *.mov *.mp4
    do mediainfo "--Inform=Video;%Duration%" $file
done | awk '{total+=$0}END{print total}'  > "test_file_awk.txt"

There seems to be a problem with the variable total in AWK. It doesn't output anything when run from Finder but works correctly from Terminal.

Do you guys have any idea what I'm doing wrong?

Best Answer

Let's start with a small example.

Fire up Automator and create a service with a single "Run Shell Script" step.

Set it to take Folders as input from Finder

Pass input as arguments. Shell is /bin/bash

Bash code:

cd "$@"

for file in *.mov *.flk *.mp4
    do
    ls -1 "$file" >> ~/Desktop/test_file_awk.txt
done

What this does it takes the directory you pass in as "$@" and goes into it then runs the command on all the files in that directory.

"$@" is in quotes to protect against spaces, as is "$file"

So, you can replicate this to your setup:

cd "$@"
i=$(for file in {*.flv,*.mov,*.mp4,*.m4v}
    do 
    /usr/local/bin/mediainfo "--Inform=Video;%Duration%" "$file"
done | awk '{total+=$0}END{print total}')

((i/=1000, sec=i%60, i/=60, min=i%60, hrs=i/60))
timestamp=$(printf "%dh%02dm%02ds" $hrs $min $sec)
echo -e "Total duration of video files in this folder:\n\n$timestamp" > _total_duration.txt

Personally, I would change the output to something like:

> ~/Desktop/"$@"_total_duration.txt

So that the data ends up on your desktop in a file named for the folder, but I have a lot of media directories and I don't want txt files in them.