Performance · Debugging
Video Freezes: It Is the Bitrate, Not the Size
I found the root cause of the "my internet is fine but the video keeps freezing" complaint by measuring: a single video had been uploaded uncompressed. I shrank the 1.4 GB file to 103 MB and added a check that blocks the same mistake at upload time.
01. Symptom
Instructors reported that one of the academy videos "freezes now and then and then resumes on its own". The clue in the screenshot was decisive: the playhead sat right at the edge of the buffered range. That is a buffer underrun: the player consumes data and the download can't keep up.
02. Root cause: the file itself
The player was a plain HTML5 <video> tag; there was no bug in the code. I measured the bitrate and size of all 62 videos:
| Metric | Problem video | Other 61 videos |
|---|---|---|
| Bitrate | 12.14 Mbps | 2.3 – 3.3 Mbps |
| Size | 1.48 GB | 90 – 811 MB |
Smooth playback needed a sustained 12 Mbps; the speed measured from the CDN was about 14 Mbps, so the margin was only about 13%. When the speed dips slightly the buffer drains and the video freezes. The cause of the freezing is the bitrate, not the size. Size only affected CDN caching (files above 512 MB weren't cached), and a 1 GB video with a low bitrate plays fine on a good connection.
03. The fix: re-encode
I re-encoded the four files above 512 MB and wrote them over the same storage key. So the path in the database didn't change, not a single line of code changed, and no deploy was needed.
ffmpeg -i input.mp4 \
-c:v libx264 -preset slow -crf 24 -maxrate 2.5M -bufsize 5M -g 60 \
-c:a aac -b:a 128k \
-movflags +faststart \
output.mp4
| Before | After | Bitrate | SSIM |
|---|---|---|---|
| 1,416 MB | 103 MB | 12.14 → 0.88 Mbps | 0.9959 |
| 811 MB | 173 MB | 3.32 → 0.71 Mbps | 0.9972 |
| 639 MB | 135 MB | 3.32 → 0.70 Mbps | 0.9976 |
| 634 MB | 117 MB | 3.32 → 0.61 Mbps | 0.9983 |
1080p was kept, the duration stayed the same and faststart is on. Instead of saying "I can't tell the difference by eye" I measured quality with SSIM (all above 0.99). All four are now under 512 MB, so the CDN can cache them.
04. Prevention: a bitrate check before upload
The problem was one file uploaded the wrong way; it could happen again. I added a gate to the admin panel's upload component that computes the bitrate in the browser as soon as a file is selected, without sending a single byte to the server.
const MAX_BITRATE_MBPS = 6
// When a file is selected, before uploading: bitrate = size * 8 / duration
export const probeBitrateMbps = (file: File): Promise<number | null> =>
new Promise((resolve) => {
const video = document.createElement('video')
const url = URL.createObjectURL(file)
video.preload = 'metadata'
video.onloadedmetadata = () => {
URL.revokeObjectURL(url)
resolve(video.duration > 0 ? (file.size * 8) / video.duration / 1_000_000 : null)
}
video.onerror = () => {
URL.revokeObjectURL(url)
resolve(null) // unreadable -> do not block
}
video.src = url
})
- •The threshold is 6 Mbps: about twice a healthy video (≈3.3 Mbps). If it is exceeded the upload is blocked, and the error message states the value and what to do ("H.264, ~2 Mbps, 1080p").
- •If metadata can't be read (a broken or unsupported file) the upload is not blocked. The check is a safety net; it must not create unexplained rejections.
- •There is deliberately no size check. Size doesn't cause freezing; blocking a large low-bitrate video would be wrong.
05. What I did not do
Server-side automatic transcoding (ffmpeg plus a queue) is a separate and large piece of work; for a single problem file the in-browser gate was judged sufficient, and I made that decision deliberately. Listening to the player's stalled/waiting events and showing the user an indicator was noted as an open task that would make diagnosis easier.
Takeaway: between "the user says their internet is fine" and "there is no bug in the code" there is a third possibility: the data. Instead of guessing the root cause, measuring every file took 10 minutes and reduced the problem to a single outlier.