安装
代码语言:javascript复制npm install --save fluent-ffmpeg
或者添加
代码语言:javascript复制"dependencies": {
"fluent-ffmpeg": "^2.1.2"
}
之后
代码语言:javascript复制npm install
视频文件推流
引用及变量
代码语言:javascript复制const ffmpeg = require('fluent-ffmpeg');
const ffmpegPath = "/Users/zhangjian/psvmc/app/me/electron/zjclass/libs/ffmpeg_mac/ffmpeg";
const inputPath = '/Users/zhangjian/psvmc/app/me/electron/zjclass/libs/mv.mp4';
const logo = '/Users/zhangjian/psvmc/app/me/electron/zjclass/libs/logo.png'; //水印
const outputPath = 'rtmp://119.3.212.205:1935/live/xiaoming';
具体的推流代码
代码语言:javascript复制var command = ffmpeg(inputPath)
.setFfmpegPath(ffmpegPath)
.inputOptions('-re')
.inputOptions('-ac 2')
.addInput(logo)
.complexFilter([{
filter: 'scale',
options: [600, -1],
inputs: '[0:v]',
outputs: 'video'
}, {
filter: 'scale',
options: [60, -1],
inputs: '[1:v]',
outputs: 'logo'
}, {
filter: 'overlay',
options: {
x: 'main_w-overlay_w-15',
y: 15
},
inputs: ['video', 'logo']
}])
.on('start', function (commandLine) {
console.log('[' new Date() '] Vedio is Pushing !');
console.log('commandLine: ' commandLine);
})
.on('error', function (err, stdout, stderr) {
console.log('error: ' err.message);
console.log('stdout: ' stdout);
console.log('stderr: ' stderr);
})
.on('end', function () {
console.log('[' new Date() '] Vedio Pushing is Finished !');
})
.addOptions([
'-vcodec libx264',
'-preset veryfast',
'-crf 22',
'-maxrate 1000k',
'-bufsize 3000k',
'-acodec libmp3lame',
'-ac 2',
'-ar 44100',
'-b:a 96k'
])
.format('flv');
command
.output(outputPath, {
end: true
})
.run();
桌面及麦克风推流(MAC)
代码语言:javascript复制const ffmpeg = require('fluent-ffmpeg');
const ffmpegPath = "/Users/zhangjian/psvmc/app/me/electron/zjclass/libs/ffmpeg_mac/ffmpeg";
const outputPath = 'rtmp://119.3.212.205:1935/live/xiaoming';
let audioStream;
function getAudioStream() {
navigator.mediaDevices.getUserMedia({audio: true, video: false})
.then(function (stream) {
audioStream = stream;
pushStream();
stream.onended = () => {
console.log('Micro audio ended.')
}
})
.catch(function (error) {
console.log('getUserMedia() failed.')
});
}
var command;
function pushStream() {
command = ffmpeg("1:0")
.setFfmpegPath(ffmpegPath)
.inputOptions('-f avfoundation')
.inputOptions('-framerate 30')
.inputOptions('-video_size 640x480')
.on('start', function (commandLine) {
console.log('[' new Date() '] Vedio is Pushing !');
console.log('commandLine: ' commandLine);
})
.on('error', function (err, stdout, stderr) {
console.log('error: ' err.message);
console.log('stdout: ' stdout);
console.log('stderr: ' stderr);
})
.on('end', function () {
console.log('[' new Date() '] Vedio Pushing is Finished !');
})
.addOptions([
'-vcodec libx264',
'-preset ultrafast',
'-acodec libmp3lame'
])
.format('flv');
command
.output(outputPath, {
end: true
})
.run();
}
document.querySelector(".start_action").addEventListener("click", function () {
// 只是获取麦克风录制权限
getAudioStream();
});
document.querySelector(".stop_action").addEventListener("click", function () {
if(command){
command.kill();
}
});
桌面 麦克风推流(Win)
代码语言:javascript复制const ffmpeg = require('fluent-ffmpeg');
const ffmpegPath = __dirname "\..\libs\ffmpeg.exe";
let command = null;
command = ffmpeg()
.setFfmpegPath(ffmpegPath)
.input('desktop')
.inputFormat('gdigrab')
.input('audio=麦克风 (2- High Definition Audio 设备)')
.inputFormat('dshow')
.addOptions([
'-vcodec libx264',
'-preset ultrafast',
'-acodec libmp3lame',
'-pix_fmt yuv422p'
])
.format('flv')
.output(outputPath, {
end: true
})
.on('start', function (commandLine) {
console.log('[' new Date() '] Vedio is Pushing !');
console.log('commandLine: ' commandLine);
})
.on('error', function (err, stdout, stderr) {
console.log('error: ' err.message);
})
.on('end', function () {
console.log('[' new Date() '] Vedio Pushing is Finished !');
});
command.run();
注意audio=麦克风 (2- High Definition Audio 设备)后面的中文名称不要用双引号括起来,括起来后推流是报错的! 引用的文件路径中的反斜杠也要注意。
这样其实生成的命令如下
代码语言:javascript复制ffmpeg -f gdigrab -i desktop -f dshow -i audio=麦克风 (2- High Definition Audio 设备) -vcodec libx264 -preset ultrafast -acodec libmp3lame -pix_fmt yuv422p -f flv rtmp://39.104.23.195:1935/live/xiaoming
但是这样的命令直接在cmd中无法运行 要把麦克风名字用双引号括起来才能运行
代码语言:javascript复制ffmpeg -f gdigrab -i desktop -f dshow -i audio="麦克风 (2- High Definition Audio 设备)" -vcodec libx264 -preset ultrafast -acodec libmp3lame -pix_fmt yuv422p -f flv rtmp://39.104.23.195:1935/live/xiaoming
这两个地方不一样真是折腾死我了,花了大半天才找到原因。