-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
236 lines (201 loc) · 7.92 KB
/
app.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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
class VideoSectionHandler {
constructor() {
this.selectedSection = null;
this.highlightOverlay = null;
this.isSelecting = false;
this.mediaRecorder = null;
this.chunks = [];
this.setupUI();
this.setupEventListeners();
}
setupUI() {
const controls = document.createElement('div');
controls.innerHTML = `
<div id="video-controls" style="
position: fixed;
top: 20px;
right: 20px;
background: #fff;
padding: 10px;
border-radius: 5px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
z-index: 10000;
">
<button id="select-section">Select Section</button>
<button id="download-video" disabled>Download Video</button>
<div id="status"></div>
</div>
`;
document.body.appendChild(controls);
this.highlightOverlay = document.createElement('div');
this.highlightOverlay.style.cssText = `
position: absolute;
background: rgba(75, 105, 255, 0.2);
border: 2px solid rgb(75, 105, 255);
pointer-events: none;
display: none;
z-index: 9999;
`;
document.body.appendChild(this.highlightOverlay);
}
setupEventListeners() {
const selectButton = document.getElementById('select-section');
const downloadButton = document.getElementById('download-video');
selectButton.addEventListener('click', () => this.startSelection());
downloadButton.addEventListener('click', () => this.handleDownload());
document.addEventListener('mousedown', (e) => this.handleMouseDown(e));
document.addEventListener('mousemove', (e) => this.handleMouseMove(e));
document.addEventListener('mouseup', (e) => this.handleMouseUp(e));
document.addEventListener('keydown', (e) => {
if (e.key === 'Escape') this.cancelSelection();
});
}
startSelection() {
this.isSelecting = true;
document.body.style.cursor = 'crosshair';
document.getElementById('status').textContent = 'Select a section containing video';
}
cancelSelection() {
this.isSelecting = false;
document.body.style.cursor = 'default';
this.highlightOverlay.style.display = 'none';
document.getElementById('status').textContent = '';
}
handleMouseDown(e) {
if (!this.isSelecting) return;
this.selectionStart = { x: e.pageX, y: e.pageY };
this.highlightOverlay.style.display = 'block';
}
handleMouseMove(e) {
if (!this.isSelecting || !this.selectionStart) return;
const rect = {
left: Math.min(this.selectionStart.x, e.pageX),
top: Math.min(this.selectionStart.y, e.pageY),
width: Math.abs(e.pageX - this.selectionStart.x),
height: Math.abs(e.pageY - this.selectionStart.y)
};
Object.assign(this.highlightOverlay.style, {
left: rect.left + 'px',
top: rect.top + 'px',
width: rect.width + 'px',
height: rect.height + 'px'
});
}
handleMouseUp(e) {
if (!this.isSelecting) return;
const rect = this.highlightOverlay.getBoundingClientRect();
const elements = document.elementsFromPoint(
rect.left + rect.width / 2,
rect.top + rect.height / 2
);
this.findAndSetupVideo(elements);
this.isSelecting = false;
document.body.style.cursor = 'default';
}
findAndSetupVideo(elements) {
const videoElement = elements.find(el =>
el.tagName === 'VIDEO' ||
el.querySelector('video')
);
if (videoElement) {
this.selectedSection = videoElement;
document.getElementById('download-video').disabled = false;
document.getElementById('status').textContent = 'Video found! Click download to save.';
} else {
document.getElementById('status').textContent = 'No video found in selection.';
this.highlightOverlay.style.display = 'none';
}
}
async setupMediaRecorder(stream) {
const options = {
mimeType: 'video/webm;codecs=vp9,opus',
videoBitsPerSecond: 8000000 // 8 Mbps
};
this.mediaRecorder = new MediaRecorder(stream, options);
this.chunks = [];
this.mediaRecorder.ondataavailable = (e) => {
if (e.data.size > 0) {
this.chunks.push(e.data);
}
};
this.mediaRecorder.onstop = () => {
const blob = new Blob(this.chunks, { type: 'video/webm' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'video_with_audio.webm';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
document.getElementById('status').textContent = 'Download complete!';
};
}
async captureVideoWithAudio(video) {
// Create canvas for video
const canvas = document.createElement('canvas');
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
const ctx = canvas.getContext('2d');
// Get video stream from canvas
const videoStream = canvas.captureStream(30); // 30 FPS
// Create audio context and source
const audioCtx = new AudioContext();
const source = audioCtx.createMediaElementSource(video);
const destination = audioCtx.createMediaStreamDestination();
source.connect(destination);
source.connect(audioCtx.destination); // Also play audio through speakers
// Combine video and audio streams
const tracks = [
...videoStream.getVideoTracks(),
...destination.stream.getAudioTracks()
];
const combinedStream = new MediaStream(tracks);
return {
stream: combinedStream,
captureFrame: () => {
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
requestAnimationFrame(() => this.captureFrame(video, ctx, canvas));
}
};
}
captureFrame(video, ctx, canvas) {
if (video.ended || !this.mediaRecorder) {
this.mediaRecorder.stop();
return;
}
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
requestAnimationFrame(() => this.captureFrame(video, ctx, canvas));
}
async handleDownload() {
const status = document.getElementById('status');
status.textContent = 'Starting capture...';
try {
const video = this.selectedSection.tagName === 'VIDEO' ?
this.selectedSection :
this.selectedSection.querySelector('video');
if (!video) {
throw new Error('Video element not found');
}
// Setup capture
const { stream, captureFrame } = await this.captureVideoWithAudio(video);
await this.setupMediaRecorder(stream);
// Start recording
video.currentTime = 0; // Reset video to start
await video.play();
this.mediaRecorder.start();
captureFrame();
status.textContent = 'Recording video with audio...';
// Stop recording when video ends
video.onended = () => {
if (this.mediaRecorder && this.mediaRecorder.state === 'recording') {
this.mediaRecorder.stop();
}
};
} catch (error) {
status.textContent = `Error: ${error.message}`;
console.error('Download error:', error);
}
}
}
const videoSectionHandler = new VideoSectionHandler();