12

The following works very nicely to determine the length of various audio/video files:

mplayer -identify file.ogg 2>/dev/null | grep ID_LENGTH

However, I want to kill mplayer's output so I can determine the length of many files more efficiently. How do I do that?

haggai_e
  • 4,689
  • 1
  • 24
  • 37

5 Answers5

16

The MPlayer source ships with a sample script called midentify, which looks like this:

#!/bin/sh
#
# This is a wrapper around the -identify functionality.
# It is supposed to escape the output properly, so it can be easily
# used in shellscripts by 'eval'ing the output of this script.
#
# Written by Tobias Diedrich <ranma+mplayer@tdiedrich.de>
# Licensed under GNU GPL.

if [ -z "$1" ]; then
        echo "Usage: midentify.sh <file> [<file> ...]"
        exit 1
fi

mplayer -vo null -ao null -frames 0 -identify "$@" 2>/dev/null |
        sed -ne '/^ID_/ {
                          s/[]()|&;<>`'"'"'\\!$" []/\\&/g;p
                        }'

The -frames 0 makes mplayer exit immediately, and the -vo null -ao null prevent it from trying to open any video or audio devices. These options are all documented in man mplayer.

ephemient
  • 198,619
  • 38
  • 280
  • 391
6

FFMPEG can give you the same information in a different format (and doesn't attempt playing the file):

ffmpeg -i <myfile>
codelogic
  • 71,764
  • 9
  • 59
  • 54
  • Is there a way of invoking this so that it doesn't complain about no output files? It works nicely, but that non-`0` exit code is annoying if you're trying to call it from another program. Also, Wheezy suggests using `avconv` instead of `ffmpeg`. – Inaimathi Oct 09 '12 at 03:45
  • 2
    @Inaimathi: Yes, ``ffprobe `` does that. – nijoakim Jul 03 '17 at 00:41
3

There's another FF-way in addition to @codelogic's method, which doesn't exit with an error:

ffprobe <file>

and look for the duration entry.

Or grep for it directly in the error stream:

ffprobe <file> 2> >(grep Duration)
nijoakim
  • 930
  • 10
  • 25
0

looks like there are a few other libs available, see time length of an mp3 file

Community
  • 1
  • 1
rogerdpack
  • 62,887
  • 36
  • 269
  • 388
-1

Download your .mp3 file, play it with your Player (ex. Windows Media Player) and the player will show the total time at the end of play.