Get MP3 Length in Linux / FreeBSD

29

3

I have a bunch of mp3 files with various length. I want to cut it down to 50%-60% length. Says, from 1 minute down to 30 seconds. It should be trivial using ffmpeg. But, I don't know how to determine the original length of it as a base for processing using ffmpeg.

Anyone have an idea?

ariefbayu

Posted 2009-10-31T01:34:31.553

Reputation: 931

Answers

31

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

A Dwarf

Posted 2009-10-31T01:34:31.553

Reputation: 17 756

Best answer as it works even with Average or Variable Bitrate MP3! – KrisWebDev – 2016-01-03T22:25:38.363

1Add \n to print new line also. > mp3info -p "%S\n" sample.mp3 // total time in seconds – nexayq – 2017-04-16T12:13:18.497

yeah, there's no way to get length info in ffmpeg. I already stated there, I use ffmpeg only after I get the length. Anyway, thanks for the heads UP. – ariefbayu – 2009-10-31T03:59:05.467

4Worth noting that this only provides the length as an Integer. So may not be accurate enough for some use cases. – Ross – 2013-05-06T11:30:11.560

24

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

quack quixote

Posted 2009-10-31T01:34:31.553

Reputation: 37 382

Beware of ffmpeg's -analyzeduration flag. Basically, the numbers it reports are estimates after a certain point in order to save CPU. – mlissner – 2014-07-24T19:28:11.807

Using an Average Bitrate MP3, ffmpeg reports an incorrect duration along with this warning Estimating duration from bitrate, this may be inaccurate. I think this answer is valid only with Constant Bitrate MP3. – KrisWebDev – 2016-01-03T22:23:27.490

4$ ffmpeg -i foo.mp3 2>&1 | awk '/Duration/ { print substr($2,0,length($2)-1) }' For just the time portion – Craig Tataryn – 2010-10-06T18:16:59.023

To sum the length of a set of MP3 files, you can use something like TOTLENGTH=0; for f in *.mp3; do LENGTH=$(mplayer -ao null -identify -frames 0 "$f" 2>&1 | awk -F= '/ID_LENGTH/ {print $2}' | awk -F. '{print $1}'); TOTLENGTH=$(($TOTLENGTH + $LENGTH)); done; echo $TOTLENGTH to print the total length of the audio in all files, in seconds. It can probably be done more efficiently, but since I wrote it as a one-off, it was good enough for my needs. (The second awk invocation strips off decimals, so the result isn't 100% accurate, but again, good enough for my needs.) – a CVn – 2013-06-23T12:29:47.133

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)

nik

Posted 2009-10-31T01:34:31.553

Reputation: 50 788

3

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

F.C.

Posted 2009-10-31T01:34:31.553

Reputation: 245

2

The solutions using ffmpeg strike me as slightly fragile, since they are parsing output that isn't quite designed as an interface. That said they will probably continue to work for several years regardless.

ffmpeg comes with a tool ffprobe to get information about audio files (lots of formats, including mp3), and can produce machine readable output. The following command gets the song duration.

ffprobe -show_streams -print_format json song.mp3 -v fatal | jq '.streams[0].duration'

Att Righ

Posted 2009-10-31T01:34:31.553

Reputation: 273

2You can just do ffprobe -show_entries stream=duration -of compact=p=0:nk=1 -v fatal song.mp3 – Gyan – 2017-01-08T19:43:51.027

1

You can use ffmpeg to get duration of file. Just use:

ffmpeg -i <infile> 2>&1 | grep "Duration" | cut -d ' ' -f 4 | sed s/,//

Monica Sol

Posted 2009-10-31T01:34:31.553

Reputation: 11

1

I personally use Mplayer to extract the information, mostly because I already have it installed and can't be bothered to install new software unnecessarily. The advantage to this is that it isn't limited to mp3 files in particular, and should work with any media file that Mplayer can handle. The following one-liner will return the track length in seconds.

mplayer -identify -ao null -vo null -frames 0 Filename.mp3 | grep ^ID_LENGTH= | cut -d = -f 2

goldPseudo

Posted 2009-10-31T01:34:31.553

Reputation: 2 100

0

I had the same problem and found the mplayer command (goldPseudo) worked well, but I subsequently discovered that if you open an album in RhythmBox you will see its status line gives the number of tracks, total play time and disc size.

AFH

Posted 2009-10-31T01:34:31.553

Reputation: 15 470

-1

Here is my simple using refering above.

mp3_full_path="$HOME/i/want/the/length/of/file.mp3"
ffmpeg -i "$mp3_full_path" 2>&1 | grep Duration | awk -F ' ' '{print $2}' | awk -F '[,|.]' '{print $1}'
10:47:15

I send that value to variable mp3_full_length

mp3_full_path="$HOME/i/want/the/length/of/file.mp3"
read -r mp3_full_length <<< `ffmpeg -i "$mp3_full_path" 2>&1 | grep Duration | awk -F ' ' '{print $2}' | awk -F '[,|.]' '{print $1}'`



# I can use this variable for my own purpose!
echo $mp3_full_length 
10:47:15

Thank you above people! I use your ideas and knowledges Thank you for reading, Have a nice day! ;)

Jeongpyo Lee

Posted 2009-10-31T01:34:31.553

Reputation: 1