feat: Lyrics
This commit is contained in:
1 parent
6ed3e169e4
commit
82b1fa0d47
17 files changed
+3863
-283
No files matched your search
+367
-22
@@ -1,7 +1,9 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<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 {
|
||||
@@ -13,6 +15,8 @@
|
||||
overflow: hidden;
|
||||
font-family: system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
|
||||
}
|
||||
|
||||
/* ── 下三分之一条(原有功能) ─────────────────────────────── */
|
||||
.lt-root {
|
||||
position: fixed;
|
||||
inset: 0;
|
||||
@@ -37,16 +41,13 @@
|
||||
0 20px 40px rgba(0, 0, 0, 0.8),
|
||||
0 0 60px rgba(255, 140, 0, 0.45);
|
||||
backdrop-filter: blur(18px);
|
||||
|
||||
overflow: hidden;
|
||||
mask-image: radial-gradient(circle at 0 0, rgba(0,0,0,0.0), rgba(0,0,0,1) 45%),
|
||||
}
|
||||
}
|
||||
.lt-container::before {
|
||||
content: '';
|
||||
position: absolute;
|
||||
inset: -40%;
|
||||
background-image: url('bg.png');
|
||||
|
||||
opacity: 0.85;
|
||||
mix-blend-mode: screen;
|
||||
pointer-events: none;
|
||||
@@ -106,20 +107,81 @@
|
||||
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">
|
||||
<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');
|
||||
@@ -127,11 +189,12 @@
|
||||
let iframeEl = null;
|
||||
let autoHideTimer = null;
|
||||
|
||||
// Replace {{key}} placeholders in html string with values from contentData
|
||||
function fillPlaceholders(html, contentData) {
|
||||
if (!html || !contentData) return html || '';
|
||||
return html.replace(/\{\{(\w+)\}\}/g, (match, key) => {
|
||||
return key in contentData ? String(contentData[key]) : match;
|
||||
return Object.prototype.hasOwnProperty.call(contentData, key)
|
||||
? String(contentData[key])
|
||||
: match;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -156,7 +219,6 @@
|
||||
}
|
||||
|
||||
function applyPreset(preset, contentData, autoHideDuration) {
|
||||
// Clear any pending auto-hide timer
|
||||
if (autoHideTimer) {
|
||||
clearTimeout(autoHideTimer);
|
||||
autoHideTimer = null;
|
||||
@@ -188,9 +250,8 @@
|
||||
document.head.appendChild(style);
|
||||
}
|
||||
|
||||
// 纯外观 preset:如果 kind=iframe,则用 iframe srcdoc 完全接管内部内容
|
||||
// kind=iframe:用 iframe srcdoc 完全接管内部内容
|
||||
if (preset.kind === 'iframe' && preset.html) {
|
||||
// 清空旧文字容器
|
||||
titleEl.textContent = '';
|
||||
subtitleEl.textContent = '';
|
||||
if (!iframeEl) {
|
||||
@@ -204,41 +265,325 @@
|
||||
ltInner.innerHTML = '';
|
||||
ltInner.appendChild(iframeEl);
|
||||
}
|
||||
// Fill placeholders with content data before setting srcdoc
|
||||
const processedHtml = fillPlaceholders(preset.html, contentData || {});
|
||||
iframeEl.srcdoc = processedHtml;
|
||||
iframeEl.srcdoc = fillPlaceholders(preset.html, contentData || {});
|
||||
}
|
||||
|
||||
resetAnimation();
|
||||
lt.classList.add('show');
|
||||
|
||||
// Schedule auto-hide if duration > 0
|
||||
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 wsUrl = proto + '//' + loc.host;
|
||||
const ws = new WebSocket(wsUrl);
|
||||
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 {
|
||||
const msg = JSON.parse(event.data);
|
||||
if (msg.type === 'activePreset') {
|
||||
applyPreset(msg.preset, msg.contentData, msg.autoHideDuration);
|
||||
}
|
||||
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 = () => {
|
||||
// simple retry
|
||||
wsRef = null;
|
||||
if (clockTimer) clearInterval(clockTimer);
|
||||
clockTimer = null;
|
||||
stopTicker();
|
||||
// 断线时保留最后一帧字幕,重连后会收到完整状态并重新同步
|
||||
setTimeout(connectWs, 2000);
|
||||
};
|
||||
|
||||
ws.onerror = () => {
|
||||
try { ws.close(); } catch (_) { /* ignore */ }
|
||||
};
|
||||
}
|
||||
|
||||
connectWs();
|
||||
|
||||
Reference in new issue
Block a user