From bf701c21103474e81474c12afe60b80ea320c7b4 Mon Sep 17 00:00:00 2001 From: idanran <96647698+idanran@users.noreply.github.com> Date: Sat, 23 Mar 2024 11:57:13 +0000 Subject: [PATCH] fix: audio encoding exception in some cases --- package-lock.json | 8 ++++---- package.json | 2 +- src/common/utils/file.ts | 37 +++++++++++++++++++++++-------------- 3 files changed, 28 insertions(+), 19 deletions(-) diff --git a/package-lock.json b/package-lock.json index 38413f2..01a8cae 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,7 +14,7 @@ "file-type": "^19.0.0", "fluent-ffmpeg": "^2.1.2", "level": "^8.0.1", - "silk-wasm": "^3.2.4", + "silk-wasm": "^3.3.2", "utf-8-validate": "^6.0.3", "uuid": "^9.0.1", "ws": "^8.16.0" @@ -5895,9 +5895,9 @@ } }, "node_modules/silk-wasm": { - "version": "3.2.4", - "resolved": "https://registry.npmjs.org/silk-wasm/-/silk-wasm-3.2.4.tgz", - "integrity": "sha512-oBkXmdIRl7cyzpoXEeEVN7v1M2yCnH1/bN8oANoYTvCqbYa5lM/CGJP47DYbpUFVO9PUpm58KP/HZaVzt4J6jw==" + "version": "3.3.2", + "resolved": "https://registry.npmjs.org/silk-wasm/-/silk-wasm-3.3.2.tgz", + "integrity": "sha512-CXG/hikSInuI/A+EIPoq+z1BRv4SJdhIQKtscOiwZS3udSOr+4DBPYmqNtlV3AKldjEhJChIRCsBtWcWRe54ng==" }, "node_modules/slash": { "version": "4.0.0", diff --git a/package.json b/package.json index e56cea1..80d9d20 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ "file-type": "^19.0.0", "fluent-ffmpeg": "^2.1.2", "level": "^8.0.1", - "silk-wasm": "^3.2.4", + "silk-wasm": "^3.3.2", "utf-8-validate": "^6.0.3", "uuid": "^9.0.1", "ws": "^8.16.0" diff --git a/src/common/utils/file.ts b/src/common/utils/file.ts index 0da7968..956374b 100644 --- a/src/common/utils/file.ts +++ b/src/common/utils/file.ts @@ -3,7 +3,7 @@ import fsPromise from "fs/promises"; import crypto from "crypto"; import ffmpeg from "fluent-ffmpeg"; import util from "util"; -import {encode, getDuration, isWav} from "silk-wasm"; +import {encode, getDuration, isWav, getWavFileInfo} from "silk-wasm"; import path from "node:path"; import {v4 as uuidv4} from "uuid"; import {checkFfmpeg, DATA_DIR, log, TEMP_DIR} from "./index"; @@ -91,7 +91,7 @@ export async function encodeSilk(filePath: string) { return isWav(fs.readFileSync(filePath)); } - async function guessDuration(pttPath: string){ + async function guessDuration(pttPath: string) { const pttFileInfo = await fsPromise.stat(pttPath) let duration = pttFileInfo.size / 1024 / 3 // 3kb/s duration = Math.floor(duration) @@ -100,9 +100,9 @@ export async function encodeSilk(filePath: string) { return duration } - function verifyDuration(oriDuration: number, guessDuration: number){ + function verifyDuration(oriDuration: number, guessDuration: number) { // 单位都是秒 - if (oriDuration - guessDuration > 10){ + if (oriDuration - guessDuration > 10) { return guessDuration } oriDuration = Math.max(1, oriDuration) @@ -126,10 +126,8 @@ export async function encodeSilk(filePath: string) { log(`语音文件${filePath}需要转换成silk`) const _isWav = await isWavFile(filePath); const wavPath = pttPath + ".wav" - if (!_isWav) { - log(`语音文件${filePath}正在转换成wav`) - // let voiceData = await fsp.readFile(filePath) - await new Promise((resolve, reject) => { + const convert = async () => { + return await new Promise((resolve, reject) => { const ffmpegPath = getConfigUtil().getConfig().ffmpeg; if (ffmpegPath) { ffmpeg.setFfmpegPath(ffmpegPath); @@ -148,10 +146,21 @@ export async function encodeSilk(filePath: string) { }); }) } - // const sampleRate = await getAudioSampleRate(filePath) || 0; - // log("音频采样率", sampleRate) - const pcm = fs.readFileSync(filePath); - const silk = await encode(pcm, 0); + let wav: Buffer + if (!_isWav) { + log(`语音文件${filePath}正在转换成wav`) + await convert() + } else { + wav = fs.readFileSync(filePath) + const allowSampleRate = [8000, 12000, 16000, 24000, 32000, 44100, 48000] + const { fmt } = getWavFileInfo(wav) + if (!allowSampleRate.includes(fmt.sampleRate)) { + wav = undefined + await convert() + } + } + wav ||= fs.readFileSync(filePath); + const silk = await encode(wav, 0); fs.writeFileSync(pttPath, silk.data); fs.unlink(wavPath, (err) => { }); @@ -286,9 +295,9 @@ export async function uri2local(uri: string, fileName: string = null): Promise