Search code examples
c#mp3naudiolamemp3

Estimate mp3 compression factor (NAudio.Lame)


I need a way to perform estimate of the future size of the file on the basis of the following parameters:

  • bit per sample
  • channels count
  • sample rate
  • samples count
  • mp3 quality (LAMEPreset)

I use NAudio.Lame package. C#, .Net

int GetBytesAmountMp3(int framesAmount, WaveFormat format, LAMEPreset quality)
{
     var compressionFactor = 0.3;???
     return framesAmount * format.BlockAlign * ?;
}

I need a way to rough estimate compressionFactor.


Solution

  • Output size prediction is at best approximate. Of the various modes (ABR, CBR, VBR) only CBR (Constant Bit Rate) has predictive output size. ABR (Adaptive Bit Rate) comes close most of the time, but can be quite different from the prediction in some cases. VBR is quality-based and can't really be predicted. There's a bit more information on all this here.

    For the LAMEPreset settings (which are translated directly from the LAME headers) the ABR_* settings are simple. Each is a label for a number that relates to the average kilobits per second (Kbps) in the output. The output ratio for these is then:

    double ratio = ((double)quality * 128) / format.averageBytesPerSecond;
    

    (where quality * 128 is average bytes per second)

    The rest of the settings produce VBR at various levels. The table here shows you the relationship between the numbers (V0 to V9) and the approximate output bit rate. LAMEPreset.V2 for instance produces roughly 190 Kbps output. The table also shows some of the named presets - STANDARD is the same as V2, etc. As noted in the source comments (see the source) the named presets are deprecated in LAME, I just didn't get around to marking them as such.

    For a full list of what settings each preset uses, have a look at the LAME Source, specifically the apply_preset method (line 320).