electron录制工具-desktopCapturer录屏

需求

录屏状态时,屏幕底部有个计时器,点击计时器停止录屏,跳转录屏结束的视频播放。

效果如下

electron-录屏演示

实现

计时器

创建一个浮窗,根据x、y坐标移动窗口的位置;

复制代码
	// 获取屏幕的主显示器信息
	const { width, height } = screen.getPrimaryDisplay().workAreaSize;

	// 设置窗口的宽度和高度
	const windowWidth = 120;
	const windowHeight = 120;
	// app.commandLine.appendSwitch('disable-features', 'IOSurfaceCapturer,DesktopCaptureMacV2')
	recordWin = new BrowserWindow({
		width: windowWidth,
		height: windowHeight,
		x: width / 2 - windowWidth / 2,
		y: height - windowHeight,
		frame: true, // 无边框
		transparent: true, // 透明窗口
		alwaysOnTop: true, // 窗口总是显示在最前面
		webPreferences: {
			preload: path.join(__dirname, 'preload.mjs'),
		},
	})

为了时间准确,使用时间戳计算录屏时长。

复制代码
const updateDuration = () => {
			if (!recordRef.current.status) return
			setDuration(formatSecondsToHMS((new Date().getTime() - recordRef.current.startTime) / 1000))
			requestAnimationFrame(updateDuration)
		}

录屏

electron提供了desktopCapturer 共享屏幕接口,渲染进程通过 navigator.mediaDevices.getUserMediaMediaRecorder 结合获取录制屏幕的blob

复制代码
// main.js
		desktopCapturer.getSources({ types: ['screen'] }).then(() => {
			recordWin?.webContents.send('start_record_main', 'screen:1:0')
			// 可以选择不同的窗口
			// for (const source of sources) {
			// 	if (source.name === 'Electron') {
			// 		maskWin?.webContents.send('SET_SOURCE', source.id)
			// 		return
			// 	}
			// }
		})

// render.js

const mediaStream = await navigator.mediaDevices.getUserMedia({
					audio: false,
					video: {
						mandatory: {
							chromeMediaSource: 'desktop',
							chromeMediaSourceId: sourceId,
						}
					}
				})
			
				const options = { mimeType: 'video/webm; codecs=vp9' }; 
				const mediaRecorder = new MediaRecorder(mediaStream, options);

				mediaRecorder.onstart = () => {
						recordRef.current.recordedChunks = [];
						recordRef.current.status = true
						updateDuration()
				}
				mediaRecorder.ondataavailable = function (event) {
						if (event.data.size > 0) {
							recordRef.current.recordedChunks.push(event.data);
						}
				};
		
				mediaRecorder.onstop = async () => {
						const blob = new Blob(recordRef.current.recordedChunks, { type: 'video/webm' });
						const url = URL.createObjectURL(blob);
						// downloadVideo(url);
						recordRef.current.recordedChunks = [];
						recordRef.current.status = false
						window.ipcRenderer.send('stop_record_render', url)
				};
				mediaRecorder.onerror = (event) => {
					console.error("MediaRecorder error:", event);
				};
				mediaRecorder.start();

跳转视频编辑页面

复制代码
	window.ipcRenderer.send('stop_record_render', url)

注:录屏也可以使用ffmpeg

代码

相关推荐
美酒没故事°2 分钟前
npm源管理器:nrm
前端·npm·npm源
用户22152044278003 分钟前
vue3组件间的通讯方式
前端·vue.js
三十_A20 分钟前
【实录】使用 patch-package 修复第三方 npm 包中的 Bug
前端·npm·bug
下位子28 分钟前
『AI 编程』用 Claude Code 从零到一开发全栈减脂追踪应用
前端·ai编程·claude
tyro曹仓舒28 分钟前
Vue单文件组件到底需不需要写name
前端·vue.js
用户479492835691529 分钟前
面试官:讲讲2FA 双因素认证原理
前端·后端·安全
乐影29 分钟前
TS 模板字符串类型:从基础到进阶的类型编程魔法
前端·typescript
龙在天31 分钟前
CSS 属性值的计算与过程
前端
云鹤_31 分钟前
【Amis源码阅读】组件注册方法远比预想的多!
前端·低代码
xinfei33 分钟前
ES6 新特性 从 ECMAScript 2015(ES6)到 ECMAScript 2025
前端