-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
83 lines (65 loc) · 2.71 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<!DOCTYPE html>
<html>
<head>
<title>Screen Recorder</title>
</head>
<body>
<button id="startButton">Start Recording</button>
<button id="stopButton" disabled>Stop Recording</button>
<script>
let mediaRecorder;
let recordedChunks = [];
const startButton = document.getElementById('startButton');
const stopButton = document.getElementById('stopButton');
async function startRecording() {
try {
const stream = await navigator.mediaDevices.getDisplayMedia({ video: { mediaSource: 'screen', mimeType: 'video/webm; codecs=vp9' } });
console.log('video stream', stream);
mediaRecorder = new MediaRecorder(stream);
console.log('mediarecorder', mediaRecorder);
mediaRecorder.ondataavailable = (event) => {
console.log('recorded chunks', event.data);
if (event.data.size > 0) {
recordedChunks.push(event.data);
}
};
mediaRecorder.onstop = async () => {
const blob = new Blob(recordedChunks, { type: 'video/webm' });
console.log('blog', blob);
const formData = new FormData();
formData.append('video', blob, 'recorded-2.webm');
// code to download video from client side
// const url = URL.createObjectURL(blob);
// console.log('url', url);
// const a = document.createElement('a');
// a.href = url;
// a.download = 'screen_recording.webm';
// a.click();
recordedChunks = [];
fetch('http://localhost:3001/recording', {
method: 'POST',
body: formData
}).then(response => {
console.log('video uploaded ', response);
}).catch(error => {
});
};
mediaRecorder.start();
startButton.disabled = true;
stopButton.disabled = false;
} catch (error) {
console.error('Error accessing screen:', error);
}
}
function stopRecording() {
if (mediaRecorder.state !== 'inactive') {
mediaRecorder.stop();
startButton.disabled = false;
stopButton.disabled = true;
}
}
startButton.addEventListener('click', startRecording);
stopButton.addEventListener('click', stopRecording);
</script>
</body>
</html>