ffmpeg-drm
Context: $ARGUMENTS
Quick start
- Encrypt HLS with AES-128: → Steps 1 → 2 → 3 → 4
- Package DASH CENC (ClearKey / Widevine / PlayReady-ready): → Steps 1 → 3 → 4
- Rotate keys mid-stream: → Step 3 with
-hls_flags periodic_rekey - Play back a
crypto:file: → Step 4 - Decrypt a segment for QA:
scripts/drm.py decrypt-segment ...
When to use
- You need to protect a VOD or live HLS stream with AES-128 full-segment encryption.
- You need to package MPEG-DASH with Common Encryption (CENC, cbcs, cenc-aes-ctr) for ClearKey, Widevine, PlayReady, or FairPlay-ready workflows.
- You need to generate HLS key info files, rotate keys, or signal ClearKey in an MPD.
- You need a reproducible, scripted encryption pipeline without a commercial packager.
- Don't use for: Widevine/PlayReady license server integration (ffmpeg can't emit PSSH boxes reliably — use Shaka Packager or Bento4), or FairPlay SAMPLE-AES-CTR signaling (use Apple
mediafilesegmenter).
Step 1 — Generate keys and IVs
A 16-byte (128-bit) AES key and a 16-byte IV. Both are binary; IVs are typically expressed as 32 hex chars.
# 16-byte key (binary file)
openssl rand 16 > enc.key
# or, stdlib-friendly:
dd if=/dev/urandom of=enc.key bs=16 count=1 2>/dev/null
# 32-hex-char IV (no "0x", no whitespace)
openssl rand -hex 16
# Key as hex (for ffmpeg -hls_enc_key / -encryption_key)
xxd -p enc.key # 32 hex chars
# KID for CENC (unique per content — distinct from the key)
openssl rand -hex 16
Or use the helper: scripts/drm.py gen-key --output enc.key and scripts/drm.py gen-iv.
Never commit keys to git. Add *.key and *.keyinfo to .gitignore. In production, fetch keys from a KMS or licensed DRM key server.
Step 2 — Build the HLS key info file
ffmpeg's HLS AES-128 mode reads a 3-line text file. Format is exact — deviation breaks encryption:
https://cdn.example.com/keys/enc.key
/absolute/path/to/enc.key
abcdef0123456789abcdef0123456789
- Line 1: Key URI the player fetches (goes into
EXT-X-KEY URI="..."in the playlist). - Line 2: Local path ffmpeg reads to encrypt — can be relative to the cwd used at launch, but absolute is safer.
- Line 3: IV in hex, no
0xprefix, no whitespace. Optional — if omitted, ffmpeg derives the IV from the segment media sequence number.
The server hosting line 1 must actually serve it with the right CORS headers and whatever auth your license scheme requires.
Step 3 — Package and encrypt
HLS AES-128 VOD (with key info file — recommended)
ffmpeg -i in.mp4 \
-c:v libx264 -c:a aac \
-f hls -hls_time 6 -hls_playlist_type vod \
-hls_key_info_file enc.keyinfo \
-hls_segment_filename 'seg_%03d.ts' \
out.m3u8
HLS AES-128 inline (no key info file)
ffmpeg -i in.mp4 \
-c:v libx264 -c:a aac \
-f hls -hls_time 6 \
-hls_enc 1 \
-hls_enc_key_url 'https://cdn.example.com/keys/enc.key' \
-hls_enc_key "$(xxd -p enc.key)" \
-hls_enc_iv "$(openssl rand -hex 16)" \
out.m3u8
Warning: -hls_enc 1 without -hls_enc_key / -hls_enc_key_url makes ffmpeg auto-generate a key and write it next to the playlist. Insecure default — always pass your own.
HLS key rotation (periodic rekey)
ffmpeg -i in.mp4 -c:v libx264 -c:a aac \
-f hls -hls_time 6 \
-hls_flags periodic_rekey \
-hls_key_info_file enc.keyinfo \
out.m3u8
When you update the enc.keyinfo file in place during a live ingest, ffmpeg will pick up the new key on the next segment boundary and emit a new EXT-X-KEY tag. ffmpeg's rotation is coarser than Shaka Packager's (no per-period labels); rewrite the keyinfo file on your cadence.
DASH Common Encryption (CENC) — cenc-aes-ctr
ffmpeg -i in.mp4 \
-c:v libx264 -c:a aac \
-encryption_scheme cenc-aes-ctr \
-encryption_key abcdef0123456789abcdef0123456789 \
-encryption_kid 11223344556677880011223344556677 \
-f dash \
-use_template 1 -use_timeline 1 \
-init_seg_name 'init-$RepresentationID$.m4s' \
-media_seg_name 'chunk-$RepresentationID$-$Number%05d$.m4s' \
manifest.mpd
-encryption_keyand-encryption_kidare both 32 hex chars (16 bytes each).- KID must be unique per content; the key is the AES secret the license server hands out.
- Emits CENC-compliant fmp4 segments and an MPD. For ClearKey playback, append a
<ContentProtection>ClearKey element (seereferences/schemes.md).
DASH CBCS (FairPlay / mixed-DRM compatible scheme)
ffmpeg -i in.mp4 -c:v libx264 -c:a aac \
-encryption_scheme cenc-aes-cbc \
-encryption_key abcdef0123456789abcdef0123456789 \
-encryption_kid 11223344556677880011223344556677 \
-f dash manifest.mpd
cenc-aes-cbc support depends on your ffmpeg build — verify with ffmpeg -h muxer=dash and ffmpeg -h muxer=mp4. For a fully CBCS-compliant FairPlay package you'll still typically need Shaka Packager.
Step 4 — Verify playback / decryption
Play an HLS AES-128 stream locally
ffplay -allowed_extensions ALL out.m3u8
ffplay fetches the key from line-1 URI of the EXT-X-KEY tag. If the URI is file:// or relative, ffplay needs -allowed_extensions ALL and a local HTTP server.
Decrypt one TS segment with openssl (QA)
# IV must match what ffmpeg used — line 3 of the keyinfo file
KEY_HEX=$(xxd -p enc.key)
IV_HEX=abcdef0123456789abcdef0123456789
openssl aes-128-cbc -d -K "$KEY_HEX" -iv "$IV_HEX" \
-in seg_000.ts -out seg_000.plain.ts
ffprobe seg_000.plain.ts # should show normal stream info
`crypto:` protocol playback (raw AES-128 file)
ffplay -i "crypto:encrypted.bin?key=$(xxd -p enc.key)&iv=abcdef0123456789abcdef0123456789"
Use for internally encrypted assets when a full HLS/DASH wrapper is overkill.
Available scripts
scripts/drm.py— subcommands:gen-key --output enc.key— 16 random bytes viasecrets.token_bytes.gen-iv— prints a 32-hex-char IV.hls-aes128 --input I --outdir D --key-url URL --key enc.key [--segments 6] [--rotate]— buildsenc.keyinfo, runs the ffmpeg HLS pipeline.dash-cenc --input I --outdir D --key HEX --kid HEX [--scheme cenc-aes-ctr|cenc-aes-cbc].decrypt-segment --input seg.ts --key enc.key --iv HEX --output plain.ts— shells out toopenssl aes-128-cbc.- Global flags:
--dry-run,--verbose(keys only printed with--verbose).