-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
91 lines (79 loc) · 2.58 KB
/
script.js
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
84
85
86
87
88
89
90
91
const durationElement = document.querySelector('#duration');
const videoElement = document.querySelector('video');
const recordButton = document.querySelector('#start-button');
const stopButton = document.querySelector('#stop-button');
let stream;
let recorder;
let duration = 0;
let interval;
function startRecording() {
navigator.mediaDevices.getDisplayMedia({ video: true }).then(function(s) {
// set the source of the video element to the stream from the screen
stream = s;
videoElement.srcObject = stream;
// create a MediaRecorder to record the screen in webm format
recorder = new MediaRecorder(stream, { mimeType: 'video/webm' });
const data = [];
videoElement.style.display = 'block';
recorder.ondataavailable = function(event) {
// store the recorded data in an array
data.push(event.data);
};
recorder.onstop = function() {
// download the recorded data as a webm file
const blob = new Blob(data, { type: 'video/webm' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.style.display = 'none';
a.href = url;
a.download = 'screen-recording.webm';
document.body.appendChild(a);
a.click();
setTimeout(function() {
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}, 100);
};
// start recording
startDurationCounter();
recorder.start();
// disable the record button and enable the stop button
recordButton.disabled = true;
stopButton.disabled = false;
});
}
function startDurationCounter() {
interval = setInterval(function() {
duration++;
let totalSeconds = duration;
hours = Math.floor(totalSeconds / 3600);
totalSeconds %= 3600;
minutes = Math.floor(totalSeconds / 60);
seconds = totalSeconds % 60;
if (hours.toString().length===1){
hours = '0'+hours
}
if (minutes.toString().length===1){
minutes = '0'+minutes
}if (seconds.toString().length===1){
seconds = '0'+seconds
}
durationElement.innerHTML = `Duration: ${hours}:${minutes}:${seconds} seconds`;
}, 1000);
}
function stopDurationCounter() {
clearInterval(interval);
durationElement.innerHTML = `video of ${duration} seconds saved.`;
duration = 0;
}
recordButton.addEventListener('click', startRecording);
stopButton.addEventListener('click', function() {
// stop recording
recorder.stop();
recordButton.disabled = false;
stopButton.disabled = true;
stopDurationCounter();
stream.getTracks().forEach(function(track) {
track.stop();
});
});