Skip to content

Instantly share code, notes, and snippets.

@sebseb7
Created October 12, 2020 22:50
Show Gist options
  • Save sebseb7/f96d410964ebbf977ae5e5ce2b86ba52 to your computer and use it in GitHub Desktop.
Save sebseb7/f96d410964ebbf977ae5e5ce2b86ba52 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
const spawn = require("child_process").spawn;
const input_rtmp = 'rtmp://127.0.0.1/feed/1';
const loop_rtmp = 'rtmp://127.0.0.1/feed/2';
const out_rtmp = 'rtmp://127.0.0.1/feed/3';
const timeout = 10; // in 1/10th of a second
var fallback=0;
function initInputStream(){
let toReturn = spawn("ffmpeg", ("-f live_flv -i "+input_rtmp+" -c copy -f mpegts -").split(" "));
toReturn.on("exit", (code) => { console.log("ffmpegIn exited with code " + code); ffmpegIn = initInputStream() });
toReturn.stdout.on("data", onData);
return toReturn;
}
function initLoopStream(){
let toReturn = spawn("ffmpeg", ("-f live_flv -i "+loop_rtmp+" -c copy -f mpegts -").split(" "));
toReturn.on("exit", (code) => { console.log("ffmpegIn exited with code " + code); ffmpegLoop = initLoopStream() });
toReturn.stdout.on("data", onDataLoop);
return toReturn;
}
const ffmpegOut = spawn("ffmpeg", ("-fflags +genpts -re -f mpegts -i - -c copy -acodec libmp3lame -ar 44100 -f flv "+out_rtmp).split(" "));
ffmpegOut.on("exit", (code) => { console.log("ffmpegOut exited with code "+code+"! Exiting..."); process.exit(code); });
var ffmpegIn = initInputStream();
var ffmpegLoop = initLoopStream();
function onData(videoData) {
console.log('sending input frames');
ffmpegOut.stdin.write(videoData);
fallback=0;
}
function onDataLoop(videoData) {
if(fallback > timeout){
console.log('sending loop frames');
ffmpegOut.stdin.write(videoData);
}
}
setInterval(function(){
fallback++;
}, 100);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment