Skip to content

Instantly share code, notes, and snippets.

@felixrieseberg
Created August 22, 2018 03:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felixrieseberg/8552668cfd3cc32758a2b0a066e7c2fa to your computer and use it in GitHub Desktop.
Save felixrieseberg/8552668cfd3cc32758a2b0a066e7c2fa to your computer and use it in GitHub Desktop.
Electron Fiddle Gist
<!DOCTYPE html>
<html lang="en">
<head>
<title>Hi</title>
</head>
<body>
<video></video>
<script>
require('./renderer.js')
</script>
</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')
let mainWindow = null
app.on('ready', () => {
mainWindow = new BrowserWindow({ height: 600, width: 600 })
mainWindow.loadFile('index.html')
})
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.
//
desktopCapturer.getSources({
types: ['window', 'screen']
}, (error, sources) => {
if (error) throw error
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()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment