Merge pull request #145 from idanran/main

fix: unable to send voice in some cases
This commit is contained in:
linyuchen 2024-03-21 10:16:27 +08:00 committed by GitHub
commit c9486b4f55
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 8 additions and 11 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.3", "silk-wasm": "^3.2.4",
"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.3", "version": "3.2.4",
"resolved": "https://registry.npmjs.org/silk-wasm/-/silk-wasm-3.2.3.tgz", "resolved": "https://registry.npmjs.org/silk-wasm/-/silk-wasm-3.2.4.tgz",
"integrity": "sha512-zZ3hgMpiPR6cFnKvCPgPpCwx6n5RoJCbEGIFlge2kAxAmgzBTf0b2F2xIPG5W4obUhQPQXXTTH074eGZJK01xw==" "integrity": "sha512-oBkXmdIRl7cyzpoXEeEVN7v1M2yCnH1/bN8oANoYTvCqbYa5lM/CGJP47DYbpUFVO9PUpm58KP/HZaVzt4J6jw=="
}, },
"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.3", "silk-wasm": "^3.2.4",
"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

@ -68,8 +68,6 @@ export async function file2base64(path: string) {
} }
export async function encodeSilk(filePath: string) { export async function encodeSilk(filePath: string) {
const fsp = require("fs").promises
function getFileHeader(filePath: string) { function getFileHeader(filePath: string) {
// 定义要读取的字节数 // 定义要读取的字节数
const bytesToRead = 7; const bytesToRead = 7;
@ -104,7 +102,6 @@ export async function encodeSilk(filePath: string) {
// } // }
try { try {
const fileName = path.basename(filePath);
const pttPath = path.join(DATA_DIR, uuidv4()); const pttPath = path.join(DATA_DIR, uuidv4());
if (getFileHeader(filePath) !== "02232153494c4b") { if (getFileHeader(filePath) !== "02232153494c4b") {
log(`语音文件${filePath}需要转换成silk`) log(`语音文件${filePath}需要转换成silk`)
@ -118,7 +115,7 @@ export async function encodeSilk(filePath: string) {
if (ffmpegPath) { if (ffmpegPath) {
ffmpeg.setFfmpegPath(ffmpegPath); ffmpeg.setFfmpegPath(ffmpegPath);
} }
ffmpeg(filePath).toFormat("wav").audioChannels(2).on('end', function () { ffmpeg(filePath).toFormat("wav").audioChannels(1).audioFrequency(24000).on('end', function () {
log('wav转换完成'); log('wav转换完成');
}) })
.on('error', function (err) { .on('error', function (err) {
@ -146,10 +143,10 @@ export async function encodeSilk(filePath: string) {
duration: silk.duration, duration: silk.duration,
}; };
} else { } else {
const pcm = fs.readFileSync(filePath); const silk = fs.readFileSync(filePath);
let duration = 0; let duration = 0;
try { try {
duration = getDuration(pcm); duration = getDuration(silk);
} catch (e) { } catch (e) {
log("获取语音文件时长失败", filePath, e.stack) log("获取语音文件时长失败", filePath, e.stack)
duration = fs.statSync(filePath).size / 1024 / 3 // 每3kb大约1s duration = fs.statSync(filePath).size / 1024 / 3 // 每3kb大约1s