想把 rtsp 的直播流转成 一帧一帧的图片
通过命令行可以实现
ffmpeg -i "rtsp://admin:[email protected]/main/Channels/" -y -f image2 img%03d.jpg
在 nodejs 中使用 fluent-ffmpeg 实现了
const ffmpeg = require('fluent-ffmpeg')
const fs = require('fs')
let ffmpegPath = 'ffmpeg/ffmpeg.exe'
ffmpeg.setFfmpegPath(ffmpegPath)
let url = 'rtsp://admin:[email protected]/main/Channels/'
// ffmpeg -i "rtsp://admin:[email protected]/main/Channels/" -y -f image2 img%03d.jpg
ffmpeg(url)
.addInputOption('-rtsp_transport', 'tcp', '-buffer_size', '102400') // 这里可以添加一些 RTSP 优化的参数
// .outputOptions(['-vframes 1', '-fflags nobuffer', '-tune zerolatency']) // 每帧一张图
.videoCodec('png') // 输出 PNG 格式图片
.on('error', function (err, stdout, stderr) {
console.log('错误信息', err.message);
})
.on('end', function () {
console.log('结束' ,'Screenshots taken');
})
.on('codecData', function () {
console.log('codecData 事件', 'Stream codecData.')
})
.output('img%03d.jpg').format("image2").noAudio().run()
但是上面两种方式都会直接生成图片 有没有办法把图片的 buffer 获取到 不写入文件中
我在用 pipe 方式可以获取到图片流 但是只有第一张图片 然后就报错了
const ffmpeg = require('fluent-ffmpeg')
const fs = require('fs')
let ffmpegPath = 'ffmpeg/ffmpeg.exe'
ffmpeg.setFfmpegPath(ffmpegPath)
let url = 'rtsp://admin:[email protected]/main/Channels/'
// ffmpeg -i "rtsp://admin:[email protected]/main/Channels/" -y -f image2 img%03d.jpg
let img = fs.createWriteStream('img.png')
ffmpeg(url)
.addInputOption('-rtsp_transport', 'tcp', '-buffer_size', '102400') // 这里可以添加一些 RTSP 优化的参数
.videoCodec('png') // 输出 PNG 格式图片
.format("image2").noAudio().pipe().on('data', function(chunk) {
img.write(chunk)
console.log('ffmpeg just wrote ' + chunk.length + ' bytes');
});
控制台信息
$ node index.js
codecData 事件 Stream codecData.
ffmpeg just wrote 98304 bytes
ffmpeg just wrote 98304 bytes
ffmpeg just wrote 98304 bytes
ffmpeg just wrote 98304 bytes
ffmpeg just wrote 98304 bytes
ffmpeg just wrote 16017 bytes
错误信息 ffmpeg exited with code 1: av_interleaved_write_frame(): Invalid argument
frame= 10 fps=0.0 q=-0.0 Lsize=N/A time=00:00:00.08 bitrate=N/A speed=0.368x
video:1003kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown
Conversion failed!
1
yangheng4922 OP 我在用 pipe 方式可以获取到图片流 但是只有第一张图片 然后就报错了
``` const ffmpeg = require('fluent-ffmpeg') const fs = require('fs') let ffmpegPath = 'ffmpeg/ffmpeg.exe' ffmpeg.setFfmpegPath(ffmpegPath) let url = 'rtsp://admin:[email protected]/main/Channels/' // ffmpeg -i "rtsp://admin:[email protected]/main/Channels/" -y -f image2 img%03d.jpg let img = fs.createWriteStream('img.png') ffmpeg(url) .addInputOption('-rtsp_transport', 'tcp', '-buffer_size', '102400') // 这里可以添加一些 RTSP 优化的参数 // .outputOptions(['-vframes 1', '-fflags nobuffer', '-tune zerolatency']) // 每帧一张图 .videoCodec('png') // 输出 PNG 格式图片 .on('error', function (err, stdout, stderr) { console.log('错误信息', err.message); }) .on('end', function () { console.log('结束' ,'Screenshots taken'); }) .on('codecData', function () { console.log('codecData 事件', 'Stream codecData.') }) .format("image2").noAudio().pipe().on('data', function(chunk) { img.write(chunk) console.log('ffmpeg just wrote ' + chunk.length + ' bytes'); }); ``` 控制台信息 ``` $ node index.js codecData 事件 Stream codecData. ffmpeg just wrote 98304 bytes ffmpeg just wrote 98304 bytes ffmpeg just wrote 98304 bytes ffmpeg just wrote 98304 bytes ffmpeg just wrote 98304 bytes ffmpeg just wrote 16017 bytes 错误信息 ffmpeg exited with code 1: av_interleaved_write_frame(): Invalid argument frame= 10 fps=0.0 q=-0.0 Lsize=N/A time=00:00:00.08 bitrate=N/A speed=0.368x video:1003kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown Conversion failed! ``` |