feat: refine subtitle flow and study UI
This commit is contained in:
1 parent
6a6fd347bd
commit
0608b84664
24 files changed
+795
-730
No files matched your search
@@ -23,11 +23,6 @@ const els = {
|
||||
testAiBtn: IS_BROWSER ? document.querySelector("#testAiBtn") : null,
|
||||
processAiSubtitlesBtn: IS_BROWSER ? document.querySelector("#processAiSubtitlesBtn") : null,
|
||||
generateLocalWhisperSubtitlesBtn: IS_BROWSER ? document.querySelector("#generateLocalWhisperSubtitlesBtn") : null,
|
||||
subtitleShiftStatus: IS_BROWSER ? document.querySelector("#subtitleShiftStatus") : null,
|
||||
shiftSubtitleEarlierSmallBtn: IS_BROWSER ? document.querySelector("#shiftSubtitleEarlierSmallBtn") : null,
|
||||
shiftSubtitleLaterSmallBtn: IS_BROWSER ? document.querySelector("#shiftSubtitleLaterSmallBtn") : null,
|
||||
shiftSubtitleEarlierBtn: IS_BROWSER ? document.querySelector("#shiftSubtitleEarlierBtn") : null,
|
||||
shiftSubtitleLaterBtn: IS_BROWSER ? document.querySelector("#shiftSubtitleLaterBtn") : null,
|
||||
refreshBtn: IS_BROWSER ? document.querySelector("#refreshVideosBtn") : null,
|
||||
message: IS_BROWSER ? document.querySelector("#adminMessage") : null,
|
||||
videoList: IS_BROWSER ? document.querySelector("#adminVideoList") : null,
|
||||
@@ -95,10 +90,6 @@ function initializeAdmin() {
|
||||
els.testAiBtn?.addEventListener("click", testAiConnection);
|
||||
els.processAiSubtitlesBtn?.addEventListener("click", processAiSubtitles);
|
||||
els.generateLocalWhisperSubtitlesBtn?.addEventListener("click", generateLocalWhisperSubtitles);
|
||||
els.shiftSubtitleEarlierSmallBtn?.addEventListener("click", () => shiftEditableSubtitles(-0.2));
|
||||
els.shiftSubtitleLaterSmallBtn?.addEventListener("click", () => shiftEditableSubtitles(0.2));
|
||||
els.shiftSubtitleEarlierBtn?.addEventListener("click", () => shiftEditableSubtitles(-0.5));
|
||||
els.shiftSubtitleLaterBtn?.addEventListener("click", () => shiftEditableSubtitles(0.5));
|
||||
bindFileInputs();
|
||||
loadAiStatus();
|
||||
loadVideos();
|
||||
@@ -164,13 +155,14 @@ async function loadAiStatus() {
|
||||
if (!els.aiStatus) return;
|
||||
try {
|
||||
const status = await apiRequest("/api/ai/status");
|
||||
const buttonState = getAiSubtitleButtonState(status, editingVideo);
|
||||
els.aiStatus.textContent = buttonState.hint || status.message || "AI 状态未知。";
|
||||
const aiButtonState = getAiSubtitleButtonState(status, editingVideo);
|
||||
const localWhisperButtonState = getLocalWhisperButtonState(status, editingVideo);
|
||||
els.aiStatus.textContent = aiButtonState.hint || localWhisperButtonState.hint || status.message || "AI 状态未知。";
|
||||
if (els.processAiSubtitlesBtn) {
|
||||
els.processAiSubtitlesBtn.disabled = buttonState.disabled;
|
||||
els.processAiSubtitlesBtn.disabled = aiButtonState.disabled;
|
||||
}
|
||||
if (els.generateLocalWhisperSubtitlesBtn) {
|
||||
els.generateLocalWhisperSubtitlesBtn.disabled = buttonState.disabled;
|
||||
els.generateLocalWhisperSubtitlesBtn.disabled = localWhisperButtonState.disabled;
|
||||
}
|
||||
} catch (error) {
|
||||
els.aiStatus.textContent = error.message || "AI 状态读取失败。";
|
||||
@@ -200,6 +192,52 @@ function getAiSubtitleButtonState(status, selectedVideo) {
|
||||
};
|
||||
}
|
||||
|
||||
function getLocalWhisperButtonState(status, selectedVideo) {
|
||||
if (!status?.localWhisperEnabled) {
|
||||
return {
|
||||
disabled: true,
|
||||
hint: "请先开启本地 Whisper。",
|
||||
};
|
||||
}
|
||||
|
||||
if (!selectedVideo) {
|
||||
return {
|
||||
disabled: true,
|
||||
hint: "请先在右侧素材列表点“编辑”。",
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
disabled: false,
|
||||
hint: "",
|
||||
};
|
||||
}
|
||||
|
||||
function shouldConfirmSubtitleOverwrite(video) {
|
||||
const subtitles = video?.subtitles || {};
|
||||
return Boolean(subtitles.en?.content);
|
||||
}
|
||||
|
||||
function setAiReportText(text) {
|
||||
if (!els.aiReport) return;
|
||||
els.aiReport.hidden = false;
|
||||
els.aiReport.textContent = text;
|
||||
}
|
||||
|
||||
function startLocalWhisperProgress() {
|
||||
const stages = [
|
||||
"处理进度:提取音频...",
|
||||
"处理进度:生成英文字幕...",
|
||||
];
|
||||
setAiReportText(stages[0]);
|
||||
|
||||
const timers = [
|
||||
window.setTimeout(() => setAiReportText(stages[1]), 1200),
|
||||
];
|
||||
|
||||
return () => timers.forEach((timer) => window.clearTimeout(timer));
|
||||
}
|
||||
|
||||
async function testAiConnection() {
|
||||
try {
|
||||
const result = await apiRequest("/api/ai/test", { method: "POST" });
|
||||
@@ -216,30 +254,36 @@ async function generateLocalWhisperSubtitles() {
|
||||
return;
|
||||
}
|
||||
|
||||
if (shouldConfirmSubtitleOverwrite(editingVideo)) {
|
||||
const confirmed = window.confirm("当前素材已有英文字幕,重新生成会覆盖它。继续吗?");
|
||||
if (!confirmed) return;
|
||||
}
|
||||
|
||||
const button = els.generateLocalWhisperSubtitlesBtn;
|
||||
if (!button) return;
|
||||
const stopProgress = startLocalWhisperProgress();
|
||||
button.disabled = true;
|
||||
button.textContent = "本地生成中...";
|
||||
button.textContent = "处理中...";
|
||||
|
||||
try {
|
||||
const result = await apiRequest(`/api/videos/${editingVideo.id}/local-whisper/subtitles`, { method: "POST" });
|
||||
const report = result.report || {};
|
||||
if (els.aiReport) {
|
||||
els.aiReport.hidden = false;
|
||||
els.aiReport.textContent = [
|
||||
`英文字幕 ${report.cleaned_count || 0} 条`,
|
||||
`中文字幕 ${report.translation_count || 0} 条`,
|
||||
report.alignment_note || "本地 Whisper 已完成",
|
||||
].join(",") + "。";
|
||||
}
|
||||
setAiReportText([
|
||||
"处理进度:完成",
|
||||
`英文字幕 ${report.english_count || 0} 条`,
|
||||
"本地 Whisper 已完成",
|
||||
].join(";") + "。");
|
||||
await loadVideos();
|
||||
const updated = videos.find((video) => String(video.id) === String(editingVideo.id));
|
||||
if (updated) startEditingVideo(updated);
|
||||
showMessage("本地字幕生成和翻译完成。");
|
||||
showMessage("本地英文字幕生成完成。");
|
||||
} catch (error) {
|
||||
showMessage(error.message || "本地字幕生成失败。", true);
|
||||
const message = error.message || "本地英文字幕生成失败。";
|
||||
setAiReportText(`处理失败:${message}`);
|
||||
showMessage(message, true);
|
||||
} finally {
|
||||
button.textContent = "本地生成并翻译字幕";
|
||||
stopProgress();
|
||||
button.textContent = "本地生成英文字幕";
|
||||
await loadAiStatus();
|
||||
}
|
||||
}
|
||||
@@ -263,7 +307,6 @@ async function processAiSubtitles() {
|
||||
els.aiReport.textContent = [
|
||||
`处理完成:删除重复 ${report.duplicate_count || 0} 条`,
|
||||
`删除空白 ${report.empty_count || 0} 条`,
|
||||
report.timestamp_calibrated ? `首句校准 ${Number(report.timestamp_offset_seconds || 0).toFixed(2)} 秒` : (report.alignment_note || "首句未自动校准"),
|
||||
`生成中文 ${report.translation_count || 0} 条`,
|
||||
].join(",") + "。";
|
||||
}
|
||||
@@ -272,7 +315,9 @@ async function processAiSubtitles() {
|
||||
if (updated) startEditingVideo(updated);
|
||||
showMessage("AI 字幕处理完成。");
|
||||
} catch (error) {
|
||||
showMessage(error.message || "AI 字幕处理失败。", true);
|
||||
const message = error.message || "AI 字幕处理失败。";
|
||||
setAiReportText(`处理失败:${message}`);
|
||||
showMessage(message, true);
|
||||
} finally {
|
||||
button.textContent = "AI 修复并翻译字幕";
|
||||
await loadAiStatus();
|
||||
@@ -370,7 +415,6 @@ function startEditingVideo(video) {
|
||||
renderSubtitleEditor();
|
||||
renderVideos();
|
||||
loadAiStatus();
|
||||
updateSubtitleShiftControls();
|
||||
}
|
||||
|
||||
function resetFormMode() {
|
||||
@@ -386,7 +430,6 @@ function resetFormMode() {
|
||||
renderSubtitleEditor();
|
||||
renderVideos();
|
||||
loadAiStatus();
|
||||
updateSubtitleShiftControls();
|
||||
}
|
||||
|
||||
function pickVideoAfterSave(videoList, response, previousEditingVideo) {
|
||||
@@ -462,9 +505,9 @@ if (typeof module !== "undefined" && module.exports) {
|
||||
parseSubtitleBlocks,
|
||||
buildWebVtt,
|
||||
getAiSubtitleButtonState,
|
||||
getLocalWhisperButtonState,
|
||||
shouldConfirmSubtitleOverwrite,
|
||||
pickVideoAfterSave,
|
||||
shiftCueTimeRange,
|
||||
shiftSubtitleCues,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -565,86 +608,6 @@ function hasSubtitleText(textRows) {
|
||||
.trim().length > 0;
|
||||
}
|
||||
|
||||
function shiftEditableSubtitles(offsetSeconds) {
|
||||
const tracks = [editableSubtitles.en, editableSubtitles.zh].filter(Boolean);
|
||||
if (!editingVideo || !tracks.length) {
|
||||
showMessage("请先编辑一个带字幕的素材。", true);
|
||||
return;
|
||||
}
|
||||
|
||||
tracks.forEach((track) => {
|
||||
track.cues = shiftSubtitleCues(track.cues, offsetSeconds);
|
||||
track.dirty = true;
|
||||
});
|
||||
renderSubtitleEditor();
|
||||
updateSubtitleShiftControls(offsetSeconds);
|
||||
}
|
||||
|
||||
function updateSubtitleShiftControls(lastOffset = 0) {
|
||||
const hasEditableCues = Boolean(editingVideo && [editableSubtitles.en, editableSubtitles.zh]
|
||||
.filter(Boolean)
|
||||
.some((track) => track.cues.length));
|
||||
[
|
||||
els.shiftSubtitleEarlierSmallBtn,
|
||||
els.shiftSubtitleLaterSmallBtn,
|
||||
els.shiftSubtitleEarlierBtn,
|
||||
els.shiftSubtitleLaterBtn,
|
||||
].forEach((button) => {
|
||||
if (button) button.disabled = !hasEditableCues;
|
||||
});
|
||||
if (!els.subtitleShiftStatus) return;
|
||||
|
||||
if (!editingVideo) {
|
||||
els.subtitleShiftStatus.textContent = "先编辑一个已有素材,再整体提前或延后字幕。";
|
||||
return;
|
||||
}
|
||||
if (!hasEditableCues) {
|
||||
els.subtitleShiftStatus.textContent = "当前素材没有可校准的字幕。";
|
||||
return;
|
||||
}
|
||||
if (lastOffset > 0) {
|
||||
els.subtitleShiftStatus.textContent = `字幕已整体延后 ${lastOffset.toFixed(1)} 秒,点“保存修改”生效。`;
|
||||
return;
|
||||
}
|
||||
if (lastOffset < 0) {
|
||||
els.subtitleShiftStatus.textContent = `字幕已整体提前 ${Math.abs(lastOffset).toFixed(1)} 秒,点“保存修改”生效。`;
|
||||
return;
|
||||
}
|
||||
els.subtitleShiftStatus.textContent = "如果字幕太早,点 +0.2/+0.5 秒;如果字幕太晚,点 -0.2/-0.5 秒。";
|
||||
}
|
||||
|
||||
function shiftSubtitleCues(cues, offsetSeconds) {
|
||||
return cues.map((cue) => ({
|
||||
...cue,
|
||||
timeRange: shiftCueTimeRange(cue.timeRange, offsetSeconds),
|
||||
}));
|
||||
}
|
||||
|
||||
function shiftCueTimeRange(timeRange, offsetSeconds) {
|
||||
const [startRaw, endRaw] = String(timeRange).split("-->").map((part) => part.trim());
|
||||
return `${formatVttTimestamp(parseVttTimestamp(startRaw) + offsetSeconds)} --> ${formatVttTimestamp(parseVttTimestamp(endRaw) + offsetSeconds)}`;
|
||||
}
|
||||
|
||||
function parseVttTimestamp(value) {
|
||||
const clean = String(value).replace(",", ".").split(/\s+/)[0];
|
||||
const parts = clean.split(":").map(Number);
|
||||
if (parts.length === 3) return parts[0] * 3600 + parts[1] * 60 + parts[2];
|
||||
if (parts.length === 2) return parts[0] * 60 + parts[1];
|
||||
return Number(clean) || 0;
|
||||
}
|
||||
|
||||
function formatVttTimestamp(seconds) {
|
||||
const totalMilliseconds = Math.max(0, Math.round((Number(seconds) || 0) * 1000));
|
||||
const hours = Math.floor(totalMilliseconds / 3600000);
|
||||
const minutes = Math.floor((totalMilliseconds % 3600000) / 60000);
|
||||
const wholeSeconds = Math.floor((totalMilliseconds % 60000) / 1000);
|
||||
const milliseconds = totalMilliseconds % 1000;
|
||||
return [
|
||||
String(hours).padStart(2, "0"),
|
||||
String(minutes).padStart(2, "0"),
|
||||
String(wholeSeconds).padStart(2, "0"),
|
||||
].join(":") + `.${String(milliseconds).padStart(3, "0")}`;
|
||||
}
|
||||
|
||||
function renderSubtitleEditor() {
|
||||
if (!els.subtitleEditor) return;
|
||||
@@ -698,7 +661,6 @@ function renderSubtitleEditor() {
|
||||
|
||||
els.subtitleEditor.appendChild(section);
|
||||
});
|
||||
updateSubtitleShiftControls();
|
||||
}
|
||||
|
||||
function appendEditedSubtitleContent(formData) {
|
||||
|
||||
Reference in new issue
Block a user