Sub topic 8
Segmenting and Packaging with HLS and DASH
This chapter assumes you know what a bitrate ladder is, how H.264 and AAC differ from containers such as MPEG-TS and fragmented MP4, and how HTTP caching works. Here you will see how renditions become small files a player can fetch one at a time.
Why segment at all
A single large MP4 forces the player to use byte-range requests and gives it no clean point to switch quality. Segmenting cuts each rendition into short files, usually 2 to 6 seconds long. Each segment is an ordinary HTTP object. That has three effects:
- A player can change rendition at any segment boundary.
- A CDN can cache each segment like any static file.
- No special streaming server is needed. Object storage plus HTTP is enough.
Keyframes decide where you can cut
A segment must start on a keyframe (an I-frame that decodes without earlier frames). Otherwise the player cannot begin decoding there. The encoder must therefore place keyframes at fixed intervals, and those intervals must line up across every rendition. If the 360p stream cuts at 4.0 seconds and the 1080p stream cuts at 4.2, a switch will glitch or stall.
The command below transcodes one rendition and segments it in one pass.
ffmpeg -i input.mp4 \
-vf scale=-2:720 \
-c:v libx264 -preset medium -b:v 2800k -maxrate 3000k -bufsize 6000k \
-g 48 -keyint_min 48 -sc_threshold 0 \
-c:a aac -b:a 128k \
-f hls -hls_time 4 -hls_playlist_type vod \
-hls_segment_filename "720p/seg_%04d.ts" \
720p/index.m3u8At 24 frames per second, -g 48 means one keyframe every 2 seconds. -keyint_min 48 stops the encoder from adding early keyframes. -sc_threshold 0 disables scene-change keyframes, which would otherwise shift the grid. -hls_time 4 then produces 4-second segments, each holding two keyframe intervals. Run the same settings for every rendition, changing only the scale and bitrates.
Playlists: the map of the stream
HLS uses two kinds of playlist. A media playlist lists the segments of one rendition. The master playlist lists the renditions themselves:
#EXTM3U
#EXT-X-STREAM-INF:BANDWIDTH=800000,RESOLUTION=640x360,CODECS="avc1.4d401e,mp4a.40.2"
360p/index.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=3000000,RESOLUTION=1280x720,CODECS="avc1.4d401f,mp4a.40.2"
720p/index.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=6000000,RESOLUTION=1920x1080,CODECS="avc1.640028,mp4a.40.2"
1080p/index.m3u8The player downloads the master playlist, picks an entry, and then reads that rendition's media playlist. BANDWIDTH is the peak bitrate the player compares against its measured throughput. CODECS lets a device skip renditions it cannot decode.
HLS versus DASH
Both protocols do the same job. They differ in packaging details.
| Aspect | HLS | DASH |
|---|---|---|
| Manifest | `.m3u8` text playlists | `.mpd` XML file |
| Segment format | MPEG-TS or fragmented MP4 | Fragmented MP4 (ISO BMFF) |
| Standard | Apple specification | ISO/IEC standard |
| Native support | Safari and Apple devices | Needs a player library on most browsers |
Fragmented MP4 (CMAF) is accepted by both. If you package once as CMAF segments, you can publish an HLS playlist and a DASH manifest over the same media files. That halves storage and keeps CDN cache hit rates high.
Common errors
- Keyframes that do not align across renditions, which causes visible glitches on switching.
- Forgetting
-sc_threshold 0, so scene changes insert extra keyframes and segments vary in length. - A master playlist whose
BANDWIDTHis lower than the real peak, so the player picks a rendition it cannot sustain. - Serving playlists with long cache lifetimes during live streams, when they change constantly.
Exercise
Your source is 30 fps and you want 4-second segments with a keyframe every 2 seconds. What value should you pass to -g, and why must every rendition use the same value?
Show answer
Use -g 60 (30 fps times 2 seconds). Every rendition must use the same interval so that segment boundaries fall at identical timestamps. The player can then switch renditions at any boundary without a gap, overlap or glitch.