Repository navigation
Expand file tree
/
Copy pathui.js
More file actions
184 lines (151 loc) · 5.82 KB
/
Copy pathui.js
File metadata and controls
184 lines (151 loc) · 5.82 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
const startBtn = document.getElementById('startBtn');
const startBufferBtn = document.getElementById('startBufferBtn');
const clipBtn = document.getElementById('clipBtn');
const downloadAllBtn = document.getElementById('downloadAllBtn');
const toggleClipList = document.getElementById('toggleClipList');
const bufferDurationInput = document.getElementById('bufferDuration');
const audioOption = document.getElementById('audioOption');
const clipList = document.getElementById('clipList');
let stream, recorder, chunks = [], isBuffering = false, bufferChunks = [], bufferRecorder, bufferStream;
let bufferMaxDuration = 10 * 60 * 1000; // max 10 minutes including 1 min buffer
let clipEntries = [];
function formatTime(date = new Date()) {
return date.toLocaleString().replace(',', '').replace(/:/g, '-');
}
function addClipToList(blob, label = 'Recording') {
const timestamp = formatTime();
const item = document.createElement('div');
item.className = 'clip-item';
const toggle = document.createElement('button');
toggle.className = 'clip-toggle';
toggle.textContent = `📁 ${label} - ${timestamp}`;
const content = document.createElement('div');
content.className = 'clip-content';
const video = document.createElement('video');
video.src = URL.createObjectURL(blob);
video.controls = true;
const downloadBtn = document.createElement('button');
downloadBtn.textContent = '⬇️ Download';
downloadBtn.onclick = () => {
const a = document.createElement('a');
a.href = video.src;
a.download = `${label}-${timestamp}.webm`;
a.click();
};
content.appendChild(video);
content.appendChild(downloadBtn);
toggle.onclick = () => {
content.style.display = content.style.display === 'none' ? 'block' : 'none';
};
item.appendChild(toggle);
item.appendChild(content);
clipList.prepend(item);
clipEntries.push({ blob, name: `${label}-${timestamp}.webm` });
}
async function getCombinedStream() {
const audioChoice = audioOption.value;
const screenStream = await navigator.mediaDevices.getDisplayMedia({
video: true,
audio: audioChoice === 'tab' || audioChoice === 'both'
});
if (audioChoice === 'mic' || audioChoice === 'both') {
const micStream = await navigator.mediaDevices.getUserMedia({ audio: true });
const audioCtx = new AudioContext();
const dest = audioCtx.createMediaStreamDestination();
const screenAudioTracks = screenStream.getAudioTracks();
if (screenAudioTracks.length) {
const screenSource = audioCtx.createMediaStreamSource(new MediaStream([screenAudioTracks[0]]));
screenSource.connect(dest);
}
const micSource = audioCtx.createMediaStreamSource(micStream);
micSource.connect(dest);
screenStream.getAudioTracks().forEach(t => screenStream.removeTrack(t));
dest.stream.getAudioTracks().forEach(track => screenStream.addTrack(track));
} else if (audioChoice === 'none') {
screenStream.getAudioTracks().forEach(track => screenStream.removeTrack(track));
}
return screenStream;
}
startBtn.onclick = async () => {
try {
stream = await getCombinedStream();
recorder = new MediaRecorder(stream);
chunks = [];
recorder.ondataavailable = e => {
if (e.data.size > 0) chunks.push(e.data);
};
recorder.onstop = () => {
const blob = new Blob(chunks, { type: 'video/webm' });
addClipToList(blob, 'Recording');
startBtn.disabled = false;
};
recorder.start();
startBtn.disabled = true;
stream.getVideoTracks()[0].addEventListener('ended', () => {
if (recorder && recorder.state !== 'inactive') {
recorder.stop();
}
if (stream) {
stream.getTracks().forEach(track => track.stop());
stream = null;
}
});
} catch (err) {
console.error(err);
alert('Recording failed: ' + err.message);
startBtn.disabled = false;
}
};
startBufferBtn.onclick = async () => {
if (isBuffering) {
bufferRecorder.stop();
bufferStream.getTracks().forEach(track => track.stop());
isBuffering = false;
startBufferBtn.textContent = '📹 Start Replay Buffer';
clipBtn.disabled = true;
return;
}
const userMinutes = Math.min(10, parseInt(bufferDurationInput.value));
bufferMaxDuration = (userMinutes + 1) * 60 * 1000; // Add 1 minute buffer
bufferStream = await getCombinedStream();
bufferChunks = [];
isBuffering = true;
clipBtn.disabled = false;
startBufferBtn.textContent = '🛑 Stop Replay Buffer';
bufferRecorder = new MediaRecorder(bufferStream, { mimeType: 'video/webm' });
bufferRecorder.ondataavailable = e => {
if (e.data.size > 0) {
bufferChunks.push({ data: e.data, timestamp: Date.now() });
const cutoff = Date.now() - bufferMaxDuration;
bufferChunks = bufferChunks.filter(c => c.timestamp >= cutoff);
}
};
bufferRecorder.start(1000); // Emit every second
};
clipBtn.onclick = () => {
if (!isBuffering || bufferChunks.length === 0) return;
const now = Date.now();
const userClipLength = Math.min(10, parseInt(bufferDurationInput.value)) * 60 * 1000;
const safeClipStart = now - userClipLength;
const validChunks = bufferChunks.filter(c => c.timestamp >= safeClipStart);
const blob = new Blob(validChunks.map(c => c.data), { type: 'video/webm' });
addClipToList(blob, 'Clip');
};
downloadAllBtn.onclick = async () => {
if (clipEntries.length === 0) return alert('No clips to download!');
const zip = new JSZip();
clipEntries.forEach(({ blob, name }) => {
zip.file(name, blob);
});
const content = await zip.generateAsync({ type: 'blob' });
const a = document.createElement('a');
a.href = URL.createObjectURL(content);
a.download = `all-clips-${Date.now()}.zip`;
a.click();
};
toggleClipList.onclick = () => {
clipList.style.display = clipList.style.display === 'none' ? 'block' : 'none';
};
window.addEventListener('beforeunload', () => {
clipEntries = [];
});