Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
add comment

6 Answers

up vote 10 down vote accepted

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
share|improve this answer
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 Oct 31 '09 at 3:59
BTW, %S is wrong as it only return the second part, without the minute part. For this, I use mp3info -x sample.mp3 –  ariefbayu Oct 31 '09 at 4:00
Hmm... I could check the man file, but I'm pretty sure %s (lowercase) is the one returning only the seconds part. %S (uppercase) returns total time in seconds. Do confirm that. –  A Dwarf Oct 31 '09 at 4:02
Yup. Just checked. It is as I say :) Check your mp3info man page –  A Dwarf Oct 31 '09 at 4:06
ups, my bad, I didn't see it was UPPERCASE, sorry. –  ariefbayu Oct 31 '09 at 4:25
show 1 more comment

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
share|improve this answer
$ ffmpeg -i foo.mp3 2>&1 | awk '/Duration/ { print substr($2,0,length($2)-1) }' For just the time portion –  Craig Tataryn Oct 6 '10 at 18:16
@CraigTataryn awesome. Should be an answer! –  Ross May 6 '13 at 11:34
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.) –  Michael Kjörling Jun 23 '13 at 12:29
add comment

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)
share|improve this answer
add comment

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
share|improve this answer
add comment

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

ffmpeg -i <infile> 2>&1 | grep "Duration" | cut -d ' ' -f 4 | sed s/,//
share|improve this answer
add comment

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}"
share|improve this answer
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.