Secure SRT to HLS Transcoding with FFmpeg

This guide explains how to ingest an encrypted Secure Reliable Transport (SRT) feed and transcode it into an HTTP Live Streaming (HLS) output using a single FFmpeg command. By configuring FFmpeg as an SRT listener with pre-shared key encryption, you can safely receive live video over public networks and immediately package it for low-latency web playback.

Prerequisites

Ensure your FFmpeg installation is compiled with SRT support (--enable-libsrt) and H.264 encoding support (--enable-libx264).

The FFmpeg Command

Execute the following command in your terminal:

ffmpeg -re -i "srt://0.0.0.0:9998?mode=listener&passphrase=YourStrongPassphrase&pbkeylen=32" \
  -c:v libx264 -preset veryfast -b:v 3000k -maxrate 3500k -bufsize 6000k \
  -g 60 -keyint_min 60 -sc_threshold 0 \
  -c:a aac -b:a 128k -ar 44100 \
  -f hls \
  -hls_time 2 \
  -hls_list_size 5 \
  -hls_flags delete_segments+append_list \
  -hls_segment_filename "/var/www/html/hls/stream_%03d.ts" \
  /var/www/html/hls/stream.m3u8

Command Parameter Breakdown

1. Secure SRT Ingest Parameters

  • srt://0.0.0.0:9998: Binds the SRT receiver to all local network interfaces on port 9998.
  • mode=listener: Sets FFmpeg to listen for incoming connections rather than initiating one (caller mode).
  • passphrase=YourStrongPassphrase: The shared secret required for decrypting the incoming stream (must be 10 to 79 characters).
  • pbkeylen=32: Defines the encryption key length. Options are 16 (AES-128), 24 (AES-192), or 32 (AES-256). Both sender and receiver must match.

2. Video and Audio Encoding

  • -c:v libx264: Encodes the video track using H.264, standard for HLS compatibility. If the incoming SRT stream is already H.264, you can replace -c:v libx264 and bitrate settings with -c:v copy to save CPU resources.
  • -g 60 -keyint_min 60 -sc_threshold 0: Enforces a fixed Group of Pictures (GOP) size. For a 30 fps stream, this ensures an I-frame every 2 seconds, which aligns with segment splitting.
  • -c:a aac -b:a 128k: Encodes audio to AAC format at 128 kbps.

3. HLS Packaging Parameters

  • -f hls: Specifies the HLS output muxer.
  • -hls_time 2: Target duration for each HLS segment in seconds.
  • -hls_list_size 5: Maintains the 5 most recent segments in the playlist file for live streaming.
  • -hls_flags delete_segments: Automatically deletes older .ts files from disk when they fall off the playlist to conserve storage.
  • -hls_segment_filename: Specifies the naming pattern and storage path for individual transport stream chunks.