0

I have a bash script that reads video files in a directory and outputs information into a .csv. The only problem I have at this point is that when I export the duration of all video files in HH:MM:SS format it cuts off the seconds for any duration over an hour so my output looks like 1:25: instead of 1:25:34. Anything under an hour outputs correctly. I know that it is not the .csv part because I put into a temporary .txt file first and it is incorrect in that .txt.

Here's what isn't working:

mediainfo $file_name > tmp_file

DURATION=`grep "Duration " tmp_file |head -n1 |sed -E 's/ //g;s/Duration://g;s/([0-9]*)ms//g;s/(([0-9]*)h)*(([0-9]*)mn)*(([0-9]*)s)*/\2:\4:\6/g;s/::/:0:/g;s/^:/0:/g'`
Erick
  • 1

2 Answers2

1

There's an even easier way:

mediainfo --Inform="General;%Duration/String3%" /path/to/file

will return the playing time in the format HH:MM:SS.MMM

(For a full list of the variables --Inform= can return, just enter:

mediainfo --Info-Parameters

on the commandline.

Kwilk
  • 41
  • 5
0

There's an easier way to get the duration out of mediainfo which saves a lot of awkward text munging.

mediainfo --Inform="General;%Duration%" /path/to/file

This will return the duration in milliseconds.

The value can then be passed to awk's strftime() function to be converted to H:M:S as so:

ms=$(($(mediainfo --Inform="General;%Duration%" /path/to/file) / 1000)) && \
   echo | awk -v t=$ms '{print strftime("%H:%M:%S", t)}'
Liam Gretton
  • 370
  • 1
  • 8