Merge branch 'main' into dev

# Conflicts:
#	src/common/utils/file.ts
This commit is contained in:
linyuchen 2024-03-21 13:05:15 +08:00
commit 175a8ceb3d
3 changed files with 14 additions and 10 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

@ -11,6 +11,7 @@ import {getConfigUtil} from "../config";
import {dbUtil} from "../db"; import {dbUtil} from "../db";
import * as fileType from "file-type"; import * as fileType from "file-type";
import {net} from "electron"; import {net} from "electron";
import config from "../../../electron.vite.config";
export function isGIF(path: string) { export function isGIF(path: string) {
@ -67,6 +68,8 @@ 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;
@ -129,7 +132,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,21 +149,22 @@ export async function encodeSilk(filePath: string) {
// const sampleRate = await getAudioSampleRate(filePath) || 0; // const sampleRate = await getAudioSampleRate(filePath) || 0;
// log("音频采样率", sampleRate) // log("音频采样率", sampleRate)
const pcm = fs.readFileSync(filePath); const pcm = fs.readFileSync(filePath);
let silk = await encode(pcm, 0); const silk = await encode(pcm, 0);
fs.writeFileSync(pttPath, silk.data); fs.writeFileSync(pttPath, silk.data);
fs.unlink(wavPath, (err) => { fs.unlink(wavPath, (err) => {
}); });
const duration = verifyDuration(silk.duration / 1000, await guessDuration(filePath)) log(`语音文件${filePath}转换成功!`, pttPath, `时长:`, silk.duration)
return { return {
converted: true, converted: true,
path: pttPath, path: pttPath,
duration duration: silk.duration,
}; };
} else { } else {
const pcm = fs.readFileSync(filePath); const silk = fs.readFileSync(filePath);
let duration = 0; let duration = 0;
const gDuration = await guessDuration(filePath) const gDuration = await guessDuration(filePath)
try { try {
duration = getDuration(silk);
duration = verifyDuration(getDuration(pcm), gDuration); duration = verifyDuration(getDuration(pcm), gDuration);
} catch (e) { } catch (e) {
log("获取语音文件时长失败, 使用文件大小推测时长", filePath, e.stack) log("获取语音文件时长失败, 使用文件大小推测时长", filePath, e.stack)