We let users upload a short MP4 that drives an AI video job. Because the job is billed per second, the server never trusts the browser's duration: it parses the MP4 boxes itself (ftyp, moov, trak, mdhd, stts, stsz) and computes the real length before quoting a price.
Last week a user uploaded a normal vertical phone clip. The browser preview played fine, yet the server answered with a generic "use an MP4 between 3 and 30 seconds" error. Two bugs were stacked here.
1. The error message hid the real reason. The client caught every upload failure and showed the same format rule. Once we passed the server's message through, the actual cause appeared: Invalid MP4 dimensions.
2. We read display size as if it were pixel size. The width and height in the track header (tkhd) are 16.16 fixed-point numbers, and they describe the display size after the pixel aspect ratio is applied. Our validator required Number.isInteger(width). Phone encoders and anamorphic sources often store values such as 607.5, and some files store 0 there. Both were rejected.
The fix:
let width = Math.round(view.getUint32(tkhd.end - 8) / 65536);
let height = Math.round(view.getUint32(tkhd.end - 4) / 65536);
if (!validSize(width, height)) {
// Fall back to the coded size in the first visual sample entry (stsd).
// fullbox(4) + entry_count(4), then entry header(8) + 24 bytes.
const entry = stsd.data + 8;
width = view.getUint16(entry + 32);
height = view.getUint16(entry + 34);
if (!validSize(width, height)) throw new Error('Invalid MP4 dimensions');
}
Takeaways:
tkhd value; it costs two lines with a synthetic MP4 fixture.
This runs in the upload path of MakeAI Media, an AI image and video studio I'm building. Drafted with AI assistance and checked against the actual code change.