happy new year,
at the moment i dnt use github
what I am doing to the streamer is passing the rtsp recording to a websocket for the stream:
For this I use ffmpeg.exe with a code like the following:
ffmpeg -f rtsp -i "rtsp: // user: pass@192.168.xx: port / cameradirection" -vf scale = 320: 240 -f mpegts -codec: v mpeg1video -bf 0 -codec: a mp2 -r 30 http://127.0.0.1: 8081/password
[{"id":"18a7750b.88455b","type":"exec","z":"fdaddf2e.250df","command":"ffmpeg -f rtsp -i \"rtsp://\" -vf scale=320:240 -f mpegts -codec:v mpeg1video -bf 0 -codec:a mp2 -r 30 http://127.0.0.1:8081/password","addpay":false,"append":"","useSpawn":"true","timer":"","oldrc":false,"name":"Decode RTSP stream","x":560,"y":980,"wires":[["652938f2.df8da8"],["652938f2.df8da8"],["652938f2.df8da8"]]},{"id":"6bd761d4.db37b","type":"inject","z":"fdaddf2e.250df","name":"Start stream","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":980,"wires":[["18a7750b.88455b"]]},{"id":"a38dafa2.5778","type":"change","z":"fdaddf2e.250df","name":"","rules":[{"t":"set","p":"kill","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":370,"y":1020,"wires":[["18a7750b.88455b"]]},{"id":"a195c6d3.fd4378","type":"inject","z":"fdaddf2e.250df","name":"Stop all streams","topic":"","payload":"SIGTERM","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":1020,"wires":[["a38dafa2.5778"]]},{"id":"652938f2.df8da8","type":"debug","z":"fdaddf2e.250df","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":830,"y":1000,"wires":[]}]
I get "http://127.0.0.1: 8081/password" with the code that I found on the web:
var fs = require('fs'),
http = require('http'),
WebSocket = require('ws');
if (process.argv.length < 3) {
console.log(
'Usage: \n' +
'node websocket-relay.js <secret> [<stream-port> <websocket-port>]'
);
process.exit();
}
var STREAM_SECRET = process.argv[2],
STREAM_PORT = process.argv[3] || 8081,
WEBSOCKET_PORT = process.argv[4] || 8082,
RECORD_STREAM = true;
// Websocket Server
var socketServer = new WebSocket.Server({port: WEBSOCKET_PORT, perMessageDeflate: false});
socketServer.connectionCount = 0;
socketServer.on('connection', function(socket, upgradeReq) {
socketServer.connectionCount++;
console.log(
'New WebSocket Connection: ',
(upgradeReq || socket.upgradeReq).socket.remoteAddress,
(upgradeReq || socket.upgradeReq).headers['user-agent'],
'('+socketServer.connectionCount+' total)'
);
socket.on('close', function(code, message){
socketServer.connectionCount--;
console.log(
'Disconnected WebSocket ('+socketServer.connectionCount+' total)'
);
});
});
socketServer.broadcast = function(data) {
socketServer.clients.forEach(function each(client) {
if (client.readyState === WebSocket.OPEN) {
client.send(data);
}
});
};
// HTTP Server to accept incomming MPEG-TS Stream from ffmpeg
var streamServer = http.createServer( function(request, response) {
var params = request.url.substr(1).split('/');
if (params[0] !== STREAM_SECRET) {
console.log(
'Failed Stream Connection: '+ request.socket.remoteAddress + ':' +
request.socket.remotePort + ' - wrong secret.'
);
response.end();
}
response.connection.setTimeout(0);
console.log(
'Stream Connected: ' +
request.socket.remoteAddress + ':' +
request.socket.remotePort
);
request.on('data', function(data){
socketServer.broadcast(data);
if (request.socket.recording) {
request.socket.recording.write(data);
}
});
request.on('end',function(){
console.log('close');
if (request.socket.recording) {
request.socket.recording.close();
}
});
// Record the stream to a local file?
if (RECORD_STREAM) {
var path = "C:/Users/anxo-vilar/Desktop/rtspvideo/videostest.mp4"//'recordings/' + Date.now() + '.mp4';
request.socket.recording = fs.createWriteStream(path);
}
}).listen(STREAM_PORT);
console.log('Listening for incomming MPEG-TS Stream on http://127.0.0.1:'+STREAM_PORT+'/<secret>');
console.log('Awaiting WebSocket connections on ws://127.0.0.1:'+WEBSOCKET_PORT+'/');
this also gives me the websocket that I will use in my template node
This is where I use jsmpeg.js
[{"id":"eb71cfa0.28cf3","type":"http in","z":"fdaddf2e.250df","name":"","url":"/video","method":"get","upload":false,"swaggerDoc":"","x":140,"y":820,"wires":[["8d9361ec.50783"]]},{"id":"ff0a5078.64a7b","type":"http response","z":"fdaddf2e.250df","name":"","statusCode":"","headers":{},"x":490,"y":820,"wires":[]},{"id":"8d9361ec.50783","type":"template","z":"fdaddf2e.250df","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<!DOCTYPE html>\n<html>\n<head>\n\t<title>JSMpeg Stream Client</title>\n\t<style type=\"text/css\">\n\t\thtml, body {\n\t\t\tbackground-color: #111;\n\t\t\ttext-align: center;\n\t\t}\n\t</style>\n\t\n</head>\n<body>\n\t<canvas id=\"video-canvas\"></canvas>\n\t<script type=\"text/javascript\" src=\"lib/jsmpeg/jsmpeg.min.js\"></script>\n\t<script type=\"text/javascript\">\n\t\tvar canvas = document.getElementById('video-canvas');\n\t\tvar url = 'ws://127.0.0.1:8082/';\n\t\tvar player = new JSMpeg.Player(url, {canvas: canvas});\n\t</script>\n</body>\n</html>\n","output":"str","x":340,"y":820,"wires":[["ff0a5078.64a7b"]]}]
in summary: for the stream I use fmpeg.exe for an http address. then I use websocket and jsmpeg for visualization
For the recorder i use fluent-ffmpeg with the following code
var ffmpeg = require('fluent-ffmpeg');
var record=ffmpeg('rtsp://admin:C0P0Admin@192.168.1.242:554/cam/realmonitor?channel=1&subtype=0&unicast=true&proto=Onvif')
// .setDuration("0:05")
.on('end', function() { console.log('saved'); })
.on('progress', (progress) => {
console.log('Processing: ' + progress.targetSize + ' KB converted');
})
.on('error', function(err) { console.log('error: ' + err); })
.save('video/output3.mp4');
console.log (record)
/*const stop = (movie) => {
return movie.ffmpegProc.stdin.write('q');
*/
/*
if(x==5){
stop(record);}*/
U can see I was trying several things but I can't stop when I want to. I can only give it a predetermined duration
try to write a Q to stop the process but it doesn't work either