Skip to main content

Encoding and Packaging

Packaging turns tracks into addressable media​

Encoding chooses the samples. Packaging chooses how those samples are divided and described. An adaptive bitrate (ABR) ladder contains compatible renditions of the same content. The player can switch between them when their timestamps and random access points line up.

An encoding ladder is not a fixed resolution-to-bitrate table. It is a set of service decisions based on source quality, content complexity, devices, network conditions, and delivery cost.

Inspect before encoding​

ffprobe -hide_banner movie.mp4

Inspect the container, duration, video and audio streams, resolution, frame rate, codec, and bitrate. If the source is 720p, making a 1080p rendition only upscales it; it does not restore detail that was never present.

An MP4 can contain separate audio and video tracks. A package may put those tracks into separate segment streams so the viewer can change language without downloading a new video rendition.

Make one VOD rendition​

ffmpeg -i movie.mp4 \
-c:v libx264 -c:a aac \
-g 60 -sc_threshold 0 \
-hls_time 6 -hls_playlist_type vod \
-hls_segment_filename "seg_%03d.ts" \
index.m3u8

This example assumes a 30 fps source when interpreting -g 60 as roughly two seconds. -hls_time 6 is a target; the HLS muxer cuts at a suitable keyframe, so actual durations can vary. The example uses a six-second target aligned to a two-second GOP, but inspect the output instead of assuming exact boundaries.

The output is a media playlist plus segment files:

index.m3u8
seg_000.ts
seg_001.ts
seg_002.ts

The playlist type vod describes a fixed VOD playlist. It does not make the segments downloadable as one movie file.

To generate fragmented MP4 segments instead, use the same source with these output options:

ffmpeg -i movie.mp4 \
-c:v libx264 -c:a aac \
-g 60 -sc_threshold 0 \
-hls_time 6 -hls_playlist_type vod \
-hls_segment_type fmp4 \
-hls_segment_filename "seg_%03d.m4s" \
index.m3u8

The muxer also writes an initialization section, typically an init.mp4 file, and references it from the playlist. Inspect the generated playlist and files for the exact output of your FFmpeg build.

Build a small ABR ladder​

This example produces three H.264 variants and a master playlist. It assumes the source is at least 1080p and has an audio stream. Create the v0, v1, and v2 directories before running it.

ffmpeg -i movie.mp4 \
-filter_complex "[0:v]split=3[a][b][c];[a]scale=-2:1080[v1];[b]scale=-2:720[v2];[c]scale=-2:480[v3]" \
-map "[v1]" -c:v:0 libx264 -b:v:0 5000k \
-map "[v2]" -c:v:1 libx264 -b:v:1 2800k \
-map "[v3]" -c:v:2 libx264 -b:v:2 1200k \
-map 0:a:0 -map 0:a:0 -map 0:a:0 -c:a aac -b:a 128k \
-g 60 -keyint_min 60 -sc_threshold 0 \
-f hls -hls_time 6 -hls_playlist_type vod \
-hls_segment_filename "v%v/seg_%03d.ts" \
-master_pl_name master.m3u8 \
-var_stream_map "v:0,a:0 v:1,a:1 v:2,a:2" \
v%v/index.m3u8

The video is split and scaled into three outputs. Each variant receives a bitrate target. Repeating the audio mapping creates an audio output for each variant in this simple example; production packaging may instead use a shared audio rendition.

The shared GOP settings aim to align random access points. The packager still needs to produce valid timelines, and the result must be checked with a media validator and on target devices.

Expected layout:

master.m3u8
v0/index.m3u8 and v0/seg_*.ts
v1/index.m3u8 and v1/seg_*.ts
v2/index.m3u8 and v2/seg_*.ts

FFmpeg's var_stream_map, segment naming, and supported options can vary by build. Check the FFmpeg HLS muxer documentation when an option behaves differently from this example.

Rate control and segment containers​

CBR targets a more constant rate over time. VBR spends more bits on complex scenes and fewer on simple ones. Capped VBR constrains peaks. These names describe rate-control behavior; none guarantees every segment has the same size or prevents buffering by itself.

An ordinary MP4 often stores indexing information for a whole file. Fragmented MP4 separates initialization metadata from media fragments:

init.mp4   [ftyp] [moov]
seg_1.m4s [moof] [mdat]
seg_2.m4s [moof] [mdat]

The init segment describes how to interpret a track. Media fragments carry timed samples. Exact box layouts and segment independence depend on the profile and how the content was packaged.

MPEG-TS (.ts) and fragmented MP4 (.m4s) are different segment/container choices. Support depends on the player path and device. Some JavaScript MSE pipelines transmux MPEG-TS before appending media; native HLS playback may follow a different path.

CMAF defines interoperable fragmented ISO BMFF media objects. When the same encoded tracks, timing, and encryption profile satisfy both protocols, HLS and DASH manifests can reference shared media objects. CMAF does not mean every HLS and DASH stream can share files without constraints.

Do not choose TS just because a device is Safari. Safari's playback path and the stream's protocol/container compatibility are separate questions. Check current platform support for the target OS and playback API.

Play the local output​

Serve the generated files from an HTTP server that supports the required cross-origin requests. Then load the master playlist in Shaka. In DevTools, follow the request chain: master playlist, selected media playlist, initialization data when used, then media segments.

Network throttling can reveal adaptation, but the exact quality chosen depends on the player estimate, available variants, buffer, device restrictions, and the throttling profile. Observe the requests rather than expecting a fixed rendition.

Gotchas​

Looks likeActuallyWhy
-hls_time 6 guarantees six-second filesIt is a target and cuts depend on keyframesSegment boundaries must land on valid access points
Matching GOP length guarantees seamless switchingCompatible timing and random access points are also requiredDuration alone does not guarantee alignment
A bitrate ladder is just three resolutionsEach rendition must be encoded and described compatiblyThe player switches between timelines
TS is required for SafariDevice support depends on protocol and playback pathHLS and segment format are separate choices
The FFmpeg command is production-readyIt is a learning example with assumptionsReal assets need validation, device tests, and tuned encoding

Resources​