কিভাবে ব্যবহারকারীর স্ক্রীন রেকর্ড করবেন

ফ্রাঁসোয়া বিউফোর্ট
François Beaufort

স্ক্রিন ক্যাপচার API এর মাধ্যমে ওয়েব প্ল্যাটফর্মে ট্যাব, উইন্ডো এবং স্ক্রিন শেয়ার করা সম্ভব। getDisplayMedia() পদ্ধতি ব্যবহারকারীকে মিডিয়া স্ট্রিম হিসাবে ক্যাপচার করার জন্য একটি স্ক্রীন নির্বাচন করতে দেয়। এই স্ট্রীমটি তখন MediaRecorder API দিয়ে রেকর্ড করা যেতে পারে বা নেটওয়ার্কের মাধ্যমে অন্যদের সাথে শেয়ার করা যেতে পারে। showOpenFilePicker() পদ্ধতির মাধ্যমে রেকর্ডিং একটি স্থানীয় ফাইলে সংরক্ষণ করা যেতে পারে।

নীচের উদাহরণটি দেখায় কিভাবে আপনি WebM ফর্ম্যাটে ব্যবহারকারীর স্ক্রীন রেকর্ড করতে পারেন, একই পৃষ্ঠায় স্থানীয়ভাবে পূর্বরূপ দেখতে পারেন এবং ব্যবহারকারীর ফাইল সিস্টেমে রেকর্ডিং সংরক্ষণ করতে পারেন৷

let stream;
let recorder;

shareScreenButton.addEventListener("click", async () => {
  // Prompt the user to share their screen.
  stream = await navigator.mediaDevices.getDisplayMedia();
  recorder = new MediaRecorder(stream);
  // Preview the screen locally.
  video.srcObject = stream;
});

stopShareScreenButton.addEventListener("click", () => {
  // Stop the stream.
  stream.getTracks().forEach(track => track.stop());
  video.srcObject = null;
});

startRecordButton.addEventListener("click", async () => {
  // For the sake of more legible code, this sample only uses the
  // `showSaveFilePicker()` method. In production, you need to
  // cater for browsers that don't support this method, as
  // outlined in https://web.dev/patterns/files/save-a-file/.

  // Prompt the user to choose where to save the recording file.
  const suggestedName = "screen-recording.webm";
  const handle = await window.showSaveFilePicker({ suggestedName });
  const writable = await handle.createWritable();

  // Start recording.
  recorder.start();
  recorder.addEventListener("dataavailable", async (event) => {
    // Write chunks to the file.
    await writable.write(event.data);
    if (recorder.state === "inactive") {
      // Close the file when the recording stops.
      await writable.close();
    }
  });
});

stopRecordButton.addEventListener("click", () => {
  // Stop the recording.
  recorder.stop();
});

ব্রাউজার সমর্থন

MediaDevices.getDisplayMedia()

ব্রাউজার সমর্থন

  • 72
  • 79
  • 66
  • 13

উৎস

MediaRecorder API

ব্রাউজার সমর্থন

  • 47
  • 79
  • 25
  • 14.1

উৎস

ফাইল সিস্টেম অ্যাক্সেস API এর showSaveFilePicker()

ব্রাউজার সমর্থন

  • 86
  • 86
  • এক্স
  • এক্স

উৎস

আরও পড়া

ডেমো

এইচটিএমএল

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link
      rel="icon"
      href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>🖥️</text></svg>"
    />
    <title>How to record the user's screen</title>
  </head>
  <body>
    <h1>How to record the user's screen</h1>
    <button id="startShareScreenButton">Start sharing screen</button>
    <button id="stopShareScreenButton" disabled>Stop sharing screen</button>
    <button id="startRecordButton" disabled>Start recording</button>
    <button id="stopRecordButton" disabled>Stop recording</button>
    <video autoplay muted playsinline></video>
    <pre id="logs"></pre>
  </body>
</html>

সিএসএস


        :root {
  color-scheme: dark light;
}
html {
  box-sizing: border-box;
}
*,
*:before,
*:after {
  box-sizing: inherit;
}
body {
  margin: 1rem;
  font-family: system-ui, sans-serif;
}
button {
  display: block;
  margin-bottom: 4px;
}
video {
  display: block;
  margin-top: 10px;
  max-width: 100%;
  background: black;
  max-height: 480px;
}
pre {
  color: red;
  white-space: pre-line;
}
        

জেএস


        const video = document.querySelector('video');
const startShareScreenButton = document.querySelector('#startShareScreenButton');
const stopShareScreenButton = document.querySelector('#stopShareScreenButton');
const startRecordButton = document.querySelector('#startRecordButton');
const stopRecordButton = document.querySelector('#stopRecordButton');

let stream;
let recorder;

startShareScreenButton.addEventListener("click", async () => {
  // Prompt the user to share their screen.
  stream = await navigator.mediaDevices.getDisplayMedia();
  recorder = new MediaRecorder(stream);
  // Preview the screen locally.
  video.srcObject = stream;

  startRecordButton.disabled = false;
  stopShareScreenButton.disabled = false;
  log("Your screen is being shared.");
});

stopShareScreenButton.addEventListener("click", () => {
  // Stop the stream.
  stream.getTracks().forEach(track => track.stop());
  video.srcObject = null;

  stopShareScreenButton.disabled = true;
  startRecordButton.disabled = true;
  stopRecordButton.disabled = true;
  log("Your screen is not shared anymore.");
});

startRecordButton.addEventListener("click", async () => {
  // For the sake of more legible code, this sample only uses the
  // `showSaveFilePicker()` method. In production, you need to
  // cater for browsers that don't support this method, as
  // outlined in https://web.dev/patterns/files/save-a-file/.

  // Prompt the user to choose where to save the recording file.
  const suggestedName = "screen-recording.webm";
  const handle = await window.showSaveFilePicker({ suggestedName });
  const writable = await handle.createWritable();

  // Start recording.
  recorder.start();
  recorder.addEventListener("dataavailable", async (event) => {
    // Write chunks to the file.
    await writable.write(event.data);
    if (recorder.state === "inactive") {
      // Close the file when the recording stops.
      await writable.close();
    }
  });

  stopRecordButton.disabled = false;
  log("Your screen is being recorded locally.");
});

stopRecordButton.addEventListener("click", () => {
  // Stop the recording.
  recorder.stop();

  stopRecordButton.disabled = true;
  log("Your screen has been successfully recorded locally.");
});