mirror of
https://github.com/LLOneBot/LLOneBot.git
synced 2024-11-22 01:56:33 +00:00
fix: audio may fail to convert
This commit is contained in:
parent
ec073da3f6
commit
1f8966aaf4
@ -64,50 +64,48 @@ export async function encodeSilk(filePath: string) {
|
|||||||
if (getFileHeader(filePath) !== "02232153494c4b") {
|
if (getFileHeader(filePath) !== "02232153494c4b") {
|
||||||
log(`语音文件${filePath}需要转换成silk`)
|
log(`语音文件${filePath}需要转换成silk`)
|
||||||
const _isWav = await isWavFile(filePath);
|
const _isWav = await isWavFile(filePath);
|
||||||
const wavPath = pttPath + ".wav"
|
const pcmPath = pttPath + ".pcm"
|
||||||
|
let sampleRate = 0
|
||||||
const convert = async () => {
|
const convert = async () => {
|
||||||
return await new Promise((resolve, reject) => {
|
return await new Promise<Buffer>((resolve, reject) => {
|
||||||
const ffmpegPath = getConfigUtil().getConfig().ffmpeg;
|
const ffmpegPath = getConfigUtil().getConfig().ffmpeg;
|
||||||
if (ffmpegPath) {
|
if (ffmpegPath) {
|
||||||
ffmpeg.setFfmpegPath(ffmpegPath);
|
ffmpeg.setFfmpegPath(ffmpegPath);
|
||||||
}
|
}
|
||||||
ffmpeg(filePath).toFormat("wav")
|
ffmpeg(filePath)
|
||||||
.audioChannels(1)
|
.outputOptions([
|
||||||
.audioFrequency(24000)
|
"-ar 24000",
|
||||||
.on('end', function () {
|
"-ac 1",
|
||||||
log('wav转换完成');
|
"-f s16le"
|
||||||
})
|
])
|
||||||
.on('error', function (err) {
|
.on("error", function (err) {
|
||||||
log(`wav转换出错: `, err.message,);
|
log(`ffmpeg转换出错: `, err.message);
|
||||||
reject(err);
|
reject(err);
|
||||||
})
|
})
|
||||||
.save(wavPath)
|
|
||||||
.on("end", () => {
|
.on("end", () => {
|
||||||
filePath = wavPath
|
sampleRate = 24000
|
||||||
resolve(wavPath);
|
const data = fs.readFileSync(pcmPath)
|
||||||
});
|
fs.unlink(pcmPath, (err) => {
|
||||||
|
})
|
||||||
|
resolve(data)
|
||||||
|
})
|
||||||
|
.save(pcmPath);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
let wav: Buffer
|
let input: Buffer
|
||||||
if (!_isWav) {
|
if (!_isWav) {
|
||||||
log(`语音文件${filePath}正在转换成wav`)
|
input = await convert()
|
||||||
await convert()
|
|
||||||
} else {
|
} else {
|
||||||
wav = fs.readFileSync(filePath)
|
input = 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(input)
|
||||||
// log(`wav文件信息`, fmt)
|
// log(`wav文件信息`, fmt)
|
||||||
if (!allowSampleRate.includes(fmt.sampleRate)) {
|
if (!allowSampleRate.includes(fmt.sampleRate)) {
|
||||||
wav = undefined
|
input = await convert()
|
||||||
await convert()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
wav ||= fs.readFileSync(filePath);
|
const silk = await encode(input, sampleRate);
|
||||||
const silk = await encode(wav, 0);
|
|
||||||
fs.writeFileSync(pttPath, silk.data);
|
fs.writeFileSync(pttPath, silk.data);
|
||||||
fs.unlink(wavPath, (err) => {
|
|
||||||
});
|
|
||||||
// const gDuration = await guessDuration(pttPath)
|
|
||||||
log(`语音文件${filePath}转换成功!`, pttPath, `时长:`, silk.duration)
|
log(`语音文件${filePath}转换成功!`, pttPath, `时长:`, silk.duration)
|
||||||
return {
|
return {
|
||||||
converted: true,
|
converted: true,
|
||||||
@ -127,7 +125,7 @@ export async function encodeSilk(filePath: string) {
|
|||||||
return {
|
return {
|
||||||
converted: false,
|
converted: false,
|
||||||
path: filePath,
|
path: filePath,
|
||||||
duration: duration,
|
duration,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user