500 lines
16 KiB
HTML
500 lines
16 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="zh-CN">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Trae Video Server我在这里随便起个名字</title>
|
|
<style>
|
|
* {
|
|
margin: 0;
|
|
padding: 0;
|
|
box-sizing: border-box;
|
|
}
|
|
|
|
body {
|
|
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
|
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
|
min-height: 100vh;
|
|
padding: 20px;
|
|
}
|
|
|
|
.container {
|
|
max-width: 1000px;
|
|
margin: 0 auto;
|
|
background: white;
|
|
border-radius: 15px;
|
|
box-shadow: 0 20px 40px rgba(0,0,0,0.1);
|
|
overflow: hidden;
|
|
}
|
|
|
|
.header {
|
|
background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
|
|
color: white;
|
|
padding: 30px;
|
|
text-align: center;
|
|
}
|
|
|
|
.header h1 {
|
|
font-size: 2.5em;
|
|
margin-bottom: 10px;
|
|
font-weight: 300;
|
|
}
|
|
|
|
.header p {
|
|
font-size: 1.1em;
|
|
opacity: 0.9;
|
|
}
|
|
|
|
.content {
|
|
padding: 40px;
|
|
}
|
|
|
|
.device-section {
|
|
margin-bottom: 40px;
|
|
}
|
|
|
|
.section-title {
|
|
font-size: 1.5em;
|
|
color: #333;
|
|
margin-bottom: 20px;
|
|
display: flex;
|
|
align-items: center;
|
|
}
|
|
|
|
.section-title::before {
|
|
content: '';
|
|
width: 4px;
|
|
height: 24px;
|
|
background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
|
|
margin-right: 15px;
|
|
border-radius: 2px;
|
|
}
|
|
|
|
.device-controls {
|
|
display: flex;
|
|
gap: 15px;
|
|
align-items: center;
|
|
flex-wrap: wrap;
|
|
margin-bottom: 20px;
|
|
}
|
|
|
|
select {
|
|
flex: 1;
|
|
min-width: 300px;
|
|
padding: 12px 15px;
|
|
border: 2px solid #e1e5e9;
|
|
border-radius: 8px;
|
|
font-size: 16px;
|
|
background: white;
|
|
transition: all 0.3s ease;
|
|
}
|
|
|
|
select:focus {
|
|
outline: none;
|
|
border-color: #4facfe;
|
|
box-shadow: 0 0 0 3px rgba(79, 172, 254, 0.1);
|
|
}
|
|
|
|
.btn {
|
|
padding: 12px 24px;
|
|
border: none;
|
|
border-radius: 8px;
|
|
font-size: 16px;
|
|
font-weight: 600;
|
|
cursor: pointer;
|
|
transition: all 0.3s ease;
|
|
text-transform: uppercase;
|
|
letter-spacing: 0.5px;
|
|
}
|
|
|
|
.btn-primary {
|
|
background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
|
|
color: white;
|
|
}
|
|
|
|
.btn-primary:hover {
|
|
transform: translateY(-2px);
|
|
box-shadow: 0 10px 20px rgba(79, 172, 254, 0.3);
|
|
}
|
|
|
|
.btn-secondary {
|
|
background: linear-gradient(135deg, #ffecd2 0%, #fcb69f 100%);
|
|
color: #8b4513;
|
|
}
|
|
|
|
.btn-secondary:hover {
|
|
transform: translateY(-2px);
|
|
box-shadow: 0 10px 20px rgba(252, 182, 159, 0.3);
|
|
}
|
|
|
|
.btn-danger {
|
|
background: linear-gradient(135deg, #ff9a9e 0%, #fecfef 100%);
|
|
color: #8b0000;
|
|
}
|
|
|
|
.btn-danger:hover {
|
|
transform: translateY(-2px);
|
|
box-shadow: 0 10px 20px rgba(255, 154, 158, 0.3);
|
|
}
|
|
|
|
.btn:disabled {
|
|
opacity: 0.6;
|
|
cursor: not-allowed;
|
|
transform: none !important;
|
|
box-shadow: none !important;
|
|
}
|
|
|
|
.status {
|
|
padding: 15px;
|
|
border-radius: 8px;
|
|
margin: 20px 0;
|
|
font-weight: 500;
|
|
}
|
|
|
|
.status.success {
|
|
background: #d4edda;
|
|
color: #155724;
|
|
border: 1px solid #c3e6cb;
|
|
}
|
|
|
|
.status.error {
|
|
background: #f8d7da;
|
|
color: #721c24;
|
|
border: 1px solid #f5c6cb;
|
|
}
|
|
|
|
.status.info {
|
|
background: #d1ecf1;
|
|
color: #0c5460;
|
|
border: 1px solid #bee5eb;
|
|
}
|
|
|
|
.preview-section {
|
|
margin-top: 40px;
|
|
padding-top: 40px;
|
|
border-top: 2px solid #f0f0f0;
|
|
}
|
|
|
|
.preview-container {
|
|
background: #f8f9fa;
|
|
border-radius: 12px;
|
|
padding: 30px;
|
|
text-align: center;
|
|
min-height: 400px;
|
|
display: flex;
|
|
flex-direction: column;
|
|
justify-content: center;
|
|
align-items: center;
|
|
}
|
|
|
|
.preview-placeholder {
|
|
color: #6c757d;
|
|
font-size: 1.2em;
|
|
margin-bottom: 20px;
|
|
}
|
|
|
|
video {
|
|
max-width: 100%;
|
|
max-height: 400px;
|
|
border-radius: 8px;
|
|
box-shadow: 0 10px 30px rgba(0,0,0,0.2);
|
|
}
|
|
|
|
.stream-info {
|
|
margin-top: 20px;
|
|
padding: 20px;
|
|
background: #e3f2fd;
|
|
border-radius: 8px;
|
|
border-left: 4px solid #2196f3;
|
|
}
|
|
|
|
.stream-info h3 {
|
|
color: #1976d2;
|
|
margin-bottom: 10px;
|
|
}
|
|
|
|
.stream-info a {
|
|
color: #1976d2;
|
|
text-decoration: none;
|
|
font-weight: 600;
|
|
}
|
|
|
|
.stream-info a:hover {
|
|
text-decoration: underline;
|
|
}
|
|
|
|
.loading {
|
|
display: inline-block;
|
|
width: 20px;
|
|
height: 20px;
|
|
border: 3px solid #f3f3f3;
|
|
border-top: 3px solid #4facfe;
|
|
border-radius: 50%;
|
|
animation: spin 1s linear infinite;
|
|
margin-right: 10px;
|
|
}
|
|
|
|
@keyframes spin {
|
|
0% { transform: rotate(0deg); }
|
|
100% { transform: rotate(360deg); }
|
|
}
|
|
|
|
.hidden {
|
|
display: none;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="container">
|
|
<div class="header">
|
|
<h1>🎥 Trae Video Server</h1>
|
|
<p>专业视频采集与流媒体服务平台</p>
|
|
</div>
|
|
|
|
<div class="content">
|
|
<div class="device-section">
|
|
<h2 class="section-title">📹 视频设备管理</h2>
|
|
|
|
<div class="device-controls">
|
|
<select id="deviceSelect">
|
|
<option value="">正在加载设备列表...</option>
|
|
</select>
|
|
<button id="refreshBtn" class="btn btn-secondary">🔄 刷新设备</button>
|
|
<button id="startBtn" class="btn btn-primary" disabled>▶️ 开始预览</button>
|
|
<button id="stopBtn" class="btn btn-danger hidden">⏹️ 停止预览</button>
|
|
</div>
|
|
|
|
<div id="status" class="status hidden"></div>
|
|
</div>
|
|
|
|
<div class="preview-section">
|
|
<h2 class="section-title">📺 实时预览</h2>
|
|
|
|
<div class="preview-container">
|
|
<div id="previewPlaceholder" class="preview-placeholder">
|
|
<p>📷 选择视频设备并点击"开始预览"来查看实时画面</p>
|
|
</div>
|
|
<video id="previewVideo" class="hidden" autoplay muted></video>
|
|
</div>
|
|
|
|
<div id="streamInfo" class="stream-info hidden">
|
|
<h3>🌐 流媒体服务信息</h3>
|
|
<p>网页预览地址: <a id="streamUrl" href="#" target="_blank">http://localhost:3000/preview</a></p>
|
|
<p>其他设备可以通过此链接访问实时视频流</p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
const { ipcRenderer } = require('electron');
|
|
|
|
let currentStream = null;
|
|
let isStreaming = false;
|
|
|
|
// DOM元素
|
|
const deviceSelect = document.getElementById('deviceSelect');
|
|
const refreshBtn = document.getElementById('refreshBtn');
|
|
const startBtn = document.getElementById('startBtn');
|
|
const stopBtn = document.getElementById('stopBtn');
|
|
const status = document.getElementById('status');
|
|
const previewVideo = document.getElementById('previewVideo');
|
|
const previewPlaceholder = document.getElementById('previewPlaceholder');
|
|
const streamInfo = document.getElementById('streamInfo');
|
|
const streamUrl = document.getElementById('streamUrl');
|
|
|
|
// 显示状态消息
|
|
function showStatus(message, type = 'info') {
|
|
status.textContent = message;
|
|
status.className = `status ${type}`;
|
|
status.classList.remove('hidden');
|
|
|
|
if (type === 'success' || type === 'error') {
|
|
setTimeout(() => {
|
|
status.classList.add('hidden');
|
|
}, 5000);
|
|
}
|
|
}
|
|
|
|
// 加载设备列表
|
|
async function loadDevices() {
|
|
try {
|
|
showStatus('正在加载设备列表...', 'info');
|
|
|
|
// 请求媒体权限
|
|
await navigator.mediaDevices.getUserMedia({ video: true });
|
|
|
|
const devices = await navigator.mediaDevices.enumerateDevices();
|
|
const videoDevices = devices.filter(device => device.kind === 'videoinput');
|
|
|
|
deviceSelect.innerHTML = '';
|
|
|
|
if (videoDevices.length === 0) {
|
|
deviceSelect.innerHTML = '<option value="">未找到视频设备</option>';
|
|
showStatus('未找到可用的视频设备', 'error');
|
|
return;
|
|
}
|
|
|
|
deviceSelect.innerHTML = '<option value="">请选择视频设备</option>';
|
|
videoDevices.forEach(device => {
|
|
const option = document.createElement('option');
|
|
option.value = device.deviceId;
|
|
option.textContent = device.label || `摄像头 ${device.deviceId.slice(0, 8)}...`;
|
|
deviceSelect.appendChild(option);
|
|
});
|
|
|
|
showStatus(`成功加载 ${videoDevices.length} 个视频设备`, 'success');
|
|
} catch (error) {
|
|
console.error('加载设备失败:', error);
|
|
deviceSelect.innerHTML = '<option value="">加载设备失败</option>';
|
|
showStatus('加载设备失败: ' + error.message, 'error');
|
|
}
|
|
}
|
|
|
|
// 开始预览
|
|
async function startPreview() {
|
|
const deviceId = deviceSelect.value;
|
|
if (!deviceId) {
|
|
showStatus('请先选择一个视频设备', 'error');
|
|
return;
|
|
}
|
|
|
|
try {
|
|
startBtn.disabled = true;
|
|
startBtn.innerHTML = '<span class="loading"></span>启动中...';
|
|
showStatus('正在启动预览...', 'info');
|
|
|
|
// 获取视频流
|
|
const constraints = {
|
|
video: {
|
|
deviceId: { exact: deviceId },
|
|
width: { ideal: 1280 },
|
|
height: { ideal: 720 }
|
|
}
|
|
};
|
|
|
|
currentStream = await navigator.mediaDevices.getUserMedia(constraints);
|
|
previewVideo.srcObject = currentStream;
|
|
|
|
// 显示预览视频
|
|
previewPlaceholder.classList.add('hidden');
|
|
previewVideo.classList.remove('hidden');
|
|
|
|
// 启动流媒体服务
|
|
const result = await ipcRenderer.invoke('start-preview', deviceId);
|
|
|
|
if (result.success) {
|
|
isStreaming = true;
|
|
startBtn.classList.add('hidden');
|
|
stopBtn.classList.remove('hidden');
|
|
deviceSelect.disabled = true;
|
|
refreshBtn.disabled = true;
|
|
|
|
// 显示流媒体信息
|
|
streamUrl.href = result.streamUrl;
|
|
streamInfo.classList.remove('hidden');
|
|
|
|
showStatus('预览已启动,流媒体服务已就绪', 'success');
|
|
|
|
// 开始广播视频帧
|
|
startBroadcasting();
|
|
} else {
|
|
throw new Error(result.message);
|
|
}
|
|
} catch (error) {
|
|
console.error('启动预览失败:', error);
|
|
showStatus('启动预览失败: ' + error.message, 'error');
|
|
|
|
if (currentStream) {
|
|
currentStream.getTracks().forEach(track => track.stop());
|
|
currentStream = null;
|
|
}
|
|
|
|
previewVideo.classList.add('hidden');
|
|
previewPlaceholder.classList.remove('hidden');
|
|
} finally {
|
|
startBtn.disabled = false;
|
|
startBtn.innerHTML = '▶️ 开始预览';
|
|
}
|
|
}
|
|
|
|
// 停止预览
|
|
async function stopPreview() {
|
|
try {
|
|
showStatus('正在停止预览...', 'info');
|
|
|
|
// 停止视频流
|
|
if (currentStream) {
|
|
currentStream.getTracks().forEach(track => track.stop());
|
|
currentStream = null;
|
|
}
|
|
|
|
// 停止流媒体服务
|
|
await ipcRenderer.invoke('stop-preview');
|
|
|
|
// 重置UI
|
|
isStreaming = false;
|
|
previewVideo.classList.add('hidden');
|
|
previewPlaceholder.classList.remove('hidden');
|
|
streamInfo.classList.add('hidden');
|
|
|
|
startBtn.classList.remove('hidden');
|
|
stopBtn.classList.add('hidden');
|
|
deviceSelect.disabled = false;
|
|
refreshBtn.disabled = false;
|
|
|
|
showStatus('预览已停止', 'success');
|
|
} catch (error) {
|
|
console.error('停止预览失败:', error);
|
|
showStatus('停止预览失败: ' + error.message, 'error');
|
|
}
|
|
}
|
|
|
|
// 广播视频帧
|
|
function startBroadcasting() {
|
|
if (!currentStream || !isStreaming) return;
|
|
|
|
const canvas = document.createElement('canvas');
|
|
const ctx = canvas.getContext('2d');
|
|
|
|
function captureFrame() {
|
|
if (!isStreaming || !currentStream) return;
|
|
|
|
const video = previewVideo;
|
|
if (video.videoWidth && video.videoHeight) {
|
|
canvas.width = video.videoWidth;
|
|
canvas.height = video.videoHeight;
|
|
ctx.drawImage(video, 0, 0);
|
|
|
|
// 将帧数据发送到主进程
|
|
const frameData = canvas.toDataURL('image/jpeg', 0.8);
|
|
ipcRenderer.send('broadcast-frame', frameData);
|
|
}
|
|
|
|
if (isStreaming) {
|
|
setTimeout(captureFrame, 33); // 30 FPS
|
|
}
|
|
}
|
|
|
|
// 等待视频加载完成后开始捕获
|
|
previewVideo.addEventListener('loadedmetadata', () => {
|
|
setTimeout(captureFrame, 1000);
|
|
});
|
|
}
|
|
|
|
// 事件监听器
|
|
deviceSelect.addEventListener('change', () => {
|
|
startBtn.disabled = !deviceSelect.value;
|
|
});
|
|
|
|
refreshBtn.addEventListener('click', loadDevices);
|
|
startBtn.addEventListener('click', startPreview);
|
|
stopBtn.addEventListener('click', stopPreview);
|
|
|
|
// 页面加载完成后初始化
|
|
document.addEventListener('DOMContentLoaded', () => {
|
|
loadDevices();
|
|
});
|
|
</script>
|
|
</body>
|
|
</html> |