19 Oct 2017

[Blog] Setting up a transcoder

Hello everyone! It's been a while since our latest post due to the IBC2017, but we're working to catch up on that again.

This post will be about a question we've heard quite a bit over the last few years: "How do we use a transcoder with MistServer?", and today I will show you two ways to do this: our trigger system, and our up-and-coming ts-exec feature.

Trigger system

Using the trigger system, we can set up a script to execute whenever a new track is added, using the STREAM_TRACK_ADD trigger. This trigger contains as a payload the name of the stream, and the id of the track that is being added. The following script could serve as a starting point, taking a stream and pushing an extra quality in 720p, 1mbit:



read streamname
read tracknumber

echo -e "Trigger $1:\n${streamname} : --${tracknumber}-- ? --${vTrack}--\n" >> ${LOGDIR}/triggerLog

vTrack=`curl -s  http://${HOST}:${HTTPPORT}/info_${streamname}.js -o - | sed -n -e 's/mistvideo\[.*\] = {/{/gp' | jq .meta.tracks 2> /dev/null | grep "video_" | sed -e 's/.*_\(.*\)":.*/\1/g'`

if [ ${vTrack} == ${tracknumber} ]; then
  echo "Starting encode" >> ${LOGDIR}/triggerLog

  ffmpeg -i http://${HOST}:${HTTPPORT}/${streamname}.mp4?rate=0 -muxdelay 0 -c:v h264 -b:v 1M -s:v 1280x720 -an -f flv rtmp://${HOST}/push/${streamname} 2> ${LOGDIR}/ffmpeg_log
  • Lines 3-5: Set up the parameters used further on in the script, this would need to be the host and port that MistServer is available on, as well as a local directory where the logs will be kept.
  • Lines 7-8: Read the streamname and tracknumber from standard input, where our trigger system sends this data.
  • Line 10: Write the trigger data to the logfile.
  • Line 12: Use curl and jq to retrieve the stream info from mistserver, and extract the id of the video track(s) if any.
  • Line 14: Check if the added track is the only video track.
  • Line 15: Added the 'encode start' to the logfile
  • Line 17: Start ffmpeg to push to the same stream, adding tracks to the already available data. (See our older post for a detailed explanation on how to use ffmpeg)

The logfile is used mainly for debug purposes, if you decide to use this script in production, please make sure logging is removed, or handled properly.


Starting with our next release (2.13), we will make things even more straightforward with our ts-exec:// feature for push outputs. This feature allows for a script or program that can receive raw ts data over standard input to be started by MistServer, and combining this with the auto push system allows to start an encode the moment a new stream becomes available.

Using this feature to adapt a stream can run your encoder whenever needed, using ffmpeg for an example gives the following push target:

ts-exec://ffmpeg -i http://localhost:8080/${wildcard}.ts -copyts -muxdelay 0 -c:v:0 h264 -b:v:0 1M -s:v:0 1280x720 -an -f flv rtmp://localhost/push/${wildcard} 

That's all there really is to it. Ofcourse, starting multiple encodes to create a multibitrate stream, or coupling in a different encoder will require some tweaks. For more details and questions, feel free to contact us.

See you all next time, our next blog will come up in a couple of days.