Created
September 8, 2020 08:47
-
-
Save sebseb7/0d312eeedd37d79164bbdeb41d3660dc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const app = express(); | |
const exec = require('child_process').exec; | |
const port = 3000; | |
var g_stats = {}; | |
var started = false; | |
var ffmpeg; | |
var lastline = ''; | |
var noline = 0; | |
app.get('/', (req, res) => { | |
res.send('<html><body>'+JSON.stringify(g_stats)+'<br/>'+started+':'+noline+':'+lastline+'</body></html') | |
}) | |
app.listen(port, () => { | |
console.log(`Example app listening at http://localhost:${port}`) | |
}) | |
setInterval(function() { | |
exec('/usr/share/speedify/speedify_cli stats 1 current', function(error, stdout, stderr){ | |
g_stats = {}; | |
for(const item of stdout.split("\n\n")){ | |
try { | |
const stats = JSON.parse(item); | |
if(stats[0] == "connection_stats") { | |
for(const conn of stats[1].connections){ | |
if(conn.adapterID != "speedify") { | |
//console.log(conn); | |
g_stats[conn.adapterID] = {'sent':conn.sendBps,'rcv':conn.receiveBps,'latency':conn.latencyMs,'conn':conn.connected}; | |
//console.log('updated'); | |
} | |
} | |
} | |
if(stats[0] == "session_stats") { | |
g_stats['session'] = {'sent':stats[1].current.bytesSent, 'minutes':stats[1].current.totalConnectedMinutes, 'failovers':stats[1].current.numFailovers}; | |
} | |
}catch(e){/*console.log(e)*/}; | |
} | |
}); | |
},10000); | |
const {spawn } = require('child_process'); | |
function start_ffmpeg() { | |
console.log('start'); | |
ffmpeg = spawn('/usr/bin/ffmpeg',[ | |
'-hide_banner','-re','-rtmp_live live','-c:v h264_mmal','-i rtmp://192.168.145.1/feed/1','-fflags +genpts+igndts+nobuffer+flush_packets','-vcodec h264_omx', | |
'-profile:v high','-minrate 850k','-maxrate 2000k','-b:v 2000k','-bufsize 1000k','-acodec copy','-f flv rtmp://greentv.rocks/feed/1' | |
],{shell: true}); | |
started = true; | |
noline = 0; | |
ffmpeg.stdout.on('data', (data) => { | |
console.log('OUT:'+data); | |
lastline = data; | |
noline = 0; | |
}); | |
ffmpeg.stderr.on('data', (data) => { | |
console.log('ERR:'+data); | |
lastline = data; | |
noline = 0; | |
}); | |
ffmpeg.on('close', (code) => { | |
started = false; | |
lastline = 'close'; | |
console.log('close'); | |
}); | |
ffmpeg.on('error', (code) => { | |
started = false; | |
lastline = 'close'; | |
console.log('error'); | |
}); | |
} | |
setInterval(function() { | |
noline++; | |
console.log(noline); | |
if(noline > 5) { | |
if(started){ | |
process.kill(ffmpeg.pid); | |
setTimeout(function(){exec('killall -KILL ffmpeg')},2000); | |
} | |
started=false; | |
} | |
if(noline > 10) { | |
if(started == false) | |
start_ffmpeg(); | |
noline = 0; | |
started = true; | |
} | |
},1000); | |
start_ffmpeg(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment