How to Find MP3 Duration from the Linux Command Line

Linux users who are used to working with music files from the command line might already be using ffmpeg, but it’s difficult to extract just the length of a file using this otherwise powerful tool. There are a few ways to use it, but there are also a few other utilities that might make the process easier. In either case, it shouldn’t be difficult to find MP3 durations from the Linux command line.

You’ll need to have a prompt open, so make sure you either search for it on the Unity Dash, start it from the System menu in Xfce4’s Whisker menu or the LXDE Application menu or perhaps hold down Ctrl, Alt and T to open a terminal box. While few MP3 editors would use the virtual console, these tricks should work at them as well.

Method 1: Checking Duration with mp3info

Most likely you won’t have mp3info installed, even if you have all of the MP3 libraries intact, so you’ll need to use sudo apt-get install mp3info on Debian or Ubuntu-based distributions to get a hold of it. This should work on any of the various Ubuntu spins including Xubuntu and Lubuntu. Due to the fact that Fedora and Red Hat have long withheld support for MP3 technology due to their wish to remain truly free, you’ll have to manually download either the source or an i386 binary from the page.

New Fedora installations should support some aspects of MP3, which means you might eventually find a package in the official repositories, but until then you can download the mp3info-0.8.5a-1.i386.rpm package and then virus scan it. If you’re looking at the file in Nautilus after downloading it, then you can push the F4 key to open a command prompt right at that directory. Otherwise, open up a command prompt as you normally would and use cd ~/Downloads to navigate to the directory you saved it in and then run sudo yum install mp3info-0.8.5a-1.i386.rpm if you have a sudoers file active. Otherwise you’ll need to type su – and push the enter key then type your password before running yum install mp3info-0.8.5a-1.i386.rpm, though you might also wish to select the file in Nautilus and see if you can install it. Ubuntu and Debian users shouldn’t have any of these issues at all, and that one sudo apt-get install command should process the package automatically.

Once you’ve processed the package, type mp3info -p “%S” nameOfTrack.mp3 at the command line and push enter to find the length. You’ll need to replace nameOfTrack.mp3 with the name of the file you’re curious about the length of. If you find that it places your prompt on the same line as the output, then try mp3info -p “%S\n” nameOfTrack.mp3 to add a new line in there. Regardless, this will generally return the time in seconds.

FreeBSD users working from x86 or x86_64 architectures should find that they can compile the source code for mp3info, but they may also be interested to note that the 0.8.5a version of the software now has an official FreeBSD port that you can locate at if you’d prefer to only work with official packages.

Method 2: Finding Duration with ffmpeg

If you’ve already installed ffmpeg and don’t want to have to install mp3info, or if you’re on a FreeBSD installation where compiling mp3info isn’t an option, then you still have a command trick you can use to find the duration easily with grep, but it might not always work. If given no other arguments, then ffmpeg will just list everything it knows about any MP3 file its fed. Assuming you wanted to work on a file called song.mp3, call ffmpeg as ffmpeg -i song.mp3 2>&1 | grep Duration to strip everything out except for the length of the music.

You may have to put the file name in quotes if it contains spaces or other characters. While those coming over from the Windows, OS X or iOS ecosystems might not be used to it, MP3 file names can actually have colons in them on many Linux and FreeBSD file systems, so you might have to use quotes in that case as well.

Keep in mind that if you have an average bitrate MP3, then ffmpeg will actually estimate the length and thus this figure might not be completely accurate. You’ll see “Estimating duration from bitrate, this may be inaccurate” if any estimations actually did occur.

Method 3: Using the ExifTool

If you’re on a distribution where you can install programs, then you might want to try sudo apt-get install libimage-exiftool-perl if you have access to the aptitude package manager such as on Ubuntu, Debian or Linux Mint. You won’t find this package on completely free software distributions like Fedora or Red Hat, though it might become available in the near future.

Should you have had no problem with the installation, you can simply type exiftool filename.mp3, replacing the name filename.mp3 with whatever file you were actually interested in. Remember that you might need to put quotes around the name but not the command if the file name has colons, slashes, spaces or anything else unusual in it. Colons are sometimes used to separate artist names and album names in the Linux and FreeBSD ecosystem.

You’ll receive a huge amount of information, which might depend on which version of the software your package installed as well as whether the file was one professionally made or not. The very last line will list the time by the word Duration, along with a bit of information about whether or not the duration of the file is approximate or not.

You could also issue exiftool filename.mp3 | grep Duration to find the line that states the duration without needing to read the rest of the material, but that’s generally only something you’d want to do if you were working with scripts.

Kevin Arrows
Kevin is a dynamic and self-motivated information technology professional, with a Thorough knowledge of all facets pertaining to network infrastructure design, implementation and administration. Superior record of delivering simultaneous large-scale mission critical projects on time and under budget.

Expert Tip

How to Find MP3 Duration from the Linux Command Line

If the issue is with your Computer or a Laptop you should try using Restoro which can scan the repositories and replace corrupt and missing files. This works in most cases, where the issue is originated due to a system corruption. You can download Restoro by clicking the Download button below.

Download Now

I'm not interested