fix: 修复语音消息取错、时长精度与转写不刷新

- 时长解析:改取 <voicemsg voicelength>(毫秒),不再误取 length(SILK 字节数)
- 时长显示:四舍五入对齐微信口径,有原生时长时不被解码时长覆盖
- 转写:重新识别改为强制重算,跳过身份级缓存短路(音频级缓存仍生效)
- 日报:语音累计秒数显示取整,不再出现小数
This commit is contained in:
Wxw-Gu committed 2026-09-20 15:40:01 +08:00
1 parent 9b82d29037
commit 9593ca0f54
21 files changed
+435 -52

No files matched your search

+9 -4
View File
@@ -1246,10 +1246,15 @@ async function runSingleExport(
const wavChannels =
audioBuffer.length >= 44 ? audioBuffer.readUInt16LE(22) : 1
const pcmBytes = Math.max(0, audioBuffer.length - 44)
message.voiceDuration = Math.max(
1,
Math.round(pcmBytes / (wavSampleRate * wavChannels * 2))
)
// 口径统一:消息解析阶段已从 <voicemsg voicelength> 拿到微信的原始秒数(带小数),
// 它是唯一权威来源,不要覆盖。只有拿不到时才退回用 PCM 字节数估算——
// 那份估算是整秒、且下限 1 秒(WAV 缺失头部时的兜底),语义不同。
if (message.voiceDuration == null) {
message.voiceDuration = Math.max(
1,
Math.round(pcmBytes / (wavSampleRate * wavChannels * 2))
)
}
} catch (error) {
keepMediaError(
request,
+1 -1
View File
@@ -588,7 +588,7 @@ const renderReportHtml = async (request: GroupReportExportRequest): Promise<stri
(item, index) => `<div class="rank tm-fragment tm-ranking-item">
${renderAvatar(item.sender, 'ranking')}
<b>${index + 1}. ${escapeHtml(item.sender)}</b>
<span>${item.count} 条 · ${item.durationSec} 秒</span>
<span>${item.count} 条 · ${Math.round(item.durationSec)} 秒</span>
</div>`
)
.join('')
+8 -5
View File
@@ -1853,12 +1853,15 @@ app.whenReady().then(async () => {
return error ? { success: false, error } : { success: true }
})
ipcMain.handle('voice:recognize', (_, reference: VoiceMessageReference) => {
if (!voiceRecognition) {
return { success: false, code: 'NOT_CONNECTED', error: '语音识别服务尚未初始化' }
ipcMain.handle(
'voice:recognize',
(_, reference: VoiceMessageReference, options?: { force?: boolean }) => {
if (!voiceRecognition) {
return { success: false, code: 'NOT_CONNECTED', error: '语音识别服务尚未初始化' }
}
return voiceRecognition.recognize(reference, options)
}
return voiceRecognition.recognize(reference)
})
)
ipcMain.handle('voice:getTranscriptSnapshot', (_, reference: VoiceMessageReference) => {
return voiceRecognition?.getTranscriptSnapshot(reference) || { state: 'pending' as const }
+31 -1
View File
@@ -125,7 +125,10 @@ export type ParsedContent =
export function parseMessageContent(content: string, messageType: number): ParsedContent {
// Voice rows may keep their binary payload outside msgContent, so an empty
// content string is still a valid voice message.
if (messageType === 34) return { type: 'voice' }
if (messageType === 34) {
const duration = parseVoiceDurationSeconds(content)
return duration === undefined ? { type: 'voice' } : { type: 'voice', duration }
}
if (!content || typeof content !== 'string') {
return { type: 'unknown', raw: content || '' }
}
@@ -157,6 +160,33 @@ export function parseMessageContent(content: string, messageType: number): Parse
}
}
/**
* 语音时长藏在解压后的 message_content 里:`<voicemsg ... voicelength="1600" ...>`,单位毫秒。
* Msg_* 表没有 voice_length 列,这是唯一来源。
*
* `<voicemsg>` 上两个极易混淆的属性(真机实测,同一条 1.6 秒语音,2026-09-20):
*
* - `voicelength="1600"` → **毫秒时长**。这条语音微信气泡显示 2"(1.6 秒四舍五入)。
* **要取的是它。**
* - `length="6672"` → **SILK 编码数据的字节数,与时长无关**。
* 已验证:`wcdb_get_voice_data` 取出的 SILK 恰好是 6672 字节,
* 解码后为 51200 字节 PCM(1.6 秒)。误取它会算出 6.672 秒,把 2" 显示成 0:07。
*
* 换算成秒后**刻意保留小数**(1600ms → 1.6):在这里取整会把精度永久丢掉,
* 后面显示层再怎么四舍五入都对不回微信的口径(微信是四舍五入到整秒)。
*
* 注:`<videomsg length="...">` 的 `length` 同理是字节数,不是时长。
*/
function parseVoiceDurationSeconds(content: string): number | undefined {
if (!content || typeof content !== 'string') return undefined
const decoded = decodeXmlEntities(stripChatroomPrefix(content))
const rawLength = extractXmlAttribute(decoded, 'voicemsg', 'voicelength')
if (!rawLength) return undefined
const milliseconds = Number(rawLength)
if (!Number.isFinite(milliseconds) || milliseconds <= 0) return undefined
return milliseconds / 1000
}
function parseVideoMessage(content: string): ParsedContent {
const decoded = decodeXmlEntities(stripChatroomPrefix(content))
const md5 = normalizeMd5(extractXmlAttribute(decoded, 'videomsg', 'md5'))
+5 -1
View File
@@ -669,6 +669,9 @@ function listSourceMessages(
: msg.mesLocalID || Math.random().toString()
)
const imageContent = contentData?.type === 'image' ? contentData : undefined
// 语音时长来自 message_content 的 <voicemsg voicelength>(毫秒)——注意不是 length,
// 那是 SILK 数据字节数。已在 parseMessageContent 里换算成秒。
const voiceDuration = contentData?.type === 'voice' ? contentData.duration : undefined
// Local ids repeat across conversations. Scope media handles to this database
// connection and image without changing the message id used by other clients.
const mediaId = imageContent
@@ -737,7 +740,8 @@ function listSourceMessages(
createTime,
recoveredFromRecallJournal,
contentData,
media
media,
voiceDuration
}
})
+21 -13
View File
@@ -37,21 +37,29 @@ export class VoicePipeline {
async run(
accountId: string,
reference: VoiceMessageReference,
signal?: AbortSignal
signal?: AbortSignal,
options?: { force?: boolean }
): Promise<{ transcript: string; language?: string; durationMs: number; cached: boolean }> {
const messageIdentity = voiceMessageIdentity(reference)
const compatible = this.transcripts.findCompatible({
accountId,
messageIdentity,
processorVersion: this.audioProcessor.version,
...this.recognizer.metadata
})
if (compatible?.transcript.trim()) {
return {
transcript: compatible.transcript.trim(),
language: compatible.language,
durationMs: compatible.durationMs,
cached: true
/*
* findCompatible 只按消息身份匹配,不含 audio_hash:音频被换掉(例如取音频的
* 逻辑修好后)时它仍会命中旧记录。用户主动触发的识别必须跳过它,重新取一次
* 音频 —— 音频级缓存 find(key) 带 audio_hash,才是正确的失效机制。
*/
if (!options?.force) {
const compatible = this.transcripts.findCompatible({
accountId,
messageIdentity,
processorVersion: this.audioProcessor.version,
...this.recognizer.metadata
})
if (compatible?.transcript.trim()) {
return {
transcript: compatible.transcript.trim(),
language: compatible.language,
durationMs: compatible.durationMs,
cached: true
}
}
}
@@ -23,6 +23,7 @@ type TranscriptUpdateListener = (update: VoiceTranscriptUpdate) => Promise<void>
type RecognitionOptions = {
priority?: VoiceRecognitionPriority
publishTranscriptUpdate?: boolean
force?: boolean
}
export class VoiceRecognitionUseCase {
@@ -108,7 +109,9 @@ export class VoiceRecognitionUseCase {
error: '请先下载语音识别模型'
} as const
}
const result = await pipeline.run(accountId, reference, signal)
const result = await pipeline.run(accountId, reference, signal, {
force: options?.force
})
if (signal.aborted || !this.isCurrentAccount(accountId, generation)) {
throw new DOMException('Recognition cancelled', 'AbortError')
}
+9 -1
View File
@@ -73,11 +73,19 @@ export class VoiceService {
pcmResult.audio.sampleRate,
pcmResult.audio.channels
)
// duration 由 PCM 字节数反算(wavData 去掉 44 字节头即 PCM),
// 用来和用户实际听到的长度对齐、排查「时长显示不对」这类问题。
// 注意它**不是权威值**:权威时长在消息 XML 的 <voicemsg voicelength>(毫秒),
// 显示层以那个为准;这里只是解码结果的自证。
const durationSeconds =
pcmData.length / (pcmResult.audio.sampleRate * pcmResult.audio.channels * 2)
console.log(
'[VoiceService] wavData length:',
wavData.length,
'base64 length:',
wavData.toString('base64').length
wavData.toString('base64').length,
'duration:',
`${durationSeconds.toFixed(2)}s`
)
const base64Data = wavData.toString('base64')