Ffmpeg Streaming
Context: $ARGUMENTS
Quick start
- Push to YouTube/Twitch (RTMP): → Step 1 (RTMP) + Step 2 (live encoder)
- Generate HLS VOD (.m3u8 + TS): → Step 1 (HLS) + Step 3 (muxer)
- HLS live with sliding window: → Step 1 (HLS live) + Step 3
- HLS ABR ladder (multi-rendition master): → Step 1 (HLS ABR)
- MPEG-DASH manifest: → Step 1 (DASH)
- Low-latency SRT transport: → Step 1 (SRT)
- Multi-output (HLS + RTMP + file): → Step 1 (Tee)
When to use
- Pushing a live encoder to an ingest (YouTube, Twitch, Facebook, Mux, Wowza, nginx-rtmp).
- Packaging a file or live source into HLS / DASH for browser/mobile playback.
- Building an ABR ladder with multiple renditions and a master playlist.
- Contribution links with very low latency (SRT, RTP, UDP, RTSP).
- Distributing a single encode to many destinations without encoding N times (tee muxer).
Step 1 — Pick a delivery protocol
| Protocol | Muxer / -f | Typical use | Latency |
| --- | --- | --- | --- |
| RTMP | -f flv to rtmp://… | Ingest to YouTube/Twitch/FB | 2–5 s |
| HLS | -f hls → .m3u8 + segments | Browser / iOS VOD + Live | 6–20 s (std), 3–7 s (LL) |
| DASH | -f dash → .mpd | Browser / Android | 6–20 s (std), 3–7 s (LL) |
| SRT | -f mpegts over srt://… | Contribution / IP backhaul | 120–400 ms |
| RTSP | -f rtsp to rtsp://… | IP cameras / private nets | ~200 ms |
| RTP | -f rtp to rtp://host:port | Low-level unicast/multicast | ~100 ms |
| UDP | -f mpegts to udp://… | Broadcast contribution | ~100 ms |
| Tee | -f tee '[f=…]out1|[f=…]out2' | One encode → many destinations | per-sink |
Decide one primary protocol first. Pick tee only after the single-sink command works.
Step 2 — Encoder settings for low-latency live
Live ingests demand a strict GOP and fast encoder. These flags are mandatory for RTMP/HLS/DASH live.
-c:v libx264 -preset veryfast -tune zerolatency \
-b:v 6000k -maxrate 6000k -bufsize 12000k \
-pix_fmt yuv420p \
-g 60 -keyint_min 60 -sc_threshold 0 \
-c:a aac -b:a 160k -ar 44100 -ac 2
Rules of thumb:
- GOP size
-g= fps × segment_time (30 fps × 2 s =-g 60). Force with-keyint_minequal to-g. -sc_threshold 0disables scene-cut keyframes → consistent GOP boundaries required for clean HLS/DASH segmentation.-tune zerolatencyonly valid forlibx264/libx265; drops B-frames and look-ahead.- YouTube/Twitch/Facebook want 2-second keyframe interval, AAC LC, 44.1 or 48 kHz.
- For files played in real-time (simulated live), prepend
-reon the input. Do NOT use-rewith a real live camera.
Step 3 — Muxer options
RTMP (push to platform ingest)
ffmpeg -i in.mp4 \
-c:v libx264 -preset veryfast -tune zerolatency \
-b:v 6000k -maxrate 6000k -bufsize 12000k \
-g 60 -keyint_min 60 -sc_threshold 0 -pix_fmt yuv420p \
-c:a aac -b:a 160k -ar 44100 \
-f flv rtmp://a.rtmp.youtube.com/live2/STREAM-KEY
Ingests: YouTube rtmp://a.rtmp.youtube.com/live2/KEY, Twitch rtmp://live.twitch.tv/app/KEY, Facebook rtmps://live-api-s.facebook.com:443/rtmp/KEY. rtmps:// needs an ffmpeg built with OpenSSL/GnuTLS.
HLS — VOD (fixed playlist)
ffmpeg -i in.mp4 \
-c:v libx264 -b:v 2500k -c:a aac -b:a 128k \
-f hls -hls_time 6 -hls_playlist_type vod \
-hls_segment_filename 'seg_%03d.ts' out.m3u8
HLS — live (sliding window)
ffmpeg -i rtmp://localhost/live/stream \
-c:v libx264 -preset veryfast -tune zerolatency \
-b:v 3000k -g 60 -keyint_min 60 -sc_threshold 0 \
-c:a aac -b:a 128k \
-f hls -hls_time 4 -hls_list_size 6 \
-hls_flags delete_segments+append_list \
out.m3u8
Use -hls_playlist_type event when you want the playlist to grow but never drop segments. Leave -hls_playlist_type unset (or event) to allow delete_segments.
HLS — fMP4 (CMAF-style)
-hls_segment_type fmp4 \
-hls_fmp4_init_filename init.mp4 \
-hls_segment_filename 'seg_%05d.m4s'
HLS — ABR (3 renditions, master playlist)
ffmpeg -i in.mp4 \
-filter_complex "[0:v]split=3[v1][v2][v3];
[v1]scale=w=1920:h=1080[v1out];
[v2]scale=w=1280:h=720[v2out];
[v3]scale=w=854:h=480[v3out]" \
-map "[v1out]" -c:v:0 libx264 -b:v:0 5000k -maxrate:v:0 5350k -bufsize:v:0 7500k \
-map "[v2out]" -c:v:1 libx264 -b:v:1 2800k -maxrate:v:1 3000k -bufsize:v:1 4200k \
-map "[v3out]" -c:v:2 libx264 -b:v:2 1400k -maxrate:v:2 1500k -bufsize:v:2 2100k \
-map a:0 -map a:0 -map a:0 \
-c:a aac -b:a 128k -ac 2 \
-preset veryfast -g 60 -keyint_min 60 -sc_threshold 0 \
-f hls -hls_time 4 -hls_playlist_type vod \
-hls_segment_filename 'stream_%v/seg_%03d.ts' \
-master_pl_name master.m3u8 \
-var_stream_map "v:0,a:0 v:1,a:1 v:2,a:2" \
'stream_%v/index.m3u8'
-var_stream_map groups are space-separated; each group lists comma-separated stream indexes for that rendition (v:N,a:N). %v in filenames expands to the variant index.
MPEG-DASH
ffmpeg -i in.mp4 \
-c:v libx264 -b:v 2500k -g 60 -keyint_min 60 -sc_threshold 0 \
-c:a aac -b:a 128k \
-f dash -seg_duration 4 -use_template 1 -use_timeline 1 \
-init_seg_name 'init-$RepresentationID$.m4s' \
-media_seg_name 'chunk-$RepresentationID$-$Number%05d$.m4s' \
manifest.mpd
SRT — listener (pull) and caller (push)
# Listener (server) — wait for a caller to connect
ffmpeg -re -i in.mp4 -c copy \
-f mpegts 'srt://0.0.0.0:9000?mode=listener&latency=120000'
# Caller (push) with passphrase (AES-128)
ffmpeg -re -i in.mp4 -c copy \
-f mpegts 'srt://receiver.example.com:9000?mode=caller&passphrase=secretpass123&pbkeylen=16&latency=120000'
latency is in microseconds (120000 = 120 ms). pbkeylen = 16/24/32 for AES-128/192/256. Passphrase must be 10–79 chars.
Tee muxer — one encode, many outputs
ffmpeg -i in.mp4 \
-c:v libx264 -preset veryfast -tune zerolatency \
-b:v 4500k -g 60 -keyint_min 60 -sc_threshold 0 \
-c:a aac -b:a 128k \
-map 0:v -map 0:a \
-f tee '[f=flv]rtmp://a.rtmp.youtube.com/live2/KEY|[f=hls:hls_time=4:hls_list_size=6:hls_flags=delete_segments]out.m3u8|[f=mpegts]archive.ts'
Escape | inside shell strings by quoting the whole tee spec. Per-sink options go inside [f=…:opt=val:opt=val].
Step 4 — Run + verify
- Print the ffmpeg command (the script prints before executing).
- Start the command; check stderr for
frame=,fps=,bitrate=,speed=.speedmust be ≥ 1× for live. - For HLS/DASH outputs: verify segments land on disk, open
.m3u8/.mpdin a player (ffplay, hls.js, dash.js, Safari). - For RTMP: the platform dashboard should show "live" within 10–30 s.
- For SRT: watch the stats printed by ffmpeg (RTT, lost pkts). If
lostgrows, raiselatency.
Available scripts
scripts/stream.py— subcommands:rtmp,hls-vod,hls-live,hls-abr,dash,srt-listener,srt-caller,tee. Stdlib only; prints the ffmpeg command, supports--dry-runand--verbose.
Workflow
- Pick the subcommand, then run:
```bash
uv run ${CLAUDE_SKILL_DIR}/scripts/stream.py <subcommand> --input INPUT [flags]
```
- Add
--dry-runfirst to print the command; run again without it to execute.