<!DOCTYPE html>
<html>
<head>
<title>Screen Recorder</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
font-family: Arial, sans-serif;
}
video {
width: 100%;
height: auto;
}
.btn {
padding: 10px 20px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
font-size: 16px;
cursor: pointer;
}
.btn:hover {
background-color: #3e8e41;
}
</style>
</head>
<body>
<div id="video-container">
<video id="video-preview" autoplay muted></video>
</div>
<div id="controls-container">
<button id="start-btn" class="btn">Start Recording</button>
<button id="stop-btn" class="btn" disabled>Stop Recording</button>
</div>
<script>
let stream;
let recorder;
let videoPreview = document.getElementById("video-preview");
let startBtn = document.getElementById("start-btn");
let stopBtn = document.getElementById("stop-btn");
async function startRecording() {
stream = await navigator.mediaDevices.getDisplayMedia({
video: { mediaSource: "screen" },
audio: false
});
videoPreview.srcObject = stream;
videoPreview.play();
recorder = new MediaRecorder(stream);
let chunks = [];
recorder.ondataavailable = function(e) {
chunks.push(e.data);
};
recorder.onstop = function() {
let blob = new Blob(chunks, { type: "video/mp4" });
let videoUrl = URL.createObjectURL(blob);
let downloadLink = document.createElement("a");
downloadLink.href = videoUrl;
downloadLink.download = "recording.mp4";
downloadLink.click();
};
recorder.start();
startBtn.disabled = true;
stopBtn.disabled = false;
}
function stopRecording() {
recorder.stop();
stream.getTracks().forEach(track => track.stop());
startBtn.disabled = false;
stopBtn.disabled = true;
}
startBtn.addEventListener("click", startRecording);
stopBtn.addEventListener("click", stopRecording);
</script>
</body>
</html>
Comments
Post a Comment