The Computer Oracle

Get MP3 Length in Linux / FreeBSD

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Hypnotic Puzzle3

--

Chapters
00:00 Get Mp3 Length In Linux / Freebsd
00:22 Accepted Answer Score 39
00:37 Answer 2 Score 30
00:56 Answer 3 Score 9
01:32 Answer 4 Score 4
01:43 Answer 5 Score 2
02:11 Thank you

--

Full question
https://superuser.com/questions/63399/ge...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#linux #mp3 #ffmpeg #freebsd

#avk47



ACCEPTED ANSWER

Score 39


With ffmpeg there's no way I know to get the length as a variable you can use on a script. But mp3info does.

mp3info -p "%S" sample.mp3   // total time in seconds



ANSWER 2

Score 30


ffmpeg will print everything it knows about the file if you don't give it any other arguments. Use grep to strip out everything but the "Duration":

$ ffmpeg -i foo.mp3 2>&1 | grep Duration
  Duration: 01:02:20.20, start: 0.000000, bitrate: 128 kb/s

You could also use mplayer. Grep for line "ID_LENGTH=":

$ mplayer -ao null -identify -frames 0 foo.mp3 2>&1 | grep ID_LENGTH
ID_LENGTH=3740.00



ANSWER 3

Score 9


Interestingly the EXIFTool application gives MP3 duration as the last line!

$ exiftool somefile.mp3
ExifTool Version Number         : 7.98
File Name                       : somefile.mp3
Directory                       : .
File Size                       : 49 MB
File Modification Date/Time     : 2009:09:10 11:04:54+05:30
File Type                       : MP3
MIME Type                       : audio/mpeg
MPEG Audio Version              : 2.5
Audio Layer                     : 3
Audio Bitrate                   : 64000
Sample Rate                     : 8000
Channel Mode                    : Single Channel
MS Stereo                       : Off
Intensity Stereo                : Off
Copyright Flag                  : False
Original Media                  : True
Emphasis                        : None
ID3 Size                        : 26
Genre                           : Blues
Duration                        : 1:47:46 (approx)



ANSWER 4

Score 4


Just another way to get the duration only using ffmpeg and grep:

# ffmpeg -i rara.mp3 2>&1 |grep -oP "[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{2}"
00:03:49.12