目标
提供一个http接口,读取rtmp视频流并定时获取截屏图片,延迟要小
采用的工具
已经尝试的方法
设置ffmpeg参数vframes 1获取一帧图片,通过二进制流返回,测试使用的source为rtmp://live.hkstv.hk.lxdns.com/live/hks1。
capture2(source, res) {
ffmpeg(source)
.outputOptions(['-vframes 1'])
.videoCodec("png")
.on('error', function (err, stdout, stderr) {
log.error(err.message);
// log.info('stdout: ' + stdout); log.info('stderr: ' + stderr);
})
.on('end', function () {
console.log('Screenshots taken');
})
.output(res, { end: true }).format("image2").run();
}
遇到的问题
通过测试发现整个过程耗时较长。启动ffmpeg进程和销毁进程会占用大量资源,有没有有效的办法能够减少截屏的耗时?或者不采用ffmpeg,通过其他方式获取rtmp的截屏图片?
那为啥不干脆写个定时任务?
定时从rtmp流中截图,保存成jpg文件,然后需要显示缩略图的就直接读取本地的缓存图就完事了啊。这样不就避免了反复fork ffmpeg进程?