forked from Testanki1/testanki1.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvideo_capture.html
More file actions
293 lines (249 loc) · 11.3 KB
/
video_capture.html
File metadata and controls
293 lines (249 loc) · 11.3 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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>视频帧精确提取器</title>
<style>
body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; text-align: center; margin: 20px; background-color: #f4f7f9; color: #333; }
h1 { color: #0056b3; }
p { max-width: 720px; margin: 10px auto; }
video { max-width: 100%; height: auto; border: 2px solid #ccc; background-color: #000; border-radius: 8px; margin-bottom: 15px;}
.controls-container { max-width: 720px; margin: 20px auto; padding: 20px; background-color: #fff; border-radius: 8px; box-shadow: 0 4px 8px rgba(0,0,0,0.1); border-top: 3px solid #007bff; }
button { padding: 10px 20px; margin: 10px 5px; font-size: 16px; cursor: pointer; border-radius: 5px; border: 1px solid #007bff; background-color: #007bff; color: white; transition: background-color 0.2s, transform 0.1s; }
button:hover:not(:disabled) { background-color: #0056b3; }
button:active:not(:disabled) { transform: scale(0.98); }
button:disabled { cursor: not-allowed; background-color: #a0a0a0; border-color: #a0a0a0; opacity: 0.7; }
#status { font-weight: bold; color: #0056b3; min-height: 20px; margin-top: 15px; }
.input-group { display: flex; align-items: center; justify-content: center; margin: 15px 0; }
.time-input { text-align: center; font-size: 1.2em; width: 120px; margin: 0 10px; padding: 8px; border: 1px solid #ccc; border-radius: 4px; }
#framePreviewContainer { margin-top: 20px; padding: 10px; border: 1px dashed #ddd; min-height: 100px; background-color: #f9f9f9; border-radius: 8px; }
#framePreview { max-width: 100%; height: auto; border: 1px solid #eee; display: none; margin: 0 auto; }
</style>
</head>
<body>
<h1>视频帧精确提取器</h1>
<p>加载视频后,暂停或拖动进度条,即可在下方工具中看到对应时间的预览帧,并可进行微调和保存。</p>
<input type="file" id="videoFile" accept="video/*">
<br><br>
<video id="videoPlayer" controls>
<source id="videoSource" type="video/mp4">
你的浏览器不支持 HTML5 视频播放。
</video>
<br>
<div class="controls-container">
<h2>精确时间帧预览与保存</h2>
<div class="input-group">
<button id="timeDecreaseBtn" title="减小 0.01 秒" disabled>« -0.01s</button>
<input type="number" id="timeInput" class="time-input" min="0" step="0.001" placeholder="时间 (秒)" disabled>
<button id="timeIncreaseBtn" title="增加 0.01 秒" disabled>» +0.01s</button>
</div>
<div id="framePreviewContainer">
<p id="previewPlaceholder">此处将显示提取的帧</p>
<img id="framePreview" alt="提取的帧预览">
</div>
<button id="saveFrameBtn" disabled>保存预览中的帧</button>
</div>
<div id="status"></div>
<script>
// --- DOM Elements ---
const videoFileInput = document.getElementById('videoFile');
const videoPlayer = document.getElementById('videoPlayer');
const videoSource = document.getElementById('videoSource');
const statusDiv = document.getElementById('status');
const timeInput = document.getElementById('timeInput');
const timeDecreaseBtn = document.getElementById('timeDecreaseBtn');
const timeIncreaseBtn = document.getElementById('timeIncreaseBtn');
const framePreview = document.getElementById('framePreview');
const previewPlaceholder = document.getElementById('previewPlaceholder');
const saveFrameBtn = document.getElementById('saveFrameBtn');
// --- State Variables ---
let videoFileName = '';
let currentFrameUrl = null;
let isSeeking = false;
let timeUpdateTimeout;
const TIME_STEP = 0.01;
// --- Canvas Setup ---
const hiddenCanvas = document.createElement('canvas');
const context = hiddenCanvas.getContext('2d');
// --- Event Listeners ---
videoFileInput.addEventListener('change', function(event) {
resetUI();
const file = event.target.files[0];
if (file) {
videoFileName = file.name.split('.').slice(0, -1).join('.') || 'video';
const url = URL.createObjectURL(file);
videoSource.src = url;
videoPlayer.load();
}
});
videoPlayer.addEventListener('loadedmetadata', function() {
if (videoPlayer.videoWidth > 0) {
hiddenCanvas.width = videoPlayer.videoWidth;
hiddenCanvas.height = videoPlayer.videoHeight;
}
timeInput.max = videoPlayer.duration.toFixed(3);
timeInput.value = "0.000";
updateButtonStates();
statusDiv.textContent = '视频已加载。';
});
// 当视频播放或暂停时更新按钮状态
videoPlayer.addEventListener('play', updateButtonStates);
videoPlayer.addEventListener('pause', () => {
updateButtonStates();
syncTimeAndPreview();
});
// 【关键改动】监听seeked事件,处理拖动进度条的情况
videoPlayer.addEventListener('seeked', () => {
// 只有当视频处于暂停状态时,拖动进度条才更新预览
if (videoPlayer.paused) {
updateButtonStates();
syncTimeAndPreview();
}
});
videoPlayer.addEventListener('error', function() {
alert('加载或播放视频时发生错误。');
resetUI();
});
// --- 精确时间控制交互 ---
timeInput.addEventListener('input', () => {
clearTimeout(timeUpdateTimeout);
timeUpdateTimeout = setTimeout(() => {
let targetTime = parseFloat(timeInput.value);
if (!isNaN(targetTime)) {
targetTime = Math.max(0, Math.min(targetTime, videoPlayer.duration));
timeInput.value = targetTime.toFixed(3);
updateFramePreview(targetTime);
}
}, 300);
});
timeDecreaseBtn.addEventListener('click', () => {
let currentTime = parseFloat(timeInput.value) || 0;
let newTime = Math.max(0, currentTime - TIME_STEP);
timeInput.value = newTime.toFixed(3);
updateFramePreview(newTime);
});
timeIncreaseBtn.addEventListener('click', () => {
let currentTime = parseFloat(timeInput.value) || 0;
let newTime = Math.min(videoPlayer.duration, currentTime + TIME_STEP);
timeInput.value = newTime.toFixed(3);
updateFramePreview(newTime);
});
saveFrameBtn.addEventListener('click', () => {
if (!currentFrameUrl) {
alert('没有可保存的预览帧。');
return;
}
const timestamp = parseFloat(timeInput.value).toFixed(3).replace('.', '_');
const filename = `${videoFileName}_frame_at_${timestamp}s.png`;
const link = document.createElement('a');
link.href = currentFrameUrl;
link.download = filename;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
statusDiv.textContent = `已保存预览中的帧: ${filename}`;
});
// --- Helper Functions ---
/**
* 【新增】同步视频当前时间到输入框并更新预览
*/
function syncTimeAndPreview() {
if (isSeeking || videoPlayer.readyState < videoPlayer.HAVE_METADATA) return;
const currentTime = videoPlayer.currentTime;
// 更新输入框的值
timeInput.value = currentTime.toFixed(3);
// 更新预览图像
updateFramePreview(currentTime);
}
async function updateFramePreview(targetTime) {
if (isSeeking || videoPlayer.readyState < videoPlayer.HAVE_METADATA) return;
isSeeking = true;
updateButtonStates();
statusDiv.textContent = `正在定位到 ${targetTime.toFixed(3)}s 并提取...`;
try {
await seekToTime(targetTime);
const frameBlob = await captureFrameAsBlob();
if (currentFrameUrl) URL.revokeObjectURL(currentFrameUrl);
currentFrameUrl = URL.createObjectURL(frameBlob);
framePreview.src = currentFrameUrl;
framePreview.style.display = 'block';
previewPlaceholder.style.display = 'none';
statusDiv.textContent = `已成功提取 ${targetTime.toFixed(3)}s 处的帧。`;
} catch (error) {
console.error('更新帧预览时出错:', error);
statusDiv.textContent = `提取失败: ${error.message}`;
alert(`提取帧时出错: ${error.message}`);
} finally {
isSeeking = false;
updateButtonStates();
}
}
function resetUI() {
statusDiv.textContent = '请选择一个视频文件。';
isSeeking = false;
if(videoPlayer.src && videoPlayer.src.startsWith('blob:')) {
URL.revokeObjectURL(videoPlayer.src);
}
videoSource.src = '';
videoFileName = '';
videoPlayer.load();
resetFramePreview();
timeInput.value = '';
timeInput.max = null;
updateButtonStates();
}
function resetFramePreview() {
if (currentFrameUrl) {
URL.revokeObjectURL(currentFrameUrl);
currentFrameUrl = null;
}
framePreview.src = '';
framePreview.style.display = 'none';
previewPlaceholder.style.display = 'block';
}
function updateButtonStates() {
const videoLoaded = videoPlayer.readyState >= videoPlayer.HAVE_METADATA;
const disableAllPreciseControls = !videoLoaded || isSeeking;
timeInput.disabled = disableAllPreciseControls;
timeDecreaseBtn.disabled = disableAllPreciseControls;
timeIncreaseBtn.disabled = disableAllPreciseControls;
saveFrameBtn.disabled = disableAllPreciseControls || !currentFrameUrl;
videoFileInput.disabled = isSeeking;
}
function seekToTime(time) {
return new Promise((resolve) => {
const onSeeked = () => {
videoPlayer.removeEventListener('seeked', onSeeked);
requestAnimationFrame(() => resolve());
};
if (Math.abs(videoPlayer.currentTime - time) < 0.01) {
requestAnimationFrame(() => resolve());
return;
}
videoPlayer.addEventListener('seeked', onSeeked);
videoPlayer.currentTime = time;
});
}
function captureFrameAsBlob() {
return new Promise((resolve, reject) => {
try {
if (hiddenCanvas.width !== videoPlayer.videoWidth || hiddenCanvas.height !== videoPlayer.videoHeight) {
hiddenCanvas.width = videoPlayer.videoWidth;
hiddenCanvas.height = videoPlayer.videoHeight;
}
context.drawImage(videoPlayer, 0, 0, hiddenCanvas.width, hiddenCanvas.height);
hiddenCanvas.toBlob(blob => {
if (blob) resolve(blob);
else reject(new Error('无法将Canvas转换为Blob。'));
}, 'image/png');
} catch (e) {
reject(new Error(`捕获帧时出错: ${e.message}`));
}
});
}
// --- Initial State ---
updateButtonStates();
</script>
</body>
</html>