Explanation
This script will take 2 streams and show them as a picture in picture stream on 1280x720 resolution. All encoding is done through x264 and is software based, meaning the CPU will be used. An specific hardware encoder can be used instead should you want to update this.
Usage
This script is meant to be ran using the executable input mkv-exec
specifically as:
mkv-exec:/path/to/script.sh stream1 stream2
You will need to provide the stream names
within MistServer as stream1
, stream2
.
Need to knows
This script has the following dependency:
Behaviour is as follows:
- FFmpeg will ingest both streams locally & start
- Should any of the stream drop black will be shown
- This script does NOT recover sources should they drop
- Expected latency is ~3-10 seconds
- Sync is NOT guaranteed, a best effort will be done
Script
Make sure to save this in a location MistServer can reach & don't forget to make it executable through chmod +x script
#!/bin/bash
#This script grabs stream1 & stream2 and audio of stream1
#Output is 1280x720 with stream2 appearing as 480p
# all CPU based encoding
STREAM1=$1
STREAM2=$2
HTTPPORT=8080
ffmpeg -probesize 32 -max_probe_packets 1 -hide_banner -loglevel warning -f lavfi -i color=c=black:s=1280x720 -i http://localhost:${HTTPPORT}/${STREAM1}.mkv -i http://localhost:${HTTPPORT}/${STREAM2}.mkv -map 1:a:0 -filter_complex '[1:v]scale=-1:-1[s0];[2:v]scale=480:-1[s1];[0:v][s0]overlay=0:0[out];[out][s1]overlay=W-w-0:H-h-0[out];[out]scale=1280:720,setsar=1:1[out]' -map [out] -c:v h264 -force_key_frames source -cluster_time_limit 0 -b:v 5000k -preset veryfast -r:v 30 -c:a aac -f matroska -