Receive SRT Stream and Transcode to HLS Using FFmpeg

This article demonstrates how to securely ingest a Secure Reliable Transport (SRT) stream and transcode it into an HTTP Live Streaming (HLS) playlist using a single, efficient FFmpeg command. You will learn the specific command-line parameters required to handle SRT encryption and configure the HLS segmenter for optimal web playback.

To achieve this in a single step, FFmpeg must be compiled with both SRT support (--enable-libsrt) and your preferred video encoder (such as --enable-libx264).

The FFmpeg Command

Below is the complete, single-line command to receive an encrypted SRT stream as a listener and transcode it to HLS:

ffmpeg -i "srt://0.0.0.0:9000?mode=listener&passphrase=YourSecurePassword123&pbkeylen=32" \
-c:v libx264 -preset veryfast -g 60 -keyint_min 60 -sc_threshold 0 \
-c:a aac -b:a 128k \
-f hls -hls_time 4 -hls_list_size 10 -hls_flags delete_segments \
-hls_segment_filename "segment_%03d.ts" playlist.m3u8

Command Breakdown

1. Secure SRT Input Options

  • srt://0.0.0.0:9000: Tells FFmpeg to bind to port 9000 on all available network interfaces.
  • mode=listener: Sets FFmpeg as the receiving node waiting for an incoming connection from an SRT caller (sender). Change to mode=caller if you need to pull the stream from a remote server instead.
  • passphrase=YourSecurePassword123: The shared secret key used to decrypt the stream. This must match the sender's passphrase and be between 10 and 79 characters long.
  • pbkeylen=32: Defines the encryption key length in bytes. 32 corresponds to AES-256 encryption. Use 16 for AES-128.

2. Video and Audio Transcoding

  • -c:v libx264: Transcodes the incoming video to H.264, which is widely compatible with HLS.
  • -preset veryfast: Balances CPU usage and encoding quality for real-time streaming.
  • -g 60 -keyint_min 60 -sc_threshold 0: Forces a keyframe (I-frame) every 60 frames. At 30fps, this creates a keyframe every 2 seconds, which is crucial for clean HLS segment cuts.
  • -c:a aac -b:a 128k: Transcodes the audio to AAC at a bitrate of 128 kbps.

3. HLS Output Options

  • -f hls: Specifies the output format as HLS.
  • -hls_time 4: Sets target segment length to 4 seconds. For seamless playback, your keyframe interval must divide evenly into this duration.
  • -hls_list_size 10: Keeps only the last 10 segments in the .m3u8 playlist file to limit storage usage.
  • -hls_flags delete_segments: Automatically deletes old .ts files from disk as they fall off the playlist.
  • -hls_segment_filename "segment_%03d.ts": Defines the naming convention for the generated video chunks.
  • playlist.m3u8: The final output playlist file that will be read by HLS video players.