Updates FFmpeg handling for platform-aware execution
Switches to platform- and architecture-specific FFmpeg binaries and dynamically resolves executable paths to improve cross-platform compatibility. Standardizes code style to use consistent double quotes. Corrects naming inconsistencies and ensures executables have proper permissions on startup. Enhances maintainability and reliability for image conversion across environments.
This commit is contained in:
1 parent
b7307a7a41
commit
c8be78de15
9 files changed
+139
-122
No files matched your search
Vendored
+28
-26
@@ -1,35 +1,33 @@
|
||||
import { spawn } from 'child_process';
|
||||
import { mkdir, writeFile, readFile, unlink } from 'fs/promises';
|
||||
import { join } from 'path';
|
||||
import { tmpdir } from 'os';
|
||||
import { randomBytes } from 'crypto';
|
||||
import { spawn } from "child_process";
|
||||
import { mkdir, writeFile, readFile, unlink } from "fs/promises";
|
||||
import { join } from "path";
|
||||
import { tmpdir, arch, platform } from "os";
|
||||
import { randomBytes } from "crypto";
|
||||
import { fileURLToPath } from "url";
|
||||
export class FFmpegConverter {
|
||||
getTempFilePath(ext) {
|
||||
const randomName = randomBytes(16).toString('hex');
|
||||
const randomName = randomBytes(16).toString("hex");
|
||||
return join(tmpdir(), `img-${randomName}.${ext}`);
|
||||
}
|
||||
async convert(inputBuffer, options) {
|
||||
const inputPath = this.getTempFilePath('tmp');
|
||||
const inputPath = this.getTempFilePath("tmp");
|
||||
const outputPath = this.getTempFilePath(options.format);
|
||||
try {
|
||||
// Write input buffer to temp file
|
||||
await writeFile(inputPath, inputBuffer);
|
||||
// Build FFmpeg command
|
||||
const args = [
|
||||
'-i', inputPath,
|
||||
'-vf', `scale=${options.width}:-1`,
|
||||
];
|
||||
const args = ["-i", inputPath, "-vf", `scale=${options.width}:-1`];
|
||||
// Format-specific options
|
||||
if (options.format === 'avif') {
|
||||
args.push('-c:v', 'libaom-av1', '-crf', '30');
|
||||
if (options.format === "avif") {
|
||||
args.push("-c:v", "libaom-av1", "-crf", "30");
|
||||
}
|
||||
else if (options.format === 'webp') {
|
||||
args.push('-c:v', 'libwebp', '-quality', '80');
|
||||
else if (options.format === "webp") {
|
||||
args.push("-c:v", "libwebp", "-quality", "80");
|
||||
}
|
||||
else if (options.format === 'jpeg') {
|
||||
args.push('-q:v', '5');
|
||||
else if (options.format === "jpeg") {
|
||||
args.push("-q:v", "5");
|
||||
}
|
||||
args.push('-y', outputPath);
|
||||
args.push("-y", outputPath);
|
||||
// Execute FFmpeg
|
||||
await this.executeFFmpeg(args);
|
||||
// Read output file
|
||||
@@ -53,13 +51,17 @@ export class FFmpegConverter {
|
||||
}
|
||||
}
|
||||
executeFFmpeg(args) {
|
||||
const currentDirectory = fileURLToPath(import.meta.url);
|
||||
const platformArchPrefix = `${platform()}-${arch()}`;
|
||||
const ffmpegExecutable = join(currentDirectory, "..", "..", "bin", platformArchPrefix, platform() === "win32" ? "ffmpeg.exe" : "ffmpeg");
|
||||
console.log(`Executing FFmpeg: ${ffmpegExecutable} ${args.join(" ")}`);
|
||||
return new Promise((resolve, reject) => {
|
||||
const ffmpeg = spawn('ffmpeg', args);
|
||||
let stderr = '';
|
||||
ffmpeg.stderr.on('data', (data) => {
|
||||
const ffmpeg = spawn(ffmpegExecutable, args);
|
||||
let stderr = "";
|
||||
ffmpeg.stderr.on("data", (data) => {
|
||||
stderr += data.toString();
|
||||
});
|
||||
ffmpeg.on('close', (code) => {
|
||||
ffmpeg.on("close", (code) => {
|
||||
if (code === 0) {
|
||||
resolve();
|
||||
}
|
||||
@@ -67,16 +69,16 @@ export class FFmpegConverter {
|
||||
reject(new Error(`FFmpeg exited with code ${code}\n${stderr}`));
|
||||
}
|
||||
});
|
||||
ffmpeg.on('error', (err) => {
|
||||
ffmpeg.on("error", (err) => {
|
||||
reject(new Error(`Failed to start FFmpeg: ${err.message}`));
|
||||
});
|
||||
});
|
||||
}
|
||||
getContentType(format) {
|
||||
const contentTypes = {
|
||||
avif: 'image/avif',
|
||||
webp: 'image/webp',
|
||||
jpeg: 'image/jpeg',
|
||||
avif: "image/avif",
|
||||
webp: "image/webp",
|
||||
jpeg: "image/jpeg",
|
||||
};
|
||||
return contentTypes[format];
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user