Reel 03 · The cutting room
Anatomy
A one-second clip, cut into two segments and pushed through a transport stream. Every number here was read from the bytes this library wrote: twelve frames, two fragments, one packet.
Where the cuts fall
A decoder can start at a key frame and nowhere else, so the packager cuts there. The target duration is what it aims for; the key frames are what decides. Below are the twelve frames of the clip, and the two segments that came out.
- starts at
- 0.00 s
- duration
- 0.60 s
- size
- 8,755 bytes
- video samples
- 6
- audio samples
- 5
The playlist that names them starts with the initialisation section a player fetches once, then one line per segment:
#EXTM3U #EXT-X-VERSION:7 #EXT-X-TARGETDURATION:1 #EXT-X-PLAYLIST-TYPE:VOD #EXT-X-INDEPENDENT-SEGMENTS #EXT-X-MAP:URI="init.mp4" #EXTINF:0.6, segment0.m4s #EXTINF:0.6, segment1.m4s #EXT-X-ENDLIST
What a segment is made of
Each segment is a movie fragment: a moof that says where
every sample is and when to show it, then the mdat that holds
them. Pick a box to see what it does.
0 to 8,755 bytes · the span is the selected box
The trun is the part a player
actually needs: a count of samples, the offset where the first one starts,
and per sample a duration, a size, the flags that mark a key frame, and a
composition offset for frames that are shown out of decode order.
188 bytes
A transport packet is always 188 bytes: four of header, then either an adaptation field, a payload, or both. This one carries the clock. Hover a byte to see what it is.
- sync
- identifier
- flags
- adaptation
- clock
- payload
Read left to right, the packet says: sync,
packet identifier 256 with the payload-unit flag set, adaptation field of
seven bytes with a clock in it, then 176 bytes of payload that begin
00 00 01 e0 — a video PES packet. The clock reads
22,356,000 ticks of 27 MHz, which is 0.828 s into the stream.
What this shows
The three sizes of a stream
A player sees segments; a decoder sees a fragment; the wire sees packets.
The three views are the same video. A segment is what a player asks for by URL and what a playlist names. Inside it, a fragment tells a decoder where the samples are without reading the whole file. Underneath, the same bytes travel as packets, because a transport stream has to be readable from anywhere in it — a receiver that joins in the middle finds the clock and the tables without needing the beginning.
Reading one and writing the other is the whole library: the packager turns a file into segments, the box layer reads a segment apart, and the transport layer does the same for packets.