How Video Streaming Works
easy 45 minA film is far too large to send as plain pictures, so a codec keeps only what is new in each frame.
See it run
Follow Asha's 10-minute play from pixels to the parents' screens. Our ladder: 0.4, 1.2, 2.4 and 4.8 Mbit/s in 4-second segments.
Operations
From pixels to bits
How large ten minutes of 1080p are before any codec, and what 4:2:0 does.
Step 1 of 4
Count the pixels
One frame
- 1920
- x
- 1080
- =
- 2,073,600
1,920 across and 1,080 down: 2,073,600 pixels in one frame.
Step 1: Count the pixels
pixels = 1920 * 1080 # 2,073,600rgb = pixels * 3 # bytes per frameyuv420 = pixels * 3 // 2 # 12 bits per pixelper_second = yuv420 * 30 * 8 # 746.5 Mbit/srung = 4_800_000 # our 1080p rungprint(per_second / rung) # about 156- Bits
- arithmetic
- Waiting
- 112 GB to 360 MB
- Savings
- about 156 times at 1080p
Say the numbers out loud: 1,493 Mbit/s raw, 746.5 as 4:2:0, 4.8 in our top rung.
In the real world
The same how video streaming works doing real jobs. Pick one and step through it.
Sam watches the play on a phone. A tunnel comes at 32 seconds.
Step 1 of 4
Before the tunnel
Rungs
- 270p
- 540p
- 720p
- 1080pnow
1080p with 20 seconds ready.
Step 1: Before the tunnel
Common mistakes
The bugs interviewers watch for, with the fix beside each.
Keyframes at different times in each rung
criticalA switch lands in the middle of a group. The player must decode frames it does not have, so it stalls or jumps.
Wrongffmpeg -i in.mp4 -c:v libx264 out_720.mp4Rightffmpeg -i in.mp4 -c:v libx264 -g 60 -keyint_min 60 \ -sc_threshold 0 -x264-params open-gop=0 out_720.mp4Choosing by the last speed alone
criticalOn our ride it stalled twice for 9.3 s. The buffer, not the last download, says how much risk is allowed.
Wrongrung = best_under(seen[-1])Rightrung = buffer_aware(buffer, seen, now)Publishing the manifest before its files
majorPlayers fetch listed segments at once and get 404 in the middle of the film.
Wrongpublish(manifest)upload(segments)Rightupload(segments)publish(manifest)A top rung above the source
majorAsha filmed at about 5.3 Mbit/s. A 10 Mbit/s rung costs storage and delivery and adds no detail.
WrongLADDER = [0.4, 1.2, 2.4, 4.8, 10]RightLADDER = [r for r in candidates if r <= source_rate]Signed tokens in the cache key
majorEvery viewer has a different token, so every request misses the edge.
Wrongcache_key = full_url_with_queryRightcache_key = path # check the token, do not key on it
Interview tips
How to explain it
- Compress, make a ladder, cut aligned segments, list them.
- Deliver files through a CDN.
- The player picks a rung per segment.
Memory trick
- MP4 is a box, H.264 is a language.
- Up slowly, down early.
- Same story, several sizes, cut at the same places.
Always say
- Closed groups, aligned keyframes.
- One task per piece and rung, retry one.
- Buffer and speed together, and a device cap.
- Origin shield and request collapsing.
Numbers to know
- Raw 1080p30: 1,493 Mbit/s.
- 4:2:0: 12 bits a pixel.
- Our ladder: 0.4, 1.2, 2.4, 4.8 Mbit/s.
- 45,000 requests, 450 at the origin.