视频录制测试页

发布时间:2026/10/28 5:47:36

视频录制测试页 !DOCTYPE htmlhtml langzh-CNheadmeta charsetUTF-8meta nameviewport contentwidthdevice-width, initial-scale1.0, maximum-scale1.0, user-scalablenotitleH5 活体录像验证 (最终修正版)/title!-- 引入 vConsole --script srchttps://unpkg.com/vconsolelatest/dist/vconsole.min.js/scriptstyle* { box-sizing: border-box; margin: 0; padding: 0; }body {font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Helvetica Neue, Arial, sans-serif;display: flex;flex-direction: column;align-items: center;justify-content: center;min-height: 100vh;background-color: #f5f7fa;color: #333;padding: 20px;}/* 视频容器样式 */#video-container {width: 100%;max-width: 480px;aspect-ratio: 16 / 9;background-color: #000;border-radius: 12px;overflow: hidden;position: relative;box-shadow: 0 4px 12px rgba(0,0,0,0.1);margin-bottom: 24px;}video {width: 100%;height: 100%;object-fit: cover;transform: scaleX(-1); /* 镜像显示 */}/* 录制中状态指示器 - 右上角淡色小按钮 */#recording-indicator {position: absolute;top: 12px;right: 12px;background-color: rgba(255, 59, 48, 0.15); /* 淡红色背景 */color: #ff3b30;font-size: 12px;font-weight: 600;padding: 4px 10px;border-radius: 12px;display: none; /* 默认隐藏 */align-items: center;gap: 6px;backdrop-filter: blur(4px);z-index: 10;animation: breathe 1.5s infinite ease-in-out;}#recording-indicator::before {content: ;display: block;width: 6px;height: 6px;background-color: #ff3b30;border-radius: 50%;}keyframes breathe {0%, 100% { opacity: 0.6; transform: scale(0.98); }50% { opacity: 1; transform: scale(1.02); }}/* 倒计时提示 */#countdown {position: absolute;top: 50%;left: 50%;transform: translate(-50%, -50%);font-size: 48px;font-weight: bold;color: white;text-shadow: 0 2px 4px rgba(0,0,0,0.5);display: none;z-index: 5;}/* 按钮组样式 */.btn-group {display: flex;gap: 16px;width: 100%;max-width: 480px;margin-bottom: 16px;}button {flex: 1;padding: 14px 0;border: none;border-radius: 8px;font-size: 16px;font-weight: 600;cursor: pointer;transition: all 0.2s;}button:active { transform: scale(0.98); }#startBtn {background-color: #007aff;color: white;}#saveBtn {background-color: #34c759;color: white;display: none;}#closeBtn {background-color: #ff9500;color: white;display: none;}/* 状态文本 */#statusText {font-size: 14px;color: #666;text-align: center;min-height: 20px;}/* 结果预览区 */#resultContainer {width: 100%;max-width: 480px;margin-top: 20px;display: none;}#resultVideo {width: 100%;border-radius: 8px;background: #000;}.file-info {margin-top: 8px;font-size: 12px;color: #888;text-align: center;}/style/headbodydiv idvideo-containervideo idpreview autoplay playsinline muted/video!-- 录制中指示器 --div idrecording-indicator录制中/div!-- 倒计时 --div idcountdown3/div/divdiv classbtn-groupbutton idstartBtn开始验证/buttonbutton idsaveBtn保存视频/buttonbutton idcloseBtn关闭摄像头/button/divp idstatusText准备就绪/p!-- 结果预览 --div idresultContainervideo idresultVideo controls/videodiv classfile-info idfileInfo/div/divscript// 初始化 vConsoleconst vConsole new window.VConsole();console.log([System] vConsole 已启动);// DOM 元素const preview document.getElementById(preview);const startBtn document.getElementById(startBtn);const saveBtn document.getElementById(saveBtn);const closeBtn document.getElementById(closeBtn);const statusText document.getElementById(statusText);const recordingIndicator document.getElementById(recording-indicator);const countdownEl document.getElementById(countdown);const resultContainer document.getElementById(resultContainer);const resultVideo document.getElementById(resultVideo);const fileInfo document.getElementById(fileInfo);// 全局变量let mediaStream null;let recorder null;let recordedChunks [];let currentBlob null;let isRecording false;// 配置参数const CONFIG {duration: 6000, // 6秒自动结束maxSize: 10 * 1024 * 1024, // 10MB 限制// 源头控制限制分辨率和帧率constraints: {audio: false,video: {width: { ideal: 640, max: 1280 },height: { ideal: 480, max: 720 },frameRate: { ideal: 20, max: 24 }}}};/*** 核心类RecorderManager* 包含录制、自动停止、二次压缩逻辑*/class RecorderManager {constructor(stream) {this.stream stream;this.chunks [];this.recorder null;// 尝试选择兼容性最好的编码格式const options { mimeType: video/webm;codecsvp9 };if (!MediaRecorder.isTypeSupported(options.mimeType)) {options.mimeType video/webm;codecsvp8;if (!MediaRecorder.isTypeSupported(options.mimeType)) {options.mimeType video/webm;}}// 过程建议设置码率虽然浏览器不一定完全遵守options.videoBitsPerSecond 1500000; // 1.5Mbpsthis.recorder new MediaRecorder(this.stream, options);console.log([Recorder] 初始化完成MIME: ${options.mimeType});this.recorder.ondataavailable (e) {if (e.data e.data.size 0) {this.chunks.push(e.data);// 实时打印当前累计大小方便调试const currentSize this.chunks.reduce((acc, cur) acc cur.size, 0);console.log([Recorder] 接收数据块: ${(currentSize / 1024).toFixed(2)} KB);}};this.recorder.onstop () {console.log([Recorder] 录制已停止);const blob new Blob(this.chunks, { type: this.recorder.mimeType });this.handleFinish(blob);};}start() {this.chunks [];this.recorder.start(100); // 每100ms触发一次 dataavailableisRecording true;// 显示 UIrecordingIndicator.style.display flex;statusText.textContent 正在录制...;console.log([Recorder] 开始录制);// 6秒倒计时 UIlet count 3;countdownEl.style.display block;countdownEl.textContent count;const timer setInterval(() {count--;if (count 0) {countdownEl.textContent count;} else {clearInterval(timer);countdownEl.style.display none;}}, 1000);// 6秒后自动停止setTimeout(() {if (this.recorder this.recorder.state recording) {console.log([Recorder] 触发6秒自动停止);this.stop();}}, CONFIG.duration);}stop() {if (this.recorder this.recorder.state ! inactive) {this.recorder.stop();}}async handleFinish(blob) {isRecording false;recordingIndicator.style.display none; // 隐藏录制中标识console.log([Recorder] 原始文件大小: ${(blob.size / 1024 / 1024).toFixed(2)} MB);// 结果兜底如果超过 10MB进行二次压缩if (blob.size CONFIG.maxSize) {console.warn([Recorder] 文件过大启动二次压缩...);statusText.textContent 文件过大正在压缩...;try {blob await this.compressBlob(blob);console.log([Recorder] 压缩后大小: ${(blob.size / 1024 / 1024).toFixed(2)} MB);} catch (err) {console.error([Recorder] 压缩失败:, err);statusText.textContent 压缩失败请重试;return;}}currentBlob blob;this.showResult(blob);}/*** 二次压缩利用 MediaRecorder 重新编码* 不依赖第三方库通过降低码率实现压缩*/compressBlob(originalBlob) {return new Promise((resolve, reject) {const url URL.createObjectURL(originalBlob);const tempVideo document.createElement(video);tempVideo.src url;tempVideo.muted true;tempVideo.playsInline true;// 创建一个新的低码率录制器const compressOptions {mimeType: video/webm,videoBitsPerSecond: 500000 // 500kbps 极低码率};let compressChunks [];let compressRecorder;tempVideo.onloadedmetadata () {try {// 捕获视频流进行重录const stream tempVideo.captureStream ? tempVideo.captureStream() : tempVideo.mozCaptureStream();compressRecorder new MediaRecorder(stream, compressOptions);compressRecorder.ondataavailable (e) {if (e.data.size 0) compressChunks.push(e.data);};compressRecorder.onstop () {URL.revokeObjectURL(url);const compressedBlob new Blob(compressChunks, { type: video/webm });resolve(compressedBlob);};compressRecorder.start();tempVideo.play();// 播放完毕即压缩完成tempVideo.onended () {if (compressRecorder.state ! inactive) {compressRecorder.stop();}};} catch (e) {URL.revokeObjectURL(url);reject(e);}};tempVideo.onerror () reject(new Error(视频加载失败));});}showResult(blob) {const url URL.createObjectURL(blob);resultVideo.src url;resultContainer.style.display block;fileInfo.textContent 大小: ${(blob.size / 1024).toFixed(2)} KB | 类型: ${blob.type};saveBtn.style.display block;closeBtn.style.display block;startBtn.textContent 重新验证;statusText.textContent ✅ 录制完成;}destroy() {if (this.stream) {this.stream.getTracks().forEach(track track.stop());}this.stream null;this.recorder null;}}let manager null;// 1. 开始验证startBtn.addEventListener(click, async () {try {statusText.textContent 正在开启摄像头...;console.log([UI] 点击开始验证);// 获取媒体流mediaStream await navigator.mediaDevices.getUserMedia(CONFIG.constraints);preview.srcObject mediaStream;// 实例化管理器并开始manager new RecorderManager(mediaStream);manager.start();startBtn.style.display none;resultContainer.style.display none;} catch (err) {console.error([Error] 获取摄像头失败:, err);statusText.textContent ❌ 无法访问摄像头: err.message;}});// 2. 保存视频saveBtn.addEventListener(click, () {if (!currentBlob) return;const url URL.createObjectURL(currentBlob);const a document.createElement(a);a.href url;a.download liveness_${Date.now()}.webm;document.body.appendChild(a);a.click();document.body.removeChild(a);URL.revokeObjectURL(url);console.log([UI] 视频已下载);});// 3. 关闭摄像头closeBtn.addEventListener(click, () {if (manager) {manager.destroy();manager null;}preview.srcObject null;currentBlob null;saveBtn.style.display none;closeBtn.style.display none;startBtn.style.display block;startBtn.textContent 开始验证;statusText.textContent 摄像头已安全关闭;recordingIndicator.style.display none; // 确保关闭时隐藏console.log([UI] 摄像头已关闭);});/script/body/html
延伸阅读

更多相关文章

2026/10/28 5:46:33

Three.js 3D 地图特效与材质实现指南 _

一、地形材质(MeshStandardMaterial) 1.1 实现原理 使用 Three.js 的 PBR(物理正确渲染)材质,结合多张纹理实现真实感地形效果: 漫反射纹理(diffuseMap):控制表面颜色 位…

2026/10/28 5:43:00

纯前端单文件舒尔特表格训练器:洗牌校验与训练节奏设计

舒尔特表格这东西,我最早是在一堆纸质训练卡里见到的:55 的方格,1 到 25 打乱填进去,用手指或眼睛按顺序找。看着简陋到有点寒酸,但它是我见过的、成本最低又最容易被量化的注意力训练工具之一。后来有人找我做一个能自…

2026/10/28 5:43:00

局部高斯拟合活动轮廓模型在医学图像分割中的应用

1. 项目概述:基于局部高斯分布拟合的活动轮廓模型在医学影像分析和计算机视觉领域,图像分割一直是个经典难题。传统阈值分割方法对噪声敏感,边缘检测算法又难以获得闭合区域。2001年由Chan和Vese提出的主动轮廓模型(Active Contou…

2026/10/28 5:43:00

电力无人机智能巡检数据集技术解析与应用实践

1. 项目背景与核心价值输电线路巡检是电力系统运维中最基础也最关键的环节之一。传统人工巡检方式存在效率低、覆盖面有限、高危作业风险等问题。我在某省级电网公司参与智能巡检项目时,亲眼见过巡线员需要攀爬数十米高的铁塔检查绝缘子,不仅耗时耗力&am…

2026/10/28 5:43:00

推理模型与通用模型:机制差异、提示语策略与选型指南

简介:这份PDF资料面向对AI大模型感兴趣的从业者与技术爱好者,系统梳理国产开源推理模型DeepSeek-R1的能力边界与实战用法,帮助读者从入门走向精通。内容围绕DeepSeek是什么、能做什么、如何使用展开,重点辨析推理模型与通用模型在…

2026/10/28 5:42:59

用Node.js和Handlebars打造自己的代码生成器,告别重复CRUD

是不是每个搞开发的,都会在某个深夜冒出一个念头:这破代码我不想再写一遍了。我在带一个后端小组的时候,这种念头几乎每周都要出现几次。业务系统越做越复杂,但代码增长的套路却高度重复:每新加一张业务表,…

2026/10/28 5:37:59

AI团队能力建模:从JD到能力图谱的完整技术路线

1. 从一份岗位说明书说起:为什么传统JD撑不起AI团队的能力管理我做过一段时间AI团队的招聘和技术管理,最头疼的事情不是招不到人,而是招进来之后发现“岗位说明书”和“实际要干的活”完全是两码事。一份典型的AI岗位JD大概长这样&#xff1a…

2026/10/27 14:24:03

全国计算机二级C语言真题PDF高效刷题指南

简介:这份PDF是全国计算机二级C语言考试的试题与答案解析,专为备考二级C语言的考生准备。内容涵盖C语言特点与设计理念、程序基本组成单位、main函数执行流程、函数嵌套调用规则、标识符命名规范、整型实型字符型数据类型、八进制十六进制数合法形式、运…

2026/10/27 9:34:54

requests+BeautifulSoup:爬虫抓取h1标题的入门实战指南

简介:一份以实战为导向的Python爬虫入门资源,面向具备基本编程基础、希望快速掌握网络数据抓取的初学者,可解决从零编写简单爬虫的常见困惑。实例以requests库发送HTTP请求、BeautifulSoup解析HTML文档,完整演示从网页中提取 标题…

2026/10/28 3:32:29

递推最小二乘参数辨识MATLAB编程:二阶系统与渐消记忆实现

简介:这是一篇面向控制系统、信号处理与机器学习等领域学习者的最小二乘参数辨识程序说明文档。资源共包含一个docx格式文档,压缩包大小七十八KB,内容围绕递推最小二乘算法和渐消记忆最小二乘算法展开:先构造一个极点位于单位圆内…

2026/10/28 0:02:13

Python股票自动交易系统源码解析:从行情获取到风控的完整链路

简介:这份资源是面向高校计算机、金融相关专业毕业设计场景的完整项目源码包,主题为基于Python的股票自动交易系统,适合需要完成毕设选题、课程设计或自学金融量化交易开发的学生与开发者参考。压缩包共收录687个文件,整体约14.93…

2026/10/28 0:02:13

视酷酷信IM源码部署指南:Java 17+MySQL 8.0.33实战配置

简介:这是一套2024年亲测可部署的视酷酷信IM即时通讯系统源码,面向具备Java、OC、C#及Vue开发能力的技术人员,解决私有化即时通讯平台快速搭建与二次定制需求,尤其适用于需集成商城、保障数据主权的中小型企业或独立开发者。资源共…

2026/10/28 0:02:13

Python爬取百度迁徙数据:接口解析、批量采集与避坑指南

简介:这份资源是一个用Python爬取百度迁徙数据的脚本压缩包,面向具备Python基础、希望入门网络爬虫与数据解析的开发者,解决按日期获取全国热门城市人口迁徙数据的问题。包内共1个文件,为单个py脚本,压缩包约1KB&#…

还想了解更多?直接咨询顾问

免费诊断 + 免费方案 + 透明报价。

全国咨询热线400-8866-253
免费获取方案
☎咨询二维码 ☎ ↑