11
This commit is contained in:
@@ -259,6 +259,25 @@ function parseTranscriptionText(trans) {
|
||||
return fullText;
|
||||
}
|
||||
|
||||
/** @returns {Array<{ text: string, start: number, end: number }>} start/end 毫秒 */
|
||||
function parseTranscriptionSentences(trans) {
|
||||
const records = [];
|
||||
if (trans?.transcripts?.length) {
|
||||
for (const t of trans.transcripts) {
|
||||
if (t.sentences?.length) {
|
||||
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);
|
||||
records.push({ text, start, end });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return records;
|
||||
}
|
||||
|
||||
async function fetchJson(url, options = {}) {
|
||||
const res = await fetch(url, options);
|
||||
const text = await res.text();
|
||||
@@ -537,7 +556,8 @@ async function aliyunAsrFiletrans(audioUrl, opts) {
|
||||
}
|
||||
const trans = await fetchJson(transUrl);
|
||||
const fullText = parseTranscriptionText(trans);
|
||||
return { success: true, text: fullText };
|
||||
const sentences = parseTranscriptionSentences(trans);
|
||||
return { success: true, text: fullText, sentences };
|
||||
}
|
||||
if (status === "FAILED" || status === "UNKNOWN") {
|
||||
const detail =
|
||||
@@ -597,10 +617,21 @@ async function localAsr(audioPath, info) {
|
||||
data.data?.records ||
|
||||
data.records ||
|
||||
[];
|
||||
const text = Array.isArray(records)
|
||||
? records.map((r) => r.text || "").join("")
|
||||
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)
|
||||
: [];
|
||||
const text = sentences.length
|
||||
? sentences.map((r) => r.text).join("")
|
||||
: data.text || data.data?.text || "";
|
||||
resolve({ success: true, text: String(text) });
|
||||
resolve({ success: true, text: String(text), sentences });
|
||||
} else {
|
||||
resolve({ success: false, error: data.msg || data.error || raw.slice(0, 200) });
|
||||
}
|
||||
@@ -656,8 +687,10 @@ module.exports = {
|
||||
aliyunOssUpload,
|
||||
aliyunAsrFiletrans,
|
||||
localAsr,
|
||||
parseTranscriptionSentences,
|
||||
openaiChat,
|
||||
resolveTtsModel,
|
||||
splitTextForTts,
|
||||
qwenTtsSynthesize,
|
||||
locateFfmpeg,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user