title: How to get the real, actual duration of an MP3 file (VBR or CBR) server-side
body: <p>I used to calculate the duration of MP3 files server-side using ffmpeg - which seemed to work fine. Today i discovered that some of the calculations were wrong. Somehow, for some reason, ffmpeg will miscalculate the duration and it seems to happen with variable bit rate mp3 files only.</p>
  
  <p>When testing this locally, i noticed that ffmpeg printed two extra lines in green.</p>
  
  <p>Command used:</p>
  
  <pre><code>ffmpeg -i song_9747c077aef8.mp3
  </code></pre>
  
  <p>ffmpeg says:</p>
  
  <pre><code>[mp3 @ 0x102052600] max_analyze_duration 5000000 reached at 5015510
  [mp3 @ 0x102052600] Estimating duration from bitrate, this may be inaccurate
  </code></pre>
  
  <p>After a nice, warm google session, i discovered some posts on this, but no solution was found.</p>
  
  <p>I then tried to increase the maximum duration:</p>
  
  <pre><code>ffmpeg -analyzeduration 999999999 -i song_9747c077aef8.mp3
  </code></pre>
  
  <p>After this, ffmpeg returned only the second line:</p>
  
  <pre><code>[mp3 @ 0x102052600] Estimating duration from bitrate, this may be inaccurate
  </code></pre>
  
  <p>But in either case, the calculated duration was just plain wrong. Comparing it to VLC i noticed that there the duration is correct.</p>
  
  <p>After more research i stumbled over mp3info - which i installed and used.</p>
  
  <pre><code>mp3info -p "%S" song_9747c077aef8.mp3
  </code></pre>
  
  <p>mp3info then returned the <strong>CORRECT</strong> duration, but only as an integer, which i cannot use as i need a more accurate number here. The reason for this was explained in a comment below, by user <em>blahdiblah</em> - mp3info is simply pulling ID3 info from the file and not actually performing any calculations.</p>
  
  <p>I also tried using mplayer to retrieve the duration, but just as ffmpeg, mplayer is returning the wrong value.</p>