Skip to content

Instantly share code, notes, and snippets.

@ericxtang
Last active August 27, 2021 01:57
Show Gist options
  • Save ericxtang/cb00024e56a9e1e40b5ad209e6e664db to your computer and use it in GitHub Desktop.
Save ericxtang/cb00024e56a9e1e40b5ad209e6e664db to your computer and use it in GitHub Desktop.
Looping one or multiple VoD files into a never ending live stream. Using Livepeer as an example.

Looping VoD

This is a quick tutorial for looping a VoD file as a live stream, using ffmpeg.

Step 1: convert source video to .ts file

In order to loop a video file, you need to convert it into the .ts format. Use this command:

ffmpeg -i input.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts output.ts

Step 2: Loop the .ts video with ffmpeg

In order to loop the video, you will need to run this ffmpeg command in a bash script. This command ensures the timestamp properly increments during looping. Be sure to put in your own stream key.

#!/bin/bash

function mixem {
  while : ; do cat output.ts ; done
}

mixem | ffmpeg -f mpegts -re -i - -c:v h264 -b:v 2m -x264opts "keyint=50:min-keyint=50:no-scenecut" -c:a aac -preset veryfast -g 50 -map 0:v -map 0:a -f flv rtmp://rtmp.livepeer.com/live/{stream-key}

Optional: concatinate multiple source files

There are many tools available to concatinate multiple video files. One option is to use the follow commands:

cat input1.ts input2.ts input3.ts > all_input.ts

ffmpeg -i all_input.ts -vf scale=w=1280:h=720:force_original_aspect_ratio=decrease -c:a aac -ar 48000 -b:a 128k -c:v h264 -profile:v main -crf 20 -g 50 -keyint_min 50 -sc_threshold 0 -b:v 2500k -maxrate 2675k -bufsize 3750k -f mpegts all_output.ts

These commands makes sure the files are merged with uniform settings like resolution, frame rate, etc. The output can be put into the looping script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment