@ash_j1997/ffmpeg-mcp-server 0.4.2 → 0.5.0
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.md +34 -22
- package/dist/index.js +283 -91
- package/dist/tools/audio.js +24 -9
- package/dist/tools/burn_subtitles.js +92 -0
- package/dist/tools/compress.js +8 -4
- package/dist/tools/concat.js +14 -3
- package/dist/tools/convert.js +77 -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 +13 -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 +5 -2
- package/dist/tools/thumbnail.js +24 -13
- package/dist/tools/trim.js +36 -14
- package/dist/tools/waveform.js +3 -3
- package/dist/utils/exec.js +139 -8
- package/dist/utils/hwaccel.js +148 -26
- package/package.json +2 -1
package/dist/tools/reformat.js
CHANGED
|
@@ -26,7 +26,7 @@ export const reformatAspectSchema = z.object({
|
|
|
26
26
|
hardwareAcceleration: z
|
|
27
27
|
.boolean()
|
|
28
28
|
.optional()
|
|
29
|
-
.describe("Use
|
|
29
|
+
.describe("Use hardware-accelerated video encoding (VideoToolbox, NVENC, QSV, etc.) when available. Default: true."),
|
|
30
30
|
});
|
|
31
31
|
const RATIO_DIMENSIONS = {
|
|
32
32
|
"9:16": { width: 1080, height: 1920 },
|
|
@@ -34,7 +34,7 @@ const RATIO_DIMENSIONS = {
|
|
|
34
34
|
"16:9": { width: 1920, height: 1080 },
|
|
35
35
|
"4:5": { width: 1080, height: 1350 },
|
|
36
36
|
};
|
|
37
|
-
export async function reformatAspectRatio(args) {
|
|
37
|
+
export async function reformatAspectRatio(args, options) {
|
|
38
38
|
const input = await ensureFileExists(args.inputPath);
|
|
39
39
|
const output = await ensureOutputDir(args.outputPath);
|
|
40
40
|
const targetRatio = args.targetRatio ?? "9:16";
|
|
@@ -43,11 +43,13 @@ export async function reformatAspectRatio(args) {
|
|
|
43
43
|
const blurAmount = args.blurAmount ?? 20;
|
|
44
44
|
const dims = RATIO_DIMENSIONS[targetRatio] || RATIO_DIMENSIONS["9:16"];
|
|
45
45
|
const { width: tw, height: th } = dims;
|
|
46
|
-
const probeInfo = await probeMedia(input);
|
|
46
|
+
const probeInfo = await probeMedia(input, { signal: options?.signal });
|
|
47
47
|
const hasAudio = probeInfo.audioStreams.length > 0;
|
|
48
|
+
const duration = probeInfo.format.durationSeconds;
|
|
48
49
|
const hwInfo = await detectHwAccel();
|
|
49
50
|
const useHw = args.hardwareAcceleration ?? hwInfo.isAvailable;
|
|
50
|
-
const
|
|
51
|
+
const isHwUsed = useHw && hwInfo.isAvailable;
|
|
52
|
+
const videoEncoder = isHwUsed ? hwInfo.h264Encoder : "libx264";
|
|
51
53
|
const ffmpegArgs = ["-i", input];
|
|
52
54
|
if (mode === "blur_background") {
|
|
53
55
|
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 +72,18 @@ export async function reformatAspectRatio(args) {
|
|
|
70
72
|
}
|
|
71
73
|
}
|
|
72
74
|
ffmpegArgs.push("-c:v", videoEncoder);
|
|
73
|
-
if (
|
|
75
|
+
if (isHwUsed) {
|
|
74
76
|
ffmpegArgs.push("-b:v", "4M", "-pix_fmt", "yuv420p");
|
|
75
77
|
}
|
|
76
78
|
else {
|
|
77
79
|
ffmpegArgs.push("-crf", "22", "-preset", "fast", "-pix_fmt", "yuv420p");
|
|
78
80
|
}
|
|
79
81
|
ffmpegArgs.push("-movflags", "+faststart", "-y", output);
|
|
80
|
-
const res = await runFfmpeg(ffmpegArgs
|
|
82
|
+
const res = await runFfmpeg(ffmpegArgs, {
|
|
83
|
+
signal: options?.signal,
|
|
84
|
+
onProgress: options?.onProgress,
|
|
85
|
+
expectedDurationSeconds: duration,
|
|
86
|
+
});
|
|
81
87
|
return {
|
|
82
88
|
success: true,
|
|
83
89
|
outputPath: output,
|
|
@@ -85,7 +91,7 @@ export async function reformatAspectRatio(args) {
|
|
|
85
91
|
mode,
|
|
86
92
|
resolution: `${tw}x${th}`,
|
|
87
93
|
encoderUsed: videoEncoder,
|
|
88
|
-
hardwareAccelerated:
|
|
94
|
+
hardwareAccelerated: isHwUsed,
|
|
89
95
|
commandExecuted: res.command,
|
|
90
96
|
durationSeconds: res.durationSeconds,
|
|
91
97
|
};
|
package/dist/tools/silence.js
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
|
-
import { ensureFileExists, ensureOutputDir, runFfmpeg } from "../utils/exec.js";
|
|
2
|
+
import { ensureFileExists, ensureOutputDir, runCommand, runFfmpeg } from "../utils/exec.js";
|
|
3
3
|
import { probeMedia } from "./probe.js";
|
|
4
|
+
import { detectHwAccel } from "../utils/hwaccel.js";
|
|
4
5
|
export const trimSilenceSchema = z.object({
|
|
5
6
|
inputPath: z.string().describe("Path to the source audio or video file."),
|
|
6
7
|
outputPath: z.string().describe("Destination path for the trimmed file (e.g. './recordings/cleaned.mp4')."),
|
|
7
8
|
mode: z
|
|
8
9
|
.enum(["start_end", "all_pauses"])
|
|
9
10
|
.default("start_end")
|
|
10
|
-
.describe("Trim mode: 'start_end' removes silence at the start and end; 'all_pauses' removes silent pauses across
|
|
11
|
+
.describe("Trim mode: 'start_end' removes silence at the start and end (preserving perfect A/V sync on videos); 'all_pauses' removes silent pauses across audio files. Default: 'start_end'."),
|
|
11
12
|
thresholdDb: z
|
|
12
13
|
.number()
|
|
13
14
|
.min(-60)
|
|
@@ -21,36 +22,96 @@ export const trimSilenceSchema = z.object({
|
|
|
21
22
|
.default(0.5)
|
|
22
23
|
.describe("Minimum silence duration in seconds to trigger a cut. Default: 0.5."),
|
|
23
24
|
});
|
|
24
|
-
export async function trimSilence(args) {
|
|
25
|
+
export async function trimSilence(args, options) {
|
|
25
26
|
const input = await ensureFileExists(args.inputPath);
|
|
26
27
|
const output = await ensureOutputDir(args.outputPath);
|
|
27
28
|
const mode = args.mode ?? "start_end";
|
|
28
29
|
const thresholdDb = args.thresholdDb ?? -35;
|
|
29
30
|
const minDuration = args.minDurationSeconds ?? 0.5;
|
|
30
|
-
const originalProbe = await probeMedia(input);
|
|
31
|
+
const originalProbe = await probeMedia(input, { signal: options?.signal });
|
|
31
32
|
if (originalProbe.audioStreams.length === 0) {
|
|
32
33
|
throw new Error(`Cannot trim silence: input file '${input}' contains no audio streams.`);
|
|
33
34
|
}
|
|
34
35
|
const isVideo = originalProbe.videoStreams.length > 0;
|
|
35
|
-
|
|
36
|
-
if (mode === "
|
|
37
|
-
|
|
36
|
+
const originalDuration = originalProbe.format.durationSeconds;
|
|
37
|
+
if (isVideo && mode === "all_pauses") {
|
|
38
|
+
throw new Error("Trimming internal pauses ('all_pauses') on video files causes severe audio/video desync. For video files, please use mode: 'start_end', or extract the audio track first with extract_audio.");
|
|
38
39
|
}
|
|
39
|
-
|
|
40
|
-
|
|
40
|
+
let commandExecuted = "";
|
|
41
|
+
let durationSeconds = 0;
|
|
42
|
+
if (isVideo && mode === "start_end") {
|
|
43
|
+
// Detect silence intervals using silencedetect filter
|
|
44
|
+
const detectRes = await runCommand("ffmpeg", ["-i", input, "-af", `silencedetect=noise=${thresholdDb}dB:d=${minDuration}`, "-f", "null", "-"], { signal: options?.signal });
|
|
45
|
+
const log = `${detectRes.stdout}\n${detectRes.stderr}`;
|
|
46
|
+
const startEndRegex = /silence_end:\s*([\d.]+)/g;
|
|
47
|
+
const startSilenceRegex = /silence_start:\s*([\d.]+)/g;
|
|
48
|
+
let startTime = 0;
|
|
49
|
+
let endTime = originalDuration;
|
|
50
|
+
// Check if there was silence at the beginning (silence_start at 0)
|
|
51
|
+
const firstSilenceMatch = log.match(/silence_start:\s*([0-9.]+)/);
|
|
52
|
+
if (firstSilenceMatch && parseFloat(firstSilenceMatch[1]) < 0.2) {
|
|
53
|
+
const endMatch = log.match(/silence_end:\s*([0-9.]+)/);
|
|
54
|
+
if (endMatch) {
|
|
55
|
+
startTime = Math.min(originalDuration - 0.5, parseFloat(endMatch[1]));
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
// Check for silence at the end
|
|
59
|
+
const allSilenceStarts = [];
|
|
60
|
+
let m;
|
|
61
|
+
while ((m = startSilenceRegex.exec(log)) !== null) {
|
|
62
|
+
allSilenceStarts.push(parseFloat(m[1]));
|
|
63
|
+
}
|
|
64
|
+
if (allSilenceStarts.length > 0) {
|
|
65
|
+
const lastStart = allSilenceStarts[allSilenceStarts.length - 1];
|
|
66
|
+
// If the last silence starts near the end and doesn't end before original duration
|
|
67
|
+
if (lastStart > startTime && originalDuration - lastStart <= minDuration * 2 + 1) {
|
|
68
|
+
endTime = lastStart;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
const hwInfo = await detectHwAccel();
|
|
72
|
+
const ffmpegArgs = ["-ss", startTime.toFixed(3), "-i", input];
|
|
73
|
+
if (endTime < originalDuration) {
|
|
74
|
+
ffmpegArgs.push("-to", endTime.toFixed(3));
|
|
75
|
+
}
|
|
76
|
+
if (hwInfo.isAvailable) {
|
|
77
|
+
ffmpegArgs.push("-c:v", hwInfo.h264Encoder, "-b:v", "4M", "-c:a", "aac", "-b:a", "192k");
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
ffmpegArgs.push("-c:v", "libx264", "-crf", "22", "-preset", "fast", "-c:a", "aac", "-b:a", "192k");
|
|
81
|
+
}
|
|
82
|
+
ffmpegArgs.push("-movflags", "+faststart", "-y", output);
|
|
83
|
+
const res = await runFfmpeg(ffmpegArgs, {
|
|
84
|
+
signal: options?.signal,
|
|
85
|
+
onProgress: options?.onProgress,
|
|
86
|
+
expectedDurationSeconds: originalDuration,
|
|
87
|
+
});
|
|
88
|
+
commandExecuted = res.command;
|
|
89
|
+
durationSeconds = res.durationSeconds;
|
|
41
90
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
91
|
+
else {
|
|
92
|
+
// Audio file or fallback
|
|
93
|
+
let audioFilter = "";
|
|
94
|
+
if (mode === "start_end") {
|
|
95
|
+
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`;
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
audioFilter = `silenceremove=stop_periods=-1:stop_duration=${minDuration}:stop_threshold=${thresholdDb}dB`;
|
|
99
|
+
}
|
|
100
|
+
const ffmpegArgs = ["-i", input, "-af", audioFilter, "-y", output];
|
|
101
|
+
const res = await runFfmpeg(ffmpegArgs, {
|
|
102
|
+
signal: options?.signal,
|
|
103
|
+
onProgress: options?.onProgress,
|
|
104
|
+
expectedDurationSeconds: originalDuration,
|
|
105
|
+
});
|
|
106
|
+
commandExecuted = res.command;
|
|
107
|
+
durationSeconds = res.durationSeconds;
|
|
45
108
|
}
|
|
46
|
-
ffmpegArgs.push("-y", output);
|
|
47
|
-
const res = await runFfmpeg(ffmpegArgs);
|
|
48
109
|
let timeSaved = 0;
|
|
49
|
-
let newDuration =
|
|
110
|
+
let newDuration = originalDuration;
|
|
50
111
|
try {
|
|
51
|
-
const newProbe = await probeMedia(output);
|
|
112
|
+
const newProbe = await probeMedia(output, { signal: options?.signal });
|
|
52
113
|
newDuration = newProbe.format.durationSeconds;
|
|
53
|
-
timeSaved = Number(Math.max(0,
|
|
114
|
+
timeSaved = Number(Math.max(0, originalDuration - newDuration).toFixed(2));
|
|
54
115
|
}
|
|
55
116
|
catch {
|
|
56
117
|
// Optional
|
|
@@ -59,11 +120,11 @@ export async function trimSilence(args) {
|
|
|
59
120
|
success: true,
|
|
60
121
|
outputPath: output,
|
|
61
122
|
mode,
|
|
62
|
-
originalDurationSeconds:
|
|
123
|
+
originalDurationSeconds: originalDuration,
|
|
63
124
|
newDurationSeconds: newDuration,
|
|
64
125
|
timeSavedSeconds: timeSaved,
|
|
65
126
|
thresholdDb,
|
|
66
|
-
commandExecuted
|
|
67
|
-
durationSeconds
|
|
127
|
+
commandExecuted,
|
|
128
|
+
durationSeconds,
|
|
68
129
|
};
|
|
69
130
|
}
|
package/dist/tools/speed.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import { runFfmpeg, ensureFileExists, ensureOutputDir } from "../utils/exec.js";
|
|
3
|
+
import { detectHwAccel } from "../utils/hwaccel.js";
|
|
4
|
+
import { probeMedia } from "./probe.js";
|
|
3
5
|
export const changeSpeedSchema = z.object({
|
|
4
6
|
inputPath: z.string().describe("Path to the source video or audio file"),
|
|
5
7
|
outputPath: z.string().describe("Target output file path"),
|
|
@@ -20,10 +22,19 @@ function getAtempoFilter(speed) {
|
|
|
20
22
|
filters.push(`atempo=${s.toFixed(4)}`);
|
|
21
23
|
return filters.join(",");
|
|
22
24
|
}
|
|
23
|
-
export async function changeSpeed(params) {
|
|
25
|
+
export async function changeSpeed(params, options) {
|
|
24
26
|
const { inputPath, outputPath, speed, muteAudio = false } = params;
|
|
25
27
|
const input = await ensureFileExists(inputPath);
|
|
26
28
|
const output = await ensureOutputDir(outputPath);
|
|
29
|
+
let expectedDuration;
|
|
30
|
+
try {
|
|
31
|
+
const probe = await probeMedia(input, { signal: options?.signal });
|
|
32
|
+
if (probe.format.durationSeconds > 0) {
|
|
33
|
+
expectedDuration = probe.format.durationSeconds / speed;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
catch { }
|
|
37
|
+
const hwInfo = await detectHwAccel();
|
|
27
38
|
const videoFilter = `setpts=${(1 / speed).toFixed(6)}*PTS`;
|
|
28
39
|
const args = ["-i", input, "-filter:v", videoFilter];
|
|
29
40
|
if (muteAudio) {
|
|
@@ -33,8 +44,18 @@ export async function changeSpeed(params) {
|
|
|
33
44
|
const audioFilter = getAtempoFilter(speed);
|
|
34
45
|
args.push("-filter:a", audioFilter);
|
|
35
46
|
}
|
|
36
|
-
|
|
37
|
-
|
|
47
|
+
if (hwInfo.isAvailable) {
|
|
48
|
+
args.push("-c:v", hwInfo.h264Encoder, "-b:v", "4M");
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
args.push("-c:v", "libx264", "-crf", "22", "-preset", "fast");
|
|
52
|
+
}
|
|
53
|
+
args.push("-pix_fmt", "yuv420p", "-movflags", "+faststart", "-y", output);
|
|
54
|
+
const result = await runFfmpeg(args, {
|
|
55
|
+
signal: options?.signal,
|
|
56
|
+
onProgress: options?.onProgress,
|
|
57
|
+
expectedDurationSeconds: expectedDuration,
|
|
58
|
+
});
|
|
38
59
|
return {
|
|
39
60
|
success: true,
|
|
40
61
|
outputPath: output,
|
package/dist/tools/split.js
CHANGED
|
@@ -21,9 +21,9 @@ export const splitScreenSchema = z.object({
|
|
|
21
21
|
hardwareAcceleration: z
|
|
22
22
|
.boolean()
|
|
23
23
|
.optional()
|
|
24
|
-
.describe("Use
|
|
24
|
+
.describe("Use hardware-accelerated video encoding (VideoToolbox, NVENC, QSV, etc.) when available. Default: true."),
|
|
25
25
|
});
|
|
26
|
-
export async function createSplitScreen(params) {
|
|
26
|
+
export async function createSplitScreen(params, options) {
|
|
27
27
|
const { firstVideoPath, secondVideoPath, outputPath, layout = "vertical_9_16", audioSource = "mix", subtitlesPath, hardwareAcceleration = true, } = params;
|
|
28
28
|
const firstInput = await ensureFileExists(firstVideoPath);
|
|
29
29
|
const secondInput = await ensureFileExists(secondVideoPath);
|
|
@@ -33,14 +33,16 @@ export async function createSplitScreen(params) {
|
|
|
33
33
|
subInput = await ensureFileExists(subtitlesPath);
|
|
34
34
|
}
|
|
35
35
|
const [firstProbe, secondProbe] = await Promise.all([
|
|
36
|
-
probeMedia(firstInput),
|
|
37
|
-
probeMedia(secondInput),
|
|
36
|
+
probeMedia(firstInput, { signal: options?.signal }),
|
|
37
|
+
probeMedia(secondInput, { signal: options?.signal }),
|
|
38
38
|
]);
|
|
39
39
|
const firstHasAudio = firstProbe.audioStreams.length > 0;
|
|
40
40
|
const secondHasAudio = secondProbe.audioStreams.length > 0;
|
|
41
|
+
const estimatedDuration = Math.max(firstProbe.format.durationSeconds, secondProbe.format.durationSeconds);
|
|
41
42
|
const hwInfo = await detectHwAccel();
|
|
42
43
|
const useHw = hardwareAcceleration && hwInfo.isAvailable;
|
|
43
|
-
const
|
|
44
|
+
const isHwUsed = useHw;
|
|
45
|
+
const videoEncoder = isHwUsed ? hwInfo.h264Encoder : "libx264";
|
|
44
46
|
// Compute dimensions per tile
|
|
45
47
|
let filter = "";
|
|
46
48
|
let totalResolution = "1080x1920";
|
|
@@ -95,11 +97,11 @@ export async function createSplitScreen(params) {
|
|
|
95
97
|
}
|
|
96
98
|
if (hasSubtitles) {
|
|
97
99
|
args.push("-map", "2:s");
|
|
98
|
-
const isMp4 = output.endsWith(".mp4") || output.endsWith(".mov");
|
|
100
|
+
const isMp4 = output.toLowerCase().endsWith(".mp4") || output.toLowerCase().endsWith(".mov");
|
|
99
101
|
args.push("-c:s", isMp4 ? "mov_text" : "srt");
|
|
100
102
|
}
|
|
101
103
|
args.push("-c:v", videoEncoder);
|
|
102
|
-
if (
|
|
104
|
+
if (isHwUsed) {
|
|
103
105
|
args.push("-b:v", "4M", "-pix_fmt", "yuv420p");
|
|
104
106
|
}
|
|
105
107
|
else {
|
|
@@ -109,7 +111,11 @@ export async function createSplitScreen(params) {
|
|
|
109
111
|
args.push("-c:a", "aac", "-b:a", "192k");
|
|
110
112
|
}
|
|
111
113
|
args.push("-movflags", "+faststart", "-y", output);
|
|
112
|
-
const res = await runFfmpeg(args
|
|
114
|
+
const res = await runFfmpeg(args, {
|
|
115
|
+
signal: options?.signal,
|
|
116
|
+
onProgress: options?.onProgress,
|
|
117
|
+
expectedDurationSeconds: estimatedDuration,
|
|
118
|
+
});
|
|
113
119
|
return {
|
|
114
120
|
success: true,
|
|
115
121
|
outputPath: output,
|
|
@@ -118,7 +124,7 @@ export async function createSplitScreen(params) {
|
|
|
118
124
|
audioSource,
|
|
119
125
|
subtitlesEmbedded: hasSubtitles,
|
|
120
126
|
encoderUsed: videoEncoder,
|
|
121
|
-
hardwareAccelerated:
|
|
127
|
+
hardwareAccelerated: isHwUsed,
|
|
122
128
|
commandExecuted: res.command,
|
|
123
129
|
durationSeconds: res.durationSeconds,
|
|
124
130
|
};
|
package/dist/tools/subtitles.js
CHANGED
|
@@ -7,7 +7,7 @@ export const addSubtitlesSchema = z.object({
|
|
|
7
7
|
language: z.string().optional().default("eng").describe("ISO 639-2 three-letter language code (e.g. 'eng', 'spa', 'fra', 'deu')"),
|
|
8
8
|
title: z.string().optional().default("English").describe("Subtitle track label displayed in video players"),
|
|
9
9
|
});
|
|
10
|
-
export async function addSubtitles(params) {
|
|
10
|
+
export async function addSubtitles(params, options) {
|
|
11
11
|
const { videoPath, subtitlesPath, outputPath, language = "eng", title = "English" } = params;
|
|
12
12
|
const video = await ensureFileExists(videoPath);
|
|
13
13
|
const subs = await ensureFileExists(subtitlesPath);
|
|
@@ -24,7 +24,10 @@ export async function addSubtitles(params) {
|
|
|
24
24
|
"-metadata:s:s:0", `title=${title}`,
|
|
25
25
|
"-y", output
|
|
26
26
|
];
|
|
27
|
-
const result = await runFfmpeg(args
|
|
27
|
+
const result = await runFfmpeg(args, {
|
|
28
|
+
signal: options?.signal,
|
|
29
|
+
onProgress: options?.onProgress,
|
|
30
|
+
});
|
|
28
31
|
return {
|
|
29
32
|
success: true,
|
|
30
33
|
outputPath: output,
|
package/dist/tools/thumbnail.js
CHANGED
|
@@ -1,13 +1,24 @@
|
|
|
1
1
|
import fs from "node:fs/promises";
|
|
2
|
-
import {
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { ensureFileExists, ensureOutputDir, runFfmpeg } from "../utils/exec.js";
|
|
3
4
|
import { probeMedia } from "./probe.js";
|
|
4
|
-
export
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
export const extractThumbnailSchema = z.object({
|
|
6
|
+
inputPath: z.string().describe("Path to the source video file."),
|
|
7
|
+
outputPath: z.string().describe("Destination image path ending in .png or .jpg (e.g. './thumb.png')."),
|
|
8
|
+
timestamp: z
|
|
9
|
+
.string()
|
|
10
|
+
.optional()
|
|
11
|
+
.describe("Timestamp to capture, such as '00:00:05', '10', or 'midpoint' for the middle frame. Default: '00:00:01'."),
|
|
12
|
+
resolution: z.string().optional().describe("Optional scaled size formatted as 'WIDTHxHEIGHT', such as '1280x720'. Defaults to source resolution."),
|
|
13
|
+
includeBase64: z.boolean().optional().describe("Return base64 image data in response for vision models. Default: true."),
|
|
14
|
+
});
|
|
15
|
+
export async function extractThumbnail(params, options) {
|
|
16
|
+
const input = await ensureFileExists(params.inputPath);
|
|
17
|
+
const output = await ensureOutputDir(params.outputPath);
|
|
18
|
+
let timestamp = params.timestamp || "00:00:01";
|
|
8
19
|
if (timestamp === "midpoint") {
|
|
9
20
|
try {
|
|
10
|
-
const info = await probeMedia(input);
|
|
21
|
+
const info = await probeMedia(input, { signal: options?.signal });
|
|
11
22
|
const midpointSeconds = Math.max(0, Math.floor(info.format.durationSeconds / 2));
|
|
12
23
|
timestamp = String(midpointSeconds);
|
|
13
24
|
}
|
|
@@ -16,19 +27,18 @@ export async function extractThumbnail(options) {
|
|
|
16
27
|
}
|
|
17
28
|
}
|
|
18
29
|
const args = [
|
|
19
|
-
"-y",
|
|
20
30
|
"-ss", timestamp,
|
|
21
31
|
"-i", input,
|
|
22
32
|
"-vframes", "1",
|
|
23
33
|
];
|
|
24
|
-
if (
|
|
25
|
-
args.push("-vf", `scale=${
|
|
34
|
+
if (params.resolution) {
|
|
35
|
+
args.push("-vf", `scale=${params.resolution.replace("x", ":")}`);
|
|
26
36
|
}
|
|
27
|
-
args.push(output);
|
|
28
|
-
await
|
|
37
|
+
args.push("-y", output);
|
|
38
|
+
const res = await runFfmpeg(args, { signal: options?.signal });
|
|
29
39
|
let base64;
|
|
30
40
|
let mimeType;
|
|
31
|
-
const shouldInclude =
|
|
41
|
+
const shouldInclude = params.includeBase64 ?? true;
|
|
32
42
|
if (shouldInclude) {
|
|
33
43
|
try {
|
|
34
44
|
const buffer = await fs.readFile(output);
|
|
@@ -44,7 +54,8 @@ export async function extractThumbnail(options) {
|
|
|
44
54
|
return {
|
|
45
55
|
outputPath: output,
|
|
46
56
|
timestampUsed: timestamp,
|
|
47
|
-
commandExecuted:
|
|
57
|
+
commandExecuted: res.command,
|
|
58
|
+
durationSeconds: res.durationSeconds,
|
|
48
59
|
base64,
|
|
49
60
|
mimeType,
|
|
50
61
|
};
|
package/dist/tools/trim.js
CHANGED
|
@@ -1,29 +1,51 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { ensureFileExists, ensureOutputDir, runFfmpeg } from "../utils/exec.js";
|
|
3
|
+
import { detectHwAccel } from "../utils/hwaccel.js";
|
|
4
|
+
export const trimMediaSchema = z.object({
|
|
5
|
+
inputPath: z.string().describe("Path to the source media file to cut."),
|
|
6
|
+
outputPath: z.string().describe("Destination path for the trimmed output file (e.g. './clip.mp4')."),
|
|
7
|
+
startTime: z.string().describe("Start timestamp formatted as 'HH:MM:SS' (e.g. '00:01:30') or seconds (e.g. '90')."),
|
|
8
|
+
endTime: z.string().optional().describe("Optional end timestamp formatted as 'HH:MM:SS' or seconds. Do not combine with duration."),
|
|
9
|
+
duration: z.string().optional().describe("Optional duration in seconds from startTime (e.g. '30'). If neither endTime nor duration is set, trims to end of file."),
|
|
10
|
+
accurateReencode: z
|
|
11
|
+
.boolean()
|
|
12
|
+
.optional()
|
|
13
|
+
.describe("Re-encode video for cuts between keyframes. Default: false (uses fast stream copy)."),
|
|
14
|
+
});
|
|
15
|
+
export async function trimMedia(params, options) {
|
|
16
|
+
const input = await ensureFileExists(params.inputPath);
|
|
17
|
+
const output = await ensureOutputDir(params.outputPath);
|
|
5
18
|
const args = ["-y"];
|
|
6
19
|
// Fast seek before -i
|
|
7
|
-
args.push("-ss",
|
|
20
|
+
args.push("-ss", params.startTime);
|
|
8
21
|
args.push("-i", input);
|
|
9
|
-
if (
|
|
10
|
-
args.push("-to",
|
|
22
|
+
if (params.endTime) {
|
|
23
|
+
args.push("-to", params.endTime);
|
|
11
24
|
}
|
|
12
|
-
else if (
|
|
13
|
-
args.push("-t",
|
|
25
|
+
else if (params.duration) {
|
|
26
|
+
args.push("-t", params.duration);
|
|
14
27
|
}
|
|
15
|
-
if (
|
|
16
|
-
|
|
17
|
-
|
|
28
|
+
if (params.accurateReencode) {
|
|
29
|
+
const hwInfo = await detectHwAccel();
|
|
30
|
+
if (hwInfo.isAvailable) {
|
|
31
|
+
args.push("-c:v", hwInfo.h264Encoder, "-b:v", "4M", "-c:a", "aac");
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
args.push("-c:v", "libx264", "-crf", "22", "-preset", "fast", "-c:a", "aac");
|
|
35
|
+
}
|
|
18
36
|
}
|
|
19
37
|
else {
|
|
20
38
|
// Lossless instantaneous stream copy
|
|
21
39
|
args.push("-c", "copy");
|
|
22
40
|
}
|
|
23
41
|
args.push(output);
|
|
24
|
-
await
|
|
42
|
+
const res = await runFfmpeg(args, {
|
|
43
|
+
signal: options?.signal,
|
|
44
|
+
onProgress: options?.onProgress,
|
|
45
|
+
});
|
|
25
46
|
return {
|
|
26
47
|
outputPath: output,
|
|
27
|
-
commandExecuted:
|
|
48
|
+
commandExecuted: res.command,
|
|
49
|
+
durationSeconds: res.durationSeconds,
|
|
28
50
|
};
|
|
29
51
|
}
|
package/dist/tools/waveform.js
CHANGED
|
@@ -8,9 +8,9 @@ export const generateWaveformSchema = z.object({
|
|
|
8
8
|
height: z.number().int().positive().optional().default(280).describe("Image height in pixels. Default: 280."),
|
|
9
9
|
color: z.string().optional().default("0x6366f1").describe("Waveform color in hex or name (such as '0x6366f1', 'white', or 'black'). Default: '0x6366f1'."),
|
|
10
10
|
scale: z.enum(["lin", "log", "sqrt"]).optional().default("lin").describe("Amplitude scale: 'lin', 'log', or 'sqrt'. Default: 'lin'."),
|
|
11
|
-
includeBase64: z.boolean().optional().describe("Return base64 image data in the response for vision models. Default: true."),
|
|
11
|
+
includeBase64: z.boolean().optional().default(true).describe("Return base64 image data in the response for vision models. Default: true."),
|
|
12
12
|
});
|
|
13
|
-
export async function generateWaveform(params) {
|
|
13
|
+
export async function generateWaveform(params, options) {
|
|
14
14
|
const { inputPath, outputPath, width = 1200, height = 280, color = "0x6366f1", scale = "lin", includeBase64 = true } = params;
|
|
15
15
|
const input = await ensureFileExists(inputPath);
|
|
16
16
|
const output = await ensureOutputDir(outputPath);
|
|
@@ -21,7 +21,7 @@ export async function generateWaveform(params) {
|
|
|
21
21
|
"-vframes", "1",
|
|
22
22
|
"-y", output
|
|
23
23
|
];
|
|
24
|
-
const result = await runFfmpeg(args);
|
|
24
|
+
const result = await runFfmpeg(args, { signal: options?.signal });
|
|
25
25
|
let base64;
|
|
26
26
|
if (includeBase64) {
|
|
27
27
|
try {
|