让HTML5的MSE播放支持RTSP协议的摄像机视频流
为了让HTML的video tag支持播放RTSP视频流,可采用以下的解决方案进行处理:
Camera –Via RSTP–> ffmpeg –Encodes to WEBM–> Node.js –Via HTML5 Video–> Browser
通过ffmpeg将RTSP视频流转换成fragment mp4,命令如下:
ffmpeg -analyzeduration 0 -re -i rtsp://address -c:v copy -map 0 \
-f segment -reset_timestamps 0 -segment_time 3600 \
-movflags faststart stream%d.mp4
Node.js代码如下:
var request = require('request');
var http = require('http');
var child_process = require("child_process");
var stdouts = {};
http.createServer(function (req, resp) {
switch (params[0])
{
case "LIVE":
resp.writeHead(200, {'Content-Type': 'video/mp4', 'Connection': 'keep-alive'});
// Start ffmpeg
var ffmpeg = child_process.spawn("ffmpeg",[
"-analyzeduration", "0", "-re",
"-i","my_RSTP_URL", // Capture offset
"-c:v", "copy", "-map", "0",
"-f", "segment", "-reset_timestamps",
"0", "-segment_time", "3600",
"-movflags", "faststart",
"-" // Output to STDOUT
]);
ffmpeg.on('exit', function()
{
console.log('ffmpeg terminado');
});
ffmpeg.on('error',function(e)
{
console.log(e);
})
ffmpeg.stdout.on('data',function(data)
{
console.log('datos'+data);
});
ffmpeg.stderr.on('data', function(data) {
console.log('stderr'+data);
});
stdouts[params[1]] = ffmpeg.stdout;
// Pipe the video output to the client response
ffmpeg.stdout.pipe(resp);
console.log("Initializing camera");
break;
}
}).listen(8088);
console.log('Server running at port 8088');
有空可以测试测试效果。