electron + fluent-ffmpeg推送桌面、音视频、窗口

1、桌面

ffmpeg()
.input('desktop') // 推送桌面配置
.inputFormat('gdigrab')
.inputOptions('-offset_x 0')
.inputOptions('-offset_y 0')
.inputOptions('-video_size 640x480')

2、音视频

// video和audio是摄像头和麦克风名称,
// 可通过navigator.mediaDevices.enumerateDevices
// 或者ffmpeg命令获取
.input(`video=${video[0]}`) // 推送摄像头
.inputFormat('dshow')
.inputOptions('-s 320*180')
.complexFilter([{
	filter: 'overlay',
	options: {
		x: 'main_w-overlay_w',
		y: 'main_h-overlay_h'
	},
	inputs: ['[0:v]', '[1:v]']
}])
.input(`audio=${audio[0]}`) // 推送音频
.inputFormat('dshow')

3、窗口

// title是窗口的名称
// desktopCapturer.getSources({types: ['window', 'screen']})获取
.input(`title=${title}`) // 推送桌面配置
.inputFormat('gdigrab')
.inputOptions('-offset_x 0')
.inputOptions('-offset_y 0')
.inputOptions('-video_size 640x480')

4、获取设备名称

// 先获取授权 否则获取的devices名称都是空的
navigator.mediaDevices.getUserMedia({audio: true,video:true});
navigator.mediaDevices.enumerateDevices().then((devices)=> {
});
import ffmpegPath from '@ffmpeg-installer/ffmpeg';
import cp from "child_process";

/**
 * 获取设备列表
 * @returns {Promise}
 */
const devices = () => {
	return new Promise((resolve, reject) => {
		// 视频设备
		const videoList = [];
		// 音频设备
		const audioList = [];
		// 视频设备标记
		let videoFlag = false;
		// 音频设备标记
		let audioFlag = false;
		const devicesCmd = cp.spawn(ffmpegPath.path, ["-f", "dshow", "-list_devices", "true", "-i", "dummy"]);
		let devices = '';
		devicesCmd.stderr.on("data", data => {
			devices += data.toString();
		});
		devicesCmd.on("close", code => {
			// 换行符
			const splitFlag = (process.platform === 'win32' ? '\r\n' : '\n')
			const lines = devices.split(splitFlag);
			for (let i = 0; i < lines.length; i++) {
				const device = lines[i]
				if (device.indexOf('[dshow') > -1) {
					if (device.indexOf('DirectShow video devices') > -1) {
						videoFlag = true;
						audioFlag = false;
					} else if (device.indexOf('DirectShow audio devices') > -1) {
						videoFlag = false;
						audioFlag = true;
					} else if (video_device_name_flag && device.indexOf('Alternative name') == -1) {
						if (device.match(/\"(.*)\"/)) {
							const videoName = device.match(/\"(.*)\"/)[1]
							videoList.push(videoName);
						}
					} else if (audioFlag && device.indexOf('Alternative name') == -1) {
						if (device.match(/\"(.*)\"/)) {
							let audioNname = device.match(/\"(.*)\"/)[1];
							audioList.push(audioNname);
						}
					}
				}
			}
			resolve({
				audio: audioList,
				video: videoList
			})
		});
	})
};

export default devices;

你可能感兴趣的:(#,Electron,react.js,javascript,ecmascript)