593 lines
18 KiB
HTML
593 lines
18 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="zh-CN">
|
|
<head>
|
|
<meta charset="UTF-8" />
|
|
<link rel="icon" href="data:," />
|
|
<link rel="stylesheet" href="/lrc.css" />
|
|
<title>OBS Lower Third Widget</title>
|
|
<style>
|
|
html, body {
|
|
margin: 0;
|
|
padding: 0;
|
|
width: 100%;
|
|
height: 100%;
|
|
background: transparent;
|
|
overflow: hidden;
|
|
font-family: system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
|
|
}
|
|
|
|
/* ── 下三分之一条(原有功能) ─────────────────────────────── */
|
|
.lt-root {
|
|
position: fixed;
|
|
inset: 0;
|
|
display: flex;
|
|
justify-content: flex-end; /* 默认右侧类似晚会信息区 */
|
|
align-items: center;
|
|
pointer-events: none;
|
|
padding: 5vh 6vw;
|
|
box-sizing: border-box;
|
|
}
|
|
.lt-container {
|
|
position: relative;
|
|
display: inline-block;
|
|
min-width: 420px;
|
|
max-width: 560px;
|
|
color: #fff;
|
|
border-radius: 22px;
|
|
padding: 16px 26px;
|
|
opacity: 0;
|
|
transform: translateX(40px) translateY(6px) scale(0.96);
|
|
box-shadow:
|
|
0 20px 40px rgba(0, 0, 0, 0.8),
|
|
0 0 60px rgba(255, 140, 0, 0.45);
|
|
backdrop-filter: blur(18px);
|
|
overflow: hidden;
|
|
}
|
|
.lt-container::before {
|
|
content: '';
|
|
position: absolute;
|
|
inset: -40%;
|
|
background-image: url('bg.png');
|
|
opacity: 0.85;
|
|
mix-blend-mode: screen;
|
|
pointer-events: none;
|
|
}
|
|
.lt-container-inner {
|
|
position: relative;
|
|
z-index: 1;
|
|
}
|
|
.lt-container.show {
|
|
animation: lt-enter 0.7s cubic-bezier(0.18, 0.9, 0.3, 1.2) forwards;
|
|
}
|
|
.lt-container.hide {
|
|
animation: lt-exit 0.55s ease-in forwards;
|
|
}
|
|
@keyframes lt-enter {
|
|
0% {
|
|
opacity: 0;
|
|
transform: translateX(70px) translateY(10px) scale(0.9);
|
|
filter: blur(8px);
|
|
}
|
|
55% {
|
|
opacity: 1;
|
|
transform: translateX(-10px) translateY(0) scale(1.03);
|
|
filter: blur(0px);
|
|
}
|
|
100% {
|
|
opacity: 1;
|
|
transform: translateX(0) translateY(0) scale(1);
|
|
filter: blur(0px);
|
|
}
|
|
}
|
|
@keyframes lt-exit {
|
|
0% {
|
|
opacity: 1;
|
|
transform: translateX(0) translateY(0) scale(1);
|
|
filter: blur(0px);
|
|
}
|
|
100% {
|
|
opacity: 0;
|
|
transform: translateX(40px) translateY(8px) scale(0.95);
|
|
filter: blur(4px);
|
|
}
|
|
}
|
|
.lt-title {
|
|
font-size: 24px;
|
|
font-weight: 800;
|
|
line-height: 1.2;
|
|
letter-spacing: 0.18em;
|
|
text-transform: uppercase;
|
|
text-shadow:
|
|
0 0 18px rgba(0,0,0,0.9),
|
|
0 0 32px rgba(255,180,120,0.6);
|
|
}
|
|
.lt-subtitle {
|
|
margin-top: 8px;
|
|
font-size: 15px;
|
|
opacity: 0.95;
|
|
line-height: 1.45;
|
|
}
|
|
|
|
/* ── LRC 字幕层 ──────────────────────────────────────────── */
|
|
.lrc-layer {
|
|
position: fixed;
|
|
inset: 0;
|
|
display: flex;
|
|
z-index: 30;
|
|
pointer-events: none;
|
|
padding: 6vh 4vw;
|
|
box-sizing: border-box;
|
|
opacity: 0;
|
|
transition: opacity 0.22s ease;
|
|
}
|
|
.lrc-layer.visible { opacity: 1; }
|
|
.lrc-layer.pos-top { align-items: flex-start; justify-content: center; }
|
|
.lrc-layer.pos-center { align-items: center; justify-content: center; }
|
|
.lrc-layer.pos-bottom { align-items: flex-end; justify-content: center; }
|
|
|
|
.lrc-align-left { justify-content: flex-start !important; }
|
|
.lrc-align-center { justify-content: center !important; }
|
|
.lrc-align-right { justify-content: flex-end !important; }
|
|
|
|
.lrc-box {
|
|
display: inline-block;
|
|
box-sizing: border-box;
|
|
text-align: center;
|
|
max-width: 86%;
|
|
transition: transform 0.2s ease;
|
|
}
|
|
.lrc-text {
|
|
white-space: pre-line;
|
|
word-break: break-word;
|
|
}
|
|
.lrc-next {
|
|
white-space: pre-line;
|
|
word-break: break-word;
|
|
margin-top: 0.35em;
|
|
}
|
|
.lrc-next:empty { display: none; }
|
|
|
|
.lrc-debug {
|
|
position: fixed;
|
|
top: 8px;
|
|
left: 8px;
|
|
z-index: 50;
|
|
color: rgba(255,255,255,0.6);
|
|
font: 11px/1.4 monospace;
|
|
display: none;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<!-- 下三分之一条 -->
|
|
<div class="lt-root">
|
|
<div id="lt" class="lt-container">
|
|
<div class="lt-container-inner" id="ltInner">
|
|
<img src="/zi.png" alt="" />
|
|
<div class="lt-title" id="ltTitle"></div>
|
|
<div class="lt-subtitle" id="ltSubtitle"></div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- LRC 字幕 -->
|
|
<div id="lrcLayer" class="lrc-layer pos-bottom lrc-align-center lrc-anim-fade">
|
|
<div id="lrcBox" class="lrc-box">
|
|
<div id="lrcText" class="lrc-text"></div>
|
|
<div id="lrcNext" class="lrc-next"></div>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
'use strict';
|
|
|
|
// ── 下三分之一条 ──────────────────────────────────────────
|
|
const lt = document.getElementById('lt');
|
|
const ltInner = document.getElementById('ltInner');
|
|
const titleEl = document.getElementById('ltTitle');
|
|
const subtitleEl = document.getElementById('ltSubtitle');
|
|
let iframeEl = null;
|
|
let autoHideTimer = null;
|
|
|
|
function fillPlaceholders(html, contentData) {
|
|
if (!html || !contentData) return html || '';
|
|
return html.replace(/\{\{(\w+)\}\}/g, (match, key) => {
|
|
return Object.prototype.hasOwnProperty.call(contentData, key)
|
|
? String(contentData[key])
|
|
: match;
|
|
});
|
|
}
|
|
|
|
function resetAnimation() {
|
|
lt.classList.remove('show', 'hide');
|
|
// 强制重排让下一次动画生效
|
|
void lt.offsetWidth;
|
|
}
|
|
|
|
function scheduleAutoHide(ms) {
|
|
if (autoHideTimer) {
|
|
clearTimeout(autoHideTimer);
|
|
autoHideTimer = null;
|
|
}
|
|
if (ms > 0) {
|
|
autoHideTimer = setTimeout(() => {
|
|
autoHideTimer = null;
|
|
resetAnimation();
|
|
lt.classList.add('hide');
|
|
}, ms);
|
|
}
|
|
}
|
|
|
|
function applyPreset(preset, contentData, autoHideDuration) {
|
|
if (autoHideTimer) {
|
|
clearTimeout(autoHideTimer);
|
|
autoHideTimer = null;
|
|
}
|
|
|
|
if (!preset) {
|
|
resetAnimation();
|
|
lt.classList.add('hide');
|
|
return;
|
|
}
|
|
|
|
// 对齐:控制整块在 OBS 画面内的左右位置
|
|
const root = lt.parentElement;
|
|
if (preset.align === 'center') {
|
|
root.style.justifyContent = 'center';
|
|
} else if (preset.align === 'left') {
|
|
root.style.justifyContent = 'flex-start';
|
|
} else {
|
|
root.style.justifyContent = 'flex-end';
|
|
}
|
|
|
|
// 外观层额外 CSS(对 .lt-root / .lt-container 做全局修饰)
|
|
const existingStyle = document.getElementById('dynamic-style');
|
|
if (existingStyle) existingStyle.remove();
|
|
if (preset.extraCss) {
|
|
const style = document.createElement('style');
|
|
style.id = 'dynamic-style';
|
|
style.textContent = preset.extraCss;
|
|
document.head.appendChild(style);
|
|
}
|
|
|
|
// kind=iframe:用 iframe srcdoc 完全接管内部内容
|
|
if (preset.kind === 'iframe' && preset.html) {
|
|
titleEl.textContent = '';
|
|
subtitleEl.textContent = '';
|
|
if (!iframeEl) {
|
|
iframeEl = document.createElement('iframe');
|
|
iframeEl.setAttribute('sandbox', 'allow-scripts allow-same-origin');
|
|
iframeEl.style.border = 'none';
|
|
iframeEl.style.width = '100%';
|
|
iframeEl.style.height = '100%';
|
|
iframeEl.style.display = 'block';
|
|
iframeEl.style.background = 'transparent';
|
|
ltInner.innerHTML = '';
|
|
ltInner.appendChild(iframeEl);
|
|
}
|
|
iframeEl.srcdoc = fillPlaceholders(preset.html, contentData || {});
|
|
}
|
|
|
|
resetAnimation();
|
|
lt.classList.add('show');
|
|
|
|
if (typeof autoHideDuration === 'number' && autoHideDuration > 0) {
|
|
scheduleAutoHide(autoHideDuration);
|
|
}
|
|
}
|
|
|
|
// ── LRC 字幕 ──────────────────────────────────────────────
|
|
const lrcLayer = document.getElementById('lrcLayer');
|
|
const lrcBox = document.getElementById('lrcBox');
|
|
const lrcText = document.getElementById('lrcText');
|
|
const lrcNext = document.getElementById('lrcNext');
|
|
|
|
/** 服务器时间 - 本地时间(毫秒) */
|
|
let clockSkewMs = 0;
|
|
let clockSamples = [];
|
|
let wsRef = null;
|
|
|
|
const lrc = {
|
|
active: false,
|
|
cues: [],
|
|
style: null,
|
|
anchor: 0,
|
|
paused: false,
|
|
pausedElapsed: 0,
|
|
tailMs: 4000,
|
|
shownIndex: -2,
|
|
tickTimer: null,
|
|
spans: null,
|
|
};
|
|
|
|
let lrcCustomStyleEl = null;
|
|
|
|
function applyLrcCustomCss(css) {
|
|
if (!lrcCustomStyleEl) {
|
|
lrcCustomStyleEl = document.createElement('style');
|
|
lrcCustomStyleEl.id = 'lrc-custom-style';
|
|
document.head.appendChild(lrcCustomStyleEl);
|
|
}
|
|
lrcCustomStyleEl.textContent = css || '';
|
|
}
|
|
|
|
function estimateClockSkew(t0, serverNow) {
|
|
const now = Date.now();
|
|
const rtt = Math.max(0, now - t0);
|
|
const sample = serverNow - (t0 + rtt / 2);
|
|
clockSamples.push(sample);
|
|
if (clockSamples.length > 7) clockSamples.shift();
|
|
const sorted = clockSamples.slice().sort((a, b) => a - b);
|
|
clockSkewMs = sorted[Math.floor(sorted.length / 2)];
|
|
}
|
|
|
|
function requestClockSync() {
|
|
if (wsRef && wsRef.readyState === WebSocket.OPEN) {
|
|
wsRef.send(JSON.stringify({ type: 'clock', t0: Date.now() }));
|
|
}
|
|
}
|
|
|
|
function serverNow() {
|
|
return Date.now() + clockSkewMs;
|
|
}
|
|
|
|
function applyLrcStyle(style) {
|
|
const s = style || {};
|
|
lrcLayer.className = 'lrc-layer';
|
|
lrcLayer.classList.add('pos-' + (s.position || 'bottom'));
|
|
lrcLayer.classList.add('lrc-align-' + (s.align || 'center'));
|
|
lrcLayer.classList.add('lrc-anim-' + (s.animation || 'fade'));
|
|
lrcLayer.style.paddingBottom = '0px';
|
|
lrcLayer.style.paddingTop = '0px';
|
|
|
|
if (s.position === 'bottom') {
|
|
lrcLayer.style.paddingBottom = (Number(s.bottomOffsetVh) || 0) + 'vh';
|
|
} else if (s.position === 'top') {
|
|
lrcLayer.style.paddingTop = (Number(s.bottomOffsetVh) || 0) + 'vh';
|
|
}
|
|
|
|
lrcBox.style.maxWidth = (Number(s.maxWidthPercent) || 86) + '%';
|
|
lrcBox.style.background = s.backgroundEnabled === false
|
|
? 'transparent'
|
|
: (s.background || 'transparent');
|
|
lrcBox.style.padding = (Number(s.paddingY) || 0) + 'px ' + (Number(s.paddingX) || 0) + 'px';
|
|
lrcBox.style.borderRadius = (Number(s.borderRadius) || 0) + 'px';
|
|
|
|
const shadow = s.textShadowEnabled === false ? 'none' : (s.textShadow || 'none');
|
|
lrcText.style.fontFamily = s.fontFamily || '';
|
|
lrcText.style.fontSize = (Number(s.fontSize) || 42) + 'px';
|
|
lrcText.style.fontWeight = String(Number(s.fontWeight) || 700);
|
|
lrcText.style.color = s.color || '#fff';
|
|
lrcText.style.letterSpacing = (Number(s.letterSpacing) || 0) + 'px';
|
|
lrcText.style.lineHeight = String(Number(s.lineHeight) || 1.35);
|
|
lrcText.style.textShadow = shadow;
|
|
lrcText.style.setProperty('--lrc-color', s.color || '#fff');
|
|
lrcText.style.setProperty(
|
|
'--lrc-karaoke-pending',
|
|
s.karaokePendingColor || 'rgba(255, 255, 255, 0.45)'
|
|
);
|
|
|
|
lrcNext.style.fontFamily = s.fontFamily || '';
|
|
lrcNext.style.fontSize = ((Number(s.fontSize) || 42) * (Number(s.nextLineScale) || 0.68)) + 'px';
|
|
lrcNext.style.color = s.nextLineColor || 'rgba(255,255,255,0.55)';
|
|
lrcNext.style.textShadow = shadow;
|
|
lrcNext.style.display = s.showNextLine === false ? 'none' : '';
|
|
|
|
applyLrcCustomCss(s.customCss || '');
|
|
}
|
|
|
|
function findCueIndex(elapsed) {
|
|
const cues = lrc.cues;
|
|
if (!cues.length) return -1;
|
|
if (elapsed < cues[0].t) return -1;
|
|
let lo = 0;
|
|
let hi = cues.length - 1;
|
|
let result = 0;
|
|
while (lo <= hi) {
|
|
const mid = (lo + hi) >> 1;
|
|
if (cues[mid].t <= elapsed) {
|
|
result = mid;
|
|
lo = mid + 1;
|
|
} else {
|
|
hi = mid - 1;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
function hideLrc() {
|
|
lrcLayer.classList.remove('visible');
|
|
}
|
|
|
|
function showLrc() {
|
|
lrcLayer.classList.add('visible');
|
|
}
|
|
|
|
function cueUsesKaraoke(cue) {
|
|
return !!(lrc.style && lrc.style.karaoke !== false && cue
|
|
&& Array.isArray(cue.segments) && cue.segments.length >= 2);
|
|
}
|
|
|
|
function buildKaraoke(cue) {
|
|
lrcText.textContent = '';
|
|
const spans = [];
|
|
cue.segments.forEach((segment) => {
|
|
const span = document.createElement('span');
|
|
span.className = 'lrc-word is-pending';
|
|
span.dataset.at = String(segment.at);
|
|
span.textContent = segment.text;
|
|
lrcText.appendChild(span);
|
|
spans.push(span);
|
|
});
|
|
lrc.spans = spans;
|
|
updateKaraoke(currentElapsed());
|
|
}
|
|
|
|
function updateKaraoke(elapsed) {
|
|
if (!lrc.spans || !lrc.spans.length) return;
|
|
let current = -1;
|
|
for (let i = 0; i < lrc.spans.length; i += 1) {
|
|
if (Number(lrc.spans[i].dataset.at) <= elapsed) current = i;
|
|
}
|
|
for (let i = 0; i < lrc.spans.length; i += 1) {
|
|
const sung = i <= current;
|
|
lrc.spans[i].classList.toggle('is-sung', sung);
|
|
lrc.spans[i].classList.toggle('is-pending', !sung);
|
|
lrc.spans[i].classList.toggle('is-current', i === current);
|
|
}
|
|
}
|
|
|
|
function renderCue(index) {
|
|
if (index === lrc.shownIndex) return;
|
|
lrc.shownIndex = index;
|
|
|
|
if (index < 0) {
|
|
hideLrc();
|
|
lrc.spans = null;
|
|
lrcText.textContent = '';
|
|
lrcNext.textContent = '';
|
|
return;
|
|
}
|
|
|
|
const cue = lrc.cues[index];
|
|
if (!cue) {
|
|
hideLrc();
|
|
return;
|
|
}
|
|
|
|
if (cueUsesKaraoke(cue)) {
|
|
buildKaraoke(cue);
|
|
} else {
|
|
lrc.spans = null;
|
|
lrcText.textContent = cue.text;
|
|
}
|
|
|
|
const next = lrc.cues[index + 1];
|
|
// 逐字模式下下一句也可能带增强标签,这里统一只显示纯文本
|
|
lrcNext.textContent = next ? next.text : '';
|
|
|
|
// 重启入场动画
|
|
lrcText.style.animation = 'none';
|
|
void lrcText.offsetWidth;
|
|
lrcText.style.animation = '';
|
|
showLrc();
|
|
}
|
|
|
|
function currentElapsed() {
|
|
if (lrc.paused) return lrc.pausedElapsed;
|
|
return serverNow() - lrc.anchor;
|
|
}
|
|
|
|
function tickLrc() {
|
|
if (!lrc.active) return;
|
|
const elapsed = currentElapsed();
|
|
const last = lrc.cues[lrc.cues.length - 1];
|
|
if (last && elapsed > last.t + lrc.tailMs) {
|
|
hideLrc();
|
|
return;
|
|
}
|
|
renderCue(findCueIndex(elapsed));
|
|
updateKaraoke(elapsed);
|
|
}
|
|
|
|
function startTicker() {
|
|
if (lrc.tickTimer) return;
|
|
lrc.tickTimer = setInterval(tickLrc, 50);
|
|
}
|
|
|
|
function stopTicker() {
|
|
if (lrc.tickTimer) {
|
|
clearInterval(lrc.tickTimer);
|
|
lrc.tickTimer = null;
|
|
}
|
|
}
|
|
|
|
function applyLrcState(state) {
|
|
if (!state || !state.playing) {
|
|
const reason = state && state.reason;
|
|
lrc.active = false;
|
|
stopTicker();
|
|
hideLrc();
|
|
lrc.shownIndex = -2;
|
|
if (reason === 'stopped' || reason === 'ended' || reason === 'cleared' || reason === 'preset-deleted') {
|
|
setTimeout(() => {
|
|
if (!lrc.active) {
|
|
lrc.spans = null;
|
|
lrcText.textContent = '';
|
|
lrcNext.textContent = '';
|
|
}
|
|
}, 240);
|
|
}
|
|
return;
|
|
}
|
|
|
|
const isSameRun = lrc.active && lrc.anchor === state.anchor && lrc.presetId === state.presetId;
|
|
lrc.active = true;
|
|
lrc.presetId = state.presetId;
|
|
lrc.cues = Array.isArray(state.cues) ? state.cues : [];
|
|
lrc.style = state.style || {};
|
|
lrc.anchor = state.anchor;
|
|
lrc.paused = !!state.paused;
|
|
lrc.pausedElapsed = Number(state.pausedElapsed) || 0;
|
|
lrc.tailMs = Number(state.tailMs) || 0;
|
|
|
|
if (!isSameRun) {
|
|
applyLrcStyle(lrc.style);
|
|
lrc.shownIndex = -2;
|
|
}
|
|
|
|
startTicker();
|
|
tickLrc();
|
|
}
|
|
|
|
// ── WebSocket ────────────────────────────────────────────
|
|
let clockTimer = null;
|
|
|
|
function connectWs() {
|
|
const loc = window.location;
|
|
const proto = loc.protocol === 'https:' ? 'wss:' : 'ws:';
|
|
const ws = new WebSocket(proto + '//' + loc.host);
|
|
wsRef = ws;
|
|
|
|
ws.onopen = () => {
|
|
requestClockSync();
|
|
if (clockTimer) clearInterval(clockTimer);
|
|
clockTimer = setInterval(requestClockSync, 20000);
|
|
};
|
|
|
|
ws.onmessage = (event) => {
|
|
let msg;
|
|
try {
|
|
msg = JSON.parse(event.data);
|
|
} catch (e) {
|
|
console.error('WS message parse error', e);
|
|
return;
|
|
}
|
|
if (msg.type === 'activePreset') {
|
|
applyPreset(msg.preset, msg.contentData, msg.autoHideDuration);
|
|
} else if (msg.type === 'lrcState') {
|
|
applyLrcState(msg.state);
|
|
} else if (msg.type === 'clock') {
|
|
estimateClockSkew(Number(msg.t0) || 0, Number(msg.serverNow) || Date.now());
|
|
}
|
|
};
|
|
|
|
ws.onclose = () => {
|
|
wsRef = null;
|
|
if (clockTimer) clearInterval(clockTimer);
|
|
clockTimer = null;
|
|
stopTicker();
|
|
// 断线时保留最后一帧字幕,重连后会收到完整状态并重新同步
|
|
setTimeout(connectWs, 2000);
|
|
};
|
|
|
|
ws.onerror = () => {
|
|
try { ws.close(); } catch (_) { /* ignore */ }
|
|
};
|
|
}
|
|
|
|
connectWs();
|
|
</script>
|
|
</body>
|
|
</html>
|