nodejs 使用fluent-ffmpeg获取电脑摄像头数据进行推流
安装fluent-ffmpeg
npm install fluent-ffmpeg
设置ffmpeg路径
FFMPEG_PATH 加入环境变量中 或者使用Ffmpeg.setFfmpegPath(path)
使用案例
var outputh = 'rtsp://' + 'ip' + ':' + 'port' + '/' + textname; var ffmpegPath = "./ffmpeg_bin/ffmpeg.exe"; var ffmpeg = require('fluent-ffmpeg'); command = new ffmpeg('video=HD USB Camera') .setFfmpegPath(ffmpegPath) .inputOptions('-f dshow') .size('800x600') .on('start', function(commandLine) { console.log("start push......." + commandLine); console.log("start command......." + command); }) .on('end', function() { console.log("storp push........") stopPush(); }) .on('error', function(err, stdout, stderr) { console.log('error:' + err.message); console.log('stdout:' + stdout); console.log('stderr:' + stderr); stopPush(); }) .addOptions([ // '-preset veryfast', '-rtsp_transport tcp', '-f rtsp' ]) .pipe(outputh, { end: true });`
停止推流
根据说明使用kill函数。但是我测试的无效,会报错 所以使用不优雅的方式直接使用child_process进行杀掉进程 var child_process = require('child_process'); var chile = child_process.exec('"taskkill" /F /IM ffmpeg.exe* /T', function(error, stdout, stderr) { if (error) { console.error(`exec error: ${error}`); return; } console.log(`stdout: ${stdout}`); console.log(`stderr: ${stderr}`); });