@tmhs/screencast-mcp 0.9.1 → 0.28.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 +59 -12
- package/dist/index.js +67 -0
- package/dist/index.js.map +1 -1
- package/dist/prompts.d.ts +10 -0
- package/dist/prompts.d.ts.map +1 -0
- package/dist/prompts.js +72 -0
- package/dist/prompts.js.map +1 -0
- package/dist/resources.d.ts +3 -0
- package/dist/resources.d.ts.map +1 -0
- package/dist/resources.js +71 -0
- package/dist/resources.js.map +1 -0
- package/dist/tools/assembleHighlights.d.ts.map +1 -1
- package/dist/tools/assembleHighlights.js +4 -3
- package/dist/tools/assembleHighlights.js.map +1 -1
- package/dist/tools/burnSubtitles.d.ts +3 -0
- package/dist/tools/burnSubtitles.d.ts.map +1 -0
- package/dist/tools/burnSubtitles.js +48 -0
- package/dist/tools/burnSubtitles.js.map +1 -0
- package/dist/tools/callout.d.ts +3 -0
- package/dist/tools/callout.d.ts.map +1 -0
- package/dist/tools/callout.js +106 -0
- package/dist/tools/callout.js.map +1 -0
- package/dist/tools/clip.d.ts.map +1 -1
- package/dist/tools/clip.js +4 -3
- package/dist/tools/clip.js.map +1 -1
- package/dist/tools/compress.d.ts.map +1 -1
- package/dist/tools/compress.js +4 -3
- package/dist/tools/compress.js.map +1 -1
- package/dist/tools/concat.d.ts.map +1 -1
- package/dist/tools/concat.js +6 -3
- package/dist/tools/concat.js.map +1 -1
- package/dist/tools/contactSheet.d.ts +3 -0
- package/dist/tools/contactSheet.d.ts.map +1 -0
- package/dist/tools/contactSheet.js +128 -0
- package/dist/tools/contactSheet.js.map +1 -0
- package/dist/tools/convert.d.ts.map +1 -1
- package/dist/tools/convert.js +4 -3
- package/dist/tools/convert.js.map +1 -1
- package/dist/tools/crop.d.ts.map +1 -1
- package/dist/tools/crop.js +5 -4
- package/dist/tools/crop.js.map +1 -1
- package/dist/tools/cutIdle.d.ts +3 -0
- package/dist/tools/cutIdle.d.ts.map +1 -0
- package/dist/tools/cutIdle.js +77 -0
- package/dist/tools/cutIdle.js.map +1 -0
- package/dist/tools/denoise.d.ts +3 -0
- package/dist/tools/denoise.d.ts.map +1 -0
- package/dist/tools/denoise.js +53 -0
- package/dist/tools/denoise.js.map +1 -0
- package/dist/tools/exportGif.d.ts +3 -0
- package/dist/tools/exportGif.d.ts.map +1 -0
- package/dist/tools/exportGif.js +100 -0
- package/dist/tools/exportGif.js.map +1 -0
- package/dist/tools/exportPreset.d.ts.map +1 -1
- package/dist/tools/exportPreset.js +4 -3
- package/dist/tools/exportPreset.js.map +1 -1
- package/dist/tools/extractAudio.d.ts.map +1 -1
- package/dist/tools/extractAudio.js +4 -3
- package/dist/tools/extractAudio.js.map +1 -1
- package/dist/tools/findText.d.ts +3 -0
- package/dist/tools/findText.d.ts.map +1 -0
- package/dist/tools/findText.js +120 -0
- package/dist/tools/findText.js.map +1 -0
- package/dist/tools/musicBed.d.ts.map +1 -1
- package/dist/tools/musicBed.js +4 -3
- package/dist/tools/musicBed.js.map +1 -1
- package/dist/tools/normalizeAudio.d.ts +3 -0
- package/dist/tools/normalizeAudio.d.ts.map +1 -0
- package/dist/tools/normalizeAudio.js +55 -0
- package/dist/tools/normalizeAudio.js.map +1 -0
- package/dist/tools/overlay.d.ts.map +1 -1
- package/dist/tools/overlay.js +4 -3
- package/dist/tools/overlay.js.map +1 -1
- package/dist/tools/readEvents.d.ts +6 -0
- package/dist/tools/readEvents.d.ts.map +1 -0
- package/dist/tools/readEvents.js +79 -0
- package/dist/tools/readEvents.js.map +1 -0
- package/dist/tools/redactRegion.d.ts.map +1 -1
- package/dist/tools/redactRegion.js +5 -4
- package/dist/tools/redactRegion.js.map +1 -1
- package/dist/tools/reframe.d.ts.map +1 -1
- package/dist/tools/reframe.js +4 -3
- package/dist/tools/reframe.js.map +1 -1
- package/dist/tools/sampleFrames.d.ts.map +1 -1
- package/dist/tools/sampleFrames.js +125 -12
- package/dist/tools/sampleFrames.js.map +1 -1
- package/dist/tools/scale.d.ts.map +1 -1
- package/dist/tools/scale.js +4 -3
- package/dist/tools/scale.js.map +1 -1
- package/dist/tools/screenDiff.d.ts +3 -0
- package/dist/tools/screenDiff.d.ts.map +1 -0
- package/dist/tools/screenDiff.js +72 -0
- package/dist/tools/screenDiff.js.map +1 -0
- package/dist/tools/screenshot.d.ts.map +1 -1
- package/dist/tools/screenshot.js +39 -7
- package/dist/tools/screenshot.js.map +1 -1
- package/dist/tools/speed.d.ts.map +1 -1
- package/dist/tools/speed.js +5 -4
- package/dist/tools/speed.js.map +1 -1
- package/dist/tools/startRecording.d.ts.map +1 -1
- package/dist/tools/startRecording.js +100 -9
- package/dist/tools/startRecording.js.map +1 -1
- package/dist/tools/stopRecording.d.ts.map +1 -1
- package/dist/tools/stopRecording.js +12 -66
- package/dist/tools/stopRecording.js.map +1 -1
- package/dist/tools/titleCard.d.ts.map +1 -1
- package/dist/tools/titleCard.js +5 -4
- package/dist/tools/titleCard.js.map +1 -1
- package/dist/tools/transcribe.d.ts +3 -0
- package/dist/tools/transcribe.d.ts.map +1 -0
- package/dist/tools/transcribe.js +59 -0
- package/dist/tools/transcribe.js.map +1 -0
- package/dist/tools/trim.d.ts.map +1 -1
- package/dist/tools/trim.js +8 -3
- package/dist/tools/trim.js.map +1 -1
- package/dist/tools/waitForChange.d.ts +3 -0
- package/dist/tools/waitForChange.d.ts.map +1 -0
- package/dist/tools/waitForChange.js +127 -0
- package/dist/tools/waitForChange.js.map +1 -0
- package/dist/tools/xfadeTransition.d.ts.map +1 -1
- package/dist/tools/xfadeTransition.js +4 -3
- package/dist/tools/xfadeTransition.js.map +1 -1
- package/dist/tools/zoom.d.ts +3 -0
- package/dist/tools/zoom.d.ts.map +1 -0
- package/dist/tools/zoom.js +94 -0
- package/dist/tools/zoom.js.map +1 -0
- package/dist/utils/anim.d.ts +33 -0
- package/dist/utils/anim.d.ts.map +1 -0
- package/dist/utils/anim.js +74 -0
- package/dist/utils/anim.js.map +1 -0
- package/dist/utils/audio.d.ts +49 -0
- package/dist/utils/audio.d.ts.map +1 -0
- package/dist/utils/audio.js +128 -0
- package/dist/utils/audio.js.map +1 -0
- package/dist/utils/callout.d.ts +63 -0
- package/dist/utils/callout.d.ts.map +1 -0
- package/dist/utils/callout.js +95 -0
- package/dist/utils/callout.js.map +1 -0
- package/dist/utils/captions.d.ts +26 -0
- package/dist/utils/captions.d.ts.map +1 -0
- package/dist/utils/captions.js +105 -0
- package/dist/utils/captions.js.map +1 -0
- package/dist/utils/diff.d.ts +66 -0
- package/dist/utils/diff.d.ts.map +1 -0
- package/dist/utils/diff.js +176 -0
- package/dist/utils/diff.js.map +1 -0
- package/dist/utils/errors.d.ts +17 -9
- package/dist/utils/errors.d.ts.map +1 -1
- package/dist/utils/errors.js +7 -6
- package/dist/utils/errors.js.map +1 -1
- package/dist/utils/events.d.ts +109 -0
- package/dist/utils/events.d.ts.map +1 -0
- package/dist/utils/events.js +289 -0
- package/dist/utils/events.js.map +1 -0
- package/dist/utils/ffmpeg.d.ts +14 -1
- package/dist/utils/ffmpeg.d.ts.map +1 -1
- package/dist/utils/ffmpeg.js +105 -5
- package/dist/utils/ffmpeg.js.map +1 -1
- package/dist/utils/idle.d.ts +23 -0
- package/dist/utils/idle.d.ts.map +1 -0
- package/dist/utils/idle.js +96 -0
- package/dist/utils/idle.js.map +1 -0
- package/dist/utils/images.d.ts +21 -0
- package/dist/utils/images.d.ts.map +1 -0
- package/dist/utils/images.js +55 -0
- package/dist/utils/images.js.map +1 -0
- package/dist/utils/jobs.d.ts +44 -0
- package/dist/utils/jobs.d.ts.map +1 -0
- package/dist/utils/jobs.js +135 -0
- package/dist/utils/jobs.js.map +1 -0
- package/dist/utils/lifecycle.d.ts +29 -0
- package/dist/utils/lifecycle.d.ts.map +1 -0
- package/dist/utils/lifecycle.js +116 -0
- package/dist/utils/lifecycle.js.map +1 -0
- package/dist/utils/media.d.ts +35 -2
- package/dist/utils/media.d.ts.map +1 -1
- package/dist/utils/media.js +75 -3
- package/dist/utils/media.js.map +1 -1
- package/dist/utils/monitors.d.ts +18 -2
- package/dist/utils/monitors.d.ts.map +1 -1
- package/dist/utils/monitors.js +50 -12
- package/dist/utils/monitors.js.map +1 -1
- package/dist/utils/ocr.d.ts +44 -0
- package/dist/utils/ocr.d.ts.map +1 -0
- package/dist/utils/ocr.js +215 -0
- package/dist/utils/ocr.js.map +1 -0
- package/dist/utils/produce.d.ts +1 -0
- package/dist/utils/produce.d.ts.map +1 -1
- package/dist/utils/produce.js +3 -2
- package/dist/utils/produce.js.map +1 -1
- package/dist/utils/resolveTarget.d.ts +5 -1
- package/dist/utils/resolveTarget.d.ts.map +1 -1
- package/dist/utils/resolveTarget.js +4 -4
- package/dist/utils/resolveTarget.js.map +1 -1
- package/dist/utils/sessions.d.ts +47 -0
- package/dist/utils/sessions.d.ts.map +1 -1
- package/dist/utils/sessions.js +170 -15
- package/dist/utils/sessions.js.map +1 -1
- package/dist/utils/sheet.d.ts +26 -0
- package/dist/utils/sheet.d.ts.map +1 -0
- package/dist/utils/sheet.js +77 -0
- package/dist/utils/sheet.js.map +1 -0
- package/dist/utils/still.d.ts +4 -0
- package/dist/utils/still.d.ts.map +1 -0
- package/dist/utils/still.js +28 -0
- package/dist/utils/still.js.map +1 -0
- package/dist/utils/targets.d.ts +27 -0
- package/dist/utils/targets.d.ts.map +1 -1
- package/dist/utils/targets.js +64 -1
- package/dist/utils/targets.js.map +1 -1
- package/dist/utils/windows.d.ts +25 -6
- package/dist/utils/windows.d.ts.map +1 -1
- package/dist/utils/windows.js +59 -20
- package/dist/utils/windows.js.map +1 -1
- package/dist/utils/zoom.d.ts +60 -0
- package/dist/utils/zoom.d.ts.map +1 -0
- package/dist/utils/zoom.js +167 -0
- package/dist/utils/zoom.js.map +1 -0
- package/package.json +6 -5
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { type ImageContent } from "./errors.js";
|
|
2
|
+
/** Default downscale width for inline previews. */
|
|
3
|
+
export declare const DEFAULT_MAX_IMAGE_WIDTH = 1280;
|
|
4
|
+
/** Default number of frames embedded in one tool result. */
|
|
5
|
+
export declare const DEFAULT_MAX_INLINE_IMAGES = 8;
|
|
6
|
+
/** Hard ceiling on embedded frames, whatever the caller asks for. */
|
|
7
|
+
export declare const MAX_INLINE_IMAGES = 20;
|
|
8
|
+
/** Build args that turn one image or video frame into a downscaled JPEG
|
|
9
|
+
* preview. The width cap only ever downscales (min of the source width and
|
|
10
|
+
* maxWidth); -2 keeps the aspect ratio. */
|
|
11
|
+
export declare function buildPreviewArgs(input: string, output: string, maxWidth: number): string[];
|
|
12
|
+
/** Clamp a caller-supplied inline-image count to [0, MAX_INLINE_IMAGES]. */
|
|
13
|
+
export declare function clampInlineCount(requested: number | undefined): number;
|
|
14
|
+
export interface InlineImages {
|
|
15
|
+
images: ImageContent[];
|
|
16
|
+
/** Frames that were not embedded because of the count cap. */
|
|
17
|
+
omitted: number;
|
|
18
|
+
}
|
|
19
|
+
/** Encode up to `maxCount` image files as downscaled JPEG image blocks. */
|
|
20
|
+
export declare function encodeInlineImages(paths: string[], maxWidth?: number, maxCount?: number): Promise<InlineImages>;
|
|
21
|
+
//# sourceMappingURL=images.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"images.d.ts","sourceRoot":"","sources":["../../src/utils/images.ts"],"names":[],"mappings":"AASA,OAAO,EAAmB,KAAK,YAAY,EAAE,MAAM,aAAa,CAAC;AAIjE,mDAAmD;AACnD,eAAO,MAAM,uBAAuB,OAAO,CAAC;AAC5C,4DAA4D;AAC5D,eAAO,MAAM,yBAAyB,IAAI,CAAC;AAC3C,qEAAqE;AACrE,eAAO,MAAM,iBAAiB,KAAK,CAAC;AAEpC;;2CAE2C;AAC3C,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,EAAE,CAW1F;AAED,4EAA4E;AAC5E,wBAAgB,gBAAgB,CAAC,SAAS,EAAE,MAAM,GAAG,SAAS,GAAG,MAAM,CAGtE;AAED,MAAM,WAAW,YAAY;IAC3B,MAAM,EAAE,YAAY,EAAE,CAAC;IACvB,8DAA8D;IAC9D,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,2EAA2E;AAC3E,wBAAsB,kBAAkB,CACtC,KAAK,EAAE,MAAM,EAAE,EACf,QAAQ,GAAE,MAAgC,EAC1C,QAAQ,GAAE,MAAkC,GAC3C,OAAO,CAAC,YAAY,CAAC,CAavB"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Inline image results (#79).
|
|
3
|
+
*
|
|
4
|
+
* Tools that produce frames can return them as MCP `image` content blocks so a
|
|
5
|
+
* client shows them to the model directly, with no filesystem tool needed.
|
|
6
|
+
* Payloads stay bounded: every frame is downscaled to a max width and
|
|
7
|
+
* re-encoded as JPEG, and only the first N frames are embedded.
|
|
8
|
+
*/
|
|
9
|
+
import { readFileSync, rmSync } from "node:fs";
|
|
10
|
+
import { ScreencastError } from "./errors.js";
|
|
11
|
+
import { runFfmpeg } from "./ffmpeg.js";
|
|
12
|
+
import { tempPath } from "./paths.js";
|
|
13
|
+
/** Default downscale width for inline previews. */
|
|
14
|
+
export const DEFAULT_MAX_IMAGE_WIDTH = 1280;
|
|
15
|
+
/** Default number of frames embedded in one tool result. */
|
|
16
|
+
export const DEFAULT_MAX_INLINE_IMAGES = 8;
|
|
17
|
+
/** Hard ceiling on embedded frames, whatever the caller asks for. */
|
|
18
|
+
export const MAX_INLINE_IMAGES = 20;
|
|
19
|
+
/** Build args that turn one image or video frame into a downscaled JPEG
|
|
20
|
+
* preview. The width cap only ever downscales (min of the source width and
|
|
21
|
+
* maxWidth); -2 keeps the aspect ratio. */
|
|
22
|
+
export function buildPreviewArgs(input, output, maxWidth) {
|
|
23
|
+
if (!Number.isInteger(maxWidth) || maxWidth < 16) {
|
|
24
|
+
throw new ScreencastError("maxImageWidth must be an integer of at least 16.");
|
|
25
|
+
}
|
|
26
|
+
return [
|
|
27
|
+
"-y", "-v", "error", "-i", input,
|
|
28
|
+
"-frames:v", "1",
|
|
29
|
+
"-vf", `scale='min(${maxWidth},iw)':-2`,
|
|
30
|
+
"-q:v", "4",
|
|
31
|
+
output,
|
|
32
|
+
];
|
|
33
|
+
}
|
|
34
|
+
/** Clamp a caller-supplied inline-image count to [0, MAX_INLINE_IMAGES]. */
|
|
35
|
+
export function clampInlineCount(requested) {
|
|
36
|
+
const n = requested ?? DEFAULT_MAX_INLINE_IMAGES;
|
|
37
|
+
return Math.max(0, Math.min(MAX_INLINE_IMAGES, Math.floor(n)));
|
|
38
|
+
}
|
|
39
|
+
/** Encode up to `maxCount` image files as downscaled JPEG image blocks. */
|
|
40
|
+
export async function encodeInlineImages(paths, maxWidth = DEFAULT_MAX_IMAGE_WIDTH, maxCount = DEFAULT_MAX_INLINE_IMAGES) {
|
|
41
|
+
const chosen = paths.slice(0, maxCount);
|
|
42
|
+
const images = [];
|
|
43
|
+
for (const path of chosen) {
|
|
44
|
+
const tmp = tempPath(".jpg");
|
|
45
|
+
try {
|
|
46
|
+
await runFfmpeg(buildPreviewArgs(path, tmp, maxWidth), 30_000);
|
|
47
|
+
images.push({ type: "image", data: readFileSync(tmp).toString("base64"), mimeType: "image/jpeg" });
|
|
48
|
+
}
|
|
49
|
+
finally {
|
|
50
|
+
rmSync(tmp, { force: true });
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return { images, omitted: paths.length - chosen.length };
|
|
54
|
+
}
|
|
55
|
+
//# sourceMappingURL=images.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"images.js","sourceRoot":"","sources":["../../src/utils/images.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAC/C,OAAO,EAAE,eAAe,EAAqB,MAAM,aAAa,CAAC;AACjE,OAAO,EA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EAAE,CAAC;QAC1B,MAAM,GAAG,GAAG,QAAQ,CAAC,MAAM,CAAC,CAAC;QAC7B,IAAI,CAAC;YACH,MAAM,SAAS,CAAC,gBAAgB,CAAC,IAAI,EAAE,GAAG,EAAE,QAAQ,CAAC,EAAE,MAAM,CAAC,CAAC;YAC/D,MAAM,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,YAAY,CAAC,GAAG,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE,QAAQ,EAAE,YAAY,EAAE,CAAC,CAAC;QACrG,CAAC;gBAAS,CAAC;YACT,MAAM,CAAC,GAAG,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;QAC/B,CAAC;IACH,CAAC;IACD,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,KAAK,CAAC,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC;AAC3D,CAAC"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { RequestHandlerExtra } from "@modelcontextprotocol/sdk/shared/protocol.js";
|
|
2
|
+
import type { ServerNotification, ServerRequest } from "@modelcontextprotocol/sdk/types.js";
|
|
3
|
+
/** The `extra` argument the SDK passes to every tool callback. */
|
|
4
|
+
export type ToolExtra = RequestHandlerExtra<ServerRequest, ServerNotification>;
|
|
5
|
+
/** Env var that overrides every edit job's timeout, in milliseconds. */
|
|
6
|
+
export declare const JOB_TIMEOUT_ENV = "SCREENCAST_JOB_TIMEOUT_MS";
|
|
7
|
+
/** Floor for a duration-scaled timeout. */
|
|
8
|
+
export declare const BASE_TIMEOUT_MS: number;
|
|
9
|
+
/** Timeout when the media's duration could not be probed. */
|
|
10
|
+
export declare const UNKNOWN_DURATION_TIMEOUT_MS: number;
|
|
11
|
+
/** Allow this many seconds of wall-clock time per second of media. A slow
|
|
12
|
+
* preset on a laptop runs around real time; 4x leaves ample headroom. */
|
|
13
|
+
export declare const SECONDS_PER_MEDIA_SECOND = 4;
|
|
14
|
+
/** Timeout for a job over `durationSec` of media: max(base, duration * 4x),
|
|
15
|
+
* or the env override when it is a positive integer. */
|
|
16
|
+
export declare function jobTimeoutMs(durationSec: number | null | undefined, env?: NodeJS.ProcessEnv): number;
|
|
17
|
+
/** `dir/name.partial.ext`: keeps the extension so ffmpeg picks the same muxer. */
|
|
18
|
+
export declare function partialPath(output: string): string;
|
|
19
|
+
/** Point a builder's args at `partial` instead of `output`. Builders put the
|
|
20
|
+
* output path last; replacing the last occurrence leaves an identical input
|
|
21
|
+
* path (rare, but legal with overwrite) untouched. */
|
|
22
|
+
export declare function retargetOutput(args: string[], output: string, partial: string): string[];
|
|
23
|
+
/** Feed `-progress pipe:1` output in chunks; returns the latest media time in
|
|
24
|
+
* seconds seen so far. Lines can split across chunks, so it buffers. */
|
|
25
|
+
export declare function createProgressParser(): (chunk: string) => number | null;
|
|
26
|
+
/** A progress callback that forwards to the client as MCP progress
|
|
27
|
+
* notifications, or undefined when the request asked for none. Updates are
|
|
28
|
+
* throttled to whole-percent steps. */
|
|
29
|
+
export declare function progressReporter(extra: ToolExtra | undefined): ((fraction: number) => void) | undefined;
|
|
30
|
+
export interface EditJob {
|
|
31
|
+
/** The final output path (already validated by resolveOutput). */
|
|
32
|
+
output: string;
|
|
33
|
+
/** Duration of the media the job writes, for timeout scaling and progress. */
|
|
34
|
+
durationSec?: number | null;
|
|
35
|
+
/** The tool callback's `extra`, for progress and cancellation. */
|
|
36
|
+
extra?: ToolExtra;
|
|
37
|
+
}
|
|
38
|
+
/** Run an ffmpeg edit job atomically: write a .partial sibling, rename it onto
|
|
39
|
+
* the output on success, and delete it on any failure. */
|
|
40
|
+
export declare function runEditJob(args: string[], job: EditJob): Promise<void>;
|
|
41
|
+
/** Probe a file's duration, or null when it cannot be read. Used to size
|
|
42
|
+
* timeouts and progress; never fails the job on its own. */
|
|
43
|
+
export declare function probeDuration(input: string): Promise<number | null>;
|
|
44
|
+
//# sourceMappingURL=jobs.d.ts.map
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AAW,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC5B,kEAAkE;IAClE,KAAK,CAAC,EAAE,SAAS,CAAC;CACnB;AAED;0DAC0D;AAC1D,wBAAsB,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,GAAG,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC,CAsB5E;AAED;4DAC4D;AAC5D,wBAAsB,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAMzE"}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Long-running edit jobs (#81).
|
|
3
|
+
*
|
|
4
|
+
* Edit tools used to run ffmpeg with a fixed 5-20 minute timeout, wrote
|
|
5
|
+
* straight to the output path, and reported nothing until they finished. A
|
|
6
|
+
* long recording could time out partway, leave a truncated file at `output`
|
|
7
|
+
* (which then blocked a retry with "Output already exists"), and the agent had
|
|
8
|
+
* no idea how far it got. runEditJob fixes all three:
|
|
9
|
+
*
|
|
10
|
+
* - the timeout scales with the media's duration (env override available);
|
|
11
|
+
* - ffmpeg writes a `.partial` sibling that is renamed onto `output` only on
|
|
12
|
+
* success and deleted on any failure, timeout, or cancellation;
|
|
13
|
+
* - `-progress pipe:1` is parsed and forwarded as MCP progress notifications
|
|
14
|
+
* when the request carried a progressToken.
|
|
15
|
+
*/
|
|
16
|
+
import { basename, dirname, extname, join } from "node:path";
|
|
17
|
+
import { renameSync, rmSync } from "node:fs";
|
|
18
|
+
import { ScreencastError } from "./errors.js";
|
|
19
|
+
import { probeMedia, runFfmpeg } from "./ffmpeg.js";
|
|
20
|
+
/** Env var that overrides every edit job's timeout, in milliseconds. */
|
|
21
|
+
export const JOB_TIMEOUT_ENV = "SCREENCAST_JOB_TIMEOUT_MS";
|
|
22
|
+
/** Floor for a duration-scaled timeout. */
|
|
23
|
+
export const BASE_TIMEOUT_MS = 10 * 60_000;
|
|
24
|
+
/** Timeout when the media's duration could not be probed. */
|
|
25
|
+
export const UNKNOWN_DURATION_TIMEOUT_MS = 30 * 60_000;
|
|
26
|
+
/** Allow this many seconds of wall-clock time per second of media. A slow
|
|
27
|
+
* preset on a laptop runs around real time; 4x leaves ample headroom. */
|
|
28
|
+
export const SECONDS_PER_MEDIA_SECOND = 4;
|
|
29
|
+
/** Timeout for a job over `durationSec` of media: max(base, duration * 4x),
|
|
30
|
+
* or the env override when it is a positive integer. */
|
|
31
|
+
export function jobTimeoutMs(durationSec, env = process.env) {
|
|
32
|
+
const override = Number(env[JOB_TIMEOUT_ENV]);
|
|
33
|
+
if (Number.isInteger(override) && override > 0)
|
|
34
|
+
return override;
|
|
35
|
+
if (durationSec == null || !(durationSec > 0))
|
|
36
|
+
return UNKNOWN_DURATION_TIMEOUT_MS;
|
|
37
|
+
return Math.max(BASE_TIMEOUT_MS, Math.ceil(durationSec * SECONDS_PER_MEDIA_SECOND * 1000));
|
|
38
|
+
}
|
|
39
|
+
/** `dir/name.partial.ext`: keeps the extension so ffmpeg picks the same muxer. */
|
|
40
|
+
export function partialPath(output) {
|
|
41
|
+
const ext = extname(output);
|
|
42
|
+
return join(dirname(output), `${basename(output, ext)}.partial${ext}`);
|
|
43
|
+
}
|
|
44
|
+
/** Point a builder's args at `partial` instead of `output`. Builders put the
|
|
45
|
+
* output path last; replacing the last occurrence leaves an identical input
|
|
46
|
+
* path (rare, but legal with overwrite) untouched. */
|
|
47
|
+
export function retargetOutput(args, output, partial) {
|
|
48
|
+
const i = args.lastIndexOf(output);
|
|
49
|
+
if (i === -1) {
|
|
50
|
+
throw new ScreencastError(`Internal error: output path ${output} is not in the ffmpeg arguments.`);
|
|
51
|
+
}
|
|
52
|
+
const next = [...args];
|
|
53
|
+
next[i] = partial;
|
|
54
|
+
return next;
|
|
55
|
+
}
|
|
56
|
+
/** Feed `-progress pipe:1` output in chunks; returns the latest media time in
|
|
57
|
+
* seconds seen so far. Lines can split across chunks, so it buffers. */
|
|
58
|
+
export function createProgressParser() {
|
|
59
|
+
let buffer = "";
|
|
60
|
+
let latest = null;
|
|
61
|
+
return (chunk) => {
|
|
62
|
+
buffer += chunk;
|
|
63
|
+
const lines = buffer.split(/\r?\n/);
|
|
64
|
+
buffer = lines.pop() ?? "";
|
|
65
|
+
for (const line of lines) {
|
|
66
|
+
// out_time_us is microseconds; ffmpeg's out_time_ms is also microseconds
|
|
67
|
+
// (a long-standing naming bug), so either works as a fallback.
|
|
68
|
+
const m = line.match(/^out_time_(?:us|ms)=(\d+)$/);
|
|
69
|
+
if (m)
|
|
70
|
+
latest = Number(m[1]) / 1_000_000;
|
|
71
|
+
}
|
|
72
|
+
return latest;
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
/** A progress callback that forwards to the client as MCP progress
|
|
76
|
+
* notifications, or undefined when the request asked for none. Updates are
|
|
77
|
+
* throttled to whole-percent steps. */
|
|
78
|
+
export function progressReporter(extra) {
|
|
79
|
+
const token = extra?._meta?.progressToken;
|
|
80
|
+
if (token === undefined || !extra)
|
|
81
|
+
return undefined;
|
|
82
|
+
let lastPercent = -1;
|
|
83
|
+
return (fraction) => {
|
|
84
|
+
const percent = Math.floor(Math.max(0, Math.min(1, fraction)) * 100);
|
|
85
|
+
if (percent <= lastPercent)
|
|
86
|
+
return;
|
|
87
|
+
lastPercent = percent;
|
|
88
|
+
extra
|
|
89
|
+
.sendNotification({
|
|
90
|
+
method: "notifications/progress",
|
|
91
|
+
params: { progressToken: token, progress: percent, total: 100 },
|
|
92
|
+
})
|
|
93
|
+
.catch(() => {
|
|
94
|
+
/* a client that went away must not fail the job */
|
|
95
|
+
});
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
/** Run an ffmpeg edit job atomically: write a .partial sibling, rename it onto
|
|
99
|
+
* the output on success, and delete it on any failure. */
|
|
100
|
+
export async function runEditJob(args, job) {
|
|
101
|
+
const partial = partialPath(job.output);
|
|
102
|
+
const report = progressReporter(job.extra);
|
|
103
|
+
const parse = createProgressParser();
|
|
104
|
+
const duration = job.durationSec != null && job.durationSec > 0 ? job.durationSec : null;
|
|
105
|
+
const ffArgs = ["-progress", "pipe:1", "-nostats", ...retargetOutput(args, job.output, partial)];
|
|
106
|
+
try {
|
|
107
|
+
await runFfmpeg(ffArgs, jobTimeoutMs(duration), {
|
|
108
|
+
signal: job.extra?.signal,
|
|
109
|
+
onStdout: report && duration
|
|
110
|
+
? (chunk) => {
|
|
111
|
+
const t = parse(chunk);
|
|
112
|
+
if (t !== null)
|
|
113
|
+
report(t / duration);
|
|
114
|
+
}
|
|
115
|
+
: undefined,
|
|
116
|
+
});
|
|
117
|
+
renameSync(partial, job.output);
|
|
118
|
+
report?.(1);
|
|
119
|
+
}
|
|
120
|
+
catch (error) {
|
|
121
|
+
rmSync(partial, { force: true });
|
|
122
|
+
throw error;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
/** Probe a file's duration, or null when it cannot be read. Used to size
|
|
126
|
+
* timeouts and progress; never fails the job on its own. */
|
|
127
|
+
export async function probeDuration(input) {
|
|
128
|
+
try {
|
|
129
|
+
return (await probeMedia(input)).durationSec;
|
|
130
|
+
}
|
|
131
|
+
catch {
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
//# sourceMappingURL=jobs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jobs.js","sourceRoot":"","sources":["../../src/utils/jobs.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAC7D,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAG7C,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAC9C,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAKpD,wEAAwE;AACxE,MAAM,CAAC,MAAM,eAAe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CAAC,CAAC,SAAS;SACd,CAAC,CAAC;QACH,UAAU,CAAC,OAAO,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;QAChC,MAAM,EAAE,CAAC,CAAC,CAAC,CAAC;IACd,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,CAAC,OAAO,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;QACjC,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC;AAED;4DAC4D;AAC5D,MAAM,CAAC,KAAK,UAAU,aAAa,CAAC,KAAa;IAC/C,IAAI,CAAC;QACH,OAAO,CAAC,MAAM,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC,WAAW,CAAC;IAC/C,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { ChildProcess } from "node:child_process";
|
|
2
|
+
import { type EndReason, type SessionRecord, type SessionStore } from "./sessions.js";
|
|
3
|
+
/** Wait up to ms for a child to exit; resolve true if it did. */
|
|
4
|
+
export declare function waitForExit(child: ChildProcess, ms: number): Promise<boolean>;
|
|
5
|
+
/** Probe a finished file's duration, or null if it is missing/unreadable. */
|
|
6
|
+
export declare function probeDuration(ffprobe: string, path: string): Promise<number | null>;
|
|
7
|
+
export interface StopOptions {
|
|
8
|
+
/** Recorded on the session; "stopped" for stop_recording, "shutdown" on exit. */
|
|
9
|
+
reason: Extract<EndReason, "stopped" | "shutdown">;
|
|
10
|
+
/** Resolves the final duration; injected so tests need no ffprobe. */
|
|
11
|
+
probe: (path: string) => Promise<number | null>;
|
|
12
|
+
/** How long to wait for a graceful 'q' before killing (default 8s). */
|
|
13
|
+
gracefulMs?: number;
|
|
14
|
+
}
|
|
15
|
+
export interface StopResult {
|
|
16
|
+
record: SessionRecord | undefined;
|
|
17
|
+
graceful: boolean;
|
|
18
|
+
durationSec: number | null;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Stop every recording this instance owns (server shutdown, #76). Stops run in
|
|
22
|
+
* parallel and the whole pass is capped at `capMs` so a wedged ffmpeg cannot
|
|
23
|
+
* hold the process open; never throws. Returns the ids it attempted.
|
|
24
|
+
*/
|
|
25
|
+
export declare function stopAll(store: SessionStore, probe: StopOptions["probe"], capMs?: number): Promise<string[]>;
|
|
26
|
+
/** Stop an active recording and record its final state. The caller has already
|
|
27
|
+
* checked the record exists and is "recording". */
|
|
28
|
+
export declare function stopSession(store: SessionStore, record: SessionRecord, opts: StopOptions): Promise<StopResult>;
|
|
29
|
+
//# sourceMappingURL=lifecycle.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lifecycle.d.ts","sourceRoot":"","sources":["../../src/utils/lifecycle.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAIvD,OAAO,EAIL,KAAK,SAAS,EACd,KAAK,aAAa,EAClB,KAAK,YAAY,EAClB,MAAM,eAAe,CAAC;AAUvB,iEAAiE;AACjE,wBAAgB,WAAW,CAAC,KAAK,EAAE,YAAY,EAAE,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAS7E;AAED,6EAA6E;AAC7E,wBAAsB,aAAa,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CASzF;AAED,MAAM,WAAW,WAAW;IAC1B,iFAAiF;IACjF,MAAM,EAAE,OAAO,CAAC,SAAS,EAAE,SAAS,GAAG,UAAU,CAAC,CAAC;IACnD,sEAAsE;IACtE,KAAK,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC;IAChD,uEAAuE;IACvE,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,UAAU;IACzB,MAAM,EAAE,aAAa,GAAG,SAAS,CAAC;IAClC,QAAQ,EAAE,OAAO,CAAC;IAClB,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;CAC5B;AAED;;;;GAIG;AACH,wBAAsB,OAAO,CAC3B,KAAK,EAAE,YAAY,EACnB,KAAK,EAAE,WAAW,CAAC,OAAO,CAAC,EAC3B,KAAK,SAAS,GACb,OAAO,CAAC,MAAM,EAAE,CAAC,CAoBnB;AAED;mDACmD;AACnD,wBAAsB,WAAW,CAC/B,KAAK,EAAE,YAAY,EACnB,MAAM,EAAE,aAAa,EACrB,IAAI,EAAE,WAAW,GAChB,OAAO,CAAC,UAAU,CAAC,CAwCrB"}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Recording stop sequence, shared by stop_recording and server shutdown.
|
|
3
|
+
*
|
|
4
|
+
* Graceful first: ffmpeg quits cleanly (and finalizes the mp4) on 'q' over
|
|
5
|
+
* stdin. If it does not exit in time, or the live handle was lost (a
|
|
6
|
+
* cross-restart stop), the process is terminated by pid; the fragmented-mp4
|
|
7
|
+
* muxing keeps a killed recording playable either way.
|
|
8
|
+
*/
|
|
9
|
+
import { existsSync } from "node:fs";
|
|
10
|
+
import { runCapture } from "./ffmpeg.js";
|
|
11
|
+
import { whenFinalized } from "./events.js";
|
|
12
|
+
import { buildProbeArgs, parseMediaInfo } from "./media.js";
|
|
13
|
+
import { isAlive, isFfmpegProcess, killPid, } from "./sessions.js";
|
|
14
|
+
/** Poll until a pid is gone (process fully exited, file handles released). */
|
|
15
|
+
async function waitForPidExit(pid, ms) {
|
|
16
|
+
const deadline = Date.now() + ms;
|
|
17
|
+
while (Date.now() < deadline && isAlive(pid)) {
|
|
18
|
+
await new Promise((r) => setTimeout(r, 100));
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
/** Wait up to ms for a child to exit; resolve true if it did. */
|
|
22
|
+
export function waitForExit(child, ms) {
|
|
23
|
+
return new Promise((resolve) => {
|
|
24
|
+
if (child.exitCode !== null || child.signalCode !== null)
|
|
25
|
+
return resolve(true);
|
|
26
|
+
const timer = setTimeout(() => resolve(false), ms);
|
|
27
|
+
child.once("exit", () => {
|
|
28
|
+
clearTimeout(timer);
|
|
29
|
+
resolve(true);
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
/** Probe a finished file's duration, or null if it is missing/unreadable. */
|
|
34
|
+
export async function probeDuration(ffprobe, path) {
|
|
35
|
+
if (!existsSync(path))
|
|
36
|
+
return null;
|
|
37
|
+
try {
|
|
38
|
+
const res = await runCapture(ffprobe, buildProbeArgs(path), 30_000);
|
|
39
|
+
if (res.code !== 0)
|
|
40
|
+
return null;
|
|
41
|
+
return parseMediaInfo(JSON.parse(res.stdout)).durationSec;
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Stop every recording this instance owns (server shutdown, #76). Stops run in
|
|
49
|
+
* parallel and the whole pass is capped at `capMs` so a wedged ffmpeg cannot
|
|
50
|
+
* hold the process open; never throws. Returns the ids it attempted.
|
|
51
|
+
*/
|
|
52
|
+
export async function stopAll(store, probe, capMs = 10_000) {
|
|
53
|
+
const ids = store.activeIds();
|
|
54
|
+
const stops = ids.map(async (id) => {
|
|
55
|
+
const record = store.get(id);
|
|
56
|
+
if (!record)
|
|
57
|
+
return;
|
|
58
|
+
try {
|
|
59
|
+
await stopSession(store, record, { reason: "shutdown", probe, gracefulMs: Math.min(8000, capMs) });
|
|
60
|
+
}
|
|
61
|
+
catch (err) {
|
|
62
|
+
process.stderr.write(`Stopping ${id} on shutdown failed: ${err instanceof Error ? err.message : String(err)}\n`);
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
let timer;
|
|
66
|
+
const cap = new Promise((resolve) => {
|
|
67
|
+
timer = setTimeout(resolve, capMs);
|
|
68
|
+
});
|
|
69
|
+
await Promise.race([Promise.allSettled(stops), cap]);
|
|
70
|
+
clearTimeout(timer);
|
|
71
|
+
return ids;
|
|
72
|
+
}
|
|
73
|
+
/** Stop an active recording and record its final state. The caller has already
|
|
74
|
+
* checked the record exists and is "recording". */
|
|
75
|
+
export async function stopSession(store, record, opts) {
|
|
76
|
+
store.markStopping(record.id);
|
|
77
|
+
try {
|
|
78
|
+
let graceful = false;
|
|
79
|
+
const child = store.getChild(record.id);
|
|
80
|
+
if (child && child.stdin && child.stdin.writable) {
|
|
81
|
+
child.stdin.write("q\n");
|
|
82
|
+
child.stdin.end();
|
|
83
|
+
graceful = await waitForExit(child, opts.gracefulMs ?? 8000);
|
|
84
|
+
if (!graceful && record.pid !== null) {
|
|
85
|
+
killPid(record.pid);
|
|
86
|
+
await waitForPidExit(record.pid, 2000);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
else if (record.pid !== null && isAlive(record.pid)) {
|
|
90
|
+
// Cross-restart stop: no stdin handle. Terminate by pid, but only if the
|
|
91
|
+
// pid is still an ffmpeg (never a recycled, unrelated process).
|
|
92
|
+
if (isFfmpegProcess(record.pid)) {
|
|
93
|
+
killPid(record.pid);
|
|
94
|
+
await waitForPidExit(record.pid, 2000);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
store.detachChild(record.id);
|
|
98
|
+
// The input event log converts once ffmpeg has exited (#118).
|
|
99
|
+
await whenFinalized(record.id);
|
|
100
|
+
const durationSec = await opts.probe(record.outputPath);
|
|
101
|
+
const updated = store.update(record.id, {
|
|
102
|
+
status: "stopped",
|
|
103
|
+
stoppedAt: new Date().toISOString(),
|
|
104
|
+
durationSec: durationSec ?? undefined,
|
|
105
|
+
endReason: opts.reason,
|
|
106
|
+
// A forced kill can transiently mark the record "failed" via the crash
|
|
107
|
+
// detector in start_recording; a deliberate stop is not a failure.
|
|
108
|
+
error: undefined,
|
|
109
|
+
});
|
|
110
|
+
return { record: updated, graceful, durationSec };
|
|
111
|
+
}
|
|
112
|
+
finally {
|
|
113
|
+
store.clearStopping(record.id);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
//# sourceMappingURL=lifecycle.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lifecycle.js","sourceRoot":"","sources":["../../src/utils/lifecycle.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAErC,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAC5D,OAAO,EACL,OAAO,EACP,eAAe,EACf,OAAO,GAIR,MAAM,eAAe,CAAC;AAEvB,8EAA8E;AAC9E,KAAK,UAAU,cAAc,CAAC,GAAW,EAAE,EAAU;IACnD,MAAM,QAAQ,GAAG,IAAI,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC;IACjC,OAAO,IAAI,CAAC,GAAG,EAAE,GAAG,QAAQ,IAAI,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC;QAC7C,MAAM,IAAI,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,UAAU,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC;IAC/C,CAAC;AACH,CAAC;AAED,iEAAiE;AACjE,MAAM,UAAU,WAAW,CAAC,KAAmB,EAAE,EAAU;IACzD,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;QAC7B,IAAI,KAAK,CAAC,QAAQ,KAAK,IAAI,IAAI,KAAK,CAAC,UAAU,KAAK,IAAI;YAAE,OAAO,OAAO,CAAC,IAAI,CAAC,CAAC;QAC/E,MAAM,KAAK,GAAG,UAAU,CAAC,GAAG,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,EAAE,CAAC,CAAC;QACnD,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,EAA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;gBACpB,MAAM,cAAc,CAAC,MAAM,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;YACzC,CAAC;QACH,CAAC;aAAM,IAAI,MAAM,CAAC,GAAG,KAAK,IAAI,IAAI,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC;YACtD,yEAAyE;YACzE,gEAAgE;YAChE,IAAI,eAAe,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC;gBAChC,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;gBACpB,MAAM,cAAc,CAAC,MAAM,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;YACzC,CAAC;QACH,CAAC;QACD,KAAK,CAAC,WAAW,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;QAC7B,8DAA8D;QAC9D,MAAM,aAAa,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;QAE/B,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;QACxD,MAAM,OAAO,GAAG,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE;YACtC,MAAM,EAAE,SAAS;YACjB,SAAS,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;YACnC,WAAW,EAAE,WAAW,IAAI,SAAS;YACrC,SAAS,EAAE,IAAI,CAAC,MAAM;YACtB,uEAAuE;YACvE,mEAAmE;YACnE,KAAK,EAAE,SAAS;SACjB,CAAC,CAAC;QACH,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,CAAC;IACpD,CAAC;YAAS,CAAC;QACT,KAAK,CAAC,aAAa,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;IACjC,CAAC;AACH,CAAC"}
|
package/dist/utils/media.d.ts
CHANGED
|
@@ -30,8 +30,41 @@ export declare function parseFrameRate(value: string | undefined): number | null
|
|
|
30
30
|
/** Reduce a parsed ffprobe JSON document to a flat MediaInfo. */
|
|
31
31
|
export declare function parseMediaInfo(probe: FfprobeJson): MediaInfo;
|
|
32
32
|
export declare function buildProbeArgs(input: string): string[];
|
|
33
|
-
/**
|
|
34
|
-
export declare
|
|
33
|
+
/** Default and ceiling for how many frames one sample_frames call may write. */
|
|
34
|
+
export declare const DEFAULT_MAX_FRAMES = 100;
|
|
35
|
+
export declare const MAX_FRAMES_CEILING = 1000;
|
|
36
|
+
export interface FpsPlan {
|
|
37
|
+
/** The rate actually used; lower than requested when clamped. */
|
|
38
|
+
fps: number;
|
|
39
|
+
/** Frames the requested rate would have written, or null when the duration is unknown. */
|
|
40
|
+
requestedFrames: number | null;
|
|
41
|
+
clamped: boolean;
|
|
42
|
+
}
|
|
43
|
+
/** Fit a requested sampling rate under a frame cap (#80). When duration * fps
|
|
44
|
+
* would exceed maxFrames, lower the rate so the frames still span the whole
|
|
45
|
+
* video instead of stopping partway. An unknown duration leaves the rate as
|
|
46
|
+
* asked; the -frames:v backstop in buildSampleByFpsArgs still applies. */
|
|
47
|
+
export declare function planFpsSampling(durationSec: number | null, fps: number, maxFrames: number): FpsPlan;
|
|
48
|
+
/** Sample frames at a fixed rate: ffmpeg -i in -vf fps=N -frames:v MAX
|
|
49
|
+
* outdir/frame_%05d.png. -frames:v is a hard backstop on the frame count. */
|
|
50
|
+
export declare function buildSampleByFpsArgs(input: string, fps: number, outputPattern: string, maxFrames?: number, dedupe?: boolean): string[];
|
|
51
|
+
/** Default scene-change threshold for UI content (#114). Screen recordings
|
|
52
|
+
* change in small regions, so this is lower than the 0.3-0.4 usual for film. */
|
|
53
|
+
export declare const DEFAULT_SCENE_THRESHOLD = 0.1;
|
|
54
|
+
/** Scan a video for scene changes: a cheap pass on a 320 px-wide copy that
|
|
55
|
+
* prints each frame whose scene score exceeds `threshold` (pts_time + score)
|
|
56
|
+
* to stdout. Frames are then grabbed at full size at those timestamps. */
|
|
57
|
+
export declare function buildSceneScanArgs(input: string, threshold?: number): string[];
|
|
58
|
+
export interface SceneChange {
|
|
59
|
+
timeSec: number;
|
|
60
|
+
score: number;
|
|
61
|
+
}
|
|
62
|
+
/** Parse `metadata=print` output into (time, score) pairs. Pure. */
|
|
63
|
+
export declare function parseSceneScan(stdout: string): SceneChange[];
|
|
64
|
+
/** Choose which frames to keep: always the opening frame (t=0), then the
|
|
65
|
+
* strongest scene changes up to maxFrames total, returned in time order.
|
|
66
|
+
* Pure. */
|
|
67
|
+
export declare function pickSceneFrames(changes: SceneChange[], maxFrames: number): number[];
|
|
35
68
|
/** Build one ffmpeg invocation per timestamp (accurate single-frame seek). */
|
|
36
69
|
export declare function buildSampleAtTimestampArgs(input: string, timestampSec: number, output: string): string[];
|
|
37
70
|
/** Trim by start + (end | duration). Stream-copy for speed. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"media.d.ts","sourceRoot":"","sources":["../../src/utils/media.ts"],"names":[],"mappings":"AAOA,OAAO,EAAkB,KAAK,OAAO,EAAE,MAAM,cAAc,CAAC;AAM5D,MAAM,WAAW,SAAS;IACxB,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;IAC3B,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,GAAG,EAAE,MAAM,GAAG,IAAI,CAAC;IACnB,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;CAC1B;AAED,UAAU,aAAa;IACrB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAED,UA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G,CAAC,EAAE,MAAM,CAAC;IACb,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED;4EAC4E;AAC5E,wBAAgB,eAAe,CAC7B,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,YAAY,EAAE,EACvB,IAAI,GAAE,aAAkB,EACxB,IAAI,CAAC,EAAE;IAAE,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAA;CAAE,GACrD,MAAM,EAAE,CAsDV"}
|
package/dist/utils/media.js
CHANGED
|
@@ -55,10 +55,82 @@ function validatePositive(value, label) {
|
|
|
55
55
|
}
|
|
56
56
|
return value;
|
|
57
57
|
}
|
|
58
|
-
/**
|
|
59
|
-
export
|
|
58
|
+
/** Default and ceiling for how many frames one sample_frames call may write. */
|
|
59
|
+
export const DEFAULT_MAX_FRAMES = 100;
|
|
60
|
+
export const MAX_FRAMES_CEILING = 1000;
|
|
61
|
+
/** Fit a requested sampling rate under a frame cap (#80). When duration * fps
|
|
62
|
+
* would exceed maxFrames, lower the rate so the frames still span the whole
|
|
63
|
+
* video instead of stopping partway. An unknown duration leaves the rate as
|
|
64
|
+
* asked; the -frames:v backstop in buildSampleByFpsArgs still applies. */
|
|
65
|
+
export function planFpsSampling(durationSec, fps, maxFrames) {
|
|
60
66
|
validatePositive(fps, "fps");
|
|
61
|
-
|
|
67
|
+
if (durationSec === null || !(durationSec > 0)) {
|
|
68
|
+
return { fps, requestedFrames: null, clamped: false };
|
|
69
|
+
}
|
|
70
|
+
const requested = Math.ceil(durationSec * fps);
|
|
71
|
+
if (requested <= maxFrames)
|
|
72
|
+
return { fps, requestedFrames: requested, clamped: false };
|
|
73
|
+
// Round down to 4 decimals so the clamped rate never overshoots the cap.
|
|
74
|
+
const clampedFps = Math.max(1e-4, Math.floor((maxFrames / durationSec) * 10_000) / 10_000);
|
|
75
|
+
return { fps: clampedFps, requestedFrames: requested, clamped: true };
|
|
76
|
+
}
|
|
77
|
+
/** Sample frames at a fixed rate: ffmpeg -i in -vf fps=N -frames:v MAX
|
|
78
|
+
* outdir/frame_%05d.png. -frames:v is a hard backstop on the frame count. */
|
|
79
|
+
export function buildSampleByFpsArgs(input, fps, outputPattern, maxFrames = DEFAULT_MAX_FRAMES, dedupe = false) {
|
|
80
|
+
validatePositive(fps, "fps");
|
|
81
|
+
// dedupe (#114): mpdecimate drops frames nearly identical to the previous
|
|
82
|
+
// kept one, and -fps_mode vfr stops the muxer re-inserting them.
|
|
83
|
+
const vf = dedupe ? `fps=${fps},mpdecimate` : `fps=${fps}`;
|
|
84
|
+
return [
|
|
85
|
+
"-y", "-i", input, "-vf", vf,
|
|
86
|
+
...(dedupe ? ["-fps_mode", "vfr"] : []),
|
|
87
|
+
"-frames:v", String(maxFrames), outputPattern,
|
|
88
|
+
];
|
|
89
|
+
}
|
|
90
|
+
/** Default scene-change threshold for UI content (#114). Screen recordings
|
|
91
|
+
* change in small regions, so this is lower than the 0.3-0.4 usual for film. */
|
|
92
|
+
export const DEFAULT_SCENE_THRESHOLD = 0.1;
|
|
93
|
+
/** Scan a video for scene changes: a cheap pass on a 320 px-wide copy that
|
|
94
|
+
* prints each frame whose scene score exceeds `threshold` (pts_time + score)
|
|
95
|
+
* to stdout. Frames are then grabbed at full size at those timestamps. */
|
|
96
|
+
export function buildSceneScanArgs(input, threshold = DEFAULT_SCENE_THRESHOLD) {
|
|
97
|
+
if (!Number.isFinite(threshold) || threshold <= 0 || threshold >= 1) {
|
|
98
|
+
throw new ScreencastError("sceneThreshold must be between 0 and 1 (exclusive).");
|
|
99
|
+
}
|
|
100
|
+
return [
|
|
101
|
+
"-v", "error", "-i", input, "-an",
|
|
102
|
+
"-vf", `scale=320:-2,select='gt(scene\\,${threshold})',metadata=print:file=-`,
|
|
103
|
+
"-f", "null", "-",
|
|
104
|
+
];
|
|
105
|
+
}
|
|
106
|
+
/** Parse `metadata=print` output into (time, score) pairs. Pure. */
|
|
107
|
+
export function parseSceneScan(stdout) {
|
|
108
|
+
const out = [];
|
|
109
|
+
let time = null;
|
|
110
|
+
for (const line of stdout.split(/\r?\n/)) {
|
|
111
|
+
const t = line.match(/pts_time:\s*(-?[\d.]+)/);
|
|
112
|
+
if (t) {
|
|
113
|
+
time = Number(t[1]);
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
const s = line.match(/lavfi\.scene_score=([\d.]+)/);
|
|
117
|
+
if (s && time !== null) {
|
|
118
|
+
out.push({ timeSec: time, score: Number(s[1]) });
|
|
119
|
+
time = null;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return out;
|
|
123
|
+
}
|
|
124
|
+
/** Choose which frames to keep: always the opening frame (t=0), then the
|
|
125
|
+
* strongest scene changes up to maxFrames total, returned in time order.
|
|
126
|
+
* Pure. */
|
|
127
|
+
export function pickSceneFrames(changes, maxFrames) {
|
|
128
|
+
const strongest = [...changes]
|
|
129
|
+
.filter((c) => c.timeSec > 0)
|
|
130
|
+
.sort((a, b) => b.score - a.score)
|
|
131
|
+
.slice(0, Math.max(0, maxFrames - 1))
|
|
132
|
+
.map((c) => c.timeSec);
|
|
133
|
+
return [0, ...strongest].sort((a, b) => a - b);
|
|
62
134
|
}
|
|
63
135
|
/** Build one ffmpeg invocation per timestamp (accurate single-frame seek). */
|
|
64
136
|
export function buildSampleAtTimestampArgs(input, timestampSec, output) {
|