Getting video ads from 25 MB to 3.5 MB

· 2 min read

Exported video ads from our editor came out at about 25 MB. Most ad networks expect about 5 MB. Now an exported ad is about 3.5 MB.

Where the size came from

An exported ad is the video plus the game engine that plays it (Phaser) plus whatever images the user added. Phaser alone is about 0.9 MB, so most of those 25 MB was the video.

So the job was to make the video fit a size budget, whatever video the user gave us.

Encode to a target size

Instead of picking a quality setting and hoping, I worked backwards from the size. File size is bitrate times duration, so if you know how long the video is and how many megabytes it can take, you can calculate the bitrate:

bitrate = size budget / duration

The export now encodes the video with HandBrake at that bitrate. If the output still comes out over budget, it re-encodes. If that isn't enough, it falls back to scaling the video down to 720px wide.

The result is about 2.6 MB of video plus about 0.9 MB of Phaser: about 3.5 MB in total. That leaves about 1.5 MB for the user's own images.

Show the size before export

Fixing the encoder wasn't enough on its own. Users couldn't see the size of their ad until they exported it and checked the file.

So I added a size indicator to the editor header. It adds up the video, Phaser and every uploaded asset, shows a breakdown per asset, and updates as assets are added or removed:

  • green under 4 MB
  • amber from 4 to 5 MB
  • red above 5 MB

Now users see roughly what the export will weigh, and which asset to cut, before they export.

One more limit on the way

Large video uploads were also failing. I traced it to a 10 MB request body limit in our middleware and raised it to 128 MB.

When a file is too big, check every limit it passes through, not just the last one.