Skip to content

Channel from a file

The file input turns a local MP4 file into a proper live stream: the file plays in a loop. This is a production tool for filler channels: a "we'll be right back" technical channel, a promo channel, and a fallback when sources go down.

Configuration

streams:
  promo:
    inputs:
    - file:
        path: /storage/promo.mp4
    dvr: {}

The file can be multi-track: several video qualities and several audio tracks in one MP4 give a full MBR stream with language selection — with no transcoder on the server.

Preparing the file with ffmpeg

For an MBR file to play seamlessly, all qualities must be perfectly aligned: identical timestamps and keyframes in the same places. This is achieved with a single ffmpeg run using the split filter:

ffmpeg -y -i source.mkv \
  -filter_complex "\
[0:v:0]split=3[vhi][vme][vlo]; \
[vhi]scale=1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080:(ow-iw)/2:(oh-ih)/2,setsar=1,fps=25,setpts=PTS-STARTPTS[v1]; \
[vme]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2,setsar=1,fps=25,setpts=PTS-STARTPTS[v2]; \
[vlo]scale=960:540:force_original_aspect_ratio=decrease,pad=960:540:(ow-iw)/2:(oh-ih)/2,setsar=1,fps=25,setpts=PTS-STARTPTS[v3]" \
  -map "[v1]" -c:v:0 libx264 -b:v:0 6000k -g:v:0 100 -keyint_min:v:0 100 \
    -sc_threshold:v:0 0 -x264-params:v:0 "scenecut=0:open_gop=0" \
  -map "[v2]" -c:v:1 libx264 -b:v:1 3000k -g:v:1 100 -keyint_min:v:1 100 \
    -sc_threshold:v:1 0 -x264-params:v:1 "scenecut=0:open_gop=0" \
  -map "[v3]" -c:v:2 libx264 -b:v:2 1200k -g:v:2 100 -keyint_min:v:2 100 \
    -sc_threshold:v:2 0 -x264-params:v:2 "scenecut=0:open_gop=0" \
  -map 0:a:0 -c:a:0 aac -b:a:0 192k -ac 2 -ar 48000 \
  -metadata:s:v:0 title="1080p" -metadata:s:v:1 title="720p" -metadata:s:v:2 title="540p" \
  -metadata:s:a:0 language=eng \
  -movflags +faststart \
  promo.mp4

What matters here:

  • one run, one source, split — all qualities get identical timestamps;
  • setpts=PTS-STARTPTS — each quality's timeline starts at zero;
  • the same fps in all qualities;
  • rigid GOP: -g = -keyint_min, -sc_threshold 0 and scenecut=0:open_gop=0 — keyframes strictly every N frames in the same places; the encoder may not insert them on scene changes;
  • AAC audio at 48 kHz stereo;
  • -movflags +faststart — the index at the front of the file;
  • -metadata:s:v title=... / language=... — quality labels and track languages end up in the playlists.

A complete working example with multiple audio tracks lives in the sapsan repository: storage/king.sh.

Verification

Open http://server/streaming/v/promo/index.m3u8 — the master playlist must list all qualities, and switching between them must not stutter.

Next steps