Streaming with FFmpeg: RTMP Basics, HLS/DASH Packaging
Thu Aug 01 2024
Push live video to RTMP and package VOD streams into HLS and MPEG‑DASH, including multi‑variant ladders.
FFmpeg can both send live streams to a service and package VOD content for adaptive delivery. This guide covers safe defaults and gotchas.
RTMP Live Ingest
Use CBR‑ish settings and a GOP aligned to your target framerate (e.g., 2s at 60 fps → -g 120).
# RTMP live to a service (replace URL/KEY) ffmpeg -re -i input.mp4 -c:v libx264 -preset veryfast -b:v 4500k -maxrate 5000k -bufsize 10000k \ -g 120 -keyint_min 120 -pix_fmt yuv420p -c:a aac -b:a 160k -ar 44100 \ -f flv rtmp://live.example.com/app/STREAM_KEY
HLS Packaging
# Multi-variant HLS (720p + 480p) ffmpeg -i input.mp4 \ -filter:v:0 "scale=w=1280:h=-2" -c:v:0 libx264 -b:v:0 3000k -c:a:0 aac -b:a:0 128k \ -filter:v:1 "scale=w=854:h=-2" -c:v:1 libx264 -b:v:1 1500k -c:a:1 aac -b:a:1 96k \ -var_stream_map "v:0,a:0 v:1,a:1" -master_pl_name master.m3u8 \ -f hls -hls_time 6 -hls_flags independent_segments \ -hls_segment_filename "v%v/seg_%06d.ts" "v%v/index.m3u8"
MPEG‑DASH Packaging
ffmpeg -i input.mp4 -map 0:v -map 0:a -c:v libx264 -b:v 3000k -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
Pitfalls
- Keyframe interval mismatches break adaptive ladders.
- HLS/DASH hosting needs correct CORS headers.
With tuned GOPs and sensible bitrates, FFmpeg can produce robust live and VOD outputs.