Skip to content

Instantly share code, notes, and snippets.

@dkzeanah
Created July 14, 2022 02:39
Show Gist options
  • Save dkzeanah/4c4e5e67558b8975350db81b86dbfd56 to your computer and use it in GitHub Desktop.
Save dkzeanah/4c4e5e67558b8975350db81b86dbfd56 to your computer and use it in GitHub Desktop.
a test
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<video></video>
</body>
</html>
// Access information about media sources that can be used to capture audio
// and video from the desktop using the navigator.mediaDevices.getUserMedia API.
//
// For more info, see:
// https://electronjs.org/docs/api/desktop-capturer
const { app, BrowserWindow } = require('electron')
const path = require('path')
app.whenReady().then(() => {
const mainWindow = new BrowserWindow({
height: 600,
width: 600,
webPreferences: {
nodeIntegration: false, // default in Electron >= 5
contextIsolation: true, // default in Electron >= 12
preload: path.join(__dirname, 'preload.js')
}
})
mainWindow.loadFile('index.html')
})
{
"name": "chilly-rest-approve-m6yuf",
"productName": "chilly-rest-approve-m6yuf",
"description": "My Electron application description",
"keywords": [],
"main": "./main.js",
"version": "1.0.0",
"author": "admin",
"scripts": {
"start": "electron ."
},
"dependencies": {},
"devDependencies": {
"electron": "19.0.6"
}
}
const { desktopCapturer } = require('electron')
// The following example shows how to capture video from
// the screen. It also grabs each window, so you could
// just grab video from a single window.
//
function startCapture () {
desktopCapturer.getSources({
types: ['window', 'screen']
}).then(async sources => {
for (let i = 0; i < sources.length; ++i) {
console.log(sources[i])
if (sources[i].id.startsWith('screen')) {
navigator.mediaDevices.getUserMedia({
audio: false,
video: {
mandatory: {
chromeMediaSource: 'desktop',
chromeMediaSourceId: sources[i].id,
minWidth: 1280,
maxWidth: 1280,
minHeight: 720,
maxHeight: 720
}
}
}).then((stream) => handleStream(stream))
.catch((error) => console.log(error))
}
}
})
}
function handleStream (stream) {
const video = document.querySelector('video')
video.srcObject = stream
video.onloadedmetadata = (e) => video.play()
}
window.addEventListener('DOMContentLoaded', () => {
startCapture()
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment