4

I'm looking all over the Internet for information in regards to calculating the frame length and it's been hard... I was able to successfully calculate the frame length in ms of MPEG-4, AAC, using:

frameLengthMs = mSamplingRate/1000

This works since there is one sample per frame on AAC. For MPEG-1 or MPEG-2 I'm confused. There are 1152 samples per frame, ok, so what do I do with that? :P

Frame sample:

MPEGDecoder(23069): mSamplesPerFrame: 1152
MPEGDecoder(23069): mBitrateIndex: 7
MPEGDecoder(23069): mFrameLength: 314
MPEGDecoder(23069): mSamplingRate: 44100
MPEGDecoder(23069): mMpegAudioVersion 3
MPEGDecoder(23069): mLayerDesc 1
MPEGDecoder(23069): mProtectionBit 1
MPEGDecoder(23069): mBitrateIndex 7
MPEGDecoder(23069): mSamplingRateFreqIndex 0
MPEGDecoder(23069): mPaddingBit 1
MPEGDecoder(23069): mPrivateBit 0
MPEGDecoder(23069): mChannelMode 1
MPEGDecoder(23069): mModeExtension 2
MPEGDecoder(23069): mCopyright 0
MPEGDecoder(23069): mOriginal 1
MPEGDecoder(23069): mEmphasis 0
MPEGDecoder(23069): mBitrate: 96kbps
Jona
  • 12,642
  • 13
  • 82
  • 124
  • Don't know about mpeg4/aac, but an MP3 audio frame is 0.028 seconds. Possibly that's carried over to mpeg4. – Marc B Jun 09 '11 at 14:55
  • MPEG4 no need for that :) So about MP3 the frame is always 28ms? Even MPEG1 MPEG2 and MPEG2.5? – Jona Jun 09 '11 at 14:56
  • That's true for audio frames. I've never looked into the video portions of MPEG, so can't say anything about those. – Marc B Jun 09 '11 at 14:58
  • Thanks for your help... If you don't mind please post your answer and I'll set it as the answer for this post :) – Jona Jun 09 '11 at 15:04
  • It's not really an answer, though. So no biggie. – Marc B Jun 09 '11 at 15:24
  • Marc, for some reason 0.028 sec seems a little too long on my tests. Which consists counting the total frames mult by 28ms. That doesn't seem to match the players decoder... well this might be another issue... – Jona Jun 09 '11 at 15:24
  • Ok, so I believe 0.028 is not the frame length... I think it's 0.026. Could you confirm? – Jona Jun 09 '11 at 15:33

1 Answers1

13

The duration of an MPEG audio frame is a function of the sampling rate and the number of samples per frame. The formula is:

frameTimeMs = (1000/SamplingRate) * SamplesPerFrame

In your case this would be

frameTimeMs = (1000/44100) * 1152

Which yields ~26ms per frame. For a different sampling rate you would get a different duration. The key is MPEG audio always represents a fixed number of samples per frame, but the time duration of each sample is dependent on the sampling rate.

Scott
  • 756
  • 7
  • 18
  • That's great! Thanks for your answer! Even dough this makes sense, how did you figure out this particular formula? – Jona Jun 16 '11 at 00:42
  • 1
    Well, the sampling rate is the number of samples per second. Period is the reciprocal of frequency (T = 1/f), so each sample has an period of 1/44100 seconds. Once you know the duration of a sample, you just multiply by the number of samples in a frame. – Scott Jun 16 '11 at 15:13
  • 1
    What about the channels carried by one frame? Ie, if we are using 2 channels (stereo), shouldn't it be frameTimeMMs = (1000/44100)*(1152*2)? The more channels are in the frame, the longer it takes, right? – rasgo May 28 '14 at 11:47
  • 1
    No, because each channel occurs simultaneously. To be strictly accurate I could have said `SamplesPerChannelPerFrame` above. – Scott May 28 '14 at 14:21
  • I'm pretty sure it's 1024, not 1000. See https://github.com/sannies/mp4parser/blob/94a316b05a95187cfacccc5d3d9483c06fbff809/isoparser/src/main/java/com/googlecode/mp4parser/authoring/tracks/AACTrackImpl.java#L144 – basin Mar 04 '15 at 13:50
  • 1
    @basin The linked code is calculating the number of AAC packets per second, based on on AAC having 1024 samples per packet (frame). The 1000 above is the number of milliseconds in a second. – Scott Mar 05 '15 at 16:04