跳到正文

剪贴板

剪贴板 h3g3ihfo

xun_GF
原文
剪贴板信息

剪贴板信息

发布
收录
更新
HTML
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>视频转盲文</title>
    <style>
        body {
            padding: 20px;
            background: #f5f5f5;
            font-family: Arial, sans-serif;
        }
        .panel {
            margin-bottom: 15px;
            display: flex;
            gap: 15px;
            align-items: center;
            flex-wrap: wrap;
        }
        button {
            padding: 8px 14px;
            cursor: pointer;
            background: #007bff;
            color: white;
            border: none;
            border-radius: 4px;
        }
        button:hover {
            background: #0056b3;
        }
        #brailleOutput {
            font-family: monospace;
            white-space: pre;
            font-size: 4px;
            line-height: 1;
            background: #ffffff;
            color: #000000;
            padding: 15px;
            margin-top: 15px;
            border: 1px solid #ccc;
            border-radius: 4px;
            overflow: auto;
            max-height: 800px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
        }
        video, canvas {
            display: none;
        }
        .slider-group {
            display: flex;
            flex-direction: column;
            gap: 5px;
            min-width: 300px;
        }
        input[type="range"] {
            width: 300px;
        }
        label {
            color: #333;
            font-weight: 500;
        }
        h3 {
            color: #333;
            margin-top: 0;
        }
        .info {
            color: #666;
            font-size: 14px;
            margin-left: 10px;
        }
    </style>
</head>
<body>

<h3>🎬 视频转盲文实时播放 🎬</span></h3>

<div class="panel">
    <input type="file" id="videoFile" accept="video/*">
    <button onclick="playVideo()">▶ 播放</button>
    <button onclick="pauseVideo()">⏸️ 暂停</button>
    <button onclick="stopVideo()">⏹️ 停止</button>
</div>

<div class="panel">
    <div class="slider-group">
        <label>宽度: <span id="wValue">200</span></label>
        <input type="range" id="width" min="10" max="1000" value="200">
    </div>
    <div class="slider-group">
        <label>高度: <span id="hValue">150</span></label>
        <input type="range" id="height" min="10" max="1000" value="150">
    </div>
    <div class="slider-group">
        <label>二值化阈值: <span id="tValue">128</span></label>
        <input type="range" id="threshold" min="10" max="255" value="128">
    </div>
    <div class="slider-group">
        <label>帧率限制: <span id="fValue">15</span> fps</label>
        <input type="range" id="fps" min="1" max="60" value="15">
    </div>
</div>

<div id="brailleOutput">⬆️ 选择视频文件,点击播放 ⬆️</div>

<video id="video" muted></video>
<canvas id="canvas"></canvas>

<script>
const video = document.getElementById('video');
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const output = document.getElementById('brailleOutput');

let animationId = null;
let isPlaying = false;
let lastFrameTime = 0;

// 滑块事件
document.getElementById('width').addEventListener('input', (e) => {
    document.getElementById('wValue').innerText = e.target.value;
});
document.getElementById('height').addEventListener('input', (e) => {
    document.getElementById('hValue').innerText = e.target.value;
});
document.getElementById('threshold').addEventListener('input', (e) => {
    document.getElementById('tValue').innerText = e.target.value;
});
document.getElementById('fps').addEventListener('input', (e) => {
    document.getElementById('fValue').innerText = e.target.value;
});

// 加载视频
document.getElementById('videoFile').addEventListener('change', (e) => {
    const file = e.target.files[0];
    if (!file) return;
    
    video.src = URL.createObjectURL(file);
    video.load();
    stopVideo();
    output.innerText = '✅ 视频已加载,点击播放';
    output.style.color = '#000';
});

// 盲文转换函数
function pixel_to_braille(block) {
    let b = 0;
    if (block[0][0] < 128) b |= 0b00000001;
    if (block[1][0] < 128) b |= 0b00000010;
    if (block[2][0] < 128) b |= 0b00000100;
    if (block[0][1] < 128) b |= 0b00001000;
    if (block[1][1] < 128) b |= 0b00010000;
    if (block[2][1] < 128) b |= 0b00100000;
    if (block[3] && block[3][0] < 128) b |= 0b01000000;
    if (block[3] && block[3][1] < 128) b |= 0b10000000;
    return String.fromCharCode(0x2800 + b);
}

function get(pixels, w, x, y) {
    return pixels[y * w + x] ?? 255;
}

// 处理单帧
function processFrame() {
    const W = parseInt(document.getElementById('width').value);
    const H = parseInt(document.getElementById('height').value);
    const T = parseInt(document.getElementById('threshold').value);
    
    canvas.width = W;
    canvas.height = H;
    ctx.drawImage(video, 0, 0, W, H);
    
    const data = ctx.getImageData(0, 0, W, H).data;
    const pixels = [];
    for (let i = 0; i < data.length; i += 4) {
        pixels.push(data[i] < T ? 0 : 255);
    }
    
    let txt = "";
    for (let y = 0; y < H; y += 4) {
        for (let x = 0; x < W; x += 2) {
            const block = [
                [get(pixels, W, x, y), get(pixels, W, x+1, y)],
                [get(pixels, W, x, y+1), get(pixels, W, x+1, y+1)],
                [get(pixels, W, x, y+2), get(pixels, W, x+1, y+2)],
                [get(pixels, W, x, y+3), get(pixels, W, x+1, y+3)],
            ];
            txt += pixel_to_braille(block);
        }
        txt += "\n";
    }
    
    output.innerText = txt;
}

// 动画循环(帧率限制)
function animate(timestamp) {
    if (!isPlaying) return;
    
    const fps = parseInt(document.getElementById('fps').value);
    const frameInterval = 1000 / fps;
    
    if (timestamp - lastFrameTime >= frameInterval) {
        if (!video.paused && !video.ended) {
            processFrame();
        }
        lastFrameTime = timestamp;
    }
    
    animationId = requestAnimationFrame(animate);
}

function playVideo() {
    if (!video.src) {
        alert('请先选择视频文件');
        return;
    }
    video.play();
    isPlaying = true;
    lastFrameTime = performance.now();
    animate(lastFrameTime);
}

function pauseVideo() {
    video.pause();
    isPlaying = false;
    if (animationId) {
        cancelAnimationFrame(animationId);
    }
}

function stopVideo() {
    video.pause();
    video.currentTime = 0;
    isPlaying = false;
    if (animationId) {
        cancelAnimationFrame(animationId);
    }
    output.innerText = '⏹️ 已停止';
}
</script>
</body>
</html>