Merge branch 'main' into dev

This commit is contained in:
linyuchen 2024-03-23 21:10:09 +08:00
commit 48d62be2d6
3 changed files with 28 additions and 19 deletions

8
package-lock.json generated
View File

@ -14,7 +14,7 @@
"file-type": "^19.0.0", "file-type": "^19.0.0",
"fluent-ffmpeg": "^2.1.2", "fluent-ffmpeg": "^2.1.2",
"level": "^8.0.1", "level": "^8.0.1",
"silk-wasm": "^3.2.4", "silk-wasm": "^3.3.2",
"utf-8-validate": "^6.0.3", "utf-8-validate": "^6.0.3",
"uuid": "^9.0.1", "uuid": "^9.0.1",
"ws": "^8.16.0" "ws": "^8.16.0"
@ -5895,9 +5895,9 @@
} }
}, },
"node_modules/silk-wasm": { "node_modules/silk-wasm": {
"version": "3.2.4", "version": "3.3.2",
"resolved": "https://registry.npmjs.org/silk-wasm/-/silk-wasm-3.2.4.tgz", "resolved": "https://registry.npmjs.org/silk-wasm/-/silk-wasm-3.3.2.tgz",
"integrity": "sha512-oBkXmdIRl7cyzpoXEeEVN7v1M2yCnH1/bN8oANoYTvCqbYa5lM/CGJP47DYbpUFVO9PUpm58KP/HZaVzt4J6jw==" "integrity": "sha512-CXG/hikSInuI/A+EIPoq+z1BRv4SJdhIQKtscOiwZS3udSOr+4DBPYmqNtlV3AKldjEhJChIRCsBtWcWRe54ng=="
}, },
"node_modules/slash": { "node_modules/slash": {
"version": "4.0.0", "version": "4.0.0",

View File

@ -19,7 +19,7 @@
"file-type": "^19.0.0", "file-type": "^19.0.0",
"fluent-ffmpeg": "^2.1.2", "fluent-ffmpeg": "^2.1.2",
"level": "^8.0.1", "level": "^8.0.1",
"silk-wasm": "^3.2.4", "silk-wasm": "^3.3.2",
"utf-8-validate": "^6.0.3", "utf-8-validate": "^6.0.3",
"uuid": "^9.0.1", "uuid": "^9.0.1",
"ws": "^8.16.0" "ws": "^8.16.0"

View File

@ -3,7 +3,7 @@ import fsPromise from "fs/promises";
import crypto from "crypto"; import crypto from "crypto";
import ffmpeg from "fluent-ffmpeg"; import ffmpeg from "fluent-ffmpeg";
import util from "util"; 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 path from "node:path";
import {v4 as uuidv4} from "uuid"; import {v4 as uuidv4} from "uuid";
import {checkFfmpeg, DATA_DIR, log, TEMP_DIR} from "./index"; import {checkFfmpeg, DATA_DIR, log, TEMP_DIR} from "./index";
@ -91,7 +91,7 @@ export async function encodeSilk(filePath: string) {
return isWav(fs.readFileSync(filePath)); return isWav(fs.readFileSync(filePath));
} }
async function guessDuration(pttPath: string){ async function guessDuration(pttPath: string) {
const pttFileInfo = await fsPromise.stat(pttPath) const pttFileInfo = await fsPromise.stat(pttPath)
let duration = pttFileInfo.size / 1024 / 3 // 3kb/s let duration = pttFileInfo.size / 1024 / 3 // 3kb/s
duration = Math.floor(duration) duration = Math.floor(duration)
@ -100,9 +100,9 @@ export async function encodeSilk(filePath: string) {
return duration return duration
} }
function verifyDuration(oriDuration: number, guessDuration: number){ function verifyDuration(oriDuration: number, guessDuration: number) {
// 单位都是秒 // 单位都是秒
if (oriDuration - guessDuration > 10){ if (oriDuration - guessDuration > 10) {
return guessDuration return guessDuration
} }
oriDuration = Math.max(1, oriDuration) oriDuration = Math.max(1, oriDuration)
@ -126,10 +126,8 @@ export async function encodeSilk(filePath: string) {
log(`语音文件${filePath}需要转换成silk`) log(`语音文件${filePath}需要转换成silk`)
const _isWav = await isWavFile(filePath); const _isWav = await isWavFile(filePath);
const wavPath = pttPath + ".wav" const wavPath = pttPath + ".wav"
if (!_isWav) { const convert = async () => {
log(`语音文件${filePath}正在转换成wav`) return await new Promise((resolve, reject) => {
// let voiceData = await fsp.readFile(filePath)
await new Promise((resolve, reject) => {
const ffmpegPath = getConfigUtil().getConfig().ffmpeg; const ffmpegPath = getConfigUtil().getConfig().ffmpeg;
if (ffmpegPath) { if (ffmpegPath) {
ffmpeg.setFfmpegPath(ffmpegPath); ffmpeg.setFfmpegPath(ffmpegPath);
@ -148,10 +146,21 @@ export async function encodeSilk(filePath: string) {
}); });
}) })
} }
// const sampleRate = await getAudioSampleRate(filePath) || 0; let wav: Buffer
// log("音频采样率", sampleRate) if (!_isWav) {
const pcm = fs.readFileSync(filePath); log(`语音文件${filePath}正在转换成wav`)
const silk = await encode(pcm, 0); 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.writeFileSync(pttPath, silk.data);
fs.unlink(wavPath, (err) => { fs.unlink(wavPath, (err) => {
}); });
@ -286,9 +295,9 @@ export async function uri2local(uri: string, fileName: string = null): Promise<U
} else if (url.protocol == "http:" || url.protocol == "https:") { } else if (url.protocol == "http:" || url.protocol == "https:") {
// 下载文件 // 下载文件
let buffer: Buffer = null; let buffer: Buffer = null;
try{ try {
buffer = await httpDownload(uri); buffer = await httpDownload(uri);
}catch (e) { } catch (e) {
res.errMsg = `${url}下载失败,` + e.toString() res.errMsg = `${url}下载失败,` + e.toString()
return res return res
} }