Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
104 lines (85 loc) · 2.76 KB
/
Copy pathscript.js
File metadata and controls
104 lines (85 loc) · 2.76 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
let stream = null,
audio = null,
mixedStream = null,
chunks = [],
recorder = null,
startButton = null,
stopButton = null,
downloadButton = null,
recordedVideo = null;
async function setupStream () {
try {
stream = await navigator.mediaDevices.getDisplayMedia({
video: true
});
audio = await navigator.mediaDevices.getUserMedia({
audio: {
echoCancellation: true,
noiseSuppression: true,
sampleRate: 44100,
},
});
setupVideoFeedback();
} catch (err) {
console.error(err);
}
}
function setupVideoFeedback() {
if (stream) {
const video = document.querySelector('.video-feedback');
video.srcObject = stream;
video.play();
} else {
console.warn('No Stream Available');
}
}
async function startRecording() {
await setupStream();
if (stream && audio) {
mixedStream = new MediaStream([...stream.getTracks(), ...audio.getTracks()]);
recorder = new MediaRecorder(mixedStream);
recorder.ondataavailable = handleDataAvailable;
recorder.onstop = handleStop;
recorder.start(1000);
startButton.disabled = true;
stopButton.disabled = false;
recordingStatus.innerText = "recording..."
} else {
console.warn('No Stream Available.');
}
}
function stopRecording () {
recorder.stop();
startButton.disabled = false;
stopButton.disabled = true;
}
function handleDataAvailable (e) {
chunks.push(e.data);
}
function handleStop (e) {
const blob = new Blob(chunks, { 'type': 'video/mp4'});
chunks = [];
downloadButton.href = URL.createObjectURL(blob);
downloadButton.download = 'video.mp4';
downloadButton.disabled = false;
recordedVideo.src = URL.createObjectURL(blob);
recordedVideo.load();
recordedVideo.onloadeddata = function() {
const rc = document.querySelector(".download-section");
rc.style.display = "flex";
rc.scrollIntoView({ behavior: "smooth", block: "start" });
recordedVideo.play();
}
stream.getTracks().forEach((track) => track.stop());
audio.getTracks().forEach((track) => track.stop());
recordingStatus.innerText = "recording stopped"
}
window.addEventListener('load', () => {
startButton = document.querySelector('.start-recording');
stopButton = document.querySelector('.stop-recording');
downloadButton = document.querySelector('.download-video');
recordedVideo = document.querySelector('.recorded-video');
recordingStatus = document.querySelector('.recording-status');
startButton.addEventListener('click', startRecording);
stopButton.addEventListener('click', stopRecording);
})