Skip to content

How to Encode HLS Streams with FFmpeg

DodaTech Updated 2026-06-24 3 min read

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 -g and -keyint_min for HLS.
  • Pre-create the segments output directory.
  • Use -master_pl_name for multi-variant streams.
  • Test HLS with Apple's mediastreamvalidator tool.
  • Validate playlists with hls.js or native browser players.

Common Mistakes with hls encode

  1. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
  2. Using head and tail instead of pattern matching, causing runtime errors on empty lists
  3. 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

### What is `-hls_time` and how does it affect playback?

-hls_time 6 creates 6-second segments. Shorter segments (2-4s) reduce latency but increase server load and file count. Longer segments (10s+) improve compression but increase startup delay. 6 seconds is a good balance.

Why does my player show "Media Source Error"?

The player cannot parse the HLS stream. Common causes: missing or corrupted segments, incorrect playlist format, codecs not supported by the player (H.265 in HLS for older devices), or CORS issues.

How do I serve HLS from a web server?

Configure your server to serve .m3u8 with Content-Type: application/vnd.apple.mpegurl and .ts with Content-Type: video/MP2T. Enable CORS headers for web players: Access-Control-Allow-Origin: *.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro