0

I am trying to get the duration of an audio file (.wav) in milliseconds.

I have seen some command lines using ffmpeg, but this library is deprecated (remplaced by avconv) on my Ubuntu version, and i didn't find anything on it.

I can get the duration by running avconv -i <file> but i am looking for the result in milliseconds.

Ludo
  • 5,060
  • 15
  • 53
  • 85

1 Answers1

0

Wrote this as a small stand-alone .shfile:

file="$1"
info=$(sox $file -n stat 2>&1)
full_length=$(echo "$info" | sed -n 's#^Length (seconds):[^0-9]*\([0-9.]*\).*$#\1#p')
seconds=$(echo $full_length | cut -f1 -d.)

if [ -n "$seconds" ]; then
    milliseconds=$(echo $full_length | cut -f2 -d. | cut -c -3)

    result=$(($seconds * 1000))
    result=$(($result + $milliseconds))

    echo "$result"
    exit
fi

echo "0"
exit

Call it like

new-file.sh test.wav

To get a result like (duration in ms)

1337

(You can find the full code with all sources I used on GitHub)

Thomas Kekeisen
  • 4,355
  • 4
  • 35
  • 54