feat: compression 19
This commit is contained in:
1 parent
8acb107862
commit
006b8ba095
1 file changed
+4
-72
@@ -4,94 +4,26 @@ import path from "node:path";
|
||||
|
||||
const STDERR_TAIL_BYTES = 64 * 1024;
|
||||
|
||||
const tokenizeCommand = (input: string): string[] => {
|
||||
const args: string[] = [];
|
||||
let current = "";
|
||||
let quote: "'" | '"' | null = null;
|
||||
let escaping = false;
|
||||
let tokenStarted = false;
|
||||
|
||||
for (const char of input) {
|
||||
if (escaping) {
|
||||
current += char;
|
||||
escaping = false;
|
||||
tokenStarted = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (char === "\\" && quote !== "'") {
|
||||
escaping = true;
|
||||
tokenStarted = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (quote) {
|
||||
if (char === quote) {
|
||||
quote = null;
|
||||
} else {
|
||||
current += char;
|
||||
}
|
||||
tokenStarted = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (char === "'" || char === '"') {
|
||||
quote = char;
|
||||
tokenStarted = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (/\s/.test(char)) {
|
||||
if (tokenStarted) {
|
||||
args.push(current);
|
||||
current = "";
|
||||
tokenStarted = false;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
current += char;
|
||||
tokenStarted = true;
|
||||
}
|
||||
|
||||
if (escaping) throw new Error("FFmpeg command ends with an incomplete escape sequence");
|
||||
if (quote) throw new Error("FFmpeg command contains an unterminated quote");
|
||||
if (tokenStarted) args.push(current);
|
||||
|
||||
return args;
|
||||
};
|
||||
|
||||
const getLogPath = (outputPath: string) => {
|
||||
const parsed = path.parse(outputPath);
|
||||
return path.join(parsed.dir, `${parsed.name}.log`);
|
||||
};
|
||||
|
||||
const formatArgument = (value: string) =>
|
||||
/\s/.test(value) ? JSON.stringify(value) : value;
|
||||
|
||||
export const runLoggedFfmpegCommand = async (
|
||||
command: string,
|
||||
inputPath: string,
|
||||
outputPath: string
|
||||
) => {
|
||||
const tokens = tokenizeCommand(command);
|
||||
if (tokens.length === 0) throw new Error("FFmpeg command is empty");
|
||||
|
||||
const resolved = tokens.map((token) => token
|
||||
const resolvedCommand = command
|
||||
.split("{input}").join(inputPath)
|
||||
.split("{output}").join(outputPath));
|
||||
|
||||
// Support both "ffmpeg -i ..." and argument-only settings such as "-i ...".
|
||||
const commandIncludesExecutable = !resolved[0]?.startsWith("-");
|
||||
const executable = commandIncludesExecutable ? resolved[0] : "ffmpeg";
|
||||
const args = commandIncludesExecutable ? resolved.slice(1) : resolved;
|
||||
.split("{output}").join(outputPath);
|
||||
const logPath = getLogPath(outputPath);
|
||||
const log = createWriteStream(logPath, { flags: "w" });
|
||||
|
||||
log.write(`Command: ${[executable, ...args].map(formatArgument).join(" ")}\n\n`);
|
||||
log.write(`Command: ${resolvedCommand}\n\n`);
|
||||
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
const child = spawn(executable, args, { shell: false });
|
||||
const child = spawn(resolvedCommand, { shell: true });
|
||||
let stderrTail = "";
|
||||
let settled = false;
|
||||
|
||||
|
||||
Reference in new issue
Block a user