ElectronJS captures the screen with low quality
Asked Answered
W

1

2

I am testing screen capture with ElectronJS. I can capture my screen but captured video has low quality than original.

  • Operating system: Linux Mint 20
  • Electron version: 11.1.0

Here is my code. I choose my screen and I display the captured screen in electron app by using video element. Some code here is irrelevant i marked with comment lines to make it clear but pasted the whole code in case you want to give it a try yourself.

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Hello World!</title>
  <meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';" />
  <style>
    body {
      margin: 0;
      overflow: hidden;
    }

    #vd {
      position: absolute;
      bottom: 300px;
    }
  </style>
</head>
<body style="background: white;">
  <video id="vd"></video>
  <button id="videoSelectBtn" class="button is-text">
    Choose a Video Source
  </button>
  <footer>
    <script>
      const { desktopCapturer, remote } = require('electron');
      const { Menu } = remote;

      // This part is not important. Just ignore the code here. It is not related with our problem. It just allows me to select my screen.

      const videoSelectBtn = document.getElementById('videoSelectBtn');
      videoSelectBtn.onclick = async () => {
        const inputSources = await desktopCapturer.getSources({
          types: ['screen']
        });

        const videoOptionsMenu = Menu.buildFromTemplate(
          inputSources.map(source => {
            return {
              label: source.name,
              click: () => selectSource(source)
            };
          })
        );

        videoOptionsMenu.popup();
      };

      // Important part starts here

      async function selectSource(source) {
        const stream = await navigator.mediaDevices
          .getUserMedia({
            audio: false,
            video: {
              mandatory: {
                chromeMediaSource: 'desktop',
                chromeMediaSourceId: source.id,
                minWidth: 1920,
                maxWidth: 1920,
                minHeight: 1080,
                maxHeight: 1080
              },
            }
          });

        const videoElement = document.getElementById('vd')

        videoElement.srcObject = stream;
        videoElement.play();
      }
    </script>
  </footer>
</body>
</html>

Here you can see the difference between my original screen and captured video. Difference is clearly visible if you focus opera icon.

difference between original and captured

Wandy answered 12/12, 2020 at 21:46 Comment(5)
I self answered this. Any help about how to get real screenshot with electronjs, not from a video capture is still welcome.Wandy
How did you resolve this ?Kary
I did not. Check my answer. If you can somehow capture a real screenshot instead of a video it might work.Wandy
There is a way in electron through which you can increase thumbnail size, did you try that?Kary
I don't remember trying anything like that. So probably not.Wandy
W
0

After a lot of time, when playing some games on geforce now I encountered the same problem. Red color is blurry. As you notice in the image, the biggest difference is in the opera icon which is also red. In electron the code in the question is not directly for screenshot. It's a workaround of getting screenshot from a video capture. So it's related with digital image compression and it's most probably something that cannot be fixed.

Read further: https://obsproject.com/forum/threads/red-color-blurry-when-recording.64851/

Wandy answered 29/11, 2021 at 23:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.