如何在不下载整个文件的情况下计算VBR mp3的BitRate?

Ali*_*Tor 5 c# streaming mp3 duration

我想在下载开始时获取远程mp3文件的持续时间信息.我可以先获取帧,但不知道哪些必须读取,Xing或VBRi.

如何通过阅读标签获取此信息?

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;

 }
Run Code Online (Sandbox Code Playgroud)

VC.*_*One 2

有点晚了,但如果其他人需要的话......

这篇 CodeProject文章有很多关于 MP3 标头的好方法。

  • 找到 XING 标头的起始位置。
  • 第 8 个字节以后是一个整数total frames如果存在,则为 4 个字节,大端字节序)。

每个 MPEG 帧给出每帧恒定数量的样本,由采样率决定,无论帧中总字节数是多少。您可以通过如下计算进行估计:

durationVBR = single_frame_time * total_frames; 
Run Code Online (Sandbox Code Playgroud)

在哪里...

single_frame_time = (SampleRate / SamplesPerFrame) * 1000;
Run Code Online (Sandbox Code Playgroud)

的常数SamplesPerFrame是:

MPEG-1

  • 第一层 = 384 个样本。
  • 第二层 = 1152 个样本。
  • 第三层 = 1152 个样本。

MPEG-2

  • 第一层 = 384 个样本。
  • 第二层 = 1152 个样本。
  • 第三层 = 576 个样本。