How to Encode HLS Streams with FFmpeg
In this tutorial, you'll learn about How to Encode HLS Streams with FFmpeg. We cover key concepts, practical examples, and best practices.
The Problem
Your FFmpeg HLS output has broken segments, the .m3u8 playlist is missing, segments are not aligned, or players cannot stream the video. HLS (HTTP Live Streaming) requires precise segment boundaries and proper playlist generation.
Quick Fix
Fix 1: Basic HLS Conversion
WRONG — converting directly without HLS settings:
ffmpeg -i input.mp4 output.m3u8
# (produces a broken playlist or single-segment output)
RIGHT — use the HLS muxer:
ffmpeg -i input.mp4 -c:v libx264 -c:a aac -f hls \
-hls_time 6 -hls_list_size 10 -hls_segment_filename "segments/%03d.ts" \
output.m3u8
# Created: output.m3u8, segments/000.ts, segments/001.ts, ...
Fix 2: Keyframe Alignment for HLS
WRONG — no forced keyframes — HLS segments start at random frames:
ffmpeg -i input.mp4 -c:v libx264 -f hls output.m3u8
# (segments do not start on keyframes — player shows artifacts)
RIGHT — force keyframes at segment boundaries:
ffmpeg -i input.mp4 -c:v libx264 -f hls \
-hls_time 6 -g 60 -keyint_min 60 \
-sc_threshold 0 -hls_segment_filename "segments/%03d.ts" \
output.m3u8
-g 60 sets GOP size to 60 frames (2 seconds at 30fps). -sc_threshold 0 disables scene-cut detection.
Fix 3: Adaptive Bitrate (Multiple Variants)
ffmpeg -i input.mp4 \
-filter_complex \
"[0:v]split=3[v1][v2][v3]; \
[v1]scale=1920:1080[v1out]; \
[v2]scale=1280:720[v2out]; \
[v3]scale=854:480[v3out]" \
-map "[v1out]" -map 0:a -c:v libx264 -b:v:0 5000k -maxrate:v:0 7500k -bufsize:v:0 10000k \
-map "[v2out]" -map 0:a -c:v libx264 -b:v:1 2500k -maxrate:v:1 3750k -bufsize:v:1 5000k \
-map "[v3out]" -map 0:a -c:v libx264 -b:v:2 1000k -maxrate:v:2 1500k -bufsize:v:2 2000k \
-f hls -hls_time 6 -master_pl_name "master.m3u8" \
-var_stream_map "v:0,a:0 v:1,a:0 v:2,a:0" \
"stream_%v/output.m3u8"
Fix 4: Segment Not Found / Playlist Empty
# Error: output.m3u8 is empty
RIGHT — ensure the output directory exists:
mkdir -p segments
ffmpeg -i input.mp4 -f hls -hls_segment_filename "segments/%03d.ts" output.m3u8
Fix 5: HLS for Live Streams
ffmpeg -i rtsp://camera:554/stream -c:v libx264 -f hls \
-hls_time 2 -hls_list_size 5 -hls_flags delete_segments+append_list \
-hls_segment_filename "live/%03d.ts" live/stream.m3u8
Fix 6: HLS Encryption
openssl rand 16 > enc.key
# Create key info file:
echo "https://example.com/enc.key" > enc.keyinfo
echo "./enc.key" >> enc.keyinfo
echo "$(openssl rand -hex 16)" >> enc.keyinfo
ffmpeg -i input.mp4 -c:v libx264 -f hls \
-hls_key_info_file enc.keyinfo output.m3u8
Use DodaTech's Stream Validator to check HLS playlists, verify segment availability, and test playback across different browsers.
Prevention
- Always force keyframes with
-gand-keyint_minfor HLS. - Pre-create the segments output directory.
- Use
-master_pl_namefor multi-variant streams. - Test HLS with Apple's
mediastreamvalidatortool. - Validate playlists with
hls.jsor native browser players.
Common Mistakes with hls encode
- Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists - Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
These mistakes appear frequently in real-world FFMPEG code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro