How to calculate BitRate for VBR mp3 without downloading the whole file?

I want to get duration information for a remote mp3 file at the beginning of its download. I can get the frames first, but don’t know what should be read, Xing or VBRi.

How can I get this information by reading tags?

MemoryStream ms = new MemoryStream(); waveOut.Play(); long offset = from; ms.Position = 0; byte[] decBuffer = new byte[50 * 1024]; while (true) { if (paused) { waveOut.Stop(); bwProvider.ClearBuffer(); break; } lock (LockObj) { byte[] readed = Helper.ReadStreamPartially(localStream, offset, 100 * 1024, orders); if (readed == null) continue; ms = new MemoryStream(readed); } Mp3Frame frame; try { frame = Mp3Frame.LoadFromStream(ms); } catch { continue; } if (frame == null) continue; int decompressed = decompressor.DecompressFrame(frame, decBuffer, 0); bwProvider.AddSamples(decBuffer, 0, decompressed); if (Helper.IsBufferNearlyFull(bwProvider)) Thread.Sleep(500); offset += ms.Position; } 
+5
source share
1 answer

A little late, but if someone else needs it ...

This CodeProject article has a lot of good education about the MP3 header.

  • Find the starting position of the XING header.
  • The 8th byte is an integer of total frames (if 4 bytes exist, the big end).

Each MPEG frame gives a constant number of samples per frame, determined by the sampling rate, regardless of the total number of bytes in the frame. You can evaluate using calculation, for example:

 durationVBR = single_frame_time * total_frames; 

Where...

 single_frame_time = (SampleRate / SamplesPerFrame) * 1000; 

Constants for SamplesPerFrame :

MPEG-1

  • Layer i = 384 samples.
  • Layer II = 1152 samples.
  • Layer III = 1152 samples.

MPEG-2

  • Layer i = 384 samples.
  • Layer II = 1152 samples.
  • Layer III = 576 samples.
+1
source

Source: https://habr.com/ru/post/1263026/


All Articles