@ash_j1997/ffmpeg-mcp-server 0.0.0-stage → 0.4.2
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/LICENSE +21 -0
- package/README.md +104 -2
- package/dist/index.js +350 -0
- package/dist/tools/audio.js +31 -0
- package/dist/tools/compress.js +119 -0
- package/dist/tools/concat.js +50 -0
- package/dist/tools/convert.js +68 -0
- package/dist/tools/probe.js +66 -0
- package/dist/tools/reformat.js +92 -0
- package/dist/tools/silence.js +69 -0
- package/dist/tools/speed.js +46 -0
- package/dist/tools/split.js +125 -0
- package/dist/tools/subtitles.js +36 -0
- package/dist/tools/thumbnail.js +51 -0
- package/dist/tools/trim.js +29 -0
- package/dist/tools/waveform.js +46 -0
- package/dist/utils/exec.js +73 -0
- package/dist/utils/hwaccel.js +45 -0
- package/package.json +45 -4
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import os from "node:os";
|
|
4
|
+
import { z } from "zod";
|
|
5
|
+
import { runFfmpeg, ensureFileExists, ensureOutputDir } from "../utils/exec.js";
|
|
6
|
+
export const concatMediaSchema = z.object({
|
|
7
|
+
inputPaths: z.array(z.string()).min(2).describe("List of media files to concatenate in sequential order"),
|
|
8
|
+
outputPath: z.string().describe("Target output file path"),
|
|
9
|
+
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
|
+
export async function concatMedia(params) {
|
|
12
|
+
const { inputPaths, outputPath, mode = "lossless" } = params;
|
|
13
|
+
const resolvedInputs = [];
|
|
14
|
+
for (const p of inputPaths) {
|
|
15
|
+
resolvedInputs.push(await ensureFileExists(p));
|
|
16
|
+
}
|
|
17
|
+
const output = await ensureOutputDir(outputPath);
|
|
18
|
+
const tmpManifest = path.join(os.tmpdir(), `mcp_concat_${Date.now()}_${Math.random().toString(36).substring(7)}.txt`);
|
|
19
|
+
const manifestContent = resolvedInputs
|
|
20
|
+
.map((p) => `file '${p.replace(/'/g, "'\\''")}'`)
|
|
21
|
+
.join("\n");
|
|
22
|
+
fs.writeFileSync(tmpManifest, manifestContent, "utf8");
|
|
23
|
+
try {
|
|
24
|
+
const args = ["-f", "concat", "-safe", "0", "-i", tmpManifest];
|
|
25
|
+
if (mode === "lossless") {
|
|
26
|
+
args.push("-c", "copy");
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
args.push("-c:v", "libx264", "-pix_fmt", "yuv420p", "-c:a", "aac", "-b:a", "192k", "-movflags", "+faststart");
|
|
30
|
+
}
|
|
31
|
+
args.push("-y", output);
|
|
32
|
+
const result = await runFfmpeg(args);
|
|
33
|
+
return {
|
|
34
|
+
success: true,
|
|
35
|
+
outputPath: output,
|
|
36
|
+
totalInputs: resolvedInputs.length,
|
|
37
|
+
mode,
|
|
38
|
+
commandExecuted: result.command,
|
|
39
|
+
durationSeconds: result.durationSeconds,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
finally {
|
|
43
|
+
if (fs.existsSync(tmpManifest)) {
|
|
44
|
+
try {
|
|
45
|
+
fs.unlinkSync(tmpManifest);
|
|
46
|
+
}
|
|
47
|
+
catch { }
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { ensureFileExists, ensureOutputDir, runCommand } from "../utils/exec.js";
|
|
2
|
+
import { detectHwAccel } from "../utils/hwaccel.js";
|
|
3
|
+
export async function convertMedia(options) {
|
|
4
|
+
const input = await ensureFileExists(options.inputPath);
|
|
5
|
+
const output = await ensureOutputDir(options.outputPath);
|
|
6
|
+
const hwInfo = await detectHwAccel();
|
|
7
|
+
const useHw = options.hardwareAcceleration ?? hwInfo.isAvailable;
|
|
8
|
+
const preset = options.preset || "auto";
|
|
9
|
+
const args = ["-y", "-i", input];
|
|
10
|
+
let isHwUsed = false;
|
|
11
|
+
if (preset === "gif") {
|
|
12
|
+
const scale = options.resolution
|
|
13
|
+
? `scale=${options.resolution.replace("x", ":")}:flags=lanczos`
|
|
14
|
+
: "scale=iw:ih:flags=lanczos";
|
|
15
|
+
const fps = options.fps ? `fps=${options.fps},` : "fps=15,";
|
|
16
|
+
const filter = `${fps}${scale},split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse`;
|
|
17
|
+
args.push("-vf", filter, output);
|
|
18
|
+
}
|
|
19
|
+
else if (preset === "web_mp4" || (preset === "auto" && output.endsWith(".mp4"))) {
|
|
20
|
+
if (useHw && hwInfo.isAvailable) {
|
|
21
|
+
args.push("-c:v", hwInfo.h264Encoder);
|
|
22
|
+
args.push("-b:v", "4M");
|
|
23
|
+
isHwUsed = true;
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
args.push("-c:v", "libx264");
|
|
27
|
+
args.push("-crf", String(options.crf ?? 23));
|
|
28
|
+
args.push("-preset", "medium");
|
|
29
|
+
}
|
|
30
|
+
args.push("-pix_fmt", "yuv420p");
|
|
31
|
+
args.push("-c:a", "aac");
|
|
32
|
+
args.push("-b:a", "192k");
|
|
33
|
+
args.push("-movflags", "+faststart");
|
|
34
|
+
if (options.resolution) {
|
|
35
|
+
args.push("-vf", `scale=${options.resolution.replace("x", ":")}`);
|
|
36
|
+
}
|
|
37
|
+
if (options.fps) {
|
|
38
|
+
args.push("-r", String(options.fps));
|
|
39
|
+
}
|
|
40
|
+
args.push(output);
|
|
41
|
+
}
|
|
42
|
+
else if (preset === "webm" || (preset === "auto" && output.endsWith(".webm"))) {
|
|
43
|
+
args.push("-c:v", "libvpx-vp9");
|
|
44
|
+
args.push("-crf", String(options.crf ?? 30));
|
|
45
|
+
args.push("-b:v", "0");
|
|
46
|
+
args.push("-c:a", "libopus");
|
|
47
|
+
args.push("-b:a", "128k");
|
|
48
|
+
if (options.resolution) {
|
|
49
|
+
args.push("-vf", `scale=${options.resolution.replace("x", ":")}`);
|
|
50
|
+
}
|
|
51
|
+
args.push(output);
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
if (options.crf) {
|
|
55
|
+
args.push("-crf", String(options.crf));
|
|
56
|
+
}
|
|
57
|
+
if (options.resolution) {
|
|
58
|
+
args.push("-vf", `scale=${options.resolution.replace("x", ":")}`);
|
|
59
|
+
}
|
|
60
|
+
args.push(output);
|
|
61
|
+
}
|
|
62
|
+
await runCommand("ffmpeg", args);
|
|
63
|
+
return {
|
|
64
|
+
outputPath: output,
|
|
65
|
+
commandExecuted: `ffmpeg ${args.join(" ")}`,
|
|
66
|
+
hardwareAccelerated: isHwUsed,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { ensureFileExists, runCommand } from "../utils/exec.js";
|
|
2
|
+
function formatBytes(bytes) {
|
|
3
|
+
if (bytes < 1024)
|
|
4
|
+
return `${bytes} B`;
|
|
5
|
+
if (bytes < 1024 * 1024)
|
|
6
|
+
return `${(bytes / 1024).toFixed(1)} KB`;
|
|
7
|
+
if (bytes < 1024 * 1024 * 1024)
|
|
8
|
+
return `${(bytes / (1024 * 1024)).toFixed(2)} MB`;
|
|
9
|
+
return `${(bytes / (1024 * 1024 * 1024)).toFixed(2)} GB`;
|
|
10
|
+
}
|
|
11
|
+
export async function probeMedia(inputPath) {
|
|
12
|
+
const resolvedPath = await ensureFileExists(inputPath);
|
|
13
|
+
const args = [
|
|
14
|
+
"-v", "quiet",
|
|
15
|
+
"-print_format", "json",
|
|
16
|
+
"-show_format",
|
|
17
|
+
"-show_streams",
|
|
18
|
+
resolvedPath,
|
|
19
|
+
];
|
|
20
|
+
const { stdout } = await runCommand("ffprobe", args);
|
|
21
|
+
const data = JSON.parse(stdout);
|
|
22
|
+
const format = data.format || {};
|
|
23
|
+
const streams = data.streams || [];
|
|
24
|
+
const size = Number(format.size) || 0;
|
|
25
|
+
const duration = Number(format.duration) || 0;
|
|
26
|
+
const bitRate = Number(format.bit_rate) || 0;
|
|
27
|
+
const videoStreams = streams
|
|
28
|
+
.filter((s) => s.codec_type === "video")
|
|
29
|
+
.map((s) => ({
|
|
30
|
+
codec: s.codec_name,
|
|
31
|
+
resolution: `${s.width}x${s.height}`,
|
|
32
|
+
width: s.width,
|
|
33
|
+
height: s.height,
|
|
34
|
+
fps: s.avg_frame_rate || s.r_frame_rate || "unknown",
|
|
35
|
+
pixelFormat: s.pix_fmt,
|
|
36
|
+
aspectRatio: s.display_aspect_ratio,
|
|
37
|
+
}));
|
|
38
|
+
const audioStreams = streams
|
|
39
|
+
.filter((s) => s.codec_type === "audio")
|
|
40
|
+
.map((s) => ({
|
|
41
|
+
codec: s.codec_name,
|
|
42
|
+
sampleRateHz: Number(s.sample_rate) || 0,
|
|
43
|
+
channels: Number(s.channels) || 0,
|
|
44
|
+
channelLayout: s.channel_layout,
|
|
45
|
+
bitrateKbps: s.bit_rate ? Math.round(Number(s.bit_rate) / 1000) : undefined,
|
|
46
|
+
}));
|
|
47
|
+
const subtitleStreams = streams
|
|
48
|
+
.filter((s) => s.codec_type === "subtitle")
|
|
49
|
+
.map((s) => ({
|
|
50
|
+
codec: s.codec_name,
|
|
51
|
+
language: s.tags?.language,
|
|
52
|
+
}));
|
|
53
|
+
return {
|
|
54
|
+
filePath: resolvedPath,
|
|
55
|
+
format: {
|
|
56
|
+
formatName: format.format_name || "unknown",
|
|
57
|
+
durationSeconds: Math.round(duration * 100) / 100,
|
|
58
|
+
sizeBytes: size,
|
|
59
|
+
sizeFormatted: formatBytes(size),
|
|
60
|
+
bitrateKbps: Math.round(bitRate / 1000),
|
|
61
|
+
},
|
|
62
|
+
videoStreams,
|
|
63
|
+
audioStreams,
|
|
64
|
+
subtitleStreams,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
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 reformatAspectSchema = z.object({
|
|
6
|
+
inputPath: z.string().describe("Path to the source video file."),
|
|
7
|
+
outputPath: z.string().describe("Destination path for the reformatted video."),
|
|
8
|
+
targetRatio: z
|
|
9
|
+
.enum(["9:16", "1:1", "16:9", "4:5"])
|
|
10
|
+
.default("9:16")
|
|
11
|
+
.describe("Target aspect ratio: '9:16' for vertical reels, '1:1' for square, '16:9' for horizontal, or '4:5' for portrait. Default: '9:16'."),
|
|
12
|
+
mode: z
|
|
13
|
+
.enum(["blur_background", "fit_pad", "crop_center"])
|
|
14
|
+
.default("blur_background")
|
|
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
|
+
padColor: z
|
|
17
|
+
.string()
|
|
18
|
+
.default("black")
|
|
19
|
+
.describe("Background color for fit_pad mode, such as 'black', 'white', or hex '0x1e1e2e'. Default: 'black'."),
|
|
20
|
+
blurAmount: z
|
|
21
|
+
.number()
|
|
22
|
+
.min(5)
|
|
23
|
+
.max(50)
|
|
24
|
+
.default(20)
|
|
25
|
+
.describe("Blur strength (5 to 50) for blur_background mode. Default: 20."),
|
|
26
|
+
hardwareAcceleration: z
|
|
27
|
+
.boolean()
|
|
28
|
+
.optional()
|
|
29
|
+
.describe("Use Apple Silicon VideoToolbox encoding when available. Default: true."),
|
|
30
|
+
});
|
|
31
|
+
const RATIO_DIMENSIONS = {
|
|
32
|
+
"9:16": { width: 1080, height: 1920 },
|
|
33
|
+
"1:1": { width: 1080, height: 1080 },
|
|
34
|
+
"16:9": { width: 1920, height: 1080 },
|
|
35
|
+
"4:5": { width: 1080, height: 1350 },
|
|
36
|
+
};
|
|
37
|
+
export async function reformatAspectRatio(args) {
|
|
38
|
+
const input = await ensureFileExists(args.inputPath);
|
|
39
|
+
const output = await ensureOutputDir(args.outputPath);
|
|
40
|
+
const targetRatio = args.targetRatio ?? "9:16";
|
|
41
|
+
const mode = args.mode ?? "blur_background";
|
|
42
|
+
const padColor = args.padColor ?? "black";
|
|
43
|
+
const blurAmount = args.blurAmount ?? 20;
|
|
44
|
+
const dims = RATIO_DIMENSIONS[targetRatio] || RATIO_DIMENSIONS["9:16"];
|
|
45
|
+
const { width: tw, height: th } = dims;
|
|
46
|
+
const probeInfo = await probeMedia(input);
|
|
47
|
+
const hasAudio = probeInfo.audioStreams.length > 0;
|
|
48
|
+
const hwInfo = await detectHwAccel();
|
|
49
|
+
const useHw = args.hardwareAcceleration ?? hwInfo.isAvailable;
|
|
50
|
+
const videoEncoder = useHw && hwInfo.isAvailable ? hwInfo.h264Encoder : "libx264";
|
|
51
|
+
const ffmpegArgs = ["-i", input];
|
|
52
|
+
if (mode === "blur_background") {
|
|
53
|
+
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]`;
|
|
54
|
+
ffmpegArgs.push("-filter_complex", filter, "-map", "[outv]");
|
|
55
|
+
}
|
|
56
|
+
else if (mode === "fit_pad") {
|
|
57
|
+
const filter = `scale=${tw}:${th}:force_original_aspect_ratio=decrease,pad=${tw}:${th}:(ow-iw)/2:(oh-ih)/2:color=${padColor},setsar=1`;
|
|
58
|
+
ffmpegArgs.push("-vf", filter);
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
const filter = `scale=${tw}:${th}:force_original_aspect_ratio=increase,crop=${tw}:${th},setsar=1`;
|
|
62
|
+
ffmpegArgs.push("-vf", filter);
|
|
63
|
+
}
|
|
64
|
+
if (hasAudio) {
|
|
65
|
+
if (mode === "blur_background") {
|
|
66
|
+
ffmpegArgs.push("-map", "0:a?", "-c:a", "aac", "-b:a", "192k");
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
ffmpegArgs.push("-c:a", "aac", "-b:a", "192k");
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
ffmpegArgs.push("-c:v", videoEncoder);
|
|
73
|
+
if (videoEncoder === "h264_videotoolbox") {
|
|
74
|
+
ffmpegArgs.push("-b:v", "4M", "-pix_fmt", "yuv420p");
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
ffmpegArgs.push("-crf", "22", "-preset", "fast", "-pix_fmt", "yuv420p");
|
|
78
|
+
}
|
|
79
|
+
ffmpegArgs.push("-movflags", "+faststart", "-y", output);
|
|
80
|
+
const res = await runFfmpeg(ffmpegArgs);
|
|
81
|
+
return {
|
|
82
|
+
success: true,
|
|
83
|
+
outputPath: output,
|
|
84
|
+
targetRatio,
|
|
85
|
+
mode,
|
|
86
|
+
resolution: `${tw}x${th}`,
|
|
87
|
+
encoderUsed: videoEncoder,
|
|
88
|
+
hardwareAccelerated: videoEncoder.includes("videotoolbox"),
|
|
89
|
+
commandExecuted: res.command,
|
|
90
|
+
durationSeconds: res.durationSeconds,
|
|
91
|
+
};
|
|
92
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { ensureFileExists, ensureOutputDir, runFfmpeg } from "../utils/exec.js";
|
|
3
|
+
import { probeMedia } from "./probe.js";
|
|
4
|
+
export const trimSilenceSchema = z.object({
|
|
5
|
+
inputPath: z.string().describe("Path to the source audio or video file."),
|
|
6
|
+
outputPath: z.string().describe("Destination path for the trimmed file (e.g. './recordings/cleaned.mp4')."),
|
|
7
|
+
mode: z
|
|
8
|
+
.enum(["start_end", "all_pauses"])
|
|
9
|
+
.default("start_end")
|
|
10
|
+
.describe("Trim mode: 'start_end' removes silence at the start and end; 'all_pauses' removes silent pauses across the file. Default: 'start_end'."),
|
|
11
|
+
thresholdDb: z
|
|
12
|
+
.number()
|
|
13
|
+
.min(-60)
|
|
14
|
+
.max(-10)
|
|
15
|
+
.default(-35)
|
|
16
|
+
.describe("Volume threshold in decibels, between -60dB and -10dB. Audio quieter than this is treated as silence. Default: -35."),
|
|
17
|
+
minDurationSeconds: z
|
|
18
|
+
.number()
|
|
19
|
+
.min(0.1)
|
|
20
|
+
.max(10)
|
|
21
|
+
.default(0.5)
|
|
22
|
+
.describe("Minimum silence duration in seconds to trigger a cut. Default: 0.5."),
|
|
23
|
+
});
|
|
24
|
+
export async function trimSilence(args) {
|
|
25
|
+
const input = await ensureFileExists(args.inputPath);
|
|
26
|
+
const output = await ensureOutputDir(args.outputPath);
|
|
27
|
+
const mode = args.mode ?? "start_end";
|
|
28
|
+
const thresholdDb = args.thresholdDb ?? -35;
|
|
29
|
+
const minDuration = args.minDurationSeconds ?? 0.5;
|
|
30
|
+
const originalProbe = await probeMedia(input);
|
|
31
|
+
if (originalProbe.audioStreams.length === 0) {
|
|
32
|
+
throw new Error(`Cannot trim silence: input file '${input}' contains no audio streams.`);
|
|
33
|
+
}
|
|
34
|
+
const isVideo = originalProbe.videoStreams.length > 0;
|
|
35
|
+
let audioFilter = "";
|
|
36
|
+
if (mode === "start_end") {
|
|
37
|
+
audioFilter = `silenceremove=start_periods=1:start_duration=${minDuration}:start_threshold=${thresholdDb}dB:detection=peak,areverse,silenceremove=start_periods=1:start_duration=${minDuration}:start_threshold=${thresholdDb}dB:detection=peak,areverse`;
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
audioFilter = `silenceremove=stop_periods=-1:stop_duration=${minDuration}:stop_threshold=${thresholdDb}dB`;
|
|
41
|
+
}
|
|
42
|
+
const ffmpegArgs = ["-i", input, "-af", audioFilter];
|
|
43
|
+
if (isVideo) {
|
|
44
|
+
ffmpegArgs.push("-c:v", "copy", "-c:a", "aac", "-b:a", "192k");
|
|
45
|
+
}
|
|
46
|
+
ffmpegArgs.push("-y", output);
|
|
47
|
+
const res = await runFfmpeg(ffmpegArgs);
|
|
48
|
+
let timeSaved = 0;
|
|
49
|
+
let newDuration = 0;
|
|
50
|
+
try {
|
|
51
|
+
const newProbe = await probeMedia(output);
|
|
52
|
+
newDuration = newProbe.format.durationSeconds;
|
|
53
|
+
timeSaved = Number(Math.max(0, originalProbe.format.durationSeconds - newDuration).toFixed(2));
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
// Optional
|
|
57
|
+
}
|
|
58
|
+
return {
|
|
59
|
+
success: true,
|
|
60
|
+
outputPath: output,
|
|
61
|
+
mode,
|
|
62
|
+
originalDurationSeconds: originalProbe.format.durationSeconds,
|
|
63
|
+
newDurationSeconds: newDuration,
|
|
64
|
+
timeSavedSeconds: timeSaved,
|
|
65
|
+
thresholdDb,
|
|
66
|
+
commandExecuted: res.command,
|
|
67
|
+
durationSeconds: res.durationSeconds,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { runFfmpeg, ensureFileExists, ensureOutputDir } from "../utils/exec.js";
|
|
3
|
+
export const changeSpeedSchema = z.object({
|
|
4
|
+
inputPath: z.string().describe("Path to the source video or audio file"),
|
|
5
|
+
outputPath: z.string().describe("Target output file path"),
|
|
6
|
+
speed: z.number().positive().min(0.1).max(50.0).describe("Playback speed multiplier (e.g., 2.0 for 2x fast-forward/timelapse, 0.5 for slow-motion)"),
|
|
7
|
+
muteAudio: z.boolean().optional().default(false).describe("Whether to strip audio track completely"),
|
|
8
|
+
});
|
|
9
|
+
function getAtempoFilter(speed) {
|
|
10
|
+
const filters = [];
|
|
11
|
+
let s = speed;
|
|
12
|
+
while (s > 2.0) {
|
|
13
|
+
filters.push("atempo=2.0");
|
|
14
|
+
s /= 2.0;
|
|
15
|
+
}
|
|
16
|
+
while (s < 0.5) {
|
|
17
|
+
filters.push("atempo=0.5");
|
|
18
|
+
s /= 0.5;
|
|
19
|
+
}
|
|
20
|
+
filters.push(`atempo=${s.toFixed(4)}`);
|
|
21
|
+
return filters.join(",");
|
|
22
|
+
}
|
|
23
|
+
export async function changeSpeed(params) {
|
|
24
|
+
const { inputPath, outputPath, speed, muteAudio = false } = params;
|
|
25
|
+
const input = await ensureFileExists(inputPath);
|
|
26
|
+
const output = await ensureOutputDir(outputPath);
|
|
27
|
+
const videoFilter = `setpts=${(1 / speed).toFixed(6)}*PTS`;
|
|
28
|
+
const args = ["-i", input, "-filter:v", videoFilter];
|
|
29
|
+
if (muteAudio) {
|
|
30
|
+
args.push("-an");
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
const audioFilter = getAtempoFilter(speed);
|
|
34
|
+
args.push("-filter:a", audioFilter);
|
|
35
|
+
}
|
|
36
|
+
args.push("-c:v", "libx264", "-pix_fmt", "yuv420p", "-movflags", "+faststart", "-y", output);
|
|
37
|
+
const result = await runFfmpeg(args);
|
|
38
|
+
return {
|
|
39
|
+
success: true,
|
|
40
|
+
outputPath: output,
|
|
41
|
+
speedMultiplier: speed,
|
|
42
|
+
audioMuted: muteAudio,
|
|
43
|
+
commandExecuted: result.command,
|
|
44
|
+
durationSeconds: result.durationSeconds,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
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 splitScreenSchema = z.object({
|
|
6
|
+
firstVideoPath: z.string().describe("Path to the top video (vertical layout) or left video (horizontal layout)."),
|
|
7
|
+
secondVideoPath: z.string().describe("Path to the bottom video (vertical layout) or right video (horizontal layout)."),
|
|
8
|
+
outputPath: z.string().describe("Destination path for the split-screen output file (e.g. './reel.mp4')."),
|
|
9
|
+
layout: z
|
|
10
|
+
.enum(["vertical_9_16", "horizontal_16_9", "square_1_1"])
|
|
11
|
+
.default("vertical_9_16")
|
|
12
|
+
.describe("Split layout: 'vertical_9_16' (1080x1920 stacked top/bottom for Reels and TikTok), 'horizontal_16_9' (1920x1080 side-by-side), or 'square_1_1' (1080x1080 stacked). Default: 'vertical_9_16'."),
|
|
13
|
+
audioSource: z
|
|
14
|
+
.enum(["mix", "first", "second", "mute"])
|
|
15
|
+
.default("mix")
|
|
16
|
+
.describe("Audio track selection: 'mix' (blends audio from both videos), 'first' (uses only top/left audio), 'second' (uses only bottom/right audio), or 'mute'. Default: 'mix'."),
|
|
17
|
+
subtitlesPath: z
|
|
18
|
+
.string()
|
|
19
|
+
.optional()
|
|
20
|
+
.describe("Optional path to an .srt or .vtt subtitle file to embed into the output video."),
|
|
21
|
+
hardwareAcceleration: z
|
|
22
|
+
.boolean()
|
|
23
|
+
.optional()
|
|
24
|
+
.describe("Use Apple Silicon VideoToolbox encoding when available. Default: true."),
|
|
25
|
+
});
|
|
26
|
+
export async function createSplitScreen(params) {
|
|
27
|
+
const { firstVideoPath, secondVideoPath, outputPath, layout = "vertical_9_16", audioSource = "mix", subtitlesPath, hardwareAcceleration = true, } = params;
|
|
28
|
+
const firstInput = await ensureFileExists(firstVideoPath);
|
|
29
|
+
const secondInput = await ensureFileExists(secondVideoPath);
|
|
30
|
+
const output = await ensureOutputDir(outputPath);
|
|
31
|
+
let subInput;
|
|
32
|
+
if (subtitlesPath) {
|
|
33
|
+
subInput = await ensureFileExists(subtitlesPath);
|
|
34
|
+
}
|
|
35
|
+
const [firstProbe, secondProbe] = await Promise.all([
|
|
36
|
+
probeMedia(firstInput),
|
|
37
|
+
probeMedia(secondInput),
|
|
38
|
+
]);
|
|
39
|
+
const firstHasAudio = firstProbe.audioStreams.length > 0;
|
|
40
|
+
const secondHasAudio = secondProbe.audioStreams.length > 0;
|
|
41
|
+
const hwInfo = await detectHwAccel();
|
|
42
|
+
const useHw = hardwareAcceleration && hwInfo.isAvailable;
|
|
43
|
+
const videoEncoder = useHw ? hwInfo.h264Encoder : "libx264";
|
|
44
|
+
// Compute dimensions per tile
|
|
45
|
+
let filter = "";
|
|
46
|
+
let totalResolution = "1080x1920";
|
|
47
|
+
if (layout === "vertical_9_16") {
|
|
48
|
+
// 1080x1920 total -> each tile is 1080x960
|
|
49
|
+
totalResolution = "1080x1920";
|
|
50
|
+
filter = "[0:v]scale=1080:960:force_original_aspect_ratio=increase,crop=1080:960,setsar=1[v0];[1:v]scale=1080:960:force_original_aspect_ratio=increase,crop=1080:960,setsar=1[v1];[v0][v1]vstack=inputs=2[outv]";
|
|
51
|
+
}
|
|
52
|
+
else if (layout === "horizontal_16_9") {
|
|
53
|
+
// 1920x1080 total -> each tile is 960x1080
|
|
54
|
+
totalResolution = "1920x1080";
|
|
55
|
+
filter = "[0:v]scale=960:1080:force_original_aspect_ratio=increase,crop=960:1080,setsar=1[v0];[1:v]scale=960:1080:force_original_aspect_ratio=increase,crop=960:1080,setsar=1[v1];[v0][v1]hstack=inputs=2[outv]";
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
// square_1_1 -> 1080x1080 total -> each tile is 1080x540
|
|
59
|
+
totalResolution = "1080x1080";
|
|
60
|
+
filter = "[0:v]scale=1080:540:force_original_aspect_ratio=increase,crop=1080:540,setsar=1[v0];[1:v]scale=1080:540:force_original_aspect_ratio=increase,crop=1080:540,setsar=1[v1];[v0][v1]vstack=inputs=2[outv]";
|
|
61
|
+
}
|
|
62
|
+
const args = ["-i", firstInput, "-i", secondInput];
|
|
63
|
+
let hasSubtitles = false;
|
|
64
|
+
if (subInput) {
|
|
65
|
+
args.push("-i", subInput);
|
|
66
|
+
hasSubtitles = true;
|
|
67
|
+
}
|
|
68
|
+
// Audio filter handling
|
|
69
|
+
let audioMapped = false;
|
|
70
|
+
if (audioSource === "mix" && firstHasAudio && secondHasAudio) {
|
|
71
|
+
filter += ";[0:a][1:a]amix=inputs=2:duration=first:dropout_transition=2[outa]";
|
|
72
|
+
args.push("-filter_complex", filter, "-map", "[outv]", "-map", "[outa]");
|
|
73
|
+
audioMapped = true;
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
args.push("-filter_complex", filter, "-map", "[outv]");
|
|
77
|
+
if (audioSource === "first" && firstHasAudio) {
|
|
78
|
+
args.push("-map", "0:a:0");
|
|
79
|
+
audioMapped = true;
|
|
80
|
+
}
|
|
81
|
+
else if (audioSource === "second" && secondHasAudio) {
|
|
82
|
+
args.push("-map", "1:a:0");
|
|
83
|
+
audioMapped = true;
|
|
84
|
+
}
|
|
85
|
+
else if (audioSource === "mix") {
|
|
86
|
+
if (firstHasAudio) {
|
|
87
|
+
args.push("-map", "0:a:0");
|
|
88
|
+
audioMapped = true;
|
|
89
|
+
}
|
|
90
|
+
else if (secondHasAudio) {
|
|
91
|
+
args.push("-map", "1:a:0");
|
|
92
|
+
audioMapped = true;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
if (hasSubtitles) {
|
|
97
|
+
args.push("-map", "2:s");
|
|
98
|
+
const isMp4 = output.endsWith(".mp4") || output.endsWith(".mov");
|
|
99
|
+
args.push("-c:s", isMp4 ? "mov_text" : "srt");
|
|
100
|
+
}
|
|
101
|
+
args.push("-c:v", videoEncoder);
|
|
102
|
+
if (videoEncoder === "h264_videotoolbox") {
|
|
103
|
+
args.push("-b:v", "4M", "-pix_fmt", "yuv420p");
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
args.push("-crf", "22", "-preset", "fast", "-pix_fmt", "yuv420p");
|
|
107
|
+
}
|
|
108
|
+
if (audioMapped) {
|
|
109
|
+
args.push("-c:a", "aac", "-b:a", "192k");
|
|
110
|
+
}
|
|
111
|
+
args.push("-movflags", "+faststart", "-y", output);
|
|
112
|
+
const res = await runFfmpeg(args);
|
|
113
|
+
return {
|
|
114
|
+
success: true,
|
|
115
|
+
outputPath: output,
|
|
116
|
+
layout,
|
|
117
|
+
resolution: totalResolution,
|
|
118
|
+
audioSource,
|
|
119
|
+
subtitlesEmbedded: hasSubtitles,
|
|
120
|
+
encoderUsed: videoEncoder,
|
|
121
|
+
hardwareAccelerated: videoEncoder.includes("videotoolbox"),
|
|
122
|
+
commandExecuted: res.command,
|
|
123
|
+
durationSeconds: res.durationSeconds,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { runFfmpeg, ensureFileExists, ensureOutputDir } from "../utils/exec.js";
|
|
3
|
+
export const addSubtitlesSchema = z.object({
|
|
4
|
+
videoPath: z.string().describe("Path to the source video file"),
|
|
5
|
+
subtitlesPath: z.string().describe("Path to subtitle file (.srt or .vtt)"),
|
|
6
|
+
outputPath: z.string().describe("Target output video path (.mp4, .mkv, or .mov)"),
|
|
7
|
+
language: z.string().optional().default("eng").describe("ISO 639-2 three-letter language code (e.g. 'eng', 'spa', 'fra', 'deu')"),
|
|
8
|
+
title: z.string().optional().default("English").describe("Subtitle track label displayed in video players"),
|
|
9
|
+
});
|
|
10
|
+
export async function addSubtitles(params) {
|
|
11
|
+
const { videoPath, subtitlesPath, outputPath, language = "eng", title = "English" } = params;
|
|
12
|
+
const video = await ensureFileExists(videoPath);
|
|
13
|
+
const subs = await ensureFileExists(subtitlesPath);
|
|
14
|
+
const output = await ensureOutputDir(outputPath);
|
|
15
|
+
const isMp4 = output.toLowerCase().endsWith(".mp4") || output.toLowerCase().endsWith(".mov");
|
|
16
|
+
const subtitleCodec = isMp4 ? "mov_text" : "srt";
|
|
17
|
+
const args = [
|
|
18
|
+
"-i", video,
|
|
19
|
+
"-i", subs,
|
|
20
|
+
"-c:v", "copy",
|
|
21
|
+
"-c:a", "copy",
|
|
22
|
+
"-c:s", subtitleCodec,
|
|
23
|
+
"-metadata:s:s:0", `language=${language}`,
|
|
24
|
+
"-metadata:s:s:0", `title=${title}`,
|
|
25
|
+
"-y", output
|
|
26
|
+
];
|
|
27
|
+
const result = await runFfmpeg(args);
|
|
28
|
+
return {
|
|
29
|
+
success: true,
|
|
30
|
+
outputPath: output,
|
|
31
|
+
subtitlesApplied: subs,
|
|
32
|
+
subtitleCodec,
|
|
33
|
+
commandExecuted: result.command,
|
|
34
|
+
durationSeconds: result.durationSeconds,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import fs from "node:fs/promises";
|
|
2
|
+
import { ensureFileExists, ensureOutputDir, runCommand } from "../utils/exec.js";
|
|
3
|
+
import { probeMedia } from "./probe.js";
|
|
4
|
+
export async function extractThumbnail(options) {
|
|
5
|
+
const input = await ensureFileExists(options.inputPath);
|
|
6
|
+
const output = await ensureOutputDir(options.outputPath);
|
|
7
|
+
let timestamp = options.timestamp || "00:00:01";
|
|
8
|
+
if (timestamp === "midpoint") {
|
|
9
|
+
try {
|
|
10
|
+
const info = await probeMedia(input);
|
|
11
|
+
const midpointSeconds = Math.max(0, Math.floor(info.format.durationSeconds / 2));
|
|
12
|
+
timestamp = String(midpointSeconds);
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
timestamp = "00:00:01";
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
const args = [
|
|
19
|
+
"-y",
|
|
20
|
+
"-ss", timestamp,
|
|
21
|
+
"-i", input,
|
|
22
|
+
"-vframes", "1",
|
|
23
|
+
];
|
|
24
|
+
if (options.resolution) {
|
|
25
|
+
args.push("-vf", `scale=${options.resolution.replace("x", ":")}`);
|
|
26
|
+
}
|
|
27
|
+
args.push(output);
|
|
28
|
+
await runCommand("ffmpeg", args);
|
|
29
|
+
let base64;
|
|
30
|
+
let mimeType;
|
|
31
|
+
const shouldInclude = options.includeBase64 ?? true;
|
|
32
|
+
if (shouldInclude) {
|
|
33
|
+
try {
|
|
34
|
+
const buffer = await fs.readFile(output);
|
|
35
|
+
if (buffer.length < 4 * 1024 * 1024) {
|
|
36
|
+
base64 = buffer.toString("base64");
|
|
37
|
+
mimeType = output.toLowerCase().endsWith(".png") ? "image/png" : "image/jpeg";
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
// Optional
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return {
|
|
45
|
+
outputPath: output,
|
|
46
|
+
timestampUsed: timestamp,
|
|
47
|
+
commandExecuted: `ffmpeg ${args.join(" ")}`,
|
|
48
|
+
base64,
|
|
49
|
+
mimeType,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { ensureFileExists, ensureOutputDir, runCommand } from "../utils/exec.js";
|
|
2
|
+
export async function trimMedia(options) {
|
|
3
|
+
const input = await ensureFileExists(options.inputPath);
|
|
4
|
+
const output = await ensureOutputDir(options.outputPath);
|
|
5
|
+
const args = ["-y"];
|
|
6
|
+
// Fast seek before -i
|
|
7
|
+
args.push("-ss", options.startTime);
|
|
8
|
+
args.push("-i", input);
|
|
9
|
+
if (options.endTime) {
|
|
10
|
+
args.push("-to", options.endTime);
|
|
11
|
+
}
|
|
12
|
+
else if (options.duration) {
|
|
13
|
+
args.push("-t", options.duration);
|
|
14
|
+
}
|
|
15
|
+
if (options.accurateReencode) {
|
|
16
|
+
// Re-encode for frame-accurate cut
|
|
17
|
+
args.push("-c:v", "libx264", "-c:a", "aac");
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
// Lossless instantaneous stream copy
|
|
21
|
+
args.push("-c", "copy");
|
|
22
|
+
}
|
|
23
|
+
args.push(output);
|
|
24
|
+
await runCommand("ffmpeg", args);
|
|
25
|
+
return {
|
|
26
|
+
outputPath: output,
|
|
27
|
+
commandExecuted: `ffmpeg ${args.join(" ")}`,
|
|
28
|
+
};
|
|
29
|
+
}
|