refactor: invoke ffmpeg without deprecated wrapper
This commit is contained in:
39
src/muxer.ts
39
src/muxer.ts
@@ -1,6 +1,6 @@
|
||||
import ffmpeg from "fluent-ffmpeg";
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import { buildMuxFfmpegArgs, runFfmpeg } from "./audio/ffmpegProcess";
|
||||
|
||||
const recordingsDir = process.env.RECORDINGS_DIR ?? "./recordings";
|
||||
|
||||
@@ -71,7 +71,7 @@ async function startMuxing() {
|
||||
|
||||
// Check if OGG file has valid header (starts with "OggS")
|
||||
const oggBuffer = fs.readFileSync(oggPath);
|
||||
const oggHeader = oggBuffer.slice(0, 4).toString();
|
||||
const oggHeader = oggBuffer.subarray(0, 4).toString();
|
||||
if (oggHeader !== "OggS") {
|
||||
console.warn(
|
||||
`[muxer] Skipping invalid OGG file (bad header): ${oggPath}`,
|
||||
@@ -114,13 +114,10 @@ async function startMuxing() {
|
||||
`[muxer] Found ${clips.length} clips. Base timestamp: ${globalStartTime}`,
|
||||
);
|
||||
|
||||
const command = ffmpeg();
|
||||
const filterParts: string[] = [];
|
||||
|
||||
console.log(`[muxer] Creating audio filters for ${clips.length} clips...`);
|
||||
clips.forEach((clip, index) => {
|
||||
command.input(clip.oggPath);
|
||||
|
||||
// Calculate delay relative to the global start time
|
||||
const delayMs = clip.meta.startTime - globalStartTime;
|
||||
|
||||
@@ -148,27 +145,25 @@ async function startMuxing() {
|
||||
);
|
||||
|
||||
const outputFilename = path.join(recordingsDir, `muxed-${Date.now()}.mp3`);
|
||||
const inputs = clips.map((clip) => clip.oggPath);
|
||||
|
||||
console.log(`[muxer] Combining clips. This might take a while...`);
|
||||
|
||||
// Using fluent-ffmpeg's complexFilter
|
||||
command
|
||||
.complexFilter(filterParts, "out")
|
||||
.audioCodec("libmp3lame")
|
||||
.save(outputFilename)
|
||||
.on("progress", (progress) => {
|
||||
if (progress.percent) {
|
||||
console.log(`[muxer] Progress: ${progress.percent.toFixed(2)}%`);
|
||||
}
|
||||
})
|
||||
.on("end", () => {
|
||||
console.log(
|
||||
`[muxer] Successfully muxed! Output saved to: ${outputFilename}`,
|
||||
);
|
||||
})
|
||||
.on("error", (err) => {
|
||||
console.error(`[muxer] FFmpeg Error:`, err);
|
||||
try {
|
||||
const args = buildMuxFfmpegArgs({
|
||||
inputs,
|
||||
filter: filterParts.join(";"),
|
||||
output: outputFilename,
|
||||
codec: "libmp3lame",
|
||||
});
|
||||
|
||||
await runFfmpeg(args);
|
||||
console.log(
|
||||
`[muxer] Successfully muxed! Output saved to: ${outputFilename}`,
|
||||
);
|
||||
} catch (err) {
|
||||
console.error(`[muxer] FFmpeg Error:`, err);
|
||||
}
|
||||
}
|
||||
|
||||
startMuxing();
|
||||
|
||||
Reference in New Issue
Block a user