mirror of
https://github.com/LLOneBot/LLOneBot.git
synced 2024-11-22 01:56:33 +00:00
fix: audio sample rate
This commit is contained in:
parent
36f7f1b026
commit
bb3bce203d
@ -6,7 +6,7 @@ import path from "node:path";
|
|||||||
import {selfInfo} from "./data";
|
import {selfInfo} from "./data";
|
||||||
import {DATA_DIR} from "./utils";
|
import {DATA_DIR} from "./utils";
|
||||||
|
|
||||||
export const HOOK_LOG = true;
|
export const HOOK_LOG = false;
|
||||||
|
|
||||||
export const ALLOW_SEND_TEMP_MSG = false;
|
export const ALLOW_SEND_TEMP_MSG = false;
|
||||||
|
|
||||||
|
@ -3,7 +3,7 @@ import {encode, getDuration, getWavFileInfo, isWav} from "silk-wasm";
|
|||||||
import fsPromise from "fs/promises";
|
import fsPromise from "fs/promises";
|
||||||
import {log} from "./log";
|
import {log} from "./log";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import {DATA_DIR} from "./index";
|
import {DATA_DIR, TEMP_DIR} from "./index";
|
||||||
import {v4 as uuidv4} from "uuid";
|
import {v4 as uuidv4} from "uuid";
|
||||||
import {getConfigUtil} from "../config";
|
import {getConfigUtil} from "../config";
|
||||||
import ffmpeg from "fluent-ffmpeg";
|
import ffmpeg from "fluent-ffmpeg";
|
||||||
@ -60,7 +60,7 @@ export async function encodeSilk(filePath: string) {
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const pttPath = path.join(DATA_DIR, uuidv4());
|
const pttPath = path.join(TEMP_DIR, uuidv4());
|
||||||
if (getFileHeader(filePath) !== "02232153494c4b") {
|
if (getFileHeader(filePath) !== "02232153494c4b") {
|
||||||
log(`语音文件${filePath}需要转换成silk`)
|
log(`语音文件${filePath}需要转换成silk`)
|
||||||
const _isWav = await isWavFile(filePath);
|
const _isWav = await isWavFile(filePath);
|
||||||
@ -71,7 +71,10 @@ export async function encodeSilk(filePath: string) {
|
|||||||
if (ffmpegPath) {
|
if (ffmpegPath) {
|
||||||
ffmpeg.setFfmpegPath(ffmpegPath);
|
ffmpeg.setFfmpegPath(ffmpegPath);
|
||||||
}
|
}
|
||||||
ffmpeg(filePath).toFormat("wav").audioChannels(1).audioFrequency(24000).on('end', function () {
|
ffmpeg(filePath).toFormat("wav")
|
||||||
|
.audioChannels(1)
|
||||||
|
.audioFrequency(16000)
|
||||||
|
.on('end', function () {
|
||||||
log('wav转换完成');
|
log('wav转换完成');
|
||||||
})
|
})
|
||||||
.on('error', function (err) {
|
.on('error', function (err) {
|
||||||
@ -93,6 +96,7 @@ export async function encodeSilk(filePath: string) {
|
|||||||
wav = fs.readFileSync(filePath)
|
wav = fs.readFileSync(filePath)
|
||||||
const allowSampleRate = [8000, 12000, 16000, 24000, 32000, 44100, 48000]
|
const allowSampleRate = [8000, 12000, 16000, 24000, 32000, 44100, 48000]
|
||||||
const {fmt} = getWavFileInfo(wav)
|
const {fmt} = getWavFileInfo(wav)
|
||||||
|
// log(`wav文件信息`, fmt)
|
||||||
if (!allowSampleRate.includes(fmt.sampleRate)) {
|
if (!allowSampleRate.includes(fmt.sampleRate)) {
|
||||||
wav = undefined
|
wav = undefined
|
||||||
await convert()
|
await convert()
|
||||||
@ -103,7 +107,7 @@ export async function encodeSilk(filePath: string) {
|
|||||||
fs.writeFileSync(pttPath, silk.data);
|
fs.writeFileSync(pttPath, silk.data);
|
||||||
fs.unlink(wavPath, (err) => {
|
fs.unlink(wavPath, (err) => {
|
||||||
});
|
});
|
||||||
const gDuration = await guessDuration(pttPath)
|
// const gDuration = await guessDuration(pttPath)
|
||||||
log(`语音文件${filePath}转换成功!`, pttPath, `时长:`, silk.duration)
|
log(`语音文件${filePath}转换成功!`, pttPath, `时长:`, silk.duration)
|
||||||
return {
|
return {
|
||||||
converted: true,
|
converted: true,
|
||||||
@ -113,12 +117,11 @@ export async function encodeSilk(filePath: string) {
|
|||||||
} else {
|
} else {
|
||||||
const silk = fs.readFileSync(filePath);
|
const silk = fs.readFileSync(filePath);
|
||||||
let duration = 0;
|
let duration = 0;
|
||||||
const gDuration = await guessDuration(filePath)
|
|
||||||
try {
|
try {
|
||||||
duration = getDuration(silk) / 1000
|
duration = getDuration(silk) / 1000
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
log("获取语音文件时长失败, 使用文件大小推测时长", filePath, e.stack)
|
log("获取语音文件时长失败, 使用文件大小推测时长", filePath, e.stack)
|
||||||
duration = gDuration;
|
duration = await guessDuration(filePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user