1,安装fluent-ffmpeg,npm install fluent-ffmpeg
2,推流代码:
//stream.js
const ffmpeg = require('fluent-ffmpeg');
const rtmpUrl = "rtmp://localhost:1935/live/desktop";
//ffmpeg -f gdigrab -i desktop -vcodec libx264 -preset:v ultrafast -tune:v zerolatency -f flv -pix_fmt yuv420p rtmp://localhost:1935/live/desktop
// 配置 ffmpeg
const command = ffmpeg()
.input('desktop') // 捕获桌面
.inputFormat('gdigrab') //windows
//.inputOptions([
// '-framerate 30',
// '-video_size 1920x1080'
//])
.outputOptions([
'-c:v libx264',
'-preset ultrafast',
'-tune zerolatency',
'-f flv',
'-pix_fmt yuv420p'
])
.output(rtmpUrl);
// 开始推流
command.run();
console.log('开始推流到:', rtmpUrl);