11
This commit is contained in:
@@ -264,6 +264,42 @@ function parseTranscriptionText(trans) {
|
||||
return fullText;
|
||||
}
|
||||
|
||||
/** @returns {Array<{ text: string, start: number, end: number }>} start/end 毫秒 */
|
||||
function normalizeAsrTimeToMs(value) {
|
||||
const n = Number(value);
|
||||
if (!Number.isFinite(n) || n < 0) return 0;
|
||||
// 对齐 Electron generateAssSubtitle:>100 视为毫秒,否则视为秒
|
||||
if (n > 100) return Math.round(n);
|
||||
return Math.round(n * 1000);
|
||||
}
|
||||
|
||||
function normalizeAsrRecord(record) {
|
||||
const text = String(record?.text || record?.sentence || "").trim();
|
||||
if (!text) return null;
|
||||
let start = normalizeAsrTimeToMs(record.start ?? record.begin ?? record.beginTime ?? 0);
|
||||
let end = normalizeAsrTimeToMs(record.end ?? record.endTime ?? start + 2000);
|
||||
if (end <= start) end = start + 2000;
|
||||
return { text, start, end };
|
||||
}
|
||||
|
||||
/** @returns {Array<{ text: string, start: number, end: number }>} */
|
||||
function normalizeAsrRecords(records) {
|
||||
if (!Array.isArray(records)) return [];
|
||||
const normalized = records.map(normalizeAsrRecord).filter(Boolean);
|
||||
const MIN_GAP_MS = 50;
|
||||
for (let i = 0; i < normalized.length - 1; i++) {
|
||||
const current = normalized[i];
|
||||
const next = normalized[i + 1];
|
||||
if (current.end >= next.start) {
|
||||
current.end = Math.max(current.start + MIN_GAP_MS, next.start - MIN_GAP_MS);
|
||||
}
|
||||
if (current.end <= current.start) {
|
||||
current.end = current.start + MIN_GAP_MS;
|
||||
}
|
||||
}
|
||||
return normalized;
|
||||
}
|
||||
|
||||
/** @returns {Array<{ text: string, start: number, end: number }>} start/end 毫秒 */
|
||||
function parseTranscriptionSentences(trans) {
|
||||
const records = [];
|
||||
@@ -273,14 +309,16 @@ function parseTranscriptionSentences(trans) {
|
||||
for (const s of t.sentences) {
|
||||
const text = String(s.text || "").trim();
|
||||
if (!text) continue;
|
||||
const start = Number(s.begin_time ?? s.start_time ?? 0);
|
||||
const end = Number(s.end_time ?? s.end_time ?? start + 2000);
|
||||
const start = normalizeAsrTimeToMs(s.begin_time ?? s.start_time ?? 0);
|
||||
const end = normalizeAsrTimeToMs(
|
||||
s.end_time ?? s.end ?? start + 2000,
|
||||
);
|
||||
records.push({ text, start, end });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return records;
|
||||
return normalizeAsrRecords(records);
|
||||
}
|
||||
|
||||
async function fetchJson(url, options = {}) {
|
||||
@@ -624,15 +662,21 @@ async function localAsr(audioPath, info) {
|
||||
data.records ||
|
||||
[];
|
||||
const sentences = Array.isArray(records)
|
||||
? records
|
||||
.map((r) => {
|
||||
const text = String(r.text || "").trim();
|
||||
if (!text) return null;
|
||||
const start = Number(r.start ?? r.begin_time ?? 0);
|
||||
const end = Number(r.end ?? r.end_time ?? start + 2000);
|
||||
return { text, start, end };
|
||||
})
|
||||
.filter(Boolean)
|
||||
? normalizeAsrRecords(
|
||||
records
|
||||
.map((r) => {
|
||||
const text = String(r.text || "").trim();
|
||||
if (!text) return null;
|
||||
const start = normalizeAsrTimeToMs(
|
||||
r.start ?? r.begin_time ?? 0,
|
||||
);
|
||||
const end = normalizeAsrTimeToMs(
|
||||
r.end ?? r.end_time ?? start + 2000,
|
||||
);
|
||||
return { text, start, end };
|
||||
})
|
||||
.filter(Boolean),
|
||||
)
|
||||
: [];
|
||||
const text = sentences.length
|
||||
? sentences.map((r) => r.text).join("")
|
||||
|
||||
Reference in New Issue
Block a user