Calculating the length of MP3 Frames in milliseconds

25,732

Solution 1

I used different approach to calculate the time of every frame in the mp3 file.. assuming that all frames have same size in the file.. so I just get the total time of the mp3 file in milliseconds.. then calculate total frames in the file and finally divide the total time by total frames.. so the formula would look like:

float frameTime = totalTimeMillisec / totalFrames;

you will get the total time of every frame in the track in milliseconds.. after I done that I got around 52 milliseconds... and that was similar to what Mark Heath said..

anyway thanks everybody for the solutions..

Solution 2

Hmm, it's strange but no one answer the question properly. I've been investigating, here's the formula:

Frame length (in ms) = (samples per frame / sample rate (in hz)) * 1000

The typical MP3 (an MPEG Layer III, version 1) has 1152 samples per frame and the sample rate is (commonly) 44100 hz. So (1152 / 44100) * 1000 = 26,122449 ms per frame.

Notice the frame length (time) does not depend on the bitrate.

More info: http://www.mp3-converter.com/mp3codec/frames.htm

Solution 3

You need to parse the MP3 frame header to get the MP3 version and layer number (see this document for the frame header format). Once you have those, you can use the following lookup table to get the number of samples in the frame.

    private static readonly int[,] samplesPerFrame = new int[,] {
        {   // MPEG Version 1
            384,    // Layer1
            1152,   // Layer2
            1152    // Layer3
        },
        {   // MPEG Version 2 & 2.5
            384,    // Layer1
            1152,   // Layer2
            576     // Layer3
        }
    };
Share:
25,732
Desolator
Author by

Desolator

I don't code for fun, I code to enhance the quality of life. Here's list of some stuff I've been working on: http://www.kooorapp.com http://www.bots-labs.com http://www.xmltojava.com http://www.zigzapps.com http://www.ezrpd.com

Updated on January 05, 2020

Comments

  • Desolator
    Desolator over 4 years

    Lets say one MP3 Frame length in bytes is 104: how to get that in milliseconds? Is there any formula or something to do that?

  • Desolator
    Desolator almost 13 years
    I have frame length and I have the total length time of the mp3 file.. but I need to somehow get the time of every frame in milliseconds.. for examples if I get 20 mp3 frames I get around 1k milliseconds which means 1 second.. how to accomplish this
  • Desolator
    Desolator almost 13 years
    ok.. its MPEG version 1 and the layer is 1152 now where should I add that to get the total time of the frame?
  • Desolator
    Desolator almost 13 years
    I don't need the datasize.. I just need the total time of that frame..
  • Mark Heath
    Mark Heath almost 13 years
    the sample rate can also be found out from the MP3 header. Sample rate is samples per second so number of samples divided by sample rate is the duration of the frame. (can't remember off the top of my head if you have to factor the number of channels in too, I think you do, so for stereo, you would halve the number of samples first)
  • Desolator
    Desolator almost 13 years
    hmm, now I did what you told me but I'm getting "0" always.. here is what I did, "mp3Frame.SampleCount / mp3Frame.SampleRate".. btw, mp3Frame.SampleCount=576 and mp3Frame.SampleRate=11025.. maybe I need to multiply the SampleCount by 2?
  • Mark Heath
    Mark Heath almost 13 years
    You need to use floating point numbers. The frame lasts around 52ms (0.052 seconds).
  • Mikhail
    Mikhail almost 13 years
    mp3-converter.com/mp3codec/frames.htm FrameSize = 144 * BitRate / (SampleRate + Padding)
  • Andrei
    Andrei almost 13 years
    ...which you get via a division.
  • nalply
    nalply over 11 years
    encrypted? Do you mean compressed? Your answer is a bit confusing.
  • Jules
    Jules almost 7 years
    "You need to use floating point numbers" -- or reverse the direction of division and work in frames-per-second rather than seconds-per-frame.
  • jayarjo
    jayarjo over 4 years
    This is should be marked as a correct answer.
  • nivs1978
    nivs1978 over 2 years
    If it's a MP3 stream, like you want to stream to a media player, and you want to stream in the same rate as the file is playing by delaying each frame. You don't know the total frames (an internet internet radio station will run forever). So the correct answer is a mix of what Mark Heath writes and lucianolev.
  • mvmn
    mvmn over 2 years
    "1152 samples per frame" - is this accurate for VBR?
  • Gumby The Green
    Gumby The Green over 2 years
    @MarkHeath The frames per sample figure applies to each channel, so no, you don't halve it for stereo. So each frame is 26ms. 1,152 samples/frame / 44,100 samples/sec = 0.026 sec/frame.
  • Paul Sanders
    Paul Sanders over 2 years
    @mvmn Yes it is, but the number of samples per frame does vary with the sample rate as outlined in Mark's answer.
  • Paul Sanders
    Paul Sanders over 2 years
    The number of samples per frame (and hence the duration) has nothing to do with the bit rate. The bit rate is an (approximate) measure of the amount of compression applied.