mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2025-07-19 12:03:37 +00:00
修复<get_record>接口
This commit is contained in:
@@ -5,9 +5,11 @@ import { promises as fs } from 'fs';
|
|||||||
import { decode } from 'silk-wasm';
|
import { decode } from 'silk-wasm';
|
||||||
const FFMPEG_PATH = process.env.FFMPEG_PATH || 'ffmpeg';
|
const FFMPEG_PATH = process.env.FFMPEG_PATH || 'ffmpeg';
|
||||||
|
|
||||||
interface Payload extends GetFilePayload {
|
const out_format = ['mp3' , 'amr' , 'wma' , 'm4a' , 'spx' , 'ogg' , 'wav' , 'flac'];
|
||||||
out_format: 'mp3' | 'amr' | 'wma' | 'm4a' | 'spx' | 'ogg' | 'wav' | 'flac';
|
|
||||||
}
|
type Payload = {
|
||||||
|
out_format : string
|
||||||
|
} & GetFilePayload
|
||||||
|
|
||||||
export default class GetRecord extends GetFileBase {
|
export default class GetRecord extends GetFileBase {
|
||||||
actionName = ActionName.GetRecord;
|
actionName = ActionName.GetRecord;
|
||||||
@@ -17,12 +19,19 @@ export default class GetRecord extends GetFileBase {
|
|||||||
if (payload.out_format && typeof payload.out_format === 'string') {
|
if (payload.out_format && typeof payload.out_format === 'string') {
|
||||||
const inputFile = res.file;
|
const inputFile = res.file;
|
||||||
if (!inputFile) throw new Error('file not found');
|
if (!inputFile) throw new Error('file not found');
|
||||||
|
if (!out_format.includes(payload.out_format)) {
|
||||||
|
throw new Error('转换失败 out_format 字段可能格式不正确');
|
||||||
|
}
|
||||||
const pcmFile = `${inputFile}.pcm`;
|
const pcmFile = `${inputFile}.pcm`;
|
||||||
const outputFile = `${inputFile}.${payload.out_format}`;
|
const outputFile = `${inputFile}.${payload.out_format}`;
|
||||||
try {
|
try {
|
||||||
await fs.access(inputFile);
|
await fs.access(inputFile);
|
||||||
|
try {
|
||||||
|
await fs.access(outputFile);
|
||||||
|
} catch (error) {
|
||||||
await this.decodeFile(inputFile, pcmFile);
|
await this.decodeFile(inputFile, pcmFile);
|
||||||
await this.convertFile(pcmFile, outputFile, payload.out_format);
|
await this.convertFile(pcmFile, outputFile, payload.out_format);
|
||||||
|
}
|
||||||
const base64Data = await fs.readFile(outputFile, { encoding: 'base64' });
|
const base64Data = await fs.readFile(outputFile, { encoding: 'base64' });
|
||||||
res.file = outputFile;
|
res.file = outputFile;
|
||||||
res.url = outputFile;
|
res.url = outputFile;
|
||||||
@@ -48,7 +57,8 @@ export default class GetRecord extends GetFileBase {
|
|||||||
|
|
||||||
private convertFile(inputFile: string, outputFile: string, format: string): Promise<void> {
|
private convertFile(inputFile: string, outputFile: string, format: string): Promise<void> {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const ffmpeg = spawn(FFMPEG_PATH, ['-f', 's16le', '-ar', '24000', '-ac', '1', '-i', inputFile, outputFile]);
|
const params = format === 'amr' ? ['-f', 's16le', '-ar', '24000', '-ac', '1', '-i', inputFile, '-ar', '8000', '-b:a', '12.2k', outputFile] : ['-f', 's16le', '-ar', '24000', '-ac', '1', '-i', inputFile, outputFile];
|
||||||
|
const ffmpeg = spawn(FFMPEG_PATH, params);
|
||||||
|
|
||||||
ffmpeg.on('close', (code) => {
|
ffmpeg.on('close', (code) => {
|
||||||
if (code === 0) {
|
if (code === 0) {
|
||||||
|
Reference in New Issue
Block a user