| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- "use strict";
- var __importDefault = (this && this.__importDefault) || function (mod) {
- return (mod && mod.__esModule) ? mod : { "default": mod };
- };
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.AudioMerger = void 0;
- const fs_1 = __importDefault(require("fs"));
- const child_process_1 = require("child_process");
- const util_1 = require("util");
- const execAsync = (0, util_1.promisify)(child_process_1.exec);
- class AudioMerger {
- // 合并多个音频文件
- static async merge(inputFiles, outputPath) {
- if (inputFiles.length === 0) {
- throw new Error('没有音频文件需要合并');
- }
- if (inputFiles.length === 1) {
- // 单个文件直接复制
- fs_1.default.copyFileSync(inputFiles[0], outputPath);
- return outputPath;
- }
- try {
- // 创建文件列表
- const listContent = inputFiles.map(f => `file '${f}'`).join('\n');
- const listFile = '/tmp/ffmpeg_list.txt';
- fs_1.default.writeFileSync(listFile, listContent);
- // 检查输出格式
- const outputExt = outputPath.split('.').pop()?.toLowerCase();
- // 构建 FFmpeg 命令
- let cmd;
- if (outputExt === 'mp3') {
- // MP3 需要转码(WAV/PCM -> MP3)
- cmd = `ffmpeg -f concat -safe 0 -i "${listFile}" -c:a libmp3lame -b:a 192k -y "${outputPath}"`;
- }
- else {
- // 其他格式直接复制
- cmd = `ffmpeg -f concat -safe 0 -i "${listFile}" -c copy -y "${outputPath}"`;
- }
- await execAsync(cmd, { timeout: 120000 });
- console.log(`✅ 音频合并成功: ${outputPath}`);
- return outputPath;
- }
- catch (error) {
- console.error('❌ 音频合并失败:', error);
- throw new Error('音频合并失败');
- }
- }
- // 获取音频时长
- static async getDuration(filePath) {
- try {
- const cmd = `ffprobe -i "${filePath}" -show_entries format=duration -v quiet -of csv="p=0"`;
- const { stdout } = await execAsync(cmd, { timeout: 10000 });
- return Math.round(parseFloat(stdout.trim()) || 0);
- }
- catch (error) {
- console.error('❌ 获取音频时长失败:', error);
- return 0;
- }
- }
- }
- exports.AudioMerger = AudioMerger;
|