Skip to main content

desktopCapturer

访问有关可用于使用 navigator.mediaDevices.getUserMedia API 从桌面捕获音频和视频的媒体源的信息。

¥Access information about media sources that can be used to capture audio and video from the desktop using the navigator.mediaDevices.getUserMedia API.

进程:主进程

¥Process: Main

以下示例显示如何从标题为 Electron 的桌面窗口捕获视频:

¥The following example shows how to capture video from a desktop window whose title is Electron:

// main.js
const { app, BrowserWindow, desktopCapturer, session } = require('electron')

app.whenReady().then(() => {
const mainWindow = new BrowserWindow()

session.defaultSession.setDisplayMediaRequestHandler((request, callback) => {
desktopCapturer.getSources({ types: ['screen'] }).then((sources) => {
// Grant access to the first screen found.
callback({ video: sources[0], audio: 'loopback' })
})
// If true, use the system picker if available.
// Note: this is currently experimental. If the system picker
// is available, it will be used and the media request handler
// will not be invoked.
}, { useSystemPicker: true })

mainWindow.loadFile('index.html')
})
// renderer.js
const startButton = document.getElementById('startButton')
const stopButton = document.getElementById('stopButton')
const video = document.querySelector('video')

startButton.addEventListener('click', () => {
navigator.mediaDevices.getDisplayMedia({
audio: true,
video: {
width: 320,
height: 240,
frameRate: 30
}
}).then(stream => {
video.srcObject = stream
video.onloadedmetadata = (e) => video.play()
}).catch(e => console.log(e))
})

stopButton.addEventListener('click', () => {
video.pause()
})
<!-- index.html -->
<html>
<meta http-equiv="content-security-policy" content="script-src 'self' 'unsafe-inline'" />
<body>
<button id="startButton" class="button">Start</button>
<button id="stopButton" class="button">Stop</button>
<video width="320" height="240" autoplay></video>
<script src="renderer.js"></script>
</body>
</html>

请参阅 navigator.mediaDevices.getDisplayMedia 了解更多信息。

¥See navigator.mediaDevices.getDisplayMedia for more information.

注意:navigator.mediaDevices.getDisplayMedia 不允许使用 deviceId 选择源 - 见 specification

¥Note: navigator.mediaDevices.getDisplayMedia does not permit the use of deviceId for selection of a source - see specification.

方法

¥Methods

desktopCapturer 模块有以下方法:

¥The desktopCapturer module has the following methods:

desktopCapturer.getSources(options)

  • options 对象

    ¥options Object

    • types 字符串[] - 列出要捕获的桌面源类型的字符串数组,可用类型可以是 screenwindow

      ¥types string[] - An array of strings that lists the types of desktop sources to be captured, available types can be screen and window.

    • thumbnailSize 尺寸(可选) - 媒体源缩略图应缩放到的大小。默认值为 150 x 150。当不需要缩略图时,将宽度或高度设置为 0。这将节省捕获每个窗口和屏幕的内容所需的处理时间。

      ¥thumbnailSize Size (optional) - The size that the media source thumbnail should be scaled to. Default is 150 x 150. Set width or height to 0 when you do not need the thumbnails. This will save the processing time required for capturing the content of each window and screen.

    • fetchWindowIcons 布尔值(可选) - 设置为 true 以启用获取窗口图标。默认值为 false。当 false 时,源的 appIcon 属性返回 null。如果源具有类型屏幕,则相同。

      ¥fetchWindowIcons boolean (optional) - Set to true to enable fetching window icons. The default value is false. When false the appIcon property of the sources return null. Same if a source has the type screen.

返回 Promise<DesktopCapturerSource[]> - 使用 DesktopCapturerSource 对象数组进行解析,每个 DesktopCapturerSource 代表一个屏幕或可以捕获的单个窗口。

¥Returns Promise<DesktopCapturerSource[]> - Resolves with an array of DesktopCapturerSource objects, each DesktopCapturerSource represents a screen or an individual window that can be captured.

注意 在 macOS 10.15 Catalina 或更高版本上捕获屏幕内容需要用户同意,这可以被 systemPreferences.getMediaAccessStatus 检测到。

¥Note Capturing the screen contents requires user consent on macOS 10.15 Catalina or higher, which can detected by systemPreferences.getMediaAccessStatus.

注意事项

¥Caveats

navigator.mediaDevices.getUserMedia 无法在 macOS 上进行音频捕获,因为存在一个基本限制,即想要访问系统音频的应用需要 签名的内核扩展。Chromium 以及 Electron 并没有提供这一点。

¥navigator.mediaDevices.getUserMedia does not work on macOS for audio capture due to a fundamental limitation whereby apps that want to access the system's audio require a signed kernel extension. Chromium, and by extension Electron, does not provide this.

可以通过使用 Soundflower 等其他 macOS 应用捕获系统音频并将其通过虚拟音频输入设备传递来规避此限制。然后可以使用 navigator.mediaDevices.getUserMedia 查询该虚拟设备。

¥It is possible to circumvent this limitation by capturing system audio with another macOS app like Soundflower and passing it through a virtual audio input device. This virtual device can then be queried with navigator.mediaDevices.getUserMedia.