How to correctly estimate mp4 H.264 video size?

11,747

It's far, far more complicated than your formula, and much simpler for you to figure out.

The codecs have hundreds of parameters and internal branches. There isn't some static factor of say "50". Even if you wanted to target a particular quality, the required bitrate varies quite a bit based on the content that's being compressed. For example, something not moving and with little variation in brightness takes way less bandwidth than a detailed dynamic scene being shot from a moving vehicle. The compression ratio is highly variable.

You can configure your codec for a target bitrate. Your video stream will then be close to that bitrate. It's that simple.

I can tell H.264 to target a constant bitrate 10 Mbps video stream from a 1920x1080 video, and it will do its best to cram it all in there.

You mentioned keyframe interval... yes, the keyframe takes up most of the bandwidth in a video stream. Therefore, you want them reasonably further apart when possible. Your setting here is more about choosing a tradeoff. Do you want your stream to re-sync more regularly (such as in broadcast to support fast channel changes, or online to reduce latency), or do you want to save the bandwidth for higher quality video from a reliable place (such as a pre-recorded file). Your video will still fit within the desired bitrate, but inserting keyframes too frequently will reduce the bandwidth available for the rest of the stream, causing lower quality. If you're unsure, just let the codec decide where to insert keyframes. The defaults are okay for general purpose, and are usually better than guessing at settings you're not familiar with.

Share:
11,747

Related videos on Youtube

Koala Yeung
Author by

Koala Yeung

Self-taught PHP / Golang developer. Developed a few Drupal modules.

Updated on June 04, 2022

Comments

  • Koala Yeung
    Koala Yeung almost 2 years

    Basically, video size is calculated:

    Video Size per Second (bps)
    =
    Frame Rate (fps)
    * Horizontal Pixels
    * Vertical Pixels
    * Bit Depth (bit)
    / Compression Ratio by Codec
    

    As I read somewhere, the rule of thumb compression ratio of mp4 video is 50. So for a 720p 24fps 24bit color video (neglecting the audio size for now), the file size is calculated to:

    24(fps) * 1280 * 720 * 24(bit) / 50
    = 10616832 (bps) = 10.125 (Mbps)
    

    If you calculate the video size of a 1 hour with the above bit rate, you'd get:

    10.125(Mbps) * 3600(s) / 8(bit per Byte) / 1024(MB per GB)  = 4.449GB
    

    ... which doesn't seems right at all. From daily experience, we know that a 1 hour long 720p H.264 mp4 is probably around 1 GB or less. The difference is almost a factor of 5.

    I know this formula doesn't include key-frame interval to calculation, which from I read matters a lot. I also know the compression ratio here is just a rule of thumb. There might also be factors that I didn't take into consideration. The problem is I have no idea how to fit in these missing factors.

    So, is there other way we can more accurately estimate a video size?

    • FBergo
      FBergo almost 6 years
      The 50 constant you have in mind does not exist, it is the bitrate setting of the encoder and the user/program performing the encoding can choose whatever bitrate he wants from a usually very wide range. Lower bitrates will lead to more compression artifacts and lower quality. MP4 is just a container format and does not define the video codec nor the bitrate for video and audio codecs. See youtube's bitrate guidelines for some insight: support.google.com/youtube/answer/1722171?hl=en
    • Koala Yeung
      Koala Yeung almost 6 years
      I know there are so many variables that you cannot accurately calculate a damn thing. What I want is an way to roughly estimate video size. A factor of 5 is simply to rough for an estimation.
    • FBergo
      FBergo almost 6 years
      That setting comes from whatever software is encoding the video. You can encode the same 1920x1080@30fps video with anything from 2 to 50 Mbps. The 2 Mbps will look horrible, but it is valid. If you are looking for upper bounds, that youtube link provides a good estimative. If you are getting videos from a specific camera or phone model, you need to check that hardware's documentation or support forums, or just test generated files with a decoder that prints the bitrate.