@ash_j1997/ffmpeg-mcp-server 0.4.2 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.ko.md +120 -0
- package/README.md +37 -23
- package/dist/index.js +283 -91
- package/dist/tools/audio.js +24 -9
- package/dist/tools/burn_subtitles.js +94 -0
- package/dist/tools/compress.js +8 -4
- package/dist/tools/concat.js +14 -3
- package/dist/tools/convert.js +78 -42
- package/dist/tools/keyframes.js +75 -0
- package/dist/tools/normalize.js +75 -0
- package/dist/tools/overlay.js +115 -0
- package/dist/tools/probe.js +6 -2
- package/dist/tools/reformat.js +14 -7
- package/dist/tools/silence.js +81 -20
- package/dist/tools/speed.js +24 -3
- package/dist/tools/split.js +15 -9
- package/dist/tools/subtitles.js +17 -4
- package/dist/tools/thumbnail.js +29 -13
- package/dist/tools/trim.js +47 -14
- package/dist/tools/waveform.js +9 -4
- package/dist/utils/exec.js +139 -8
- package/dist/utils/hwaccel.js +179 -26
- package/package.json +2 -1
package/dist/tools/compress.js
CHANGED
|
@@ -10,16 +10,16 @@ export const compressMediaSchema = z.object({
|
|
|
10
10
|
preset: z.enum(["github_pr", "discord", "email", "balanced", "high_quality"]).optional().default("balanced").describe("Preset when targetSizeMB is not set: 'github_pr', 'discord', 'email', 'balanced', or 'high_quality'. Default: 'balanced'."),
|
|
11
11
|
crf: z.number().int().min(18).max(45).optional().describe("Quality value between 18 (higher quality) and 45 (smaller file). Overrides preset."),
|
|
12
12
|
maxWidth: z.number().int().positive().optional().describe("Maximum video width in pixels, such as 1280 for 720p. Keeps aspect ratio."),
|
|
13
|
-
hardwareAcceleration: z.boolean().optional().describe("Use
|
|
13
|
+
hardwareAcceleration: z.boolean().optional().describe("Use hardware-accelerated video encoding (VideoToolbox, NVENC, QSV, etc.) when available. Default: true."),
|
|
14
14
|
});
|
|
15
|
-
export async function compressMedia(params) {
|
|
15
|
+
export async function compressMedia(params, options) {
|
|
16
16
|
const { inputPath, outputPath, targetSizeMB, preset = "balanced", crf, maxWidth, hardwareAcceleration } = params;
|
|
17
17
|
const input = await ensureFileExists(inputPath);
|
|
18
18
|
const output = await ensureOutputDir(outputPath);
|
|
19
19
|
const hwInfo = await detectHwAccel();
|
|
20
20
|
const useHw = hardwareAcceleration ?? hwInfo.isAvailable;
|
|
21
21
|
const statsBefore = fs.statSync(input);
|
|
22
|
-
const probe = await probeMedia(input);
|
|
22
|
+
const probe = await probeMedia(input, { signal: options?.signal });
|
|
23
23
|
const duration = probe.format.durationSeconds || 10;
|
|
24
24
|
const hasAudio = probe.audioStreams.length > 0;
|
|
25
25
|
const args = ["-i", input];
|
|
@@ -103,7 +103,11 @@ export async function compressMedia(params) {
|
|
|
103
103
|
args.push("-movflags", "+faststart");
|
|
104
104
|
}
|
|
105
105
|
args.push("-y", output);
|
|
106
|
-
const result = await runFfmpeg(args
|
|
106
|
+
const result = await runFfmpeg(args, {
|
|
107
|
+
signal: options?.signal,
|
|
108
|
+
onProgress: options?.onProgress,
|
|
109
|
+
expectedDurationSeconds: duration,
|
|
110
|
+
});
|
|
107
111
|
const statsAfter = fs.statSync(output);
|
|
108
112
|
return {
|
|
109
113
|
success: true,
|
package/dist/tools/concat.js
CHANGED
|
@@ -3,12 +3,13 @@ import path from "node:path";
|
|
|
3
3
|
import os from "node:os";
|
|
4
4
|
import { z } from "zod";
|
|
5
5
|
import { runFfmpeg, ensureFileExists, ensureOutputDir } from "../utils/exec.js";
|
|
6
|
+
import { detectHwAccel } from "../utils/hwaccel.js";
|
|
6
7
|
export const concatMediaSchema = z.object({
|
|
7
8
|
inputPaths: z.array(z.string()).min(2).describe("List of media files to concatenate in sequential order"),
|
|
8
9
|
outputPath: z.string().describe("Target output file path"),
|
|
9
10
|
mode: z.enum(["lossless", "reencode"]).optional().default("lossless").describe("Concatenation mode. 'lossless' uses stream copy without quality loss (inputs must share codecs). 'reencode' normalizes differing resolutions/codecs."),
|
|
10
11
|
});
|
|
11
|
-
export async function concatMedia(params) {
|
|
12
|
+
export async function concatMedia(params, options) {
|
|
12
13
|
const { inputPaths, outputPath, mode = "lossless" } = params;
|
|
13
14
|
const resolvedInputs = [];
|
|
14
15
|
for (const p of inputPaths) {
|
|
@@ -26,10 +27,20 @@ export async function concatMedia(params) {
|
|
|
26
27
|
args.push("-c", "copy");
|
|
27
28
|
}
|
|
28
29
|
else {
|
|
29
|
-
|
|
30
|
+
const hwInfo = await detectHwAccel();
|
|
31
|
+
if (hwInfo.isAvailable) {
|
|
32
|
+
args.push("-c:v", hwInfo.h264Encoder, "-b:v", "4M");
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
args.push("-c:v", "libx264", "-crf", "22", "-preset", "fast");
|
|
36
|
+
}
|
|
37
|
+
args.push("-pix_fmt", "yuv420p", "-c:a", "aac", "-b:a", "192k", "-movflags", "+faststart");
|
|
30
38
|
}
|
|
31
39
|
args.push("-y", output);
|
|
32
|
-
const result = await runFfmpeg(args
|
|
40
|
+
const result = await runFfmpeg(args, {
|
|
41
|
+
signal: options?.signal,
|
|
42
|
+
onProgress: options?.onProgress,
|
|
43
|
+
});
|
|
33
44
|
return {
|
|
34
45
|
success: true,
|
|
35
46
|
outputPath: output,
|
package/dist/tools/convert.js
CHANGED
|
@@ -1,68 +1,104 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { ensureFileExists, ensureOutputDir, runFfmpeg } from "../utils/exec.js";
|
|
2
3
|
import { detectHwAccel } from "../utils/hwaccel.js";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
import { probeMedia } from "./probe.js";
|
|
5
|
+
export const convertMediaSchema = z.object({
|
|
6
|
+
inputPath: z.string().describe("Path to the source media file to convert"),
|
|
7
|
+
outputPath: z.string().describe("Destination path for the converted file (e.g. ./output.mp4, ./output.webm, ./animation.gif)"),
|
|
8
|
+
preset: z
|
|
9
|
+
.enum(["web_mp4", "webm", "gif", "auto"])
|
|
10
|
+
.optional()
|
|
11
|
+
.describe("Encoding preset: 'web_mp4' for browser playback, 'webm' for WebM video, 'gif' for animated GIF, or 'auto'. Default: 'auto'."),
|
|
12
|
+
crf: z
|
|
13
|
+
.number()
|
|
14
|
+
.min(0)
|
|
15
|
+
.max(51)
|
|
16
|
+
.optional()
|
|
17
|
+
.describe("Quality factor from 0 to 51. Lower numbers mean higher quality. Default: 23 for MP4, 30 for WebM."),
|
|
18
|
+
resolution: z
|
|
19
|
+
.string()
|
|
20
|
+
.regex(/^\d{2,5}x\d{2,5}$/, "Resolution must be formatted as 'WIDTHxHEIGHT' (e.g. '1920x1080')")
|
|
21
|
+
.optional()
|
|
22
|
+
.describe("Target size formatted as 'WIDTHxHEIGHT', such as '1920x1080' or '1280x720'"),
|
|
23
|
+
fps: z
|
|
24
|
+
.number()
|
|
25
|
+
.positive()
|
|
26
|
+
.optional()
|
|
27
|
+
.describe("Target frame rate in frames per second, such as 30, 60, or 15"),
|
|
28
|
+
hardwareAcceleration: z
|
|
29
|
+
.boolean()
|
|
30
|
+
.optional()
|
|
31
|
+
.describe("Use hardware-accelerated video encoding (VideoToolbox, NVENC, QSV, etc.) when available. Default: true."),
|
|
32
|
+
});
|
|
33
|
+
export async function convertMedia(params, options) {
|
|
34
|
+
const input = await ensureFileExists(params.inputPath);
|
|
35
|
+
const output = await ensureOutputDir(params.outputPath);
|
|
36
|
+
let inputDuration;
|
|
37
|
+
try {
|
|
38
|
+
const probe = await probeMedia(input, { signal: options?.signal });
|
|
39
|
+
inputDuration = probe.format.durationSeconds;
|
|
40
|
+
}
|
|
41
|
+
catch { }
|
|
6
42
|
const hwInfo = await detectHwAccel();
|
|
7
|
-
const useHw =
|
|
8
|
-
const preset =
|
|
9
|
-
const args = ["-
|
|
43
|
+
const useHw = params.hardwareAcceleration ?? hwInfo.isAvailable;
|
|
44
|
+
const preset = params.preset || "auto";
|
|
45
|
+
const args = ["-i", input];
|
|
10
46
|
let isHwUsed = false;
|
|
47
|
+
let encoderUsed = "auto";
|
|
11
48
|
if (preset === "gif") {
|
|
12
|
-
|
|
13
|
-
|
|
49
|
+
encoderUsed = "gif (palettegen/paletteuse)";
|
|
50
|
+
const scale = params.resolution
|
|
51
|
+
? `scale=${params.resolution.replace("x", ":")}:flags=lanczos`
|
|
14
52
|
: "scale=iw:ih:flags=lanczos";
|
|
15
|
-
const fps =
|
|
53
|
+
const fps = params.fps ? `fps=${params.fps},` : "fps=15,";
|
|
16
54
|
const filter = `${fps}${scale},split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse`;
|
|
17
|
-
args.push("-vf", filter
|
|
55
|
+
args.push("-vf", filter);
|
|
18
56
|
}
|
|
19
|
-
else if (preset === "web_mp4" || (preset === "auto" && output.endsWith(".mp4"))) {
|
|
57
|
+
else if (preset === "web_mp4" || (preset === "auto" && output.toLowerCase().endsWith(".mp4"))) {
|
|
20
58
|
if (useHw && hwInfo.isAvailable) {
|
|
21
|
-
|
|
22
|
-
args.push("-b:v", "4M");
|
|
59
|
+
encoderUsed = hwInfo.h264Encoder;
|
|
60
|
+
args.push("-c:v", hwInfo.h264Encoder, "-b:v", "4M");
|
|
23
61
|
isHwUsed = true;
|
|
24
62
|
}
|
|
25
63
|
else {
|
|
26
|
-
|
|
27
|
-
args.push("-crf", String(
|
|
28
|
-
args.push("-preset", "medium");
|
|
64
|
+
encoderUsed = "libx264";
|
|
65
|
+
args.push("-c:v", "libx264", "-crf", String(params.crf ?? 23), "-preset", "medium");
|
|
29
66
|
}
|
|
30
|
-
args.push("-pix_fmt", "yuv420p");
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
args.push("-movflags", "+faststart");
|
|
34
|
-
if (options.resolution) {
|
|
35
|
-
args.push("-vf", `scale=${options.resolution.replace("x", ":")}`);
|
|
67
|
+
args.push("-pix_fmt", "yuv420p", "-c:a", "aac", "-b:a", "192k", "-movflags", "+faststart");
|
|
68
|
+
if (params.resolution) {
|
|
69
|
+
args.push("-vf", `scale=${params.resolution.replace("x", ":")}`);
|
|
36
70
|
}
|
|
37
|
-
if (
|
|
38
|
-
args.push("-r", String(
|
|
71
|
+
if (params.fps) {
|
|
72
|
+
args.push("-r", String(params.fps));
|
|
39
73
|
}
|
|
40
|
-
args.push(output);
|
|
41
74
|
}
|
|
42
|
-
else if (preset === "webm" || (preset === "auto" && output.endsWith(".webm"))) {
|
|
43
|
-
|
|
44
|
-
args.push("-crf", String(
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
args.push("-b:a", "128k");
|
|
48
|
-
if (options.resolution) {
|
|
49
|
-
args.push("-vf", `scale=${options.resolution.replace("x", ":")}`);
|
|
75
|
+
else if (preset === "webm" || (preset === "auto" && output.toLowerCase().endsWith(".webm"))) {
|
|
76
|
+
encoderUsed = "libvpx-vp9";
|
|
77
|
+
args.push("-c:v", "libvpx-vp9", "-crf", String(params.crf ?? 30), "-b:v", "0", "-c:a", "libopus", "-b:a", "128k");
|
|
78
|
+
if (params.resolution) {
|
|
79
|
+
args.push("-vf", `scale=${params.resolution.replace("x", ":")}`);
|
|
50
80
|
}
|
|
51
|
-
args.push(output);
|
|
52
81
|
}
|
|
53
82
|
else {
|
|
54
|
-
|
|
55
|
-
|
|
83
|
+
encoderUsed = "copy/default";
|
|
84
|
+
if (params.crf) {
|
|
85
|
+
args.push("-crf", String(params.crf));
|
|
56
86
|
}
|
|
57
|
-
if (
|
|
58
|
-
args.push("-vf", `scale=${
|
|
87
|
+
if (params.resolution) {
|
|
88
|
+
args.push("-vf", `scale=${params.resolution.replace("x", ":")}`);
|
|
59
89
|
}
|
|
60
|
-
args.push(output);
|
|
61
90
|
}
|
|
62
|
-
|
|
91
|
+
args.push("-y", output);
|
|
92
|
+
const res = await runFfmpeg(args, {
|
|
93
|
+
signal: options?.signal,
|
|
94
|
+
onProgress: options?.onProgress,
|
|
95
|
+
expectedDurationSeconds: inputDuration,
|
|
96
|
+
});
|
|
63
97
|
return {
|
|
64
98
|
outputPath: output,
|
|
65
|
-
commandExecuted:
|
|
99
|
+
commandExecuted: res.command,
|
|
66
100
|
hardwareAccelerated: isHwUsed,
|
|
101
|
+
encoderUsed,
|
|
102
|
+
durationSeconds: res.durationSeconds,
|
|
67
103
|
};
|
|
68
104
|
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import fs from "node:fs/promises";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { ensureFileExists, ensureOutputDir, runFfmpeg } from "../utils/exec.js";
|
|
4
|
+
import { probeMedia } from "./probe.js";
|
|
5
|
+
export const extractKeyframesSchema = z.object({
|
|
6
|
+
inputPath: z.string().describe("Path to the source video file."),
|
|
7
|
+
outputPath: z.string().describe("Destination image path (e.g. './keyframes_grid.png' or './frames/frame_%02d.jpg')."),
|
|
8
|
+
count: z
|
|
9
|
+
.number()
|
|
10
|
+
.int()
|
|
11
|
+
.min(2)
|
|
12
|
+
.max(24)
|
|
13
|
+
.optional()
|
|
14
|
+
.default(6)
|
|
15
|
+
.describe("Number of representative frames to extract across the video timeline. Default: 6."),
|
|
16
|
+
layout: z
|
|
17
|
+
.enum(["montage", "individual"])
|
|
18
|
+
.optional()
|
|
19
|
+
.default("montage")
|
|
20
|
+
.describe("Output format: 'montage' combines all frames into a single contact-sheet grid image for vision models; 'individual' exports separate frame images. Default: 'montage'."),
|
|
21
|
+
includeBase64: z
|
|
22
|
+
.boolean()
|
|
23
|
+
.optional()
|
|
24
|
+
.default(true)
|
|
25
|
+
.describe("Inline base64 image data in response for vision models. Default: true."),
|
|
26
|
+
});
|
|
27
|
+
export async function extractKeyframes(params, options) {
|
|
28
|
+
const { inputPath, outputPath, count = 6, layout = "montage", includeBase64 = true } = params;
|
|
29
|
+
const input = await ensureFileExists(inputPath);
|
|
30
|
+
const output = await ensureOutputDir(outputPath);
|
|
31
|
+
const probe = await probeMedia(input, { signal: options?.signal });
|
|
32
|
+
const duration = Math.max(1, probe.format.durationSeconds);
|
|
33
|
+
// Compute montage dimensions (e.g. 6 -> 3x2, 4 -> 2x2, 9 -> 3x3)
|
|
34
|
+
const cols = count <= 3 ? count : Math.ceil(Math.sqrt(count));
|
|
35
|
+
const rows = Math.ceil(count / cols);
|
|
36
|
+
const args = ["-i", input];
|
|
37
|
+
if (layout === "montage") {
|
|
38
|
+
// Extract evenly spaced frames and tile them into a contact sheet
|
|
39
|
+
const filter = `fps=${count}/${duration},scale=480:-1,tile=${cols}x${rows}`;
|
|
40
|
+
args.push("-vf", filter, "-vframes", "1", "-y", output);
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
// Export individual frames
|
|
44
|
+
const filter = `fps=${count}/${duration},scale=640:-1`;
|
|
45
|
+
args.push("-vf", filter, "-vframes", String(count), "-y", output);
|
|
46
|
+
}
|
|
47
|
+
const res = await runFfmpeg(args, { signal: options?.signal });
|
|
48
|
+
let base64;
|
|
49
|
+
let mimeType;
|
|
50
|
+
if (includeBase64 && layout === "montage") {
|
|
51
|
+
try {
|
|
52
|
+
const buffer = await fs.readFile(output);
|
|
53
|
+
if (buffer.length < 5 * 1024 * 1024) {
|
|
54
|
+
base64 = buffer.toString("base64");
|
|
55
|
+
mimeType = output.toLowerCase().endsWith(".jpg") || output.toLowerCase().endsWith(".jpeg")
|
|
56
|
+
? "image/jpeg"
|
|
57
|
+
: "image/png";
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
// Optional
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return {
|
|
65
|
+
success: true,
|
|
66
|
+
outputPath: output,
|
|
67
|
+
frameCount: count,
|
|
68
|
+
layout,
|
|
69
|
+
gridDimensions: layout === "montage" ? `${cols}x${rows}` : undefined,
|
|
70
|
+
commandExecuted: res.command,
|
|
71
|
+
durationSeconds: res.durationSeconds,
|
|
72
|
+
base64,
|
|
73
|
+
mimeType,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { ensureFileExists, ensureOutputDir, runFfmpeg } from "../utils/exec.js";
|
|
3
|
+
import { probeMedia } from "./probe.js";
|
|
4
|
+
export const normalizeAudioSchema = z.object({
|
|
5
|
+
inputPath: z.string().describe("Path to the source audio or video file."),
|
|
6
|
+
outputPath: z.string().describe("Destination path for the normalized media file."),
|
|
7
|
+
targetLufs: z
|
|
8
|
+
.number()
|
|
9
|
+
.min(-30)
|
|
10
|
+
.max(-10)
|
|
11
|
+
.optional()
|
|
12
|
+
.default(-16)
|
|
13
|
+
.describe("Target integrated loudness in LUFS (-16 for podcasts/Reels, -14 for YouTube, -23 for broadcast). Default: -16."),
|
|
14
|
+
targetPeakDb: z
|
|
15
|
+
.number()
|
|
16
|
+
.min(-5)
|
|
17
|
+
.max(0)
|
|
18
|
+
.optional()
|
|
19
|
+
.default(-1.5)
|
|
20
|
+
.describe("Maximum allowed true peak level in dBTP. Default: -1.5."),
|
|
21
|
+
voiceClarity: z
|
|
22
|
+
.boolean()
|
|
23
|
+
.optional()
|
|
24
|
+
.default(false)
|
|
25
|
+
.describe("Apply speech enhancement filters (80Hz high-pass to remove mic rumble and gentle compressor for presence). Default: false."),
|
|
26
|
+
});
|
|
27
|
+
export async function normalizeAudio(params, options) {
|
|
28
|
+
const { inputPath, outputPath, targetLufs = -16, targetPeakDb = -1.5, voiceClarity = false } = params;
|
|
29
|
+
const input = await ensureFileExists(inputPath);
|
|
30
|
+
const output = await ensureOutputDir(outputPath);
|
|
31
|
+
const probe = await probeMedia(input, { signal: options?.signal });
|
|
32
|
+
if (probe.audioStreams.length === 0) {
|
|
33
|
+
throw new Error(`Cannot normalize audio: input file '${input}' contains no audio streams.`);
|
|
34
|
+
}
|
|
35
|
+
const isVideo = probe.videoStreams.length > 0;
|
|
36
|
+
const duration = probe.format.durationSeconds;
|
|
37
|
+
let audioFilter = `loudnorm=I=${targetLufs}:TP=${targetPeakDb}:LRA=11`;
|
|
38
|
+
if (voiceClarity) {
|
|
39
|
+
audioFilter = `highpass=f=80,acompressor=threshold=-21dB:ratio=3:attack=15:release=100,${audioFilter}`;
|
|
40
|
+
}
|
|
41
|
+
const args = ["-i", input, "-af", audioFilter];
|
|
42
|
+
if (isVideo) {
|
|
43
|
+
// Lossless copy for video stream, encode audio to AAC
|
|
44
|
+
args.push("-c:v", "copy", "-c:a", "aac", "-b:a", "192k", "-movflags", "+faststart");
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
const isWav = output.toLowerCase().endsWith(".wav");
|
|
48
|
+
const isMp3 = output.toLowerCase().endsWith(".mp3");
|
|
49
|
+
if (isWav) {
|
|
50
|
+
args.push("-c:a", "pcm_s16le");
|
|
51
|
+
}
|
|
52
|
+
else if (isMp3) {
|
|
53
|
+
args.push("-c:a", "libmp3lame", "-b:a", "192k");
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
args.push("-c:a", "aac", "-b:a", "192k");
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
args.push("-y", output);
|
|
60
|
+
const res = await runFfmpeg(args, {
|
|
61
|
+
signal: options?.signal,
|
|
62
|
+
onProgress: options?.onProgress,
|
|
63
|
+
expectedDurationSeconds: duration,
|
|
64
|
+
});
|
|
65
|
+
return {
|
|
66
|
+
success: true,
|
|
67
|
+
outputPath: output,
|
|
68
|
+
targetLufs,
|
|
69
|
+
targetPeakDb,
|
|
70
|
+
voiceClarityEnabled: voiceClarity,
|
|
71
|
+
isVideo,
|
|
72
|
+
commandExecuted: res.command,
|
|
73
|
+
durationSeconds: res.durationSeconds,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { ensureFileExists, ensureOutputDir, runFfmpeg } from "../utils/exec.js";
|
|
3
|
+
import { detectHwAccel } from "../utils/hwaccel.js";
|
|
4
|
+
import { probeMedia } from "./probe.js";
|
|
5
|
+
export const addOverlaySchema = z.object({
|
|
6
|
+
videoPath: z.string().describe("Path to the base video file."),
|
|
7
|
+
overlayPath: z.string().describe("Path to the watermark, logo, or overlay image (.png, .jpg, or .webp)."),
|
|
8
|
+
outputPath: z.string().describe("Destination path for the output video (.mp4 or .mov)."),
|
|
9
|
+
position: z
|
|
10
|
+
.enum(["top_left", "top_right", "bottom_left", "bottom_right", "center"])
|
|
11
|
+
.optional()
|
|
12
|
+
.default("bottom_right")
|
|
13
|
+
.describe("Corner or screen placement for the overlay. Default: 'bottom_right'."),
|
|
14
|
+
padding: z
|
|
15
|
+
.number()
|
|
16
|
+
.int()
|
|
17
|
+
.min(0)
|
|
18
|
+
.max(500)
|
|
19
|
+
.optional()
|
|
20
|
+
.default(20)
|
|
21
|
+
.describe("Margin padding in pixels from the edge. Default: 20."),
|
|
22
|
+
scaleWidth: z
|
|
23
|
+
.number()
|
|
24
|
+
.int()
|
|
25
|
+
.positive()
|
|
26
|
+
.optional()
|
|
27
|
+
.describe("Optional width in pixels to scale the overlay, keeping its original aspect ratio (e.g. 150)."),
|
|
28
|
+
opacity: z
|
|
29
|
+
.number()
|
|
30
|
+
.min(0.05)
|
|
31
|
+
.max(1.0)
|
|
32
|
+
.optional()
|
|
33
|
+
.default(1.0)
|
|
34
|
+
.describe("Overlay opacity from 0.05 (subtle watermark) to 1.0 (fully opaque). Default: 1.0."),
|
|
35
|
+
hardwareAcceleration: z
|
|
36
|
+
.boolean()
|
|
37
|
+
.optional()
|
|
38
|
+
.default(true)
|
|
39
|
+
.describe("Use hardware-accelerated video encoding when available. Default: true."),
|
|
40
|
+
});
|
|
41
|
+
export async function addOverlay(params, options) {
|
|
42
|
+
const { videoPath, overlayPath, outputPath, position = "bottom_right", padding = 20, scaleWidth, opacity = 1.0, hardwareAcceleration = true, } = params;
|
|
43
|
+
const video = await ensureFileExists(videoPath);
|
|
44
|
+
const overlay = await ensureFileExists(overlayPath);
|
|
45
|
+
const output = await ensureOutputDir(outputPath);
|
|
46
|
+
const probe = await probeMedia(video, { signal: options?.signal });
|
|
47
|
+
const duration = probe.format.durationSeconds;
|
|
48
|
+
const hasAudio = probe.audioStreams.length > 0;
|
|
49
|
+
const hwInfo = await detectHwAccel();
|
|
50
|
+
const useHw = hardwareAcceleration && hwInfo.isAvailable;
|
|
51
|
+
const videoEncoder = useHw ? hwInfo.h264Encoder : "libx264";
|
|
52
|
+
// Build overlay filter chain
|
|
53
|
+
let overlayFilterChain = "[1:v]";
|
|
54
|
+
if (scaleWidth) {
|
|
55
|
+
overlayFilterChain += `scale=${scaleWidth}:-1,`;
|
|
56
|
+
}
|
|
57
|
+
if (opacity < 1.0) {
|
|
58
|
+
overlayFilterChain += `format=rgba,colorchannelmixer=aa=${opacity.toFixed(2)}[logo]`;
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
overlayFilterChain += "format=rgba[logo]";
|
|
62
|
+
}
|
|
63
|
+
let coords = "W-w-20:H-h-20";
|
|
64
|
+
switch (position) {
|
|
65
|
+
case "top_left":
|
|
66
|
+
coords = `${padding}:${padding}`;
|
|
67
|
+
break;
|
|
68
|
+
case "top_right":
|
|
69
|
+
coords = `W-w-${padding}:${padding}`;
|
|
70
|
+
break;
|
|
71
|
+
case "bottom_left":
|
|
72
|
+
coords = `${padding}:H-h-${padding}`;
|
|
73
|
+
break;
|
|
74
|
+
case "bottom_right":
|
|
75
|
+
coords = `W-w-${padding}:H-h-${padding}`;
|
|
76
|
+
break;
|
|
77
|
+
case "center":
|
|
78
|
+
coords = "(W-w)/2:(H-h)/2";
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
const filter = `${overlayFilterChain};[0:v][logo]overlay=${coords}[outv]`;
|
|
82
|
+
const args = [
|
|
83
|
+
"-i", video,
|
|
84
|
+
"-i", overlay,
|
|
85
|
+
"-filter_complex", filter,
|
|
86
|
+
"-map", "[outv]",
|
|
87
|
+
];
|
|
88
|
+
if (hasAudio) {
|
|
89
|
+
args.push("-map", "0:a?", "-c:a", "copy");
|
|
90
|
+
}
|
|
91
|
+
args.push("-c:v", videoEncoder);
|
|
92
|
+
if (useHw) {
|
|
93
|
+
args.push("-b:v", "4M", "-pix_fmt", "yuv420p");
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
args.push("-crf", "22", "-preset", "fast", "-pix_fmt", "yuv420p");
|
|
97
|
+
}
|
|
98
|
+
args.push("-movflags", "+faststart", "-y", output);
|
|
99
|
+
const res = await runFfmpeg(args, {
|
|
100
|
+
signal: options?.signal,
|
|
101
|
+
onProgress: options?.onProgress,
|
|
102
|
+
expectedDurationSeconds: duration,
|
|
103
|
+
});
|
|
104
|
+
return {
|
|
105
|
+
success: true,
|
|
106
|
+
outputPath: output,
|
|
107
|
+
position,
|
|
108
|
+
padding,
|
|
109
|
+
opacity,
|
|
110
|
+
encoderUsed: videoEncoder,
|
|
111
|
+
hardwareAccelerated: useHw,
|
|
112
|
+
commandExecuted: res.command,
|
|
113
|
+
durationSeconds: res.durationSeconds,
|
|
114
|
+
};
|
|
115
|
+
}
|
package/dist/tools/probe.js
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
1
2
|
import { ensureFileExists, runCommand } from "../utils/exec.js";
|
|
3
|
+
export const probeMediaSchema = z.object({
|
|
4
|
+
inputPath: z.string().describe("Path to the media file to inspect (e.g. './video.mp4')"),
|
|
5
|
+
});
|
|
2
6
|
function formatBytes(bytes) {
|
|
3
7
|
if (bytes < 1024)
|
|
4
8
|
return `${bytes} B`;
|
|
@@ -8,7 +12,7 @@ function formatBytes(bytes) {
|
|
|
8
12
|
return `${(bytes / (1024 * 1024)).toFixed(2)} MB`;
|
|
9
13
|
return `${(bytes / (1024 * 1024 * 1024)).toFixed(2)} GB`;
|
|
10
14
|
}
|
|
11
|
-
export async function probeMedia(inputPath) {
|
|
15
|
+
export async function probeMedia(inputPath, options) {
|
|
12
16
|
const resolvedPath = await ensureFileExists(inputPath);
|
|
13
17
|
const args = [
|
|
14
18
|
"-v", "quiet",
|
|
@@ -17,7 +21,7 @@ export async function probeMedia(inputPath) {
|
|
|
17
21
|
"-show_streams",
|
|
18
22
|
resolvedPath,
|
|
19
23
|
];
|
|
20
|
-
const { stdout } = await runCommand("ffprobe", args);
|
|
24
|
+
const { stdout } = await runCommand("ffprobe", args, { signal: options?.signal });
|
|
21
25
|
const data = JSON.parse(stdout);
|
|
22
26
|
const format = data.format || {};
|
|
23
27
|
const streams = data.streams || [];
|
package/dist/tools/reformat.js
CHANGED
|
@@ -15,6 +15,7 @@ export const reformatAspectSchema = z.object({
|
|
|
15
15
|
.describe("Format mode: 'blur_background' adds blurred video borders, 'fit_pad' adds solid color padding, and 'crop_center' zooms and crops the center. Default: 'blur_background'."),
|
|
16
16
|
padColor: z
|
|
17
17
|
.string()
|
|
18
|
+
.regex(/^(0x[0-9a-fA-F]{6}|#[0-9a-fA-F]{6}|[a-zA-Z]{3,20})$/, "padColor must be hex (e.g. '0x1e1e2e') or color name (e.g. 'black', 'white')")
|
|
18
19
|
.default("black")
|
|
19
20
|
.describe("Background color for fit_pad mode, such as 'black', 'white', or hex '0x1e1e2e'. Default: 'black'."),
|
|
20
21
|
blurAmount: z
|
|
@@ -26,7 +27,7 @@ export const reformatAspectSchema = z.object({
|
|
|
26
27
|
hardwareAcceleration: z
|
|
27
28
|
.boolean()
|
|
28
29
|
.optional()
|
|
29
|
-
.describe("Use
|
|
30
|
+
.describe("Use hardware-accelerated video encoding (VideoToolbox, NVENC, QSV, etc.) when available. Default: true."),
|
|
30
31
|
});
|
|
31
32
|
const RATIO_DIMENSIONS = {
|
|
32
33
|
"9:16": { width: 1080, height: 1920 },
|
|
@@ -34,7 +35,7 @@ const RATIO_DIMENSIONS = {
|
|
|
34
35
|
"16:9": { width: 1920, height: 1080 },
|
|
35
36
|
"4:5": { width: 1080, height: 1350 },
|
|
36
37
|
};
|
|
37
|
-
export async function reformatAspectRatio(args) {
|
|
38
|
+
export async function reformatAspectRatio(args, options) {
|
|
38
39
|
const input = await ensureFileExists(args.inputPath);
|
|
39
40
|
const output = await ensureOutputDir(args.outputPath);
|
|
40
41
|
const targetRatio = args.targetRatio ?? "9:16";
|
|
@@ -43,11 +44,13 @@ export async function reformatAspectRatio(args) {
|
|
|
43
44
|
const blurAmount = args.blurAmount ?? 20;
|
|
44
45
|
const dims = RATIO_DIMENSIONS[targetRatio] || RATIO_DIMENSIONS["9:16"];
|
|
45
46
|
const { width: tw, height: th } = dims;
|
|
46
|
-
const probeInfo = await probeMedia(input);
|
|
47
|
+
const probeInfo = await probeMedia(input, { signal: options?.signal });
|
|
47
48
|
const hasAudio = probeInfo.audioStreams.length > 0;
|
|
49
|
+
const duration = probeInfo.format.durationSeconds;
|
|
48
50
|
const hwInfo = await detectHwAccel();
|
|
49
51
|
const useHw = args.hardwareAcceleration ?? hwInfo.isAvailable;
|
|
50
|
-
const
|
|
52
|
+
const isHwUsed = useHw && hwInfo.isAvailable;
|
|
53
|
+
const videoEncoder = isHwUsed ? hwInfo.h264Encoder : "libx264";
|
|
51
54
|
const ffmpegArgs = ["-i", input];
|
|
52
55
|
if (mode === "blur_background") {
|
|
53
56
|
const filter = `[0:v]split=2[bg][fg];[bg]scale=${tw}:${th}:force_original_aspect_ratio=increase,crop=${tw}:${th},boxblur=${blurAmount}:${blurAmount}[blurred];[fg]scale=${tw}:${th}:force_original_aspect_ratio=decrease[sharp];[blurred][sharp]overlay=(W-w)/2:(H-h)/2,setsar=1[outv]`;
|
|
@@ -70,14 +73,18 @@ export async function reformatAspectRatio(args) {
|
|
|
70
73
|
}
|
|
71
74
|
}
|
|
72
75
|
ffmpegArgs.push("-c:v", videoEncoder);
|
|
73
|
-
if (
|
|
76
|
+
if (isHwUsed) {
|
|
74
77
|
ffmpegArgs.push("-b:v", "4M", "-pix_fmt", "yuv420p");
|
|
75
78
|
}
|
|
76
79
|
else {
|
|
77
80
|
ffmpegArgs.push("-crf", "22", "-preset", "fast", "-pix_fmt", "yuv420p");
|
|
78
81
|
}
|
|
79
82
|
ffmpegArgs.push("-movflags", "+faststart", "-y", output);
|
|
80
|
-
const res = await runFfmpeg(ffmpegArgs
|
|
83
|
+
const res = await runFfmpeg(ffmpegArgs, {
|
|
84
|
+
signal: options?.signal,
|
|
85
|
+
onProgress: options?.onProgress,
|
|
86
|
+
expectedDurationSeconds: duration,
|
|
87
|
+
});
|
|
81
88
|
return {
|
|
82
89
|
success: true,
|
|
83
90
|
outputPath: output,
|
|
@@ -85,7 +92,7 @@ export async function reformatAspectRatio(args) {
|
|
|
85
92
|
mode,
|
|
86
93
|
resolution: `${tw}x${th}`,
|
|
87
94
|
encoderUsed: videoEncoder,
|
|
88
|
-
hardwareAccelerated:
|
|
95
|
+
hardwareAccelerated: isHwUsed,
|
|
89
96
|
commandExecuted: res.command,
|
|
90
97
|
durationSeconds: res.durationSeconds,
|
|
91
98
|
};
|