@uploadfilm/sdk 0.0.0-stage → 0.2.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/LICENSE +4 -0
- package/README.md +529 -2
- package/dist/ai/embed.d.ts +58 -0
- package/dist/ai/fake.d.ts +41 -0
- package/dist/ai/http.d.ts +17 -0
- package/dist/ai/index.d.ts +27 -0
- package/dist/ai/languages.d.ts +2 -0
- package/dist/ai/ollama.d.ts +23 -0
- package/dist/ai/openaiCompatible.d.ts +54 -0
- package/dist/ai/passages.d.ts +28 -0
- package/dist/ai/transcribe.d.ts +84 -0
- package/dist/ai/types.d.ts +141 -0
- package/dist/ai/usage.d.ts +66 -0
- package/dist/ai/whisperJson.d.ts +15 -0
- package/dist/ai/workersAi.d.ts +56 -0
- package/dist/ai.js +1 -0
- package/dist/ai.mjs +1 -0
- package/dist/cards/files.d.ts +91 -0
- package/dist/cards/index.d.ts +15 -0
- package/dist/cards/layouts.d.ts +121 -0
- package/dist/cards/probe.d.ts +69 -0
- package/dist/cards/proxies.d.ts +70 -0
- package/dist/cards/select.d.ts +68 -0
- package/dist/cards/volumes.d.ts +170 -0
- package/dist/cards/xml.d.ts +80 -0
- package/dist/cards.js +1 -0
- package/dist/cards.mjs +1 -0
- package/dist/copy/checksums.d.ts +66 -0
- package/dist/copy/engine.d.ts +24 -0
- package/dist/copy/errors.d.ts +109 -0
- package/dist/copy/index.d.ts +15 -0
- package/dist/copy/steps.d.ts +28 -0
- package/dist/copy/sweep.d.ts +24 -0
- package/dist/copy/types.d.ts +363 -0
- package/dist/embed/player.js +2 -0
- package/dist/host/adapters.d.ts +107 -0
- package/dist/host/detect.d.ts +68 -0
- package/dist/host/encoders.d.ts +34 -0
- package/dist/host/environment.d.ts +53 -0
- package/dist/host/index.d.ts +20 -0
- package/dist/host/linux.d.ts +94 -0
- package/dist/host/mac.d.ts +47 -0
- package/dist/host/network.d.ts +54 -0
- package/dist/host/node.d.ts +13 -0
- package/dist/host/policy.d.ts +101 -0
- package/dist/host/tuner.d.ts +48 -0
- package/dist/host/types.d.ts +118 -0
- package/dist/host-node.js +1 -0
- package/dist/host-node.mjs +1 -0
- package/dist/host.js +1 -0
- package/dist/host.mjs +1 -0
- package/dist/images/index.d.ts +11 -0
- package/dist/images/settings.d.ts +43 -0
- package/dist/images/types.d.ts +83 -0
- package/dist/images/url.d.ts +65 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +1 -0
- package/dist/index.mjs +1 -0
- package/dist/player/adapters/hlsjs.d.ts +28 -0
- package/dist/player/bandwidthMemory.d.ts +28 -0
- package/dist/player/captions/index.d.ts +55 -0
- package/dist/player/embed/code.d.ts +195 -0
- package/dist/player/embed/constants.d.ts +4 -0
- package/dist/player/embed/controller.d.ts +62 -0
- package/dist/player/embed/element.d.ts +32 -0
- package/dist/player/embed/frame.d.ts +65 -0
- package/dist/player/embed/global.d.ts +14 -0
- package/dist/player/embed/index.d.ts +15 -0
- package/dist/player/embed/loader.d.ts +1 -0
- package/dist/player/embed/oembed.d.ts +74 -0
- package/dist/player/embed/protocol.d.ts +64 -0
- package/dist/player/engine.d.ts +215 -0
- package/dist/player/index.d.ts +18 -0
- package/dist/player/start.d.ts +62 -0
- package/dist/player/store.d.ts +10 -0
- package/dist/player/storyboard/index.d.ts +161 -0
- package/dist/player/stream.d.ts +206 -0
- package/dist/player/tracks.d.ts +41 -0
- package/dist/player.js +1 -0
- package/dist/player.mjs +1 -0
- package/dist/proxies/args.d.ts +19 -0
- package/dist/proxies/budget.d.ts +57 -0
- package/dist/proxies/camera.d.ts +104 -0
- package/dist/proxies/formats.d.ts +36 -0
- package/dist/proxies/index.d.ts +18 -0
- package/dist/proxies/jobs.d.ts +98 -0
- package/dist/proxies/node.d.ts +32 -0
- package/dist/proxies/plan.d.ts +89 -0
- package/dist/proxies/probe.d.ts +39 -0
- package/dist/proxies/render.d.ts +104 -0
- package/dist/proxies/safety.d.ts +45 -0
- package/dist/proxies/types.d.ts +83 -0
- package/dist/proxies/upload.d.ts +14 -0
- package/dist/react/BigPlay.d.ts +26 -0
- package/dist/react/Player.d.ts +58 -0
- package/dist/react/embed/index.d.ts +45 -0
- package/dist/react/examples/CustomControls.d.ts +8 -0
- package/dist/react/images/Image.d.ts +16 -0
- package/dist/react/images/index.d.ts +4 -0
- package/dist/react/images/useImage.d.ts +47 -0
- package/dist/react/index.d.ts +17 -0
- package/dist/react/storyboard/index.d.ts +58 -0
- package/dist/react/theme.d.ts +37 -0
- package/dist/react/useAdapterErrors.d.ts +8 -0
- package/dist/react/usePlaybackStart.d.ts +18 -0
- package/dist/react/usePlayer.d.ts +28 -0
- package/dist/react/useReducedMotion.d.ts +2 -0
- package/dist/react/useTimedTrack.d.ts +22 -0
- package/dist/react.js +1 -0
- package/dist/react.mjs +1 -0
- package/dist/server/auth/apiKeys.d.ts +30 -0
- package/dist/server/auth/cache.d.ts +21 -0
- package/dist/server/auth/deviceTokens.d.ts +38 -0
- package/dist/server/auth/envelope.d.ts +15 -0
- package/dist/server/auth/grants.d.ts +76 -0
- package/dist/server/auth/index.d.ts +19 -0
- package/dist/server/auth/jwt.d.ts +65 -0
- package/dist/server/auth/keyring.d.ts +31 -0
- package/dist/server/auth/kv.d.ts +12 -0
- package/dist/server/auth/repository.d.ts +45 -0
- package/dist/server/auth/revocations.d.ts +21 -0
- package/dist/server/auth/scope.d.ts +4 -0
- package/dist/server/auth/serviceAuth.d.ts +24 -0
- package/dist/server/auth/signedUrls.d.ts +28 -0
- package/dist/server/embed/index.d.ts +36 -0
- package/dist/server/embed/lifecycle.d.ts +53 -0
- package/dist/server/embed/playerScript.d.ts +4 -0
- package/dist/server/images/index.d.ts +15 -0
- package/dist/server/images/job.d.ts +80 -0
- package/dist/server/images/layout.d.ts +24 -0
- package/dist/server/images/params.d.ts +90 -0
- package/dist/server/images/route.d.ts +101 -0
- package/dist/server/images/sign.d.ts +55 -0
- package/dist/server/index.d.ts +42 -0
- package/dist/server/media/client.d.ts +99 -0
- package/dist/server/media/frameCheck.d.ts +62 -0
- package/dist/server/media/index.d.ts +10 -0
- package/dist/server/media/protocol.d.ts +556 -0
- package/dist/server/media/storyboard.d.ts +144 -0
- package/dist/server/media/transcript.d.ts +143 -0
- package/dist/server/pipeline/index.d.ts +12 -0
- package/dist/server/pipeline/match.d.ts +10 -0
- package/dist/server/pipeline/pipeline.d.ts +182 -0
- package/dist/server/pipeline/queues.d.ts +108 -0
- package/dist/server/pipeline/sources.d.ts +54 -0
- package/dist/server/r2/client.d.ts +164 -0
- package/dist/server/r2/crc32.d.ts +24 -0
- package/dist/server/r2/encoding.d.ts +35 -0
- package/dist/server/r2/errors.d.ts +9 -0
- package/dist/server/r2/integrity.d.ts +25 -0
- package/dist/server/r2/parts.d.ts +46 -0
- package/dist/server/r2/sign.d.ts +47 -0
- package/dist/server/redis/index.d.ts +39 -0
- package/dist/server/state/memory.d.ts +18 -0
- package/dist/server/state/r2.d.ts +73 -0
- package/dist/server/state/types.d.ts +57 -0
- package/dist/server/storage/chunked.d.ts +25 -0
- package/dist/server/storage/index.d.ts +12 -0
- package/dist/server/storage/r2.d.ts +96 -0
- package/dist/server/storage/s3.d.ts +144 -0
- package/dist/server/testing/fake-r2.d.ts +410 -0
- package/dist/server/testing/index.d.ts +10 -0
- package/dist/server/testing/kv-contract.d.ts +14 -0
- package/dist/server/transfer/handler.d.ts +217 -0
- package/dist/server/transfer/keys.d.ts +45 -0
- package/dist/server/transfer/protocol.d.ts +190 -0
- package/dist/server/zip/client.d.ts +32 -0
- package/dist/server/zip/generations.d.ts +83 -0
- package/dist/server/zip/index.d.ts +12 -0
- package/dist/server/zip/planner.d.ts +94 -0
- package/dist/server/zip/sign.d.ts +22 -0
- package/dist/server-media.js +1 -0
- package/dist/server-media.mjs +1 -0
- package/dist/server-redis.js +1 -0
- package/dist/server-redis.mjs +1 -0
- package/dist/server-testing.js +1 -0
- package/dist/server-testing.mjs +1 -0
- package/dist/server.js +1 -0
- package/dist/server.mjs +1 -0
- package/dist/storage/browserFile.d.ts +19 -0
- package/dist/storage/index.d.ts +12 -0
- package/dist/storage/local.d.ts +9 -0
- package/dist/storage/presigned.d.ts +22 -0
- package/dist/sync/ffmpeg.d.ts +44 -0
- package/dist/sync/index.d.ts +17 -0
- package/dist/sync/match.d.ts +28 -0
- package/dist/sync/probe.d.ts +30 -0
- package/dist/sync/timecode.d.ts +50 -0
- package/dist/sync/transcript.d.ts +35 -0
- package/dist/sync/types.d.ts +172 -0
- package/dist/sync/waveform.d.ts +89 -0
- package/dist/sync.js +1 -0
- package/dist/sync.mjs +1 -0
- package/dist/transfer/claims.d.ts +41 -0
- package/dist/transfer/client.d.ts +267 -0
- package/dist/transfer/download.d.ts +192 -0
- package/dist/transfer/engine.d.ts +402 -0
- package/dist/transfer/errors.d.ts +50 -0
- package/dist/transfer/growing/bytes.d.ts +17 -0
- package/dist/transfer/growing/index.d.ts +12 -0
- package/dist/transfer/growing/parts.d.ts +157 -0
- package/dist/transfer/growing/retry.d.ts +38 -0
- package/dist/transfer/growing/store.d.ts +66 -0
- package/dist/transfer/growing/upload.d.ts +126 -0
- package/dist/transfer/index.d.ts +40 -0
- package/dist/transfer/integrity.d.ts +93 -0
- package/dist/transfer/limits.d.ts +34 -0
- package/dist/transfer/node.d.ts +63 -0
- package/dist/transfer/pool.d.ts +83 -0
- package/dist/transfer/protocol.d.ts +172 -0
- package/dist/transfer/signer.d.ts +29 -0
- package/dist/transfer/storage.d.ts +135 -0
- package/dist/transfer/timing.d.ts +132 -0
- package/dist/transfer/transports/electronNet.d.ts +62 -0
- package/dist/transfer/transports/fetch.d.ts +15 -0
- package/dist/transfer/transports/retryAfter.d.ts +12 -0
- package/dist/transfer/transports/undici.d.ts +72 -0
- package/dist/transfer/tuner.d.ts +56 -0
- package/dist/workers/cli.mjs +115 -0
- package/dist/workers/media/config.mjs +53 -0
- package/dist/workers/media/container/Dockerfile +19 -0
- package/dist/workers/media/container/FFMPEG.md +129 -0
- package/dist/workers/media/container/runner.mjs +2690 -0
- package/dist/workers/media/index.mjs +1011 -0
- package/dist/workers/media/init/config.ts +52 -0
- package/dist/workers/media/init/worker.ts +8 -0
- package/dist/workers/media/init/wrangler.toml +44 -0
- package/dist/workers/media/runner.mjs +2656 -0
- package/dist/workers/media/types/build/entry/config.d.mts +6 -0
- package/dist/workers/media/types/build/entry/index.d.mts +43 -0
- package/dist/workers/media/types/build/entry/program.d.mts +13 -0
- package/dist/workers/media/types/build/entry/runner.d.mts +12 -0
- package/dist/workers/media/types/build/entry/settings.d.mts +40 -0
- package/dist/workers/media/types/src/config.d.mts +135 -0
- package/dist/workers/media/types/src/jobs.d.mts +73 -0
- package/dist/workers/media/types/src/runner/container.d.mts +32 -0
- package/dist/workers/media/types/src/runner/ffmpeg.d.mts +227 -0
- package/dist/workers/media/types/src/runner/job.d.mts +107 -0
- package/dist/workers/media/types/src/runner/node.d.mts +71 -0
- package/dist/workers/media/types/src/runner/plugins.d.mts +45 -0
- package/dist/workers/media/types/src/serve.d.mts +107 -0
- package/dist/workers/media/types/src/steps/audio.d.mts +33 -0
- package/dist/workers/media/types/src/steps/edit.d.mts +62 -0
- package/dist/workers/media/types/src/steps/frameCheck.d.mts +44 -0
- package/dist/workers/media/types/src/steps/gate.d.mts +18 -0
- package/dist/workers/media/types/src/steps/paths.d.mts +1 -0
- package/dist/workers/media/types/src/steps/prep.d.mts +60 -0
- package/dist/workers/media/types/src/steps/render.d.mts +40 -0
- package/dist/workers/media/types/src/steps/runner.d.mts +30 -0
- package/dist/workers/media/types/src/steps/transcribe.d.mts +89 -0
- package/dist/workers/media/types/src/storyboard/index.d.mts +61 -0
- package/dist/workers/media/types/src/transcoder.d.mts +30 -0
- package/dist/workers/media/types/src/worker.d.mts +30 -0
- package/dist/workers/zip/index.mjs +589 -0
- package/dist/workers/zip/init/config.ts +22 -0
- package/dist/workers/zip/init/worker.ts +5 -0
- package/dist/workers/zip/init/wrangler.toml +25 -0
- package/dist/workers/zip/types/build/entry/index.d.mts +5 -0
- package/dist/workers/zip/types/src/config.d.mts +35 -0
- package/dist/workers/zip/types/src/serve.d.mts +86 -0
- package/dist/workers/zip/types/src/worker.d.mts +12 -0
- package/dist/zip/index.d.ts +13 -0
- package/dist/zip/layout.d.ts +129 -0
- package/dist/zip/names.d.ts +34 -0
- package/dist/zip/plan.d.ts +115 -0
- package/dist/zip/sw.d.ts +7 -0
- package/package.json +313 -4
|
@@ -0,0 +1,2690 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
3
|
+
var __esm = (fn, res, err) => function __init() {
|
|
4
|
+
if (err) throw err[0];
|
|
5
|
+
try {
|
|
6
|
+
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
7
|
+
} catch (e) {
|
|
8
|
+
throw err = [e], e;
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
var __export = (target, all) => {
|
|
12
|
+
for (var name in all)
|
|
13
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
// src/server/media/protocol.ts
|
|
17
|
+
function rungsFor(rungs, width, height, maxShort) {
|
|
18
|
+
const even4 = (n) => Math.max(2, Math.round(n / 2) * 2);
|
|
19
|
+
if (maxShort && Math.min(width, height) > maxShort) {
|
|
20
|
+
const k = maxShort / Math.min(width, height);
|
|
21
|
+
width = even4(width * k);
|
|
22
|
+
height = even4(height * k);
|
|
23
|
+
}
|
|
24
|
+
const short = Math.min(width, height);
|
|
25
|
+
const out = [];
|
|
26
|
+
for (const r of rungs) {
|
|
27
|
+
if (r.short >= short * 0.9) break;
|
|
28
|
+
const k = r.short / short;
|
|
29
|
+
out.push({ width: even4(width * k), height: even4(height * k), kbps: r.kbps, peak: r.peak, top: false });
|
|
30
|
+
}
|
|
31
|
+
const top = [...rungs].reverse().find((r) => r.short <= short * 1.1) ?? rungs[0];
|
|
32
|
+
out.push({ width: even4(width), height: even4(height), kbps: top.kbps, peak: top.peak, top: true });
|
|
33
|
+
return out;
|
|
34
|
+
}
|
|
35
|
+
function stepSettings(version, over = {}) {
|
|
36
|
+
return {
|
|
37
|
+
version,
|
|
38
|
+
audio: { kbps: 24, chunkS: 600, ...over.audio },
|
|
39
|
+
vad: { noiseDb: -40, minSilenceS: 0.6, edgeS: 0.3, ...over.vad },
|
|
40
|
+
transcript: { model: "@cf/openai/whisper-large-v3-turbo", ...over.transcript },
|
|
41
|
+
frameCheck: { gapFactor: 1.5, jitter: 0.1, maxErrorLines: 20, ...over.frameCheck },
|
|
42
|
+
...over.storyboard ? { storyboard: { ...over.storyboard } } : {},
|
|
43
|
+
// Only when given: the settings of a product that doesn't use the editor's steps stay what they were.
|
|
44
|
+
...over.prep ? { prep: { ...over.prep } } : {},
|
|
45
|
+
...over.render ? { render: { ...over.render } } : {}
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
function parseSteps(x, allowed = STEPS) {
|
|
49
|
+
if (!Array.isArray(x) || !x.length || x.length > STEPS.length * 2) return null;
|
|
50
|
+
for (const s of x) if (typeof s !== "string" || !allowed.includes(s)) return null;
|
|
51
|
+
const out = ALL_STEPS.filter((s) => x.includes(s));
|
|
52
|
+
if (isEditJob(out) && out.length !== 1) return null;
|
|
53
|
+
return out;
|
|
54
|
+
}
|
|
55
|
+
function segmentNames(seg) {
|
|
56
|
+
for (const s of seg) if (!NAME.test(s)) throw new Error(`bad segment name: ${s}`);
|
|
57
|
+
return seg.join("|");
|
|
58
|
+
}
|
|
59
|
+
function ladderFile(rel, seg) {
|
|
60
|
+
if (rel === "master.m3u8") return "playlist";
|
|
61
|
+
const m = new RegExp(`^v(\\d{1,2})\\/(index\\.m3u8|init(?:_\\d{1,2})?\\.mp4|(?:${segmentNames(seg)})-\\d{5,6}\\.m4s)$`).exec(rel);
|
|
62
|
+
if (!m) return null;
|
|
63
|
+
return m[2] === "index.m3u8" ? "playlist" : m[2].startsWith("init") ? "init" : "segment";
|
|
64
|
+
}
|
|
65
|
+
function outputFile(rel, steps, seg) {
|
|
66
|
+
if (steps.includes("prep")) {
|
|
67
|
+
if (rel === PREP_MEDIA) return "edit-media";
|
|
68
|
+
if (rel === PREP_PEAKS) return "edit-peaks";
|
|
69
|
+
if (rel === PREP_RESULT) return "edit-result";
|
|
70
|
+
if (/^edit\/sb-(?:\d{3}|[1-9]\d{3})\.jpg$/.test(rel)) return "edit-filmstrip";
|
|
71
|
+
}
|
|
72
|
+
if (steps.includes("render")) {
|
|
73
|
+
if (rel === RENDER_VIDEO) return "render-video";
|
|
74
|
+
if (rel === RENDER_POSTER) return "render-poster";
|
|
75
|
+
if (rel === RENDER_RESULT) return "render-result";
|
|
76
|
+
}
|
|
77
|
+
if (steps.includes("ladder")) {
|
|
78
|
+
const k = ladderFile(rel, seg);
|
|
79
|
+
if (k) return k;
|
|
80
|
+
}
|
|
81
|
+
if (steps.includes("poster") && rel === "poster.jpg") return "poster";
|
|
82
|
+
if (steps.includes("storyboard")) {
|
|
83
|
+
if (rel === "storyboard/storyboard.json") return "storyboard-index";
|
|
84
|
+
if (/^storyboard\/sb-\d{3,4}\.(?:jpg|webp)$/.test(rel) || rel === "storyboard/storyboard.vtt") return "storyboard";
|
|
85
|
+
}
|
|
86
|
+
if (steps.includes("faststart") && rel === "faststart.mp4") return "faststart";
|
|
87
|
+
if (wantsAudio(steps)) {
|
|
88
|
+
if (rel === AUDIO_INDEX) return "audio-index";
|
|
89
|
+
if (/^audio\/a-\d{3,4}\.ogg$/.test(rel)) return "audio";
|
|
90
|
+
}
|
|
91
|
+
if (steps.includes("transcript")) {
|
|
92
|
+
if (rel === TRANSCRIPT_VTT) return "transcript";
|
|
93
|
+
if (rel === TRANSCRIPT_JSON) return "transcript-json";
|
|
94
|
+
}
|
|
95
|
+
if (steps.includes("frame-check") && rel === FRAME_CHECK_FILE) return "frame-check";
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
function completionFile(steps) {
|
|
99
|
+
if (steps.includes("prep")) return PREP_RESULT;
|
|
100
|
+
if (steps.includes("render")) return RENDER_RESULT;
|
|
101
|
+
if (steps.includes("ladder")) return "master.m3u8";
|
|
102
|
+
if (steps.includes("faststart")) return "faststart.mp4";
|
|
103
|
+
if (steps.includes("storyboard")) return "storyboard/storyboard.json";
|
|
104
|
+
if (steps.includes("poster") || !hasExtraSteps(steps)) return "poster.jpg";
|
|
105
|
+
if (wantsAudio(steps)) return AUDIO_INDEX;
|
|
106
|
+
return FRAME_CHECK_FILE;
|
|
107
|
+
}
|
|
108
|
+
function prepSettingsOf(ss) {
|
|
109
|
+
const p = { ...DEFAULT_PREP, ...ss?.prep };
|
|
110
|
+
const int2 = (v, d, lo, hi) => Math.min(hi, Math.max(lo, Math.round(num(v, d))));
|
|
111
|
+
return {
|
|
112
|
+
maxShort: int2(p.maxShort, 1080, 144, 4320) & ~1,
|
|
113
|
+
maxSeconds: int2(p.maxSeconds, 4 * 3600, 1, RENDER_LIMITS.seconds),
|
|
114
|
+
preset: /^[a-z]{1,12}$/.test(String(p.preset)) ? String(p.preset) : DEFAULT_PREP.preset,
|
|
115
|
+
crf: int2(p.crf, 22, 0, 51),
|
|
116
|
+
audioKbps: int2(p.audioKbps, 128, 32, 512),
|
|
117
|
+
columns: int2(p.columns, 10, 1, 20),
|
|
118
|
+
rows: int2(p.rows, 10, 1, 20),
|
|
119
|
+
tileLongSide: int2(p.tileLongSide, 320, 32, 1280) & ~1,
|
|
120
|
+
minIntervalS: int2(p.minIntervalS, 2, 1, 3600),
|
|
121
|
+
maxFrames: int2(p.maxFrames, 400, 1, 4e3),
|
|
122
|
+
jpegQ: int2(p.jpegQ, 5, 2, 31),
|
|
123
|
+
peaksRate: int2(p.peaksRate, 100, 1, 1e3),
|
|
124
|
+
processTimeoutS: int2(p.processTimeoutS, 110 * 60, 10, 24 * 3600)
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
function renderSettingsOf(ss) {
|
|
128
|
+
const r = { ...DEFAULT_RENDER, ...ss?.render };
|
|
129
|
+
const int2 = (v, d, lo, hi) => Math.min(hi, Math.max(lo, Math.round(num(v, d))));
|
|
130
|
+
return {
|
|
131
|
+
maxShort: int2(r.maxShort, 1080, 144, 4320) & ~1,
|
|
132
|
+
maxParts: int2(r.maxParts, 200, 1, RENDER_LIMITS.parts),
|
|
133
|
+
maxSources: int2(r.maxSources, 50, 1, RENDER_LIMITS.sources),
|
|
134
|
+
maxSeconds: int2(r.maxSeconds, 4 * 3600, 1, RENDER_LIMITS.seconds),
|
|
135
|
+
minPartMs: int2(r.minPartMs, 50, 1, 6e4),
|
|
136
|
+
preset: /^[a-z]{1,12}$/.test(String(r.preset)) ? String(r.preset) : DEFAULT_RENDER.preset,
|
|
137
|
+
crf: int2(r.crf, 21, 0, 51),
|
|
138
|
+
audioKbps: int2(r.audioKbps, 128, 32, 512),
|
|
139
|
+
posterWidth: int2(r.posterWidth, 1280, 64, 4096) & ~1,
|
|
140
|
+
partTimeoutS: int2(r.partTimeoutS, 90 * 60, 10, 24 * 3600)
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
function prepResultProblem(r, s, job) {
|
|
144
|
+
if (!isObj(r) || r.format !== EDIT_RESULT_FORMAT || typeof r.converted !== "boolean" || typeof r.hasAudio !== "boolean") return "prep.json: bad shape";
|
|
145
|
+
if (r.media !== (r.converted ? PREP_MEDIA : null)) return "prep.json: media";
|
|
146
|
+
if (!isInt(r.durationMs, 1, s.maxSeconds * 1e3 + 6e4) || !isInt(r.width, 2, RENDER_LIMITS.frame) || !isInt(r.height, 2, RENDER_LIMITS.frame) || !isInt(r.bytes, 1, Number.MAX_SAFE_INTEGER)) return "prep.json: bad numbers";
|
|
147
|
+
if (typeof r.fps !== "number" || !(r.fps > 0 && r.fps <= 1e3)) return "prep.json: bad fps";
|
|
148
|
+
if (Math.min(r.width, r.height) > Math.min(s.maxShort, job?.maxShort ?? Infinity)) return "prep.json: over the cap";
|
|
149
|
+
if (job && !r.converted && r.bytes !== job.bytes) return "prep.json: not the source's size";
|
|
150
|
+
const f = r.filmstrip;
|
|
151
|
+
if (!isObj(f) || f.columns !== s.columns || f.rows !== s.rows || !isInt(f.count, 1, s.maxFrames) || !isInt(f.interval, 1, 1e6) || !isInt(f.tileWidth, 1, 4096) || !isInt(f.tileHeight, 1, 4096) || !Array.isArray(f.sheets))
|
|
152
|
+
return "prep.json: bad filmstrip";
|
|
153
|
+
if (f.sheets.length !== Math.ceil(f.count / (s.columns * s.rows)) || f.sheets.some((x, i) => x !== prepSheetName(i))) return "prep.json: bad sheets";
|
|
154
|
+
if (r.peaks !== null) {
|
|
155
|
+
const p = r.peaks;
|
|
156
|
+
if (!isObj(p) || p.file !== PREP_PEAKS || p.rate !== s.peaksRate || !isInt(p.buckets, 1, peaksBytes(s.maxSeconds + 60, s.peaksRate) / 2)) return "prep.json: bad peaks";
|
|
157
|
+
if (!r.hasAudio) return "prep.json: peaks without sound";
|
|
158
|
+
} else if (r.hasAudio) return "prep.json: sound without peaks";
|
|
159
|
+
return null;
|
|
160
|
+
}
|
|
161
|
+
function parseRect(v, maxW, maxH) {
|
|
162
|
+
if (!isObj(v) || !onlyKeys(v, ["x", "y", "width", "height"])) return "bad crop";
|
|
163
|
+
if (!isInt(v.x, 0, maxW) || !isInt(v.y, 0, maxH) || !isInt(v.width, 2, maxW) || !isInt(v.height, 2, maxH)) return "bad crop";
|
|
164
|
+
if (v.x + v.width > maxW || v.y + v.height > maxH) return "crop outside the frame";
|
|
165
|
+
return { x: v.x, y: v.y, width: v.width, height: v.height };
|
|
166
|
+
}
|
|
167
|
+
function parseRenderJob(x, s = DEFAULT_RENDER) {
|
|
168
|
+
const bad = (error) => ({ error });
|
|
169
|
+
if (!isObj(x) || !onlyKeys(x, ["sources", "editList"])) return bad("bad render");
|
|
170
|
+
const src = x.sources;
|
|
171
|
+
if (!Array.isArray(src) || !src.length || src.length > Math.min(s.maxSources, RENDER_LIMITS.sources)) return bad("bad sources");
|
|
172
|
+
const sources = [];
|
|
173
|
+
for (const r of src) {
|
|
174
|
+
if (!isObj(r) || !onlyKeys(r, ["src", "etag", "bytes"]) || typeof r.src !== "string" || !RENDER_KEY.test(r.src)) return bad("bad source");
|
|
175
|
+
if (typeof r.etag !== "string" || !/^[\x21-\x7e]{1,80}$/.test(r.etag) || !isInt(r.bytes, 1, Number.MAX_SAFE_INTEGER)) return bad("bad source");
|
|
176
|
+
sources.push({ src: r.src, etag: r.etag, bytes: r.bytes });
|
|
177
|
+
}
|
|
178
|
+
const el = x.editList;
|
|
179
|
+
if (!isObj(el) || !onlyKeys(el, ["version", "output", "parts"])) return bad("bad editList");
|
|
180
|
+
if (el.version !== EDIT_LIST_VERSION) return bad("unknown editList version");
|
|
181
|
+
const o = el.output;
|
|
182
|
+
if (!isObj(o) || !onlyKeys(o, ["width", "height", "fps", "crop", "maxShort"])) return bad("bad output");
|
|
183
|
+
if (!isInt(o.width, 16, RENDER_LIMITS.frame) || !isInt(o.height, 16, RENDER_LIMITS.frame)) return bad("bad output frame");
|
|
184
|
+
if (typeof o.fps !== "number" || !(o.fps >= 1 && o.fps <= RENDER_LIMITS.fps) || Math.round(o.fps * 1e3) !== o.fps * 1e3) return bad("bad fps");
|
|
185
|
+
let crop;
|
|
186
|
+
if (o.crop !== void 0) {
|
|
187
|
+
const c = parseRect(o.crop, o.width, o.height);
|
|
188
|
+
if (typeof c === "string") return bad(`output: ${c}`);
|
|
189
|
+
if (c.width < 16 || c.height < 16) return bad("output: crop too small");
|
|
190
|
+
crop = c;
|
|
191
|
+
}
|
|
192
|
+
if (o.maxShort !== void 0 && !isInt(o.maxShort, 144, 4320)) return bad("bad maxShort");
|
|
193
|
+
const p = el.parts;
|
|
194
|
+
if (!Array.isArray(p) || !p.length || p.length > Math.min(s.maxParts, RENDER_LIMITS.parts)) return bad("bad parts");
|
|
195
|
+
const parts = [];
|
|
196
|
+
const used = /* @__PURE__ */ new Set();
|
|
197
|
+
let total = 0;
|
|
198
|
+
for (let i = 0; i < p.length; i++) {
|
|
199
|
+
const q = p[i];
|
|
200
|
+
if (!isObj(q) || !onlyKeys(q, ["source", "inMs", "outMs", "muted", "crop"])) return bad(`part ${i}: bad shape`);
|
|
201
|
+
if (!isInt(q.source, 0, sources.length - 1)) return bad(`part ${i}: bad source`);
|
|
202
|
+
const maxMs = RENDER_LIMITS.seconds * 1e3;
|
|
203
|
+
if (!isInt(q.inMs, 0, maxMs) || !isInt(q.outMs, 1, maxMs) || q.outMs - q.inMs < Math.max(s.minPartMs, Math.ceil(1e3 / o.fps))) return bad(`part ${i}: bad times`);
|
|
204
|
+
if (q.muted !== void 0 && q.muted !== true) return bad(`part ${i}: bad muted`);
|
|
205
|
+
let pc;
|
|
206
|
+
if (q.crop !== void 0) {
|
|
207
|
+
const c = parseRect(q.crop, RENDER_LIMITS.frame, RENDER_LIMITS.frame);
|
|
208
|
+
if (typeof c === "string") return bad(`part ${i}: ${c}`);
|
|
209
|
+
pc = c;
|
|
210
|
+
}
|
|
211
|
+
used.add(q.source);
|
|
212
|
+
total += q.outMs - q.inMs;
|
|
213
|
+
parts.push({ source: q.source, inMs: q.inMs, outMs: q.outMs, ...q.muted ? { muted: true } : {}, ...pc ? { crop: pc } : {} });
|
|
214
|
+
}
|
|
215
|
+
if (used.size !== sources.length) return bad("a source no part uses");
|
|
216
|
+
if (total > s.maxSeconds * 1e3) return bad("too long");
|
|
217
|
+
const output = { width: o.width, height: o.height, fps: o.fps, ...crop ? { crop } : {}, ...o.maxShort !== void 0 ? { maxShort: o.maxShort } : {} };
|
|
218
|
+
return { job: { sources, editList: { version: EDIT_LIST_VERSION, output, parts } } };
|
|
219
|
+
}
|
|
220
|
+
function renderFrames(e) {
|
|
221
|
+
let t = 0;
|
|
222
|
+
const fps = e.output.fps;
|
|
223
|
+
return e.parts.map((p) => {
|
|
224
|
+
const a = Math.ceil(t * fps / 1e3 - 1e-9);
|
|
225
|
+
t += p.outMs - p.inMs;
|
|
226
|
+
return Math.max(1, Math.ceil(t * fps / 1e3 - 1e-9) - a);
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
function renderSamples(e) {
|
|
230
|
+
let t = 0;
|
|
231
|
+
return e.parts.map((p) => {
|
|
232
|
+
const a = Math.round(t * RENDER_SAMPLE_RATE / 1e3);
|
|
233
|
+
t += p.outMs - p.inMs;
|
|
234
|
+
return Math.round(t * RENDER_SAMPLE_RATE / 1e3) - a;
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
function renderFrame(e, s = DEFAULT_RENDER) {
|
|
238
|
+
const even4 = (v) => Math.max(2, Math.floor(v / 2) * 2);
|
|
239
|
+
const w = e.output.crop ? e.output.crop.width : e.output.width;
|
|
240
|
+
const h = e.output.crop ? e.output.crop.height : e.output.height;
|
|
241
|
+
const cap = Math.min(s.maxShort, e.output.maxShort ?? Infinity);
|
|
242
|
+
const k = Math.min(1, cap / Math.min(w, h));
|
|
243
|
+
return { width: even4(w * k), height: even4(h * k) };
|
|
244
|
+
}
|
|
245
|
+
function renderResultProblem(r, e, s) {
|
|
246
|
+
if (!isObj(r) || r.format !== EDIT_RESULT_FORMAT) return "result.json: bad shape";
|
|
247
|
+
const frames = renderFrames(e).reduce((a, b) => a + b, 0);
|
|
248
|
+
const samples = renderSamples(e).reduce((a, b) => a + b, 0);
|
|
249
|
+
const size = renderFrame(e, s);
|
|
250
|
+
if (r.frames !== frames || r.samples !== samples || r.fps !== e.output.fps || r.parts !== e.parts.length) return "result.json: not this edit list";
|
|
251
|
+
if (r.width !== size.width || r.height !== size.height) return "result.json: not this frame";
|
|
252
|
+
if (!isInt(r.durationMs, 1, RENDER_LIMITS.seconds * 1e3) || Math.abs(r.durationMs - editListMs(e)) > Math.ceil(1e3 / e.output.fps)) return "result.json: not within one frame of the edit list";
|
|
253
|
+
if (!isInt(r.bytes, 1, Number.MAX_SAFE_INTEGER)) return "result.json: bad bytes";
|
|
254
|
+
return null;
|
|
255
|
+
}
|
|
256
|
+
var keyframeExpr, STEPS, EDIT_STEPS, ALL_STEPS, EXTRA_STEPS, hasExtraSteps, isEditJob, wantsAudio, DEFAULT_STEP_SETTINGS, storyboardV2, jobVersion, AUDIO_INDEX, TRANSCRIPT_VTT, TRANSCRIPT_JSON, FRAME_CHECK_FILE, NAME, MAX_EXPECT_DURATION_S, validExpectDuration, EXPECT_HEADER, VERSION_HEADER, PREP_MEDIA, PREP_PEAKS, PREP_RESULT, prepSheetName, RENDER_VIDEO, RENDER_POSTER, RENDER_RESULT, EDIT_RESULT_FORMAT, DEFAULT_PREP, DEFAULT_RENDER, RENDER_LIMITS, MAX_RENDER_BODY, num, filmstripInterval, peaksBytes, isInt, onlyKeys, isObj, EDIT_LIST_VERSION, RENDER_KEY, editListMs, RENDER_SAMPLE_RATE;
|
|
257
|
+
var init_protocol = __esm({
|
|
258
|
+
"src/server/media/protocol.ts"() {
|
|
259
|
+
"use strict";
|
|
260
|
+
keyframeExpr = (first, every) => `expr:gte(t,if(lt(n_forced,2),n_forced*${first},(n_forced-1)*${every}))`;
|
|
261
|
+
STEPS = ["ladder", "poster", "storyboard", "faststart", "audio", "transcript", "frame-check"];
|
|
262
|
+
EDIT_STEPS = ["prep", "render"];
|
|
263
|
+
ALL_STEPS = [...STEPS, ...EDIT_STEPS];
|
|
264
|
+
EXTRA_STEPS = ["audio", "transcript", "frame-check"];
|
|
265
|
+
hasExtraSteps = (steps) => steps.some((s) => EXTRA_STEPS.includes(s) || EDIT_STEPS.includes(s));
|
|
266
|
+
isEditJob = (steps) => steps.some((s) => EDIT_STEPS.includes(s));
|
|
267
|
+
wantsAudio = (steps) => steps.includes("audio") || steps.includes("transcript");
|
|
268
|
+
DEFAULT_STEP_SETTINGS = stepSettings("steps/1");
|
|
269
|
+
storyboardV2 = (steps, settings) => steps.includes("storyboard") && !!settings?.storyboard;
|
|
270
|
+
jobVersion = (ladderVersion, steps, settings = DEFAULT_STEP_SETTINGS) => {
|
|
271
|
+
const s = typeof settings === "string" ? { version: settings } : settings;
|
|
272
|
+
return hasExtraSteps(steps) || storyboardV2(steps, s) ? `${ladderVersion}+${s.version}` : ladderVersion;
|
|
273
|
+
};
|
|
274
|
+
AUDIO_INDEX = "audio/audio.json";
|
|
275
|
+
TRANSCRIPT_VTT = "transcript.vtt";
|
|
276
|
+
TRANSCRIPT_JSON = "transcript.json";
|
|
277
|
+
FRAME_CHECK_FILE = "frame-check.json";
|
|
278
|
+
NAME = /^[a-z0-9]{1,8}$/;
|
|
279
|
+
MAX_EXPECT_DURATION_S = 7 * 24 * 3600;
|
|
280
|
+
validExpectDuration = (x) => typeof x === "number" && Number.isFinite(x) && x > 0 && x <= MAX_EXPECT_DURATION_S;
|
|
281
|
+
EXPECT_HEADER = "x-media-expect-duration";
|
|
282
|
+
VERSION_HEADER = "x-media-version";
|
|
283
|
+
PREP_MEDIA = "edit/media.mp4";
|
|
284
|
+
PREP_PEAKS = "edit/peaks.bin";
|
|
285
|
+
PREP_RESULT = "edit/prep.json";
|
|
286
|
+
prepSheetName = (i) => `edit/sb-${String(i).padStart(3, "0")}.jpg`;
|
|
287
|
+
RENDER_VIDEO = "render/video.mp4";
|
|
288
|
+
RENDER_POSTER = "render/poster.jpg";
|
|
289
|
+
RENDER_RESULT = "render/result.json";
|
|
290
|
+
EDIT_RESULT_FORMAT = 1;
|
|
291
|
+
DEFAULT_PREP = {
|
|
292
|
+
maxShort: 1080,
|
|
293
|
+
maxSeconds: 4 * 3600,
|
|
294
|
+
preset: "veryfast",
|
|
295
|
+
crf: 22,
|
|
296
|
+
audioKbps: 128,
|
|
297
|
+
columns: 10,
|
|
298
|
+
rows: 10,
|
|
299
|
+
tileLongSide: 320,
|
|
300
|
+
minIntervalS: 2,
|
|
301
|
+
maxFrames: 400,
|
|
302
|
+
jpegQ: 5,
|
|
303
|
+
peaksRate: 100,
|
|
304
|
+
processTimeoutS: 110 * 60
|
|
305
|
+
};
|
|
306
|
+
DEFAULT_RENDER = {
|
|
307
|
+
maxShort: 1080,
|
|
308
|
+
maxParts: 200,
|
|
309
|
+
maxSources: 50,
|
|
310
|
+
maxSeconds: 4 * 3600,
|
|
311
|
+
minPartMs: 50,
|
|
312
|
+
preset: "veryfast",
|
|
313
|
+
crf: 21,
|
|
314
|
+
audioKbps: 128,
|
|
315
|
+
posterWidth: 1280,
|
|
316
|
+
partTimeoutS: 90 * 60
|
|
317
|
+
};
|
|
318
|
+
RENDER_LIMITS = { parts: 500, sources: 100, seconds: 24 * 3600, frame: 8192, fps: 120 };
|
|
319
|
+
MAX_RENDER_BODY = 64 * 1024;
|
|
320
|
+
num = (v, d) => typeof v === "number" && Number.isFinite(v) ? v : d;
|
|
321
|
+
filmstripInterval = (seconds, s) => Math.max(s.minIntervalS, Math.ceil(Math.max(0, seconds) / s.maxFrames));
|
|
322
|
+
peaksBytes = (seconds, rate) => 2 * Math.ceil(Math.max(0, seconds) * rate);
|
|
323
|
+
isInt = (v, lo, hi) => typeof v === "number" && Number.isSafeInteger(v) && v >= lo && v <= hi;
|
|
324
|
+
onlyKeys = (o, keys) => Object.keys(o).every((k) => keys.includes(k));
|
|
325
|
+
isObj = (v) => !!v && typeof v === "object" && !Array.isArray(v);
|
|
326
|
+
EDIT_LIST_VERSION = 1;
|
|
327
|
+
RENDER_KEY = /^(?!\/)(?!.*(?:^|\/)\.\.(?:\/|$))[\x21-\x7e]{1,1024}$/;
|
|
328
|
+
editListMs = (e) => e.parts.reduce((n, p) => n + p.outMs - p.inMs, 0);
|
|
329
|
+
RENDER_SAMPLE_RATE = 48e3;
|
|
330
|
+
}
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
// src/server/media/transcript.ts
|
|
334
|
+
var init_transcript = __esm({
|
|
335
|
+
"src/server/media/transcript.ts"() {
|
|
336
|
+
"use strict";
|
|
337
|
+
}
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
// src/server/media/frameCheck.ts
|
|
341
|
+
var init_frameCheck = __esm({
|
|
342
|
+
"src/server/media/frameCheck.ts"() {
|
|
343
|
+
"use strict";
|
|
344
|
+
}
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
// src/server/media/storyboard.ts
|
|
348
|
+
function storyboardSettings(version, over = {}) {
|
|
349
|
+
const s = {
|
|
350
|
+
version,
|
|
351
|
+
intervalS: 2,
|
|
352
|
+
maxFrames: 200,
|
|
353
|
+
size: 160,
|
|
354
|
+
cols: 5,
|
|
355
|
+
rows: 5,
|
|
356
|
+
quality: 60,
|
|
357
|
+
minQuality: 20,
|
|
358
|
+
maxSheetBytes: 1e5,
|
|
359
|
+
maxBytes: 1e6,
|
|
360
|
+
...over
|
|
361
|
+
};
|
|
362
|
+
checkSettings(s);
|
|
363
|
+
return s;
|
|
364
|
+
}
|
|
365
|
+
function int(n, lo, hi) {
|
|
366
|
+
return typeof n === "number" && Number.isInteger(n) && n >= lo && n <= hi;
|
|
367
|
+
}
|
|
368
|
+
function checkSettings(s) {
|
|
369
|
+
const bad = (what) => {
|
|
370
|
+
throw new Error(`storyboard settings: bad ${what}`);
|
|
371
|
+
};
|
|
372
|
+
if (typeof s.version !== "string" || !s.version) bad("version");
|
|
373
|
+
if (!(typeof s.intervalS === "number" && s.intervalS >= 0.1 && s.intervalS <= 3600)) bad("intervalS");
|
|
374
|
+
if (!int(s.maxFrames, 1, 1e4)) bad("maxFrames");
|
|
375
|
+
if (!int(s.size, 16, 1024) || s.size % 2) bad("size");
|
|
376
|
+
if (!int(s.cols, 1, 32) || !int(s.rows, 1, 32)) bad("cols/rows");
|
|
377
|
+
if (!int(s.quality, 1, 100) || !int(s.minQuality, 1, 100) || s.minQuality > s.quality) bad("quality/minQuality");
|
|
378
|
+
if (!int(s.maxSheetBytes, 1e3, 1e8) || !int(s.maxBytes, s.maxSheetBytes, 1e9))
|
|
379
|
+
bad("maxSheetBytes/maxBytes");
|
|
380
|
+
}
|
|
381
|
+
function planStoryboard(picture, s = DEFAULT_STORYBOARD_SETTINGS) {
|
|
382
|
+
const w = picture.width > 0 ? picture.width : 16;
|
|
383
|
+
const h = picture.height > 0 ? picture.height : 9;
|
|
384
|
+
const seconds = Number.isFinite(picture.seconds) && picture.seconds > 0 ? picture.seconds : 0;
|
|
385
|
+
const k = s.size / Math.max(w, h);
|
|
386
|
+
const interval = Math.ceil(Math.max(s.intervalS, seconds / s.maxFrames) * 1e3) / 1e3;
|
|
387
|
+
const expected = seconds ? Math.min(s.maxFrames, Math.max(1, Math.ceil(seconds / interval - 1e-9))) : 1;
|
|
388
|
+
return {
|
|
389
|
+
interval,
|
|
390
|
+
maxFrames: s.maxFrames,
|
|
391
|
+
expected,
|
|
392
|
+
width: even(w * k),
|
|
393
|
+
height: even(h * k),
|
|
394
|
+
cols: s.cols,
|
|
395
|
+
rows: s.rows,
|
|
396
|
+
seconds
|
|
397
|
+
};
|
|
398
|
+
}
|
|
399
|
+
function storyboardCues(plan, count) {
|
|
400
|
+
const per = plan.cols * plan.rows;
|
|
401
|
+
const out = [];
|
|
402
|
+
for (let i = 0; i < count; i++) {
|
|
403
|
+
const k = i % per;
|
|
404
|
+
const start = round3(i * plan.interval);
|
|
405
|
+
const next = round3((i + 1) * plan.interval);
|
|
406
|
+
const end = i === count - 1 ? Math.max(next, round3(plan.seconds)) : next;
|
|
407
|
+
out.push({
|
|
408
|
+
start,
|
|
409
|
+
end,
|
|
410
|
+
sheet: Math.floor(i / per),
|
|
411
|
+
x: k % plan.cols * plan.width,
|
|
412
|
+
y: Math.floor(k / plan.cols) * plan.height,
|
|
413
|
+
w: plan.width,
|
|
414
|
+
h: plan.height
|
|
415
|
+
});
|
|
416
|
+
}
|
|
417
|
+
return out;
|
|
418
|
+
}
|
|
419
|
+
function round3(n) {
|
|
420
|
+
return Math.round(n * 1e3) / 1e3;
|
|
421
|
+
}
|
|
422
|
+
function cueTime(s) {
|
|
423
|
+
const ms = Math.round(s * 1e3);
|
|
424
|
+
const h = Math.floor(ms / 36e5);
|
|
425
|
+
const m = Math.floor(ms / 6e4) % 60;
|
|
426
|
+
const sec = Math.floor(ms / 1e3) % 60;
|
|
427
|
+
const p = (n, w = 2) => String(n).padStart(w, "0");
|
|
428
|
+
return `${p(h)}:${p(m)}:${p(sec)}.${p(ms % 1e3, 3)}`;
|
|
429
|
+
}
|
|
430
|
+
function storyboardVtt(plan, count) {
|
|
431
|
+
const lines = ["WEBVTT", ""];
|
|
432
|
+
for (const c of storyboardCues(plan, count)) {
|
|
433
|
+
lines.push(
|
|
434
|
+
`${cueTime(c.start)} --> ${cueTime(c.end)}`,
|
|
435
|
+
`${storyboardSheetName(c.sheet).slice(STORYBOARD_DIR.length + 1)}#xywh=${c.x},${c.y},${c.w},${c.h}`,
|
|
436
|
+
""
|
|
437
|
+
);
|
|
438
|
+
}
|
|
439
|
+
return lines.join("\n");
|
|
440
|
+
}
|
|
441
|
+
function storyboardIndex(plan, count, version, bytes) {
|
|
442
|
+
const n = sheetCount(plan, count);
|
|
443
|
+
return {
|
|
444
|
+
version,
|
|
445
|
+
interval: plan.interval,
|
|
446
|
+
cols: plan.cols,
|
|
447
|
+
rows: plan.rows,
|
|
448
|
+
width: plan.width,
|
|
449
|
+
height: plan.height,
|
|
450
|
+
count,
|
|
451
|
+
seconds: round3(plan.seconds),
|
|
452
|
+
sheets: Array.from({ length: n }, (_, i) => storyboardSheetName(i)),
|
|
453
|
+
vtt: STORYBOARD_VTT,
|
|
454
|
+
bytes
|
|
455
|
+
};
|
|
456
|
+
}
|
|
457
|
+
var DEFAULT_STORYBOARD_SETTINGS, even, STORYBOARD_DIR, STORYBOARD_VTT, STORYBOARD_INDEX, storyboardSheetName, sheetCount;
|
|
458
|
+
var init_storyboard = __esm({
|
|
459
|
+
"src/server/media/storyboard.ts"() {
|
|
460
|
+
"use strict";
|
|
461
|
+
DEFAULT_STORYBOARD_SETTINGS = storyboardSettings("storyboard/1");
|
|
462
|
+
even = (n) => Math.max(2, Math.round(n / 2) * 2);
|
|
463
|
+
STORYBOARD_DIR = "storyboard";
|
|
464
|
+
STORYBOARD_VTT = "storyboard/storyboard.vtt";
|
|
465
|
+
STORYBOARD_INDEX = "storyboard/storyboard.json";
|
|
466
|
+
storyboardSheetName = (i) => `storyboard/sb-${String(i + 1).padStart(3, "0")}.webp`;
|
|
467
|
+
sheetCount = (plan, count) => Math.ceil(count / (plan.cols * plan.rows));
|
|
468
|
+
}
|
|
469
|
+
});
|
|
470
|
+
|
|
471
|
+
// src/server/media/index.ts
|
|
472
|
+
var init_media = __esm({
|
|
473
|
+
"src/server/media/index.ts"() {
|
|
474
|
+
"use strict";
|
|
475
|
+
init_protocol();
|
|
476
|
+
init_transcript();
|
|
477
|
+
init_frameCheck();
|
|
478
|
+
init_storyboard();
|
|
479
|
+
}
|
|
480
|
+
});
|
|
481
|
+
|
|
482
|
+
// ../media-worker/build/out/stage/src/runner/ffmpeg.ts
|
|
483
|
+
function preChain(p) {
|
|
484
|
+
const f = [];
|
|
485
|
+
if (p.interlaced) f.push("bwdif=mode=send_frame:parity=auto:deint=all");
|
|
486
|
+
if (isWideSdr(p)) f.push("scale=in_color_matrix=bt2020:out_color_matrix=bt709");
|
|
487
|
+
if (isHdr(p)) {
|
|
488
|
+
f.push(
|
|
489
|
+
`zscale=t=linear:npl=203:tin=${p.transfer}:pin=${p.primaries || "bt2020"}:min=${p.matrix || "bt2020nc"}`,
|
|
490
|
+
"format=gbrpf32le",
|
|
491
|
+
"zscale=p=bt709",
|
|
492
|
+
"tonemap=tonemap=mobius:param=0.8:desat=0",
|
|
493
|
+
"zscale=t=bt709:m=bt709:r=tv",
|
|
494
|
+
"format=yuv420p"
|
|
495
|
+
);
|
|
496
|
+
}
|
|
497
|
+
return f.join(",");
|
|
498
|
+
}
|
|
499
|
+
function shown(p) {
|
|
500
|
+
const d = displayed(p);
|
|
501
|
+
if (!p.sar || p.sar === 1) return d;
|
|
502
|
+
const even4 = (n) => Math.max(2, Math.round(n / 2) * 2);
|
|
503
|
+
const r = (p.rotation % 360 + 360) % 360;
|
|
504
|
+
return r === 90 || r === 270 ? { width: d.width, height: even4(d.height * p.sar) } : { width: even4(d.width * p.sar), height: d.height };
|
|
505
|
+
}
|
|
506
|
+
function copyCandidate(p, l) {
|
|
507
|
+
if (p.videoCodec !== "h264" || !/^yuvj?420p$/.test(p.pixFmt) || p.rotation % 360 !== 0) return false;
|
|
508
|
+
if (p.interlaced || isHdr(p) || p.vfr || p.still) return false;
|
|
509
|
+
if (isWideSdr(p)) return false;
|
|
510
|
+
if (!p.bitrate || p.bitrate > l.copyMaxBps) return false;
|
|
511
|
+
return p.duration > 0;
|
|
512
|
+
}
|
|
513
|
+
function canCopyTop(p, l) {
|
|
514
|
+
if (!copyCandidate(p, l)) return false;
|
|
515
|
+
if (p.editList || p.packetErrors) return false;
|
|
516
|
+
if (p.keyframes.length < 1 || p.keyframes[0] > 0.5) return false;
|
|
517
|
+
const first = l.firstSegmentS ?? 1;
|
|
518
|
+
if (!p.keyframes.some((k) => k - p.keyframes[0] >= first && k < p.duration)) return false;
|
|
519
|
+
for (let i = 1; i < p.keyframes.length; i++) if (p.keyframes[i] - p.keyframes[i - 1] > l.copyMaxGopS) return false;
|
|
520
|
+
if (p.duration - p.keyframes[p.keyframes.length - 1] > l.copyMaxGopS) return false;
|
|
521
|
+
return true;
|
|
522
|
+
}
|
|
523
|
+
function displayed(p) {
|
|
524
|
+
const r = (p.rotation % 360 + 360) % 360;
|
|
525
|
+
return r === 90 || r === 270 ? { width: p.height, height: p.width } : { width: p.width, height: p.height };
|
|
526
|
+
}
|
|
527
|
+
function needsKeyframes(p, l, steps, maxShort) {
|
|
528
|
+
if (!steps.includes("ladder")) return false;
|
|
529
|
+
const d = displayed(p);
|
|
530
|
+
if (maxShort && Math.min(d.width, d.height) > maxShort) return false;
|
|
531
|
+
return copyCandidate(p, l);
|
|
532
|
+
}
|
|
533
|
+
function storyboardPlan(p) {
|
|
534
|
+
const d = shown(p);
|
|
535
|
+
const width = 160;
|
|
536
|
+
const height = Math.max(2, Math.round(width * (d.height || 9) / (d.width || 16) / 2) * 2);
|
|
537
|
+
const interval = Math.max(1, Math.ceil((pictureSeconds(p) || 1) / 100));
|
|
538
|
+
return { interval, cols: 10, rows: 10, width, height };
|
|
539
|
+
}
|
|
540
|
+
function sheetArgs(thumbs, out, plan) {
|
|
541
|
+
return ["-hide_banner", "-nostdin", "-y", "-loglevel", "error", "-framerate", "1", "-i", `${thumbs}/t-%05d.jpg`, "-vf", `tile=${plan.cols}x${plan.rows}`, "-q:v", "5", `${out}/storyboard/sb-%03d.jpg`];
|
|
542
|
+
}
|
|
543
|
+
function jobArgs(input, out, p, o) {
|
|
544
|
+
const l = o.ladder;
|
|
545
|
+
const steps = o.steps ?? ["ladder"];
|
|
546
|
+
const maxShort = o.maxShort;
|
|
547
|
+
const d = displayed(p);
|
|
548
|
+
const ladder = steps.includes("ladder");
|
|
549
|
+
const rungs = ladder ? rungsFor(l.rungs, d.width, d.height, maxShort) : [];
|
|
550
|
+
const copy = ladder && canCopyTop(p, l) && !(maxShort && Math.min(d.width, d.height) > maxShort);
|
|
551
|
+
const hdr = isHdr(p);
|
|
552
|
+
const kf = copy ? p.keyframes.map((t) => t.toFixed(3)).join(",") : keyframeExpr(l.firstSegmentS, l.keyframeS);
|
|
553
|
+
const encoded = rungs.filter((r) => !(r.top && copy));
|
|
554
|
+
const args = ["-hide_banner", "-nostdin", "-y", "-loglevel", "error", ...o.inputArgs ?? [], "-i", input];
|
|
555
|
+
if (o.inputArgs) args.splice(args.indexOf("-nostdin"), 1);
|
|
556
|
+
const fillEnd = p.sparseRate && p.lastFrameAt !== void 0 ? Math.max(p.lastFrameAt + 1 / p.sparseRate, (p.videoStart ?? 0) + (p.videoDuration ?? 0)) - (p.fileStart ?? 0) : 0;
|
|
557
|
+
const fill = p.sparseRate ? `fps=fps=${p.sparseRate}:eof_action=pass,${fillEnd > 0 ? `trim=end=${fillEnd.toFixed(3)},` : ""}` : "";
|
|
558
|
+
const branches = encoded.map((r, i) => `[s${i}]${fill}scale=${r.width}:${r.height}:flags=bicubic,format=yuv420p[v${i}]`);
|
|
559
|
+
if (steps.includes("poster")) {
|
|
560
|
+
const sq = shown(p);
|
|
561
|
+
const top = rungsFor(l.rungs, sq.width, sq.height, Math.min(maxShort ?? 1080, 1080)).at(-1);
|
|
562
|
+
const at = pictureSeconds(p) >= 2 ? 1 : 0;
|
|
563
|
+
branches.push(`[s${branches.length}]trim=start=${at},setpts=PTS-STARTPTS,scale=${top.width}:${top.height}:flags=bicubic,${p.sar && p.sar !== 1 ? "setsar=1," : ""}format=yuvj420p[poster]`);
|
|
564
|
+
}
|
|
565
|
+
let sb = null;
|
|
566
|
+
if (steps.includes("storyboard") && o.storyboard) branches.push(o.storyboard.branch(`[s${branches.length}]`));
|
|
567
|
+
else if (steps.includes("storyboard")) {
|
|
568
|
+
sb = storyboardPlan(p);
|
|
569
|
+
const pick = p.sparseRate && pictureSeconds(p) >= 2 * sb.interval ? `fps=fps=1/${sb.interval}:start_time=0:round=up` : `select='isnan(prev_selected_t)+gte(t-prev_selected_t\\,${sb.interval})'`;
|
|
570
|
+
branches.push(`[s${branches.length}]${pick},scale=${sb.width}:${sb.height}:flags=bicubic,format=yuvj420p[sb]`);
|
|
571
|
+
}
|
|
572
|
+
if (o.stepArgs) branches.push(...o.stepArgs.videoBranches(branches.length));
|
|
573
|
+
const chains = [];
|
|
574
|
+
const pre = preChain(p);
|
|
575
|
+
if (branches.length) chains.push(`[0:v:${p.video}]${pre ? `${pre},` : ""}split=${branches.length}${branches.map((_, i) => `[s${i}]`).join("")}`, ...branches);
|
|
576
|
+
chains.push(...o.stepArgs?.chains ?? []);
|
|
577
|
+
if (chains.length) args.push("-filter_complex", chains.join(";"));
|
|
578
|
+
if (ladder) {
|
|
579
|
+
let e = 0;
|
|
580
|
+
const vopts = [];
|
|
581
|
+
rungs.forEach((r, i) => {
|
|
582
|
+
if (r.top && copy) {
|
|
583
|
+
args.push("-map", `0:v:${p.video}`);
|
|
584
|
+
vopts.push(`-c:v:${i}`, "copy");
|
|
585
|
+
} else {
|
|
586
|
+
args.push("-map", `[v${e++}]`);
|
|
587
|
+
vopts.push(
|
|
588
|
+
`-c:v:${i}`,
|
|
589
|
+
"libx264",
|
|
590
|
+
`-preset:v:${i}`,
|
|
591
|
+
maxShort ? l.cappedPreset : l.preset,
|
|
592
|
+
`-profile:v:${i}`,
|
|
593
|
+
"high",
|
|
594
|
+
`-b:v:${i}`,
|
|
595
|
+
`${r.kbps}k`,
|
|
596
|
+
`-maxrate:v:${i}`,
|
|
597
|
+
`${r.peak}k`,
|
|
598
|
+
`-bufsize:v:${i}`,
|
|
599
|
+
`${r.kbps * 2}k`,
|
|
600
|
+
`-g:v:${i}`,
|
|
601
|
+
"100000",
|
|
602
|
+
`-keyint_min:v:${i}`,
|
|
603
|
+
"1",
|
|
604
|
+
`-sc_threshold:v:${i}`,
|
|
605
|
+
"0",
|
|
606
|
+
`-force_key_frames:v:${i}`,
|
|
607
|
+
kf,
|
|
608
|
+
...hdr ? [`-color_primaries:v:${i}`, "bt709", `-color_trc:v:${i}`, "bt709", `-colorspace:v:${i}`, "bt709"] : [],
|
|
609
|
+
...isWideSdr(p) ? [`-colorspace:v:${i}`, "bt709"] : []
|
|
610
|
+
);
|
|
611
|
+
}
|
|
612
|
+
if (p.hasAudio) args.push("-map", audioIn(p));
|
|
613
|
+
});
|
|
614
|
+
args.push(...vopts);
|
|
615
|
+
if (p.hasAudio) args.push("-c:a", "aac", "-b:a", `${l.audioKbps}k`, "-ac", "2");
|
|
616
|
+
const fit = p.hasAudio ? audioFitTo(p) : 0;
|
|
617
|
+
if (fit) args.push("-af", `apad=whole_dur=${fit.toFixed(3)},atrim=duration=${fit.toFixed(3)}`);
|
|
618
|
+
args.push(
|
|
619
|
+
// Variable frame rate (phones) keeps its own timing: nothing invented or dropped. Except two frames at the same
|
|
620
|
+
// time (broken timestamps): only one of them can be shown, and a rung that has both shows nothing after a seek in
|
|
621
|
+
// WebKit. "vfr" drops just those, for a VFR source (a constant-rate one has none: its command is unchanged).
|
|
622
|
+
"-fps_mode",
|
|
623
|
+
p.vfr ? "vfr" : "passthrough",
|
|
624
|
+
"-max_muxing_queue_size",
|
|
625
|
+
"4096",
|
|
626
|
+
"-f",
|
|
627
|
+
"hls",
|
|
628
|
+
// Short first segments: the first frame needs ~1 s of video, not 4.
|
|
629
|
+
"-hls_init_time",
|
|
630
|
+
String(l.firstSegmentS),
|
|
631
|
+
"-hls_time",
|
|
632
|
+
String(l.segmentS),
|
|
633
|
+
"-hls_playlist_type",
|
|
634
|
+
"vod",
|
|
635
|
+
"-hls_segment_type",
|
|
636
|
+
"fmp4",
|
|
637
|
+
"-hls_flags",
|
|
638
|
+
"independent_segments",
|
|
639
|
+
"-hls_fmp4_init_filename",
|
|
640
|
+
"init.mp4",
|
|
641
|
+
"-hls_segment_filename",
|
|
642
|
+
`${out}/v%v/${l.segmentName}-%05d.m4s`,
|
|
643
|
+
"-master_pl_name",
|
|
644
|
+
"master.m3u8",
|
|
645
|
+
"-var_stream_map",
|
|
646
|
+
rungs.map((_, i) => p.hasAudio ? `v:${i},a:${i}` : `v:${i}`).join(" "),
|
|
647
|
+
"-progress",
|
|
648
|
+
"pipe:1",
|
|
649
|
+
"-nostats",
|
|
650
|
+
`${out}/v%v/index.m3u8`
|
|
651
|
+
);
|
|
652
|
+
} else {
|
|
653
|
+
args.push("-progress", "pipe:1", "-nostats");
|
|
654
|
+
}
|
|
655
|
+
if (steps.includes("poster")) args.push("-map", "[poster]", "-frames:v", "1", "-q:v", "3", "-update", "1", `${out}/poster.jpg`);
|
|
656
|
+
if (steps.includes("storyboard") && o.storyboard) args.push(...o.storyboard.outputs);
|
|
657
|
+
if (sb) args.push("-map", "[sb]", "-fps_mode", "passthrough", "-q:v", "5", `${o.thumbs ?? `${out}/../thumbs`}/t-%05d.jpg`);
|
|
658
|
+
if (steps.includes("faststart")) {
|
|
659
|
+
args.push("-map", `0:v:${p.video}`, ...p.hasAudio || p.audioUndecodable ? ["-map", "0:a?"] : [], "-c", "copy", "-movflags", "+faststart", "-f", "mp4", `${out}/faststart.mp4`);
|
|
660
|
+
}
|
|
661
|
+
args.push(...o.stepArgs?.outputs ?? []);
|
|
662
|
+
return args;
|
|
663
|
+
}
|
|
664
|
+
function audioFitTo(p) {
|
|
665
|
+
if (p.videoStart === void 0 || p.audioStart === void 0 || !p.videoDuration || !p.audioDuration) return 0;
|
|
666
|
+
const lastFrameEnd = p.lastFrameAt !== void 0 ? p.lastFrameAt + 1 / (p.sparseRate || 30) : 0;
|
|
667
|
+
const pictureEnd2 = Math.max(p.videoStart + p.videoDuration, lastFrameEnd);
|
|
668
|
+
const audioEnd = p.audioStart + p.audioDuration;
|
|
669
|
+
if (audioEnd >= pictureEnd2 + 0.1 - 1e-3 && audioEnd <= pictureEnd2 + 0.2) return 0;
|
|
670
|
+
return pictureEnd2 - p.audioStart + 0.1;
|
|
671
|
+
}
|
|
672
|
+
function fastStartArgs(input, out, p) {
|
|
673
|
+
return ["-hide_banner", "-nostdin", "-y", "-loglevel", "error", "-i", input, "-map", `0:v:${p.video}`, ...p.hasAudio ? ["-map", "0:a?"] : [], "-c", "copy", "-movflags", "+faststart", "-f", "mp4", `${out}/faststart.mp4`];
|
|
674
|
+
}
|
|
675
|
+
function parseProbe(json, keyframeCsv, audioChoice) {
|
|
676
|
+
let j;
|
|
677
|
+
try {
|
|
678
|
+
j = JSON.parse(json);
|
|
679
|
+
} catch {
|
|
680
|
+
return null;
|
|
681
|
+
}
|
|
682
|
+
const videos = (j.streams ?? []).filter((s) => s.codec_type === "video");
|
|
683
|
+
const vi = videos.findIndex((s) => !s.disposition?.attached_pic);
|
|
684
|
+
if (vi < 0) return null;
|
|
685
|
+
const v = videos[vi];
|
|
686
|
+
const sd = v.side_data_list?.find((x) => typeof x.rotation === "number");
|
|
687
|
+
const tagRot = Number(v.tags?.rotate ?? 0);
|
|
688
|
+
const audios = (j.streams ?? []).filter((s) => s.codec_type === "audio");
|
|
689
|
+
const usable = audios.map((s, i) => decodableAudio(s.codec_name) ? i : -1).filter((i) => i >= 0 && (audioChoice === void 0 || i >= audioChoice));
|
|
690
|
+
const a = usable.length ? audios[usable[0]] : void 0;
|
|
691
|
+
const num2 = (x) => x === void 0 || x === null || x === "N/A" || x === "" || !Number.isFinite(Number(x)) ? void 0 : Number(x);
|
|
692
|
+
const streamSeconds = (st) => {
|
|
693
|
+
if (!st) return void 0;
|
|
694
|
+
const own = num2(st.duration);
|
|
695
|
+
if (own !== void 0) return own;
|
|
696
|
+
const tags = st.tags ?? {};
|
|
697
|
+
const m = /^(\d+):(\d{2}):(\d{2}(?:\.\d+)?)$/.exec(String(tags.DURATION ?? tags.duration ?? ""));
|
|
698
|
+
return m ? Math.max(0, Number(m[1]) * 3600 + Number(m[2]) * 60 + Number(m[3]) - (num2(st.start_time) ?? 0)) : void 0;
|
|
699
|
+
};
|
|
700
|
+
const ratio = (x) => {
|
|
701
|
+
const m = /^(\d+)[:/](\d+)$/.exec(String(x ?? ""));
|
|
702
|
+
return m && Number(m[2]) ? Number(m[1]) / Number(m[2]) : 0;
|
|
703
|
+
};
|
|
704
|
+
const colour = (x) => typeof x === "string" && x && x !== "unknown" && x !== "unspecified" ? x : void 0;
|
|
705
|
+
const sar = ratio(v.sample_aspect_ratio);
|
|
706
|
+
const r = ratio(v.r_frame_rate);
|
|
707
|
+
const avg = ratio(v.avg_frame_rate);
|
|
708
|
+
const vfr = r > 0 && avg > 0 && Math.abs(r - avg) / r > 0.01;
|
|
709
|
+
const sparse = vfr && avg < r * 0.5;
|
|
710
|
+
const format = String(j.format?.format_name ?? "");
|
|
711
|
+
const videoDuration = streamSeconds(v);
|
|
712
|
+
const imageDemuxer = /(^|,)(image2|image2pipe|[a-z0-9]+_pipe)(,|$)/.test(format);
|
|
713
|
+
const oneFrame = videos.every((s) => num2(s.nb_frames) === 1 && !(streamSeconds(s) > 0)) && !(num2(j.format?.duration) > 0);
|
|
714
|
+
const transfer = colour(v.color_transfer);
|
|
715
|
+
const primaries = colour(v.color_primaries);
|
|
716
|
+
const matrix = colour(v.color_space);
|
|
717
|
+
return {
|
|
718
|
+
video: vi,
|
|
719
|
+
width: Number(v.width) || 0,
|
|
720
|
+
height: Number(v.height) || 0,
|
|
721
|
+
rotation: Math.round(Math.abs(sd?.rotation ?? tagRot ?? 0)),
|
|
722
|
+
videoCodec: String(v.codec_name ?? "").toLowerCase(),
|
|
723
|
+
pixFmt: String(v.pix_fmt ?? "").toLowerCase(),
|
|
724
|
+
bitrate: Number(j.format?.bit_rate ?? 0) || 0,
|
|
725
|
+
hasAudio: !!a,
|
|
726
|
+
...usable[0] ? { audio: usable[0] } : {},
|
|
727
|
+
...usable.length > 1 ? { audioFallbacks: usable.slice(1) } : {},
|
|
728
|
+
...audios.length && !a ? { audioUndecodable: true } : {},
|
|
729
|
+
duration: Number(j.format?.duration ?? 0) || 0,
|
|
730
|
+
...videoDuration > 0 ? { videoDuration } : {},
|
|
731
|
+
keyframes: keyframeCsv.split(/[\s,]+/).filter(Boolean).map(Number).filter((n) => Number.isFinite(n)).sort((a2, b) => a2 - b),
|
|
732
|
+
.../^(tt|bb|tb|bt)$/.test(String(v.field_order ?? "")) ? { interlaced: true } : {},
|
|
733
|
+
...transfer ? { transfer } : {},
|
|
734
|
+
...primaries ? { primaries } : {},
|
|
735
|
+
...matrix ? { matrix } : {},
|
|
736
|
+
...sar > 0 && Math.abs(sar - 1) > 1e-3 ? { sar } : {},
|
|
737
|
+
...vfr ? { vfr } : {},
|
|
738
|
+
...sparse ? { sparseRate: Math.min(60, Math.round(r * 1e3) / 1e3) } : {},
|
|
739
|
+
...num2(j.format?.start_time) !== void 0 ? { fileStart: num2(j.format?.start_time) } : {},
|
|
740
|
+
...num2(v.start_time) !== void 0 ? { videoStart: num2(v.start_time) } : {},
|
|
741
|
+
...a && num2(a.start_time) !== void 0 ? { audioStart: num2(a.start_time) } : {},
|
|
742
|
+
...a && streamSeconds(a) > 0 ? { audioDuration: streamSeconds(a) } : {},
|
|
743
|
+
...imageDemuxer || oneFrame ? { still: true } : {}
|
|
744
|
+
};
|
|
745
|
+
}
|
|
746
|
+
function packetRow(line) {
|
|
747
|
+
const cols = line.trim().split(",");
|
|
748
|
+
if (cols.length > 2 && !/^[A-Z_]+$/.test(cols[1])) return { t: cols[0], dur: cols[1], flags: cols[2] };
|
|
749
|
+
return { t: cols[0], flags: cols[1] };
|
|
750
|
+
}
|
|
751
|
+
function packetFacts(csv) {
|
|
752
|
+
const pts = [];
|
|
753
|
+
let editList = false;
|
|
754
|
+
let end;
|
|
755
|
+
for (const line of csv.split("\n")) {
|
|
756
|
+
const { t, flags, dur } = packetRow(line);
|
|
757
|
+
if (t === void 0 || t === "" || !Number.isFinite(Number(t))) continue;
|
|
758
|
+
if (flags?.includes("D")) {
|
|
759
|
+
editList = true;
|
|
760
|
+
continue;
|
|
761
|
+
}
|
|
762
|
+
pts.push(Number(t));
|
|
763
|
+
if (dur !== void 0 && dur !== "" && Number.isFinite(Number(dur))) end = Math.max(end ?? -Infinity, Number(t) + Number(dur));
|
|
764
|
+
}
|
|
765
|
+
pts.sort((a, b) => a - b);
|
|
766
|
+
let lo = Infinity;
|
|
767
|
+
let hi = 0;
|
|
768
|
+
let step = Infinity;
|
|
769
|
+
for (let i = 1; i < pts.length; i++) {
|
|
770
|
+
const d = pts[i] - pts[i - 1];
|
|
771
|
+
lo = Math.min(lo, d);
|
|
772
|
+
hi = Math.max(hi, d);
|
|
773
|
+
if (d > 1e-6) step = Math.min(step, d);
|
|
774
|
+
}
|
|
775
|
+
const vfr = pts.length > 2 && (lo <= 1e-6 || hi > lo * 1.5 + 1e-4);
|
|
776
|
+
return { keyframes: keyframesFromPackets(csv), editList, vfr, ...pts.length ? { last: pts[pts.length - 1] } : {}, ...Number.isFinite(step) ? { minGap: step, maxGap: hi } : {}, ...end !== void 0 ? { end } : {} };
|
|
777
|
+
}
|
|
778
|
+
function keyframesFromPackets(csv) {
|
|
779
|
+
const out = [];
|
|
780
|
+
for (const line of csv.split("\n")) {
|
|
781
|
+
const { t, flags } = packetRow(line);
|
|
782
|
+
if (flags?.includes("K")) {
|
|
783
|
+
const n = Number(t);
|
|
784
|
+
if (t !== "" && Number.isFinite(n)) out.push(n);
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
return out.sort((a, b) => a - b).join(",");
|
|
788
|
+
}
|
|
789
|
+
function withPacketEnd(p, facts) {
|
|
790
|
+
const pictureEnd2 = facts.end ?? facts.last + 1 / (p.sparseRate || 30);
|
|
791
|
+
const start = p.videoStart ?? p.fileStart ?? 0;
|
|
792
|
+
const fromPackets = pictureEnd2 - start;
|
|
793
|
+
const lastFrameAt = Math.max(facts.last, pictureEnd2 - 1 / (p.sparseRate || 30));
|
|
794
|
+
if (!(fromPackets > (p.videoDuration || 0) + 0.1)) return { ...p, lastFrameAt };
|
|
795
|
+
return { ...p, lastFrameAt, videoDuration: fromPackets, duration: Math.max(p.duration, pictureEnd2 - (p.fileStart ?? 0)) };
|
|
796
|
+
}
|
|
797
|
+
function bestFirst(master) {
|
|
798
|
+
const lines = master.split("\n");
|
|
799
|
+
const head = [];
|
|
800
|
+
const variants = [];
|
|
801
|
+
const tail = [];
|
|
802
|
+
for (let i = 0; i < lines.length; i++) {
|
|
803
|
+
const l = lines[i];
|
|
804
|
+
if (l.startsWith("#EXT-X-STREAM-INF:")) {
|
|
805
|
+
const bw = Number(/(?:^|[:,])BANDWIDTH=(\d+)/.exec(l)?.[1] ?? 0);
|
|
806
|
+
variants.push({ bw, lines: [l, lines[i + 1] ?? ""] });
|
|
807
|
+
i++;
|
|
808
|
+
} else if (variants.length) tail.push(l);
|
|
809
|
+
else head.push(l);
|
|
810
|
+
}
|
|
811
|
+
if (variants.length < 2) return master;
|
|
812
|
+
variants.sort((a, b) => b.bw - a.bw);
|
|
813
|
+
return [...head, ...variants.flatMap((v) => [...v.lines, ""]), ...tail.filter((l) => l.trim())].join("\n").replace(/\n{3,}/g, "\n\n").trimEnd() + "\n";
|
|
814
|
+
}
|
|
815
|
+
var isHdr, isWideSdr, decodableAudio, NO_AUDIO_DECODER, audioIn, pictureSeconds, ffmpegHasWork, keyframeProbeArgs;
|
|
816
|
+
var init_ffmpeg = __esm({
|
|
817
|
+
"../media-worker/build/out/stage/src/runner/ffmpeg.ts"() {
|
|
818
|
+
"use strict";
|
|
819
|
+
init_media();
|
|
820
|
+
isHdr = (p) => p.transfer === "smpte2084" || p.transfer === "arib-std-b67";
|
|
821
|
+
isWideSdr = (p) => /^bt2020/.test(p.matrix ?? "") && !isHdr(p);
|
|
822
|
+
decodableAudio = (codec) => typeof codec === "string" && codec !== "" && codec !== "unknown" && codec !== "none" && !NO_AUDIO_DECODER.has(codec);
|
|
823
|
+
NO_AUDIO_DECODER = /* @__PURE__ */ new Set(["apple_apac"]);
|
|
824
|
+
audioIn = (p) => `0:a:${p.audio ?? 0}`;
|
|
825
|
+
pictureSeconds = (p) => p.videoDuration || p.duration;
|
|
826
|
+
ffmpegHasWork = (steps, p) => steps.some((s) => s !== "audio" && s !== "transcript") || wantsAudio(steps) && p.hasAudio;
|
|
827
|
+
keyframeProbeArgs = (src, video, durations = false) => [
|
|
828
|
+
"-v",
|
|
829
|
+
"error",
|
|
830
|
+
"-select_streams",
|
|
831
|
+
`v:${video}`,
|
|
832
|
+
"-show_entries",
|
|
833
|
+
durations ? "packet=pts_time,duration_time,flags" : "packet=pts_time,flags",
|
|
834
|
+
"-of",
|
|
835
|
+
"csv=p=0",
|
|
836
|
+
src
|
|
837
|
+
];
|
|
838
|
+
}
|
|
839
|
+
});
|
|
840
|
+
|
|
841
|
+
// ../media-worker/build/out/stage/src/runner/plugins.ts
|
|
842
|
+
function pickPlugin(plugins, sig) {
|
|
843
|
+
for (const p of plugins ?? []) {
|
|
844
|
+
try {
|
|
845
|
+
if (p.claims(sig)) return p;
|
|
846
|
+
} catch {
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
return null;
|
|
850
|
+
}
|
|
851
|
+
function probeOfDecoded(d) {
|
|
852
|
+
return { video: 0, width: d.width, height: d.height, rotation: 0, videoCodec: "rawvideo", pixFmt: "", bitrate: 0, hasAudio: false, keyframes: [], duration: d.duration };
|
|
853
|
+
}
|
|
854
|
+
var init_plugins = __esm({
|
|
855
|
+
"../media-worker/build/out/stage/src/runner/plugins.ts"() {
|
|
856
|
+
"use strict";
|
|
857
|
+
}
|
|
858
|
+
});
|
|
859
|
+
|
|
860
|
+
// ../media-worker/build/out/stage/src/steps/edit.ts
|
|
861
|
+
import { closeSync, openSync, readSync, readdirSync, statSync, writeSync } from "node:fs";
|
|
862
|
+
import { join } from "node:path";
|
|
863
|
+
function demuxerOf(head) {
|
|
864
|
+
if (head.length >= 4 && head[0] === 26 && head[1] === 69 && head[2] === 223 && head[3] === 163) return "matroska";
|
|
865
|
+
if (head.length < 8) return null;
|
|
866
|
+
const size = (head[0] << 24 >>> 0) + (head[1] << 16) + (head[2] << 8) + head[3];
|
|
867
|
+
const type = String.fromCharCode(head[4], head[5], head[6], head[7]);
|
|
868
|
+
if (!FIRST_BOXES.has(type)) return null;
|
|
869
|
+
return size === 0 || size === 1 || size >= 8 ? "mov" : null;
|
|
870
|
+
}
|
|
871
|
+
function readHead(path, n = 16) {
|
|
872
|
+
const fd = openSync(path, "r");
|
|
873
|
+
try {
|
|
874
|
+
const b = Buffer.alloc(n);
|
|
875
|
+
return new Uint8Array(b.subarray(0, readSync(fd, b, 0, n, 0)));
|
|
876
|
+
} finally {
|
|
877
|
+
closeSync(fd);
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
async function probeSource(ctx, path, what = "the source") {
|
|
881
|
+
const d = demuxerOf(readHead(path));
|
|
882
|
+
if (!d) throw new JobError(`unsupported: ${what} doesn't start like a video file (MP4, MOV or WebM/Matroska)`, "unsupported");
|
|
883
|
+
let json;
|
|
884
|
+
try {
|
|
885
|
+
json = await ctx.exec(ctx.ffprobe, ["-v", "error", "-protocol_whitelist", "file", "-f", d, "-show_streams", "-show_format", "-of", "json", path]);
|
|
886
|
+
} catch (e) {
|
|
887
|
+
throw new JobError(`unsupported: ${what} can't be read (${e.message.slice(0, 300)})`, "unsupported");
|
|
888
|
+
}
|
|
889
|
+
const probe = parseProbe(json, "");
|
|
890
|
+
if (!probe || !probe.width || !probe.height) throw new JobError(`unsupported: no picture in ${what}`, "unsupported");
|
|
891
|
+
if (probe.still) throw new JobError(`unsupported: ${what} is a still picture`, "unsupported");
|
|
892
|
+
if (["", "unknown", "none", "prores_raw"].includes(probe.videoCodec)) throw new JobError(`unsupported: no decoder for ${what}'s picture (${probe.videoCodec || "unknown"})`, "unsupported");
|
|
893
|
+
if (probe.audioUndecodable) throw new JobError(`unsupported: no decoder for ${what}'s sound`, "unsupported");
|
|
894
|
+
return { probe, demuxer: d, fps: nominalFps(json, probe.video) };
|
|
895
|
+
}
|
|
896
|
+
function nominalFps(json, video) {
|
|
897
|
+
const v = (JSON.parse(json).streams ?? []).filter((s) => s.codec_type === "video")[video] ?? {};
|
|
898
|
+
const rate = (x) => {
|
|
899
|
+
const m = /^(\d+)\/(\d+)$/.exec(String(x ?? ""));
|
|
900
|
+
return m && Number(m[2]) ? Number(m[1]) / Number(m[2]) : 0;
|
|
901
|
+
};
|
|
902
|
+
const r = rate(v.avg_frame_rate) || rate(v.r_frame_rate);
|
|
903
|
+
return r > 0 && r <= 1e3 ? Math.round(r * 1e3) / 1e3 : 30;
|
|
904
|
+
}
|
|
905
|
+
async function packetSpan(ctx, path, d, stream) {
|
|
906
|
+
let count = 0;
|
|
907
|
+
let start = Infinity;
|
|
908
|
+
let end = 0;
|
|
909
|
+
let durations = 0;
|
|
910
|
+
let rest = "";
|
|
911
|
+
const take = (line) => {
|
|
912
|
+
const [pts, dur] = line.trim().split(",");
|
|
913
|
+
const t = Number(pts);
|
|
914
|
+
if (pts === "" || pts === "N/A" || !Number.isFinite(t)) return;
|
|
915
|
+
const d2 = Number(dur);
|
|
916
|
+
count++;
|
|
917
|
+
start = Math.min(start, t);
|
|
918
|
+
const len = Number.isFinite(d2) && d2 > 0 ? d2 : 0;
|
|
919
|
+
durations += len;
|
|
920
|
+
end = Math.max(end, t + len);
|
|
921
|
+
};
|
|
922
|
+
await ctx.exec(ctx.ffprobe, ["-v", "error", "-protocol_whitelist", "file", "-f", d, "-select_streams", stream, "-show_entries", "packet=pts_time,duration_time", "-of", "csv=p=0", path], {
|
|
923
|
+
onStdout: (s) => {
|
|
924
|
+
const lines = (rest + s).split("\n");
|
|
925
|
+
rest = lines.pop() ?? "";
|
|
926
|
+
for (const l of lines) take(l);
|
|
927
|
+
}
|
|
928
|
+
});
|
|
929
|
+
if (rest) take(rest);
|
|
930
|
+
return { count, start: count ? start : 0, end, durations };
|
|
931
|
+
}
|
|
932
|
+
function needDisk(need, free) {
|
|
933
|
+
if (need > free) throw new JobError(`disk: the job needs at least ${Math.ceil(need / 1e6)} MB, the runner has ${Math.floor(free / 1e6)} MB free`, "disk");
|
|
934
|
+
}
|
|
935
|
+
function listFiles(dir, base = "") {
|
|
936
|
+
const out = [];
|
|
937
|
+
for (const e of readdirSync(join(dir, base), { withFileTypes: true })) {
|
|
938
|
+
const rel = base ? `${base}/${e.name}` : e.name;
|
|
939
|
+
if (e.isDirectory()) out.push(...listFiles(dir, rel));
|
|
940
|
+
else out.push(rel);
|
|
941
|
+
}
|
|
942
|
+
return out;
|
|
943
|
+
}
|
|
944
|
+
function phases(onStatus) {
|
|
945
|
+
const st = { state: "running", phase: "download", progress: 0, timings: {} };
|
|
946
|
+
let t = Date.now();
|
|
947
|
+
const emit = () => onStatus({ ...st, timings: { ...st.timings } });
|
|
948
|
+
return {
|
|
949
|
+
st,
|
|
950
|
+
emit,
|
|
951
|
+
/** Close the current phase (its seconds into the timings) and start the next. */
|
|
952
|
+
next(name, phase) {
|
|
953
|
+
st.timings[name] = Math.round((Date.now() - t) / 100) / 10;
|
|
954
|
+
t = Date.now();
|
|
955
|
+
if (phase) st.phase = phase;
|
|
956
|
+
emit();
|
|
957
|
+
}
|
|
958
|
+
};
|
|
959
|
+
}
|
|
960
|
+
function appendFile(to, from) {
|
|
961
|
+
const out = openSync(to, "a");
|
|
962
|
+
const fin = openSync(from, "r");
|
|
963
|
+
const buf = Buffer.alloc(1 << 20);
|
|
964
|
+
try {
|
|
965
|
+
for (; ; ) {
|
|
966
|
+
const n = readSync(fin, buf, 0, buf.length, null);
|
|
967
|
+
if (n <= 0) break;
|
|
968
|
+
for (let o = 0; o < n; ) o += writeSync(out, buf, o, n - o);
|
|
969
|
+
}
|
|
970
|
+
} finally {
|
|
971
|
+
closeSync(fin);
|
|
972
|
+
closeSync(out);
|
|
973
|
+
}
|
|
974
|
+
}
|
|
975
|
+
function hasMoof(path, maxBoxes = 1e5) {
|
|
976
|
+
const fd = openSync(path, "r");
|
|
977
|
+
try {
|
|
978
|
+
const size = statSync(path).size;
|
|
979
|
+
const h = Buffer.alloc(16);
|
|
980
|
+
let at = 0;
|
|
981
|
+
for (let i = 0; i < maxBoxes && at + 8 <= size; i++) {
|
|
982
|
+
if (readSync(fd, h, 0, 16, at) < 8) return false;
|
|
983
|
+
if (h.toString("latin1", 4, 8) === "moof") return true;
|
|
984
|
+
let len = h.readUInt32BE(0);
|
|
985
|
+
if (len === 1) len = Number(h.readBigUInt64BE(8));
|
|
986
|
+
if (len < 8 || at + len > size) return false;
|
|
987
|
+
at += len;
|
|
988
|
+
}
|
|
989
|
+
return false;
|
|
990
|
+
} finally {
|
|
991
|
+
closeSync(fd);
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
var FIRST_BOXES, sourceInput, sizeOf, isDiskFull;
|
|
995
|
+
var init_edit = __esm({
|
|
996
|
+
"../media-worker/build/out/stage/src/steps/edit.ts"() {
|
|
997
|
+
"use strict";
|
|
998
|
+
init_job();
|
|
999
|
+
init_ffmpeg();
|
|
1000
|
+
FIRST_BOXES = /* @__PURE__ */ new Set(["ftyp", "moov", "mdat", "wide", "free", "skip", "pnot", "styp"]);
|
|
1001
|
+
sourceInput = (path, d) => ["-protocol_whitelist", "file", "-f", d, "-i", path];
|
|
1002
|
+
sizeOf = (f) => statSync(f).size;
|
|
1003
|
+
isDiskFull = (e) => e?.code === "ENOSPC" || /No space left on device|ENOSPC/.test(String(e?.message ?? e));
|
|
1004
|
+
}
|
|
1005
|
+
});
|
|
1006
|
+
|
|
1007
|
+
// ../media-worker/build/out/stage/src/steps/render.ts
|
|
1008
|
+
var render_exports = {};
|
|
1009
|
+
__export(render_exports, {
|
|
1010
|
+
AAC_PRIMING: () => AAC_PRIMING,
|
|
1011
|
+
RENDER_MOVFLAGS: () => RENDER_MOVFLAGS,
|
|
1012
|
+
joinArgs: () => joinArgs,
|
|
1013
|
+
partArgs: () => partArgs,
|
|
1014
|
+
partFilter: () => partFilter,
|
|
1015
|
+
pictureLead: () => pictureLead,
|
|
1016
|
+
posterArgs: () => posterArgs,
|
|
1017
|
+
runRender: () => runRender
|
|
1018
|
+
});
|
|
1019
|
+
import { mkdirSync, rmSync, statSync as statSync2, writeFileSync } from "node:fs";
|
|
1020
|
+
import { join as join2 } from "node:path";
|
|
1021
|
+
function partFilter(part, p, e, s) {
|
|
1022
|
+
const W = e.output.width;
|
|
1023
|
+
const H = e.output.height;
|
|
1024
|
+
const f = [];
|
|
1025
|
+
const pre = preChain(p);
|
|
1026
|
+
if (pre) f.push(pre);
|
|
1027
|
+
const sh = shown(p);
|
|
1028
|
+
if (p.sar) f.push(`scale=${sh.width}:${sh.height}`, "setsar=1");
|
|
1029
|
+
if (part.crop) f.push(`crop=${even2(part.crop.width)}:${even2(part.crop.height)}:${part.crop.x}:${part.crop.y}`);
|
|
1030
|
+
f.push(`scale=${W}:${H}:force_original_aspect_ratio=decrease:force_divisible_by=2:out_range=tv`, `pad=${W}:${H}:(ow-iw)/2:(oh-ih)/2:color=black`, "setsar=1");
|
|
1031
|
+
const oc = e.output.crop;
|
|
1032
|
+
if (oc) f.push(`crop=${even2(oc.width)}:${even2(oc.height)}:${oc.x}:${oc.y}`);
|
|
1033
|
+
const fin = renderFrame(e, s);
|
|
1034
|
+
const before = oc ? { width: even2(oc.width), height: even2(oc.height) } : { width: W, height: H };
|
|
1035
|
+
if (fin.width !== before.width || fin.height !== before.height) f.push(`scale=${fin.width}:${fin.height}`);
|
|
1036
|
+
f.push(`fps=${e.output.fps}`, "format=yuv420p");
|
|
1037
|
+
return f.join(",");
|
|
1038
|
+
}
|
|
1039
|
+
function pictureLead(e) {
|
|
1040
|
+
const frames = renderFrames(e);
|
|
1041
|
+
let f = 0;
|
|
1042
|
+
let ms = 0;
|
|
1043
|
+
return e.parts.map((p, k) => {
|
|
1044
|
+
const lead = f / e.output.fps - ms / 1e3;
|
|
1045
|
+
f += frames[k];
|
|
1046
|
+
ms += p.outMs - p.inMs;
|
|
1047
|
+
return Math.max(0, lead);
|
|
1048
|
+
});
|
|
1049
|
+
}
|
|
1050
|
+
function partArgs(part, src, e, s, frames, samples, out, lead = 0) {
|
|
1051
|
+
const fps = e.output.fps;
|
|
1052
|
+
const lenS = (part.outMs - part.inMs) / 1e3;
|
|
1053
|
+
const silent = !!part.muted || !src.probe.hasAudio;
|
|
1054
|
+
const g = String(Math.max(1, Math.round(fps * 2)));
|
|
1055
|
+
const filter = partFilter(part, src.probe, e, s).replace(`fps=${fps},`, `setpts=PTS-${lead.toFixed(6)}/TB,fps=${fps}:start_time=0:round=up,`);
|
|
1056
|
+
const v = `[0:v:${src.probe.video}]${filter},tpad=stop_mode=clone:stop_duration=${(lenS + 1).toFixed(3)},trim=end_frame=${frames},setpts=N/(${fps}*TB)[v]`;
|
|
1057
|
+
const a = `[${silent ? "1:a" : audioIn(src.probe).replace(/^0:/, "1:")}]aresample=${RENDER_SAMPLE_RATE}:async=1:first_pts=0,aformat=sample_fmts=s16:channel_layouts=stereo,apad,atrim=end_sample=${samples}[a]`;
|
|
1058
|
+
const ss = (part.inMs / 1e3).toFixed(3);
|
|
1059
|
+
return [
|
|
1060
|
+
"-hide_banner",
|
|
1061
|
+
"-nostdin",
|
|
1062
|
+
"-y",
|
|
1063
|
+
"-loglevel",
|
|
1064
|
+
"error",
|
|
1065
|
+
// The picture: seeked to the keyframe before the in-point and decoded from there, every frame kept (their times
|
|
1066
|
+
// before the in-point negative), so the frame on screen AT the in-point is there for frame 0 even when the next one
|
|
1067
|
+
// comes much later (a sparse screen recording: an accurate seek drops it, and the next frame stretched back over
|
|
1068
|
+
// the part). No -t: the trim ends it, after exactly its frames.
|
|
1069
|
+
"-noaccurate_seek",
|
|
1070
|
+
"-ss",
|
|
1071
|
+
ss,
|
|
1072
|
+
...sourceInput(src.path, src.demuxer),
|
|
1073
|
+
...silent ? ["-f", "lavfi", "-i", `anullsrc=r=${RENDER_SAMPLE_RATE}:cl=stereo`] : ["-ss", ss, "-t", (lenS + 2 / fps).toFixed(3), ...sourceInput(src.path, src.demuxer)],
|
|
1074
|
+
"-filter_complex",
|
|
1075
|
+
`${v};${a}`,
|
|
1076
|
+
"-map",
|
|
1077
|
+
"[v]",
|
|
1078
|
+
"-frames:v",
|
|
1079
|
+
String(frames),
|
|
1080
|
+
"-an",
|
|
1081
|
+
"-sn",
|
|
1082
|
+
"-dn",
|
|
1083
|
+
"-c:v",
|
|
1084
|
+
"libx264",
|
|
1085
|
+
"-preset",
|
|
1086
|
+
s.preset,
|
|
1087
|
+
"-crf",
|
|
1088
|
+
String(s.crf),
|
|
1089
|
+
"-profile:v",
|
|
1090
|
+
"high",
|
|
1091
|
+
"-g",
|
|
1092
|
+
g,
|
|
1093
|
+
"-keyint_min",
|
|
1094
|
+
g,
|
|
1095
|
+
"-sc_threshold",
|
|
1096
|
+
"0",
|
|
1097
|
+
"-color_range",
|
|
1098
|
+
"tv",
|
|
1099
|
+
"-f",
|
|
1100
|
+
"matroska",
|
|
1101
|
+
out.ts,
|
|
1102
|
+
"-map",
|
|
1103
|
+
"[a]",
|
|
1104
|
+
"-f",
|
|
1105
|
+
"s16le",
|
|
1106
|
+
"-ac",
|
|
1107
|
+
"2",
|
|
1108
|
+
"-ar",
|
|
1109
|
+
String(RENDER_SAMPLE_RATE),
|
|
1110
|
+
out.pcm
|
|
1111
|
+
];
|
|
1112
|
+
}
|
|
1113
|
+
function joinArgs(list, pcm, out, s) {
|
|
1114
|
+
return [
|
|
1115
|
+
"-hide_banner",
|
|
1116
|
+
"-nostdin",
|
|
1117
|
+
"-y",
|
|
1118
|
+
"-loglevel",
|
|
1119
|
+
"error",
|
|
1120
|
+
"-protocol_whitelist",
|
|
1121
|
+
"file",
|
|
1122
|
+
"-f",
|
|
1123
|
+
"concat",
|
|
1124
|
+
"-safe",
|
|
1125
|
+
"0",
|
|
1126
|
+
"-i",
|
|
1127
|
+
list,
|
|
1128
|
+
"-protocol_whitelist",
|
|
1129
|
+
"file",
|
|
1130
|
+
"-f",
|
|
1131
|
+
"s16le",
|
|
1132
|
+
"-ar",
|
|
1133
|
+
String(RENDER_SAMPLE_RATE),
|
|
1134
|
+
"-ac",
|
|
1135
|
+
"2",
|
|
1136
|
+
"-i",
|
|
1137
|
+
pcm,
|
|
1138
|
+
"-map",
|
|
1139
|
+
"0:v:0",
|
|
1140
|
+
"-map",
|
|
1141
|
+
"1:a:0",
|
|
1142
|
+
"-c:v",
|
|
1143
|
+
"copy",
|
|
1144
|
+
"-c:a",
|
|
1145
|
+
"aac",
|
|
1146
|
+
"-b:a",
|
|
1147
|
+
`${s.audioKbps}k`,
|
|
1148
|
+
"-ar",
|
|
1149
|
+
String(RENDER_SAMPLE_RATE),
|
|
1150
|
+
"-ac",
|
|
1151
|
+
"2",
|
|
1152
|
+
"-map_metadata",
|
|
1153
|
+
"-1",
|
|
1154
|
+
"-movflags",
|
|
1155
|
+
RENDER_MOVFLAGS,
|
|
1156
|
+
"-f",
|
|
1157
|
+
"mp4",
|
|
1158
|
+
out
|
|
1159
|
+
];
|
|
1160
|
+
}
|
|
1161
|
+
async function runRender(spec, io, ctx, onStatus) {
|
|
1162
|
+
const s = renderSettingsOf(spec.stepSettings);
|
|
1163
|
+
const ss = spec.stepSettings ?? DEFAULT_STEP_SETTINGS;
|
|
1164
|
+
const parsed = parseRenderJob(spec.render, s);
|
|
1165
|
+
if ("error" in parsed) throw new JobError(`render: ${parsed.error}`, "unsupported");
|
|
1166
|
+
const { sources, editList: e } = parsed.job;
|
|
1167
|
+
if (!io.downloadSource) throw new JobError("render: this runner can't download a render's sources", "unsupported");
|
|
1168
|
+
const timeoutMs = s.partTimeoutS * 1e3;
|
|
1169
|
+
const frames = renderFrames(e);
|
|
1170
|
+
const samples = renderSamples(e);
|
|
1171
|
+
const totalFrames = frames.reduce((a, b) => a + b, 0);
|
|
1172
|
+
const totalSamples = samples.reduce((a, b) => a + b, 0);
|
|
1173
|
+
const WORK = io.workDir;
|
|
1174
|
+
rmSync(WORK, { recursive: true, force: true });
|
|
1175
|
+
try {
|
|
1176
|
+
const out = join2(WORK, "out");
|
|
1177
|
+
mkdirSync(join2(out, "render"), { recursive: true });
|
|
1178
|
+
const ph = phases(onStatus);
|
|
1179
|
+
ph.emit();
|
|
1180
|
+
needDisk(Math.max(...sources.map((x) => x.bytes)) + 4 * (totalSamples + Math.max(...samples)), (io.freeBytes ?? freeBytesOf)(WORK));
|
|
1181
|
+
const pcm = join2(WORK, "audio.pcm");
|
|
1182
|
+
writeFileSync(pcm, new Uint8Array(0));
|
|
1183
|
+
const probes = /* @__PURE__ */ new Map();
|
|
1184
|
+
let have = null;
|
|
1185
|
+
const fetchSource = async (i) => {
|
|
1186
|
+
if (have?.i === i) return have.path;
|
|
1187
|
+
if (have) rmSync(have.path, { force: true });
|
|
1188
|
+
have = null;
|
|
1189
|
+
const path = join2(WORK, `source-${i}`);
|
|
1190
|
+
try {
|
|
1191
|
+
await io.downloadSource(i, path, () => {
|
|
1192
|
+
});
|
|
1193
|
+
} catch (e2) {
|
|
1194
|
+
throw e2 instanceof JobError ? e2 : new JobError(e2.message, "source");
|
|
1195
|
+
}
|
|
1196
|
+
if (statSync2(path).size !== sources[i].bytes) throw new JobError(`source ${i}: got ${statSync2(path).size} of ${sources[i].bytes} bytes`, "source");
|
|
1197
|
+
if (!probes.has(i)) {
|
|
1198
|
+
const r = await probeSource(ctx, path, `source ${i}`);
|
|
1199
|
+
probes.set(i, { probe: r.probe, demuxer: r.demuxer });
|
|
1200
|
+
}
|
|
1201
|
+
have = { i, path };
|
|
1202
|
+
return path;
|
|
1203
|
+
};
|
|
1204
|
+
const segs = [];
|
|
1205
|
+
const leads = pictureLead(e);
|
|
1206
|
+
const total = editListMs(e);
|
|
1207
|
+
let doneMs = 0;
|
|
1208
|
+
ph.next("prepare", "encode");
|
|
1209
|
+
for (let k = 0; k < e.parts.length; k++) {
|
|
1210
|
+
const part = e.parts[k];
|
|
1211
|
+
const path = await fetchSource(part.source);
|
|
1212
|
+
const src = probes.get(part.source);
|
|
1213
|
+
const sh = shown(src.probe);
|
|
1214
|
+
if (part.crop && (part.crop.x + part.crop.width > sh.width || part.crop.y + part.crop.height > sh.height))
|
|
1215
|
+
throw new JobError(`render: part ${k}'s crop is outside source ${part.source} (${sh.width}\xD7${sh.height})`, "unsupported");
|
|
1216
|
+
const ts = join2(WORK, `part-${String(k).padStart(4, "0")}.mkv`);
|
|
1217
|
+
const a = join2(WORK, `part-${k}.pcm`);
|
|
1218
|
+
try {
|
|
1219
|
+
await ctx.exec(ctx.ffmpeg, partArgs(part, { path, ...src }, e, s, frames[k], samples[k], { ts, pcm: a }, leads[k]), { timeoutMs });
|
|
1220
|
+
} catch (err) {
|
|
1221
|
+
const why = unsupportedReason(err.message);
|
|
1222
|
+
if (isDiskFull(err)) throw new JobError(`disk: part ${k}: ${err.message.slice(-300)}`, "disk");
|
|
1223
|
+
throw why ? new JobError(`render: part ${k}: ${why}`, "unsupported") : new JobError(`render: part ${k}: ${err.message.slice(-400)}`, "source");
|
|
1224
|
+
}
|
|
1225
|
+
const got = await packetSpan(ctx, ts, "matroska", "v:0");
|
|
1226
|
+
if (got.count !== frames[k]) throw new JobError(`render: part ${k} has ${got.count} of ${frames[k]} frames`, "source");
|
|
1227
|
+
if (sizeOf(a) !== samples[k] * 4) throw new JobError(`render: part ${k} has ${sizeOf(a) / 4} of ${samples[k]} samples`, "source");
|
|
1228
|
+
try {
|
|
1229
|
+
appendFile(pcm, a);
|
|
1230
|
+
} catch (err) {
|
|
1231
|
+
throw isDiskFull(err) ? new JobError(`disk: ${err.message}`, "disk") : err;
|
|
1232
|
+
}
|
|
1233
|
+
rmSync(a, { force: true });
|
|
1234
|
+
segs.push(ts);
|
|
1235
|
+
doneMs += part.outMs - part.inMs;
|
|
1236
|
+
ph.st.progress = 0.05 + 0.75 * (doneMs / total);
|
|
1237
|
+
}
|
|
1238
|
+
if (have) rmSync(have.path, { force: true });
|
|
1239
|
+
ph.next("encode", "join");
|
|
1240
|
+
const list = join2(WORK, "parts.txt");
|
|
1241
|
+
writeFileSync(list, "ffconcat version 1.0\n" + segs.map((x, k) => `file '${x}'
|
|
1242
|
+
duration ${(frames[k] / e.output.fps).toFixed(9)}
|
|
1243
|
+
`).join(""));
|
|
1244
|
+
const video = join2(out, RENDER_VIDEO);
|
|
1245
|
+
try {
|
|
1246
|
+
await ctx.exec(ctx.ffmpeg, joinArgs(list, pcm, video, s), { timeoutMs });
|
|
1247
|
+
} catch (err) {
|
|
1248
|
+
throw isDiskFull(err) ? new JobError(`disk: the join: ${err.message.slice(-300)}`, "disk") : err;
|
|
1249
|
+
}
|
|
1250
|
+
for (const x of [...segs, pcm, list]) rmSync(x, { force: true });
|
|
1251
|
+
const vs = await packetSpan(ctx, video, "mov", "v:0");
|
|
1252
|
+
const as = await packetSpan(ctx, video, "mov", "a:0");
|
|
1253
|
+
const frameS = 1 / e.output.fps;
|
|
1254
|
+
if (vs.count !== totalFrames) throw new JobError(`render: the file has ${vs.count} of ${totalFrames} frames`, "source");
|
|
1255
|
+
const pictureS = vs.end - vs.start;
|
|
1256
|
+
if (Math.abs(pictureS - totalFrames / e.output.fps) > frameS / 2) throw new JobError(`render: the picture runs ${pictureS.toFixed(3)} s for ${totalFrames} frames`, "source");
|
|
1257
|
+
const soundS = as.end - as.start;
|
|
1258
|
+
const coded = Math.ceil((totalSamples + AAC_PRIMING) / 1024) * 1024;
|
|
1259
|
+
if (soundS < (totalSamples + AAC_PRIMING) / RENDER_SAMPLE_RATE - 1e-3 || soundS > coded / RENDER_SAMPLE_RATE + 1e-3) throw new JobError(`render: the sound runs ${soundS.toFixed(3)} s for ${totalSamples} samples`, "source");
|
|
1260
|
+
const skew = as.start + AAC_PRIMING / RENDER_SAMPLE_RATE - vs.start;
|
|
1261
|
+
if (Math.abs(skew) > 1e-3) throw new JobError(`render: the sound starts ${(skew * 1e3).toFixed(1)} ms off the picture`, "source");
|
|
1262
|
+
if (Math.abs(totalFrames * 1e3 / e.output.fps - total) > 1e3 / e.output.fps) throw new Error("render: frame plan isn't within one frame of the edit list");
|
|
1263
|
+
ph.st.progress = 0.85;
|
|
1264
|
+
await ctx.exec(ctx.ffmpeg, posterArgs(video, join2(out, RENDER_POSTER), s), { timeoutMs: 12e4 });
|
|
1265
|
+
const size = renderFrame(e, s);
|
|
1266
|
+
const result = {
|
|
1267
|
+
format: EDIT_RESULT_FORMAT,
|
|
1268
|
+
durationMs: Math.round(totalFrames * 1e3 / e.output.fps),
|
|
1269
|
+
frames: totalFrames,
|
|
1270
|
+
fps: e.output.fps,
|
|
1271
|
+
samples: totalSamples,
|
|
1272
|
+
width: size.width,
|
|
1273
|
+
height: size.height,
|
|
1274
|
+
bytes: sizeOf(video),
|
|
1275
|
+
parts: e.parts.length
|
|
1276
|
+
};
|
|
1277
|
+
const problem = renderResultProblem(result, e, s);
|
|
1278
|
+
if (problem) throw new Error(`render: ${problem}`);
|
|
1279
|
+
writeFileSync(join2(out, RENDER_RESULT), JSON.stringify(result));
|
|
1280
|
+
ph.next("join", "upload");
|
|
1281
|
+
for (const f of [RENDER_VIDEO, RENDER_POSTER, RENDER_RESULT]) await io.upload(f, join2(out, f));
|
|
1282
|
+
ph.next("upload");
|
|
1283
|
+
const c = ctx.counts();
|
|
1284
|
+
const done = {
|
|
1285
|
+
state: "done",
|
|
1286
|
+
progress: 1,
|
|
1287
|
+
ladder: jobVersion(spec.ladder.version, ["render"], ss),
|
|
1288
|
+
timings: { ...ph.st.timings, sourceMB: Math.round(sources.reduce((n, x) => n + x.bytes, 0) / 1e6), seconds: Math.round(total / 1e3), parts: e.parts.length, decodes: c.decodes, processes: c.processes }
|
|
1289
|
+
};
|
|
1290
|
+
onStatus(done);
|
|
1291
|
+
return done;
|
|
1292
|
+
} finally {
|
|
1293
|
+
rmSync(WORK, { recursive: true, force: true });
|
|
1294
|
+
}
|
|
1295
|
+
}
|
|
1296
|
+
var even2, AAC_PRIMING, RENDER_MOVFLAGS, posterArgs;
|
|
1297
|
+
var init_render = __esm({
|
|
1298
|
+
"../media-worker/build/out/stage/src/steps/render.ts"() {
|
|
1299
|
+
"use strict";
|
|
1300
|
+
init_media();
|
|
1301
|
+
init_ffmpeg();
|
|
1302
|
+
init_job();
|
|
1303
|
+
init_edit();
|
|
1304
|
+
even2 = (n) => Math.max(2, Math.floor(n / 2) * 2);
|
|
1305
|
+
AAC_PRIMING = 1024;
|
|
1306
|
+
RENDER_MOVFLAGS = "+frag_keyframe+empty_moov+default_base_moof+delay_moov";
|
|
1307
|
+
posterArgs = (video, out, s) => [
|
|
1308
|
+
"-hide_banner",
|
|
1309
|
+
"-nostdin",
|
|
1310
|
+
"-y",
|
|
1311
|
+
"-loglevel",
|
|
1312
|
+
"error",
|
|
1313
|
+
...sourceInput(video, "mov"),
|
|
1314
|
+
"-frames:v",
|
|
1315
|
+
"1",
|
|
1316
|
+
"-vf",
|
|
1317
|
+
`scale='min(${s.posterWidth},iw)':-2`,
|
|
1318
|
+
"-q:v",
|
|
1319
|
+
"3",
|
|
1320
|
+
out
|
|
1321
|
+
];
|
|
1322
|
+
}
|
|
1323
|
+
});
|
|
1324
|
+
|
|
1325
|
+
// ../media-worker/build/out/stage/src/steps/prep.ts
|
|
1326
|
+
var prep_exports = {};
|
|
1327
|
+
__export(prep_exports, {
|
|
1328
|
+
editFrame: () => editFrame,
|
|
1329
|
+
isEditReady: () => isEditReady,
|
|
1330
|
+
peaksOf: () => peaksOf,
|
|
1331
|
+
peaksSampleRate: () => peaksSampleRate,
|
|
1332
|
+
prepArgs: () => prepArgs,
|
|
1333
|
+
runPrep: () => runPrep,
|
|
1334
|
+
sheetArgs: () => sheetArgs2,
|
|
1335
|
+
tileSize: () => tileSize
|
|
1336
|
+
});
|
|
1337
|
+
import { closeSync as closeSync2, mkdirSync as mkdirSync2, openSync as openSync2, readSync as readSync2, readdirSync as readdirSync2, rmSync as rmSync2, statSync as statSync3, writeFileSync as writeFileSync2 } from "node:fs";
|
|
1338
|
+
import { join as join3 } from "node:path";
|
|
1339
|
+
function editFrame(p, cap) {
|
|
1340
|
+
const s = shown(p);
|
|
1341
|
+
const short = Math.min(s.width, s.height);
|
|
1342
|
+
if (short <= cap) return { width: even3(s.width), height: even3(s.height) };
|
|
1343
|
+
const k = cap / short;
|
|
1344
|
+
return { width: even3(s.width * k), height: even3(s.height * k) };
|
|
1345
|
+
}
|
|
1346
|
+
function tileSize(p, long) {
|
|
1347
|
+
return p.width >= p.height ? { width: long, height: even3(long * p.height / p.width) } : { width: even3(long * p.width / p.height), height: long };
|
|
1348
|
+
}
|
|
1349
|
+
function isEditReady(path, d, p, cap, audioCodec) {
|
|
1350
|
+
if (d !== "mov" || !hasMoof(path)) return false;
|
|
1351
|
+
const head = readHead(path, 12);
|
|
1352
|
+
const brand = String.fromCharCode(...head.subarray(8, 12));
|
|
1353
|
+
if (String.fromCharCode(...head.subarray(4, 8)) === "ftyp" && brand === "qt ") return false;
|
|
1354
|
+
if (p.videoCodec !== "h264" || !/^yuvj?420p$/.test(p.pixFmt)) return false;
|
|
1355
|
+
if (p.hasAudio && audioCodec !== "aac") return false;
|
|
1356
|
+
if (p.rotation % 360 !== 0 || p.sar || p.interlaced || isHdr(p) || /^bt2020/.test(p.matrix ?? "")) return false;
|
|
1357
|
+
return p.width % 2 === 0 && p.height % 2 === 0 && Math.min(p.width, p.height) <= cap;
|
|
1358
|
+
}
|
|
1359
|
+
function prepArgs(src, d, p, plan, s, out) {
|
|
1360
|
+
const pre = preChain(p);
|
|
1361
|
+
const v = `[0:v:${p.video}]${pre ? `${pre},` : ""}`;
|
|
1362
|
+
const chains = [];
|
|
1363
|
+
if (plan.convert) chains.push(`${v}split=2[c][f]`, `[c]scale=${plan.frame.width}:${plan.frame.height}:out_range=tv,setsar=1,format=yuv420p[cv]`);
|
|
1364
|
+
else chains.push(`${v}null[f]`);
|
|
1365
|
+
chains.push(`[f]setpts=PTS-${plan.videoStart.toFixed(6)}/TB,fps=1/${plan.interval}:round=up:start_time=0,scale=${plan.tile.width}:${plan.tile.height},setsar=1[fs]`);
|
|
1366
|
+
if (p.hasAudio) {
|
|
1367
|
+
chains.push(`[${audioIn(p)}]asetpts=PTS-${plan.videoStart.toFixed(6)}/TB,aresample=${peaksSampleRate(s.peaksRate)}:async=1:first_pts=0,aformat=sample_fmts=s16:channel_layouts=mono[pk]`);
|
|
1368
|
+
}
|
|
1369
|
+
const g = String(Math.max(1, Math.round(Math.min(60, plan.fps || 30) * 2)));
|
|
1370
|
+
return [
|
|
1371
|
+
"-hide_banner",
|
|
1372
|
+
"-nostdin",
|
|
1373
|
+
"-y",
|
|
1374
|
+
"-loglevel",
|
|
1375
|
+
"error",
|
|
1376
|
+
...sourceInput(src, d),
|
|
1377
|
+
"-filter_complex",
|
|
1378
|
+
chains.join(";"),
|
|
1379
|
+
...plan.convert ? [
|
|
1380
|
+
"-map",
|
|
1381
|
+
"[cv]",
|
|
1382
|
+
...p.hasAudio ? ["-map", audioIn(p)] : [],
|
|
1383
|
+
"-sn",
|
|
1384
|
+
"-dn",
|
|
1385
|
+
"-map_metadata",
|
|
1386
|
+
"-1",
|
|
1387
|
+
"-c:v",
|
|
1388
|
+
"libx264",
|
|
1389
|
+
"-preset",
|
|
1390
|
+
s.preset,
|
|
1391
|
+
"-crf",
|
|
1392
|
+
String(s.crf),
|
|
1393
|
+
"-profile:v",
|
|
1394
|
+
"high",
|
|
1395
|
+
"-g",
|
|
1396
|
+
g,
|
|
1397
|
+
"-keyint_min",
|
|
1398
|
+
g,
|
|
1399
|
+
"-sc_threshold",
|
|
1400
|
+
"0",
|
|
1401
|
+
...p.hasAudio ? ["-c:a", "aac", "-b:a", `${s.audioKbps}k`, "-ar", "48000", "-ac", "2"] : [],
|
|
1402
|
+
// delay_moov: the moov carries the edit lists that start the picture at 0 (B-frames) and hide the AAC
|
|
1403
|
+
// priming; without it FFmpeg's fragmented MP4 starts the picture 2 frames late (review round 2, Codex).
|
|
1404
|
+
"-movflags",
|
|
1405
|
+
"+frag_keyframe+empty_moov+default_base_moof+delay_moov",
|
|
1406
|
+
"-f",
|
|
1407
|
+
"mp4",
|
|
1408
|
+
out.media
|
|
1409
|
+
] : [],
|
|
1410
|
+
"-map",
|
|
1411
|
+
"[fs]",
|
|
1412
|
+
"-frames:v",
|
|
1413
|
+
String(plan.thumbnails),
|
|
1414
|
+
"-q:v",
|
|
1415
|
+
String(s.jpegQ),
|
|
1416
|
+
"-f",
|
|
1417
|
+
"image2",
|
|
1418
|
+
`${out.thumbs}/t-%05d.jpg`,
|
|
1419
|
+
...p.hasAudio ? ["-map", "[pk]", "-f", "s16le", "-ac", "1", "-ar", String(peaksSampleRate(s.peaksRate)), out.pcm] : []
|
|
1420
|
+
];
|
|
1421
|
+
}
|
|
1422
|
+
function peaksOf(pcm, per) {
|
|
1423
|
+
const size = statSync3(pcm).size;
|
|
1424
|
+
const out = new Int8Array(2 * Math.ceil(Math.floor(size / 2) / per));
|
|
1425
|
+
const fd = openSync2(pcm, "r");
|
|
1426
|
+
const buf = Buffer.alloc(1 << 20);
|
|
1427
|
+
let lo = 32767;
|
|
1428
|
+
let hi = -32768;
|
|
1429
|
+
let n = 0;
|
|
1430
|
+
let k = 0;
|
|
1431
|
+
let carry = -1;
|
|
1432
|
+
try {
|
|
1433
|
+
for (let at = 0; at < size; ) {
|
|
1434
|
+
const got = readSync2(fd, buf, 0, buf.length, at);
|
|
1435
|
+
if (got <= 0) break;
|
|
1436
|
+
at += got;
|
|
1437
|
+
let i = 0;
|
|
1438
|
+
const sample = (v) => {
|
|
1439
|
+
if (v < lo) lo = v;
|
|
1440
|
+
if (v > hi) hi = v;
|
|
1441
|
+
if (++n === per) {
|
|
1442
|
+
out[k++] = lo >> 8;
|
|
1443
|
+
out[k++] = hi >> 8;
|
|
1444
|
+
lo = 32767;
|
|
1445
|
+
hi = -32768;
|
|
1446
|
+
n = 0;
|
|
1447
|
+
}
|
|
1448
|
+
};
|
|
1449
|
+
if (carry >= 0) {
|
|
1450
|
+
sample((buf[0] << 8 | carry) << 16 >> 16);
|
|
1451
|
+
carry = -1;
|
|
1452
|
+
i = 1;
|
|
1453
|
+
}
|
|
1454
|
+
for (; i + 1 < got; i += 2) sample(buf.readInt16LE(i));
|
|
1455
|
+
if (i < got) carry = buf[i];
|
|
1456
|
+
}
|
|
1457
|
+
} finally {
|
|
1458
|
+
closeSync2(fd);
|
|
1459
|
+
}
|
|
1460
|
+
if (n > 0) {
|
|
1461
|
+
out[k++] = lo >> 8;
|
|
1462
|
+
out[k++] = hi >> 8;
|
|
1463
|
+
}
|
|
1464
|
+
return out.subarray(0, k);
|
|
1465
|
+
}
|
|
1466
|
+
async function runPrep(spec, io, ctx, onStatus) {
|
|
1467
|
+
const s = prepSettingsOf(spec.stepSettings);
|
|
1468
|
+
const ss = spec.stepSettings ?? DEFAULT_STEP_SETTINGS;
|
|
1469
|
+
const cap = Math.min(s.maxShort, spec.maxShort ?? Infinity);
|
|
1470
|
+
const timeoutMs = s.processTimeoutS * 1e3;
|
|
1471
|
+
const WORK = io.workDir;
|
|
1472
|
+
rmSync2(WORK, { recursive: true, force: true });
|
|
1473
|
+
try {
|
|
1474
|
+
const out = join3(WORK, "out");
|
|
1475
|
+
const thumbs = join3(WORK, "thumbs");
|
|
1476
|
+
mkdirSync2(join3(out, "edit"), { recursive: true });
|
|
1477
|
+
mkdirSync2(thumbs, { recursive: true });
|
|
1478
|
+
const src = join3(WORK, "source");
|
|
1479
|
+
const pcm = join3(WORK, "peaks.pcm");
|
|
1480
|
+
const ph = phases(onStatus);
|
|
1481
|
+
ph.emit();
|
|
1482
|
+
needDisk((spec.sourceBytes || 0) * 2, (io.freeBytes ?? freeBytesOf)(WORK));
|
|
1483
|
+
let got = 0;
|
|
1484
|
+
try {
|
|
1485
|
+
await io.download(src, (n2) => {
|
|
1486
|
+
got += n2;
|
|
1487
|
+
ph.st.progress = spec.sourceBytes ? Math.min(0.1, got / spec.sourceBytes * 0.1) : 0;
|
|
1488
|
+
});
|
|
1489
|
+
} catch (e) {
|
|
1490
|
+
throw e instanceof JobError ? e : new JobError(e.message, "source");
|
|
1491
|
+
}
|
|
1492
|
+
if (spec.sourceBytes && statSync3(src).size !== spec.sourceBytes) throw new JobError(`source: got ${statSync3(src).size} of ${spec.sourceBytes} bytes`, "source");
|
|
1493
|
+
ph.next("download", "probe");
|
|
1494
|
+
const { probe, demuxer, fps } = await probeSource(ctx, src);
|
|
1495
|
+
const span = await packetSpan(ctx, src, demuxer, `v:${probe.video}`);
|
|
1496
|
+
const seconds = span.count ? span.end - span.start : probe.videoDuration ?? probe.duration;
|
|
1497
|
+
if (!(seconds > 0)) throw new JobError("unsupported: the picture has no length", "unsupported");
|
|
1498
|
+
const expect = spec.expectDurationS && Number.isFinite(spec.expectDurationS) && spec.expectDurationS > 0 ? spec.expectDurationS : 0;
|
|
1499
|
+
if (expect && seconds < expect - shortfallAllowed(expect)) throw new JobError(`source damaged: the picture has ${seconds.toFixed(1)} of ${expect.toFixed(1)} s (the length the job expects)`, "source");
|
|
1500
|
+
if (seconds > s.maxSeconds) throw new JobError(`unsupported: too long (${Math.round(seconds)} s, at most ${s.maxSeconds} s)`, "unsupported");
|
|
1501
|
+
const audioCodec = probe.hasAudio ? audioCodecOf(await ctx.exec(ctx.ffprobe, ["-v", "error", "-protocol_whitelist", "file", "-f", demuxer, "-show_streams", "-select_streams", "a", "-of", "json", src]), probe.audio ?? 0) : null;
|
|
1502
|
+
const convert = !isEditReady(src, demuxer, probe, cap, audioCodec);
|
|
1503
|
+
const frame = editFrame(probe, cap);
|
|
1504
|
+
const plan = {
|
|
1505
|
+
convert,
|
|
1506
|
+
frame,
|
|
1507
|
+
tile: tileSize(frame, s.tileLongSide),
|
|
1508
|
+
interval: filmstripInterval(seconds, s),
|
|
1509
|
+
thumbnails: Math.min(s.maxFrames, Math.max(1, Math.ceil(seconds / filmstripInterval(seconds, s) - 1e-9))),
|
|
1510
|
+
videoStart: Math.max(0, (probe.videoStart ?? 0) - (probe.fileStart ?? 0)),
|
|
1511
|
+
fps
|
|
1512
|
+
};
|
|
1513
|
+
ph.next("probe", "encode");
|
|
1514
|
+
const media = join3(out, PREP_MEDIA);
|
|
1515
|
+
try {
|
|
1516
|
+
await ctx.exec(ctx.ffmpeg, prepArgs(src, demuxer, probe, plan, s, { media, thumbs, pcm }), { timeoutMs });
|
|
1517
|
+
} catch (e) {
|
|
1518
|
+
const m = e.message;
|
|
1519
|
+
const why = unsupportedReason(m);
|
|
1520
|
+
if (why) throw new JobError(why, "unsupported");
|
|
1521
|
+
throw new JobError(/took longer than/.test(m) ? `prep: ${m}` : `source: FFmpeg couldn't read it (${m.slice(-300)})`, /took longer than/.test(m) ? void 0 : "source");
|
|
1522
|
+
}
|
|
1523
|
+
ph.st.progress = 0.8;
|
|
1524
|
+
ph.next("encode", "check");
|
|
1525
|
+
const ready = convert ? media : src;
|
|
1526
|
+
let readyFps = fps;
|
|
1527
|
+
let readySeconds = seconds;
|
|
1528
|
+
if (convert) {
|
|
1529
|
+
if (!statSync3(media, { throwIfNoEntry: false })) throw new JobError("prep: no edit-ready file written", "source");
|
|
1530
|
+
const r = await probeSource(ctx, media, "the edit-ready file");
|
|
1531
|
+
if (r.probe.videoCodec !== "h264" || probe.hasAudio && !r.probe.hasAudio || !hasMoof(media)) throw new JobError("prep: the edit-ready file isn't fragmented H.264/AAC", "source");
|
|
1532
|
+
const rs = await packetSpan(ctx, media, "mov", "v:0");
|
|
1533
|
+
readySeconds = rs.end - rs.start;
|
|
1534
|
+
readyFps = r.fps;
|
|
1535
|
+
if (readySeconds < seconds - shortfallAllowed(seconds)) throw new JobError(`source damaged: the edit-ready file has ${readySeconds.toFixed(1)} of ${seconds.toFixed(1)} s`, "source");
|
|
1536
|
+
}
|
|
1537
|
+
const count = readdirSync2(thumbs).filter((f) => /^t-\d{5}\.jpg$/.test(f)).length;
|
|
1538
|
+
if (!count) throw new JobError("prep: no picture decoded", "source");
|
|
1539
|
+
if (count < plan.thumbnails - 1) throw new JobError(`source damaged: the filmstrip has ${count} of ${plan.thumbnails} thumbnails`, "source");
|
|
1540
|
+
await ctx.exec(ctx.ffmpeg, sheetArgs2(thumbs, join3(out, "edit"), s), { timeoutMs });
|
|
1541
|
+
rmSync2(thumbs, { recursive: true, force: true });
|
|
1542
|
+
const per = s.columns * s.rows;
|
|
1543
|
+
const sheets = Array.from({ length: Math.ceil(count / per) }, (_, i) => prepSheetName(i));
|
|
1544
|
+
for (const f of sheets) if (!statSync3(join3(out, f), { throwIfNoEntry: false })) throw new JobError(`prep: ${f} is missing`, "source");
|
|
1545
|
+
let peaks = null;
|
|
1546
|
+
if (probe.hasAudio) {
|
|
1547
|
+
if (!statSync3(pcm, { throwIfNoEntry: false }) || statSync3(pcm).size < 2) throw new JobError("prep: the sound decoded to nothing", "source");
|
|
1548
|
+
const data = peaksOf(pcm, peaksSampleRate(s.peaksRate) / s.peaksRate);
|
|
1549
|
+
rmSync2(pcm, { force: true });
|
|
1550
|
+
writeFileSync2(join3(out, PREP_PEAKS), data);
|
|
1551
|
+
peaks = { file: PREP_PEAKS, rate: s.peaksRate, buckets: data.length / 2 };
|
|
1552
|
+
}
|
|
1553
|
+
const result = {
|
|
1554
|
+
format: EDIT_RESULT_FORMAT,
|
|
1555
|
+
converted: convert,
|
|
1556
|
+
media: convert ? PREP_MEDIA : null,
|
|
1557
|
+
durationMs: Math.max(1, Math.round(readySeconds * 1e3)),
|
|
1558
|
+
width: convert ? frame.width : shown(probe).width,
|
|
1559
|
+
height: convert ? frame.height : shown(probe).height,
|
|
1560
|
+
fps: readyFps,
|
|
1561
|
+
hasAudio: !!peaks,
|
|
1562
|
+
bytes: sizeOf(ready),
|
|
1563
|
+
filmstrip: { interval: plan.interval, count, columns: s.columns, rows: s.rows, tileWidth: plan.tile.width, tileHeight: plan.tile.height, sheets },
|
|
1564
|
+
peaks
|
|
1565
|
+
};
|
|
1566
|
+
const problem = prepResultProblem(result, s);
|
|
1567
|
+
if (problem) throw new Error(`prep: ${problem}`);
|
|
1568
|
+
writeFileSync2(join3(out, PREP_RESULT), JSON.stringify(result));
|
|
1569
|
+
rmSync2(src, { force: true });
|
|
1570
|
+
ph.next("check", "upload");
|
|
1571
|
+
const files = listFiles(out);
|
|
1572
|
+
const unknown = files.filter((f) => !outputFile(f, ["prep"], []));
|
|
1573
|
+
if (unknown.length) throw new Error(`unexpected output: ${unknown.slice(0, 3).join(", ")}`);
|
|
1574
|
+
const order = [...files.filter((f) => f === PREP_MEDIA), ...files.filter((f) => f !== PREP_MEDIA && f !== PREP_RESULT).sort(), PREP_RESULT];
|
|
1575
|
+
let n = 0;
|
|
1576
|
+
for (const f of order) {
|
|
1577
|
+
await io.upload(f, join3(out, f));
|
|
1578
|
+
ph.st.progress = 0.9 + 0.1 * (++n / order.length);
|
|
1579
|
+
}
|
|
1580
|
+
ph.next("upload");
|
|
1581
|
+
const c = ctx.counts();
|
|
1582
|
+
const done = {
|
|
1583
|
+
state: "done",
|
|
1584
|
+
progress: 1,
|
|
1585
|
+
ladder: jobVersion(spec.ladder.version, ["prep"], ss),
|
|
1586
|
+
// Proof the expected length was checked (the Worker refuses a done without it).
|
|
1587
|
+
...expect ? { expectDurationS: spec.expectDurationS } : {},
|
|
1588
|
+
timings: { ...ph.st.timings, sourceMB: Math.round(spec.sourceBytes / 1e6), seconds: Math.round(seconds), decodes: c.decodes, processes: c.processes, converted: convert ? 1 : 0 }
|
|
1589
|
+
};
|
|
1590
|
+
onStatus(done);
|
|
1591
|
+
return done;
|
|
1592
|
+
} finally {
|
|
1593
|
+
rmSync2(WORK, { recursive: true, force: true });
|
|
1594
|
+
}
|
|
1595
|
+
}
|
|
1596
|
+
function audioCodecOf(json, i) {
|
|
1597
|
+
const a = (JSON.parse(json).streams ?? []).filter((x) => x.codec_type === "audio")[i];
|
|
1598
|
+
return a ? String(a.codec_name ?? "") : null;
|
|
1599
|
+
}
|
|
1600
|
+
var even3, peaksSampleRate, sheetArgs2;
|
|
1601
|
+
var init_prep = __esm({
|
|
1602
|
+
"../media-worker/build/out/stage/src/steps/prep.ts"() {
|
|
1603
|
+
"use strict";
|
|
1604
|
+
init_media();
|
|
1605
|
+
init_ffmpeg();
|
|
1606
|
+
init_job();
|
|
1607
|
+
init_edit();
|
|
1608
|
+
even3 = (n) => Math.max(2, Math.round(n / 2) * 2);
|
|
1609
|
+
peaksSampleRate = (rate) => rate * Math.max(1, Math.round(8e3 / rate));
|
|
1610
|
+
sheetArgs2 = (thumbs, outDir, s) => [
|
|
1611
|
+
"-hide_banner",
|
|
1612
|
+
"-nostdin",
|
|
1613
|
+
"-y",
|
|
1614
|
+
"-loglevel",
|
|
1615
|
+
"error",
|
|
1616
|
+
"-protocol_whitelist",
|
|
1617
|
+
"file",
|
|
1618
|
+
"-f",
|
|
1619
|
+
"image2",
|
|
1620
|
+
"-framerate",
|
|
1621
|
+
"1",
|
|
1622
|
+
"-i",
|
|
1623
|
+
`${thumbs}/t-%05d.jpg`,
|
|
1624
|
+
"-vf",
|
|
1625
|
+
`tile=${s.columns}x${s.rows}`,
|
|
1626
|
+
"-q:v",
|
|
1627
|
+
String(s.jpegQ),
|
|
1628
|
+
"-start_number",
|
|
1629
|
+
"0",
|
|
1630
|
+
`${outDir}/sb-%03d.jpg`
|
|
1631
|
+
];
|
|
1632
|
+
}
|
|
1633
|
+
});
|
|
1634
|
+
|
|
1635
|
+
// ../media-worker/build/out/stage/src/steps/paths.ts
|
|
1636
|
+
function filterPath(p) {
|
|
1637
|
+
if (!SAFE.test(p)) throw new Error(`work path not usable inside a filter graph: ${JSON.stringify(p).slice(0, 200)}`);
|
|
1638
|
+
return p;
|
|
1639
|
+
}
|
|
1640
|
+
var SAFE;
|
|
1641
|
+
var init_paths = __esm({
|
|
1642
|
+
"../media-worker/build/out/stage/src/steps/paths.ts"() {
|
|
1643
|
+
"use strict";
|
|
1644
|
+
SAFE = /^[A-Za-z0-9/_.+-]+$/;
|
|
1645
|
+
}
|
|
1646
|
+
});
|
|
1647
|
+
|
|
1648
|
+
// ../media-worker/build/out/stage/src/steps/audio.ts
|
|
1649
|
+
function audioFilter(work, s, input = "0:a:0") {
|
|
1650
|
+
const w = audioWork(work);
|
|
1651
|
+
return `[${input}]aformat=channel_layouts=mono,aresample=${AUDIO_RATE}:async=1:first_pts=0,silencedetect=n=${s.vad.noiseDb}dB:d=${s.vad.minSilenceS},ametadata=mode=print:key=lavfi.silence_start:file=${filterPath(w.silenceStart)},ametadata=mode=print:key=lavfi.silence_end:file=${filterPath(w.silenceEnd)}[aud]`;
|
|
1652
|
+
}
|
|
1653
|
+
function audioOutputArgs(out, work, s) {
|
|
1654
|
+
return [
|
|
1655
|
+
"-map",
|
|
1656
|
+
"[aud]",
|
|
1657
|
+
"-c:a",
|
|
1658
|
+
"libopus",
|
|
1659
|
+
"-b:a",
|
|
1660
|
+
`${s.audio.kbps}k`,
|
|
1661
|
+
"-ac",
|
|
1662
|
+
"1",
|
|
1663
|
+
"-ar",
|
|
1664
|
+
String(AUDIO_RATE),
|
|
1665
|
+
"-f",
|
|
1666
|
+
"segment",
|
|
1667
|
+
"-segment_time",
|
|
1668
|
+
String(s.audio.chunkS),
|
|
1669
|
+
"-reset_timestamps",
|
|
1670
|
+
"1",
|
|
1671
|
+
"-segment_list",
|
|
1672
|
+
audioWork(work).list,
|
|
1673
|
+
"-segment_list_type",
|
|
1674
|
+
"csv",
|
|
1675
|
+
`${out}/audio/a-%03d.ogg`
|
|
1676
|
+
];
|
|
1677
|
+
}
|
|
1678
|
+
function parseSilences(startTxt, endTxt, seconds) {
|
|
1679
|
+
const starts = [...startTxt.matchAll(/lavfi\.silence_start=(-?[\d.]+)/g)].map((m) => Math.max(0, Number(m[1])));
|
|
1680
|
+
const ends = [...endTxt.matchAll(/lavfi\.silence_end=(-?[\d.]+)/g)].map((m) => Number(m[1]));
|
|
1681
|
+
const out = [];
|
|
1682
|
+
let j = 0;
|
|
1683
|
+
for (const st of starts) {
|
|
1684
|
+
while (j < ends.length && ends[j] < st) j++;
|
|
1685
|
+
const end = j < ends.length ? ends[j++] : seconds;
|
|
1686
|
+
if (end > st) out.push({ start: Math.round(st * 1e3) / 1e3, end: Math.round(end * 1e3) / 1e3 });
|
|
1687
|
+
}
|
|
1688
|
+
return out;
|
|
1689
|
+
}
|
|
1690
|
+
function parseChunkList(csv) {
|
|
1691
|
+
const out = [];
|
|
1692
|
+
for (const line of csv.split("\n")) {
|
|
1693
|
+
const [file, a, b] = line.trim().split(",");
|
|
1694
|
+
if (!file || !/^a-\d{3,4}\.ogg$/.test(file)) continue;
|
|
1695
|
+
const start = Number(a);
|
|
1696
|
+
const end = Number(b);
|
|
1697
|
+
if (!Number.isFinite(start) || !Number.isFinite(end)) continue;
|
|
1698
|
+
out.push({ file: `audio/${file}`, start, end: Math.max(start, end) });
|
|
1699
|
+
}
|
|
1700
|
+
return out.sort((x, y) => x.start - y.start);
|
|
1701
|
+
}
|
|
1702
|
+
function audioIndex(o, s) {
|
|
1703
|
+
const seconds = o.chunks.length ? Math.max(o.seconds, o.chunks.at(-1).end) : o.hasAudio ? o.seconds : 0;
|
|
1704
|
+
return {
|
|
1705
|
+
format: 1,
|
|
1706
|
+
codec: "opus",
|
|
1707
|
+
sampleRate: AUDIO_RATE,
|
|
1708
|
+
hasAudio: o.hasAudio,
|
|
1709
|
+
seconds: Math.round(seconds * 1e3) / 1e3,
|
|
1710
|
+
chunks: o.chunks,
|
|
1711
|
+
silences: o.silences,
|
|
1712
|
+
vad: { noiseDb: s.vad.noiseDb, minSilenceS: s.vad.minSilenceS }
|
|
1713
|
+
};
|
|
1714
|
+
}
|
|
1715
|
+
var AUDIO_RATE, audioWork;
|
|
1716
|
+
var init_audio = __esm({
|
|
1717
|
+
"../media-worker/build/out/stage/src/steps/audio.ts"() {
|
|
1718
|
+
"use strict";
|
|
1719
|
+
init_paths();
|
|
1720
|
+
AUDIO_RATE = 16e3;
|
|
1721
|
+
audioWork = (work) => ({
|
|
1722
|
+
silenceStart: `${work}/silence-start.txt`,
|
|
1723
|
+
silenceEnd: `${work}/silence-end.txt`,
|
|
1724
|
+
list: `${work}/audio-list.csv`
|
|
1725
|
+
});
|
|
1726
|
+
}
|
|
1727
|
+
});
|
|
1728
|
+
|
|
1729
|
+
// ../media-worker/build/out/stage/src/steps/frameCheck.ts
|
|
1730
|
+
function parseFramePrint(text) {
|
|
1731
|
+
const pts = [];
|
|
1732
|
+
for (const m of text.matchAll(/^frame:\s*\d+\s+pts:\s*(-?\d+)/gm)) {
|
|
1733
|
+
const p = Number(m[1]);
|
|
1734
|
+
if (Number.isFinite(p)) pts.push(p);
|
|
1735
|
+
}
|
|
1736
|
+
if (!pts.length && text.trim()) return null;
|
|
1737
|
+
return { tb: [1, FRAME_TB], pts };
|
|
1738
|
+
}
|
|
1739
|
+
function decoderErrorLines(stderr) {
|
|
1740
|
+
return stderr.split(/\r?\n/).map((l) => l.trim()).filter((l) => l && !/^(?:frame|fps|stream_\d|bitrate|total_size|out_time|speed|progress|dup_frames|drop_frames)=/.test(l));
|
|
1741
|
+
}
|
|
1742
|
+
function median(xs) {
|
|
1743
|
+
if (!xs.length) return 0;
|
|
1744
|
+
const s = [...xs].sort((a, b) => a - b);
|
|
1745
|
+
const m = s.length >>> 1;
|
|
1746
|
+
return s.length % 2 ? s[m] : (s[m - 1] + s[m]) / 2;
|
|
1747
|
+
}
|
|
1748
|
+
function analyzeFrames(input, s) {
|
|
1749
|
+
const sec = input.tb[0] / input.tb[1];
|
|
1750
|
+
const pts = input.pts;
|
|
1751
|
+
const deltas = [];
|
|
1752
|
+
const at = [];
|
|
1753
|
+
const duplicates = [];
|
|
1754
|
+
const outOfOrder = [];
|
|
1755
|
+
let duplicateCount = 0;
|
|
1756
|
+
let outOfOrderCount = 0;
|
|
1757
|
+
let lo = Infinity;
|
|
1758
|
+
let hi = -Infinity;
|
|
1759
|
+
for (const p of pts) {
|
|
1760
|
+
if (p < lo) lo = p;
|
|
1761
|
+
if (p > hi) hi = p;
|
|
1762
|
+
}
|
|
1763
|
+
let last = pts[0];
|
|
1764
|
+
for (let i = 1; i < pts.length; i++) {
|
|
1765
|
+
const d = pts[i] - last;
|
|
1766
|
+
if (d === 0) {
|
|
1767
|
+
duplicateCount++;
|
|
1768
|
+
if (duplicates.length < LIST_MAX) duplicates.push(r6(pts[i] * sec));
|
|
1769
|
+
} else if (d < 0) {
|
|
1770
|
+
outOfOrderCount++;
|
|
1771
|
+
if (outOfOrder.length < LIST_MAX) outOfOrder.push(r6(pts[i] * sec));
|
|
1772
|
+
} else {
|
|
1773
|
+
deltas.push(d);
|
|
1774
|
+
at.push(last);
|
|
1775
|
+
last = pts[i];
|
|
1776
|
+
}
|
|
1777
|
+
}
|
|
1778
|
+
const usual = median(deltas);
|
|
1779
|
+
const gaps = [];
|
|
1780
|
+
let gapCount = 0;
|
|
1781
|
+
let missingFrames = 0;
|
|
1782
|
+
let even4 = 0;
|
|
1783
|
+
let offCadence = 0;
|
|
1784
|
+
const window = [];
|
|
1785
|
+
for (let i = 0; i < deltas.length; i++) {
|
|
1786
|
+
const d = deltas[i];
|
|
1787
|
+
window.length = 0;
|
|
1788
|
+
for (let j = Math.max(0, i - LOCAL); j <= Math.min(deltas.length - 1, i + LOCAL); j++) if (j !== i) window.push(deltas[j]);
|
|
1789
|
+
const local = window.length ? median(window) : usual;
|
|
1790
|
+
if (Math.abs(d - usual) <= usual * s.jitter) even4++;
|
|
1791
|
+
if (Math.abs(local - usual) > usual * s.jitter) offCadence++;
|
|
1792
|
+
if (local > 0 && d >= local * s.gapFactor) {
|
|
1793
|
+
const missing = Math.max(1, Math.round(d / local) - 1);
|
|
1794
|
+
gapCount++;
|
|
1795
|
+
missingFrames += missing;
|
|
1796
|
+
if (gaps.length < LIST_MAX) gaps.push({ at: r6(at[i] * sec), seconds: r6((d - local) * sec), missing });
|
|
1797
|
+
}
|
|
1798
|
+
}
|
|
1799
|
+
const errors = input.errors ?? [];
|
|
1800
|
+
const interval = r6(usual * sec);
|
|
1801
|
+
return {
|
|
1802
|
+
format: 1,
|
|
1803
|
+
ok: pts.length > 0 && gapCount === 0 && duplicateCount === 0 && outOfOrderCount === 0 && errors.length === 0,
|
|
1804
|
+
frames: pts.length,
|
|
1805
|
+
seconds: pts.length ? r6((hi - lo + usual) * sec) : 0,
|
|
1806
|
+
interval,
|
|
1807
|
+
fps: interval > 0 ? Math.round(1 / interval * 1e3) / 1e3 : 0,
|
|
1808
|
+
even: deltas.length ? Math.round(even4 / deltas.length * 1e4) / 1e4 : 1,
|
|
1809
|
+
// More than a tenth of the stream at another cadence than the usual one.
|
|
1810
|
+
variable: deltas.length >= 10 && offCadence / deltas.length > 0.1,
|
|
1811
|
+
gaps,
|
|
1812
|
+
gapCount,
|
|
1813
|
+
missingFrames,
|
|
1814
|
+
duplicates,
|
|
1815
|
+
duplicateCount,
|
|
1816
|
+
outOfOrder,
|
|
1817
|
+
outOfOrderCount,
|
|
1818
|
+
decodeErrors: errors.slice(0, s.maxErrorLines).map((l) => l.slice(0, 300)),
|
|
1819
|
+
decodeErrorCount: errors.length
|
|
1820
|
+
};
|
|
1821
|
+
}
|
|
1822
|
+
var frameCheckWork, frameCheckBranch, FRAME_TB, frameCheckOutputArgs, LIST_MAX, r6, LOCAL;
|
|
1823
|
+
var init_frameCheck2 = __esm({
|
|
1824
|
+
"../media-worker/build/out/stage/src/steps/frameCheck.ts"() {
|
|
1825
|
+
"use strict";
|
|
1826
|
+
init_paths();
|
|
1827
|
+
frameCheckWork = (work) => `${work}/frames.txt`;
|
|
1828
|
+
frameCheckBranch = (input, work) => `${input}scale=16:16:flags=area,format=gray,settb=1/${FRAME_TB},signalstats,metadata=mode=print:key=lavfi.signalstats.YAVG:file=${filterPath(frameCheckWork(work))}[fc]`;
|
|
1829
|
+
FRAME_TB = 1e6;
|
|
1830
|
+
frameCheckOutputArgs = () => ["-map", "[fc]", "-fps_mode", "passthrough", "-f", "null", "-"];
|
|
1831
|
+
LIST_MAX = 100;
|
|
1832
|
+
r6 = (n) => Math.round(n * 1e6) / 1e6;
|
|
1833
|
+
LOCAL = 4;
|
|
1834
|
+
}
|
|
1835
|
+
});
|
|
1836
|
+
|
|
1837
|
+
// ../media-worker/build/out/stage/src/steps/runner.ts
|
|
1838
|
+
var runner_exports = {};
|
|
1839
|
+
__export(runner_exports, {
|
|
1840
|
+
StepError: () => StepError,
|
|
1841
|
+
finishSteps: () => finishSteps,
|
|
1842
|
+
stepArgs: () => stepArgs
|
|
1843
|
+
});
|
|
1844
|
+
import { readdirSync as readdirSync3, readFileSync, rmSync as rmSync3, statSync as statSync4, writeFileSync as writeFileSync3 } from "node:fs";
|
|
1845
|
+
import { join as join4 } from "node:path";
|
|
1846
|
+
function stepArgs(j) {
|
|
1847
|
+
const frameCheck = j.steps.includes("frame-check");
|
|
1848
|
+
const audio = wantsAudio(j.steps) && j.hasAudio;
|
|
1849
|
+
return {
|
|
1850
|
+
videoBranches: (first) => frameCheck ? [frameCheckBranch(`[s${first}]`, j.work)] : [],
|
|
1851
|
+
chains: audio ? [audioFilter(j.work, j.settings, j.audioInput)] : [],
|
|
1852
|
+
outputs: [...audio ? audioOutputArgs(j.out, j.work, j.settings) : [], ...frameCheck ? frameCheckOutputArgs() : []]
|
|
1853
|
+
};
|
|
1854
|
+
}
|
|
1855
|
+
function finishSteps(j) {
|
|
1856
|
+
const txt = (f) => statSync4(f, { throwIfNoEntry: false }) ? readFileSync(f, "utf8") : "";
|
|
1857
|
+
if (wantsAudio(j.steps)) {
|
|
1858
|
+
const w = audioWork(j.work);
|
|
1859
|
+
const chunks = j.hasAudio ? parseChunkList(txt(w.list)) : [];
|
|
1860
|
+
if (j.hasAudio && !chunks.length) throw new StepError("audio: no audio written");
|
|
1861
|
+
const made = readdirSync3(join4(j.out, "audio")).filter((f) => f.endsWith(".ogg")).length;
|
|
1862
|
+
if (made !== chunks.length) throw new StepError(`audio: ${made} chunks written, ${chunks.length} listed`);
|
|
1863
|
+
const seconds = chunks.at(-1)?.end ?? 0;
|
|
1864
|
+
const index = audioIndex({ hasAudio: j.hasAudio, chunks, silences: parseSilences(txt(w.silenceStart), txt(w.silenceEnd), seconds), seconds }, j.settings);
|
|
1865
|
+
writeFileSync3(join4(j.out, AUDIO_INDEX), JSON.stringify(index));
|
|
1866
|
+
}
|
|
1867
|
+
if (j.steps.includes("frame-check")) {
|
|
1868
|
+
const list = frameCheckWork(j.work);
|
|
1869
|
+
const frames = statSync4(list, { throwIfNoEntry: false }) ? parseFramePrint(readFileSync(list, "utf8")) : null;
|
|
1870
|
+
if (!frames) throw new StepError("frame-check: no frame timestamps written");
|
|
1871
|
+
writeFileSync3(join4(j.out, FRAME_CHECK_FILE), JSON.stringify(analyzeFrames({ ...frames, errors: decoderErrorLines(j.stderr) }, j.settings.frameCheck)));
|
|
1872
|
+
rmSync3(list, { force: true });
|
|
1873
|
+
}
|
|
1874
|
+
}
|
|
1875
|
+
var StepError;
|
|
1876
|
+
var init_runner = __esm({
|
|
1877
|
+
"../media-worker/build/out/stage/src/steps/runner.ts"() {
|
|
1878
|
+
"use strict";
|
|
1879
|
+
init_media();
|
|
1880
|
+
init_audio();
|
|
1881
|
+
init_frameCheck2();
|
|
1882
|
+
StepError = class extends Error {
|
|
1883
|
+
code = "source";
|
|
1884
|
+
};
|
|
1885
|
+
}
|
|
1886
|
+
});
|
|
1887
|
+
|
|
1888
|
+
// ../media-worker/build/out/stage/src/storyboard/index.ts
|
|
1889
|
+
var storyboard_exports = {};
|
|
1890
|
+
__export(storyboard_exports, {
|
|
1891
|
+
StoryboardError: () => StoryboardError,
|
|
1892
|
+
cellsFor: () => cellsFor,
|
|
1893
|
+
finishStoryboard: () => finishStoryboard,
|
|
1894
|
+
lastTimes: () => lastTimes,
|
|
1895
|
+
pictureEnd: () => pictureEnd,
|
|
1896
|
+
planStoryboard: () => planStoryboard,
|
|
1897
|
+
printedTimes: () => printedTimes,
|
|
1898
|
+
qualitySteps: () => qualitySteps,
|
|
1899
|
+
sheetArgs: () => sheetArgs3,
|
|
1900
|
+
storyboardBranch: () => storyboardBranch,
|
|
1901
|
+
storyboardOutput: () => storyboardOutput,
|
|
1902
|
+
storyboardSettings: () => storyboardSettings
|
|
1903
|
+
});
|
|
1904
|
+
import { closeSync as closeSync3, linkSync, mkdirSync as mkdirSync3, openSync as openSync3, readFileSync as readFileSync2, readSync as readSync3, readdirSync as readdirSync4, rmSync as rmSync4, statSync as statSync5, writeFileSync as writeFileSync4 } from "node:fs";
|
|
1905
|
+
import { join as join5 } from "node:path";
|
|
1906
|
+
function storyboardBranch(input, plan, work, label = "[sb]") {
|
|
1907
|
+
const w = filterPath2(work);
|
|
1908
|
+
const note = (file) => `metadata=mode=print:key=uf_sb:file=${w}/${file}`;
|
|
1909
|
+
const pick = `select='lt(selected_n\\,${plan.maxFrames})*gte(t\\,selected_n*${plan.interval})'`;
|
|
1910
|
+
return `${input}metadata=mode=add:key=uf_sb:value=1,${note(FRAMES)},${pick},${note(PICKED)},scale=${plan.width}:${plan.height}:flags=bicubic,format=rgb24${label}`;
|
|
1911
|
+
}
|
|
1912
|
+
function filterPath2(p) {
|
|
1913
|
+
if (!SAFE2.test(p)) throw new Error(`storyboard: work path not usable inside a filter graph: ${JSON.stringify(p).slice(0, 200)}`);
|
|
1914
|
+
return p;
|
|
1915
|
+
}
|
|
1916
|
+
function printedTimes(text) {
|
|
1917
|
+
const out = [];
|
|
1918
|
+
for (const m of text.matchAll(/^frame:\d+\s+pts:\S+\s+pts_time:(-?[\d.]+(?:e[-+]?\d+)?)\s*$/gm)) out.push(Number(m[1]));
|
|
1919
|
+
return out.filter((t) => Number.isFinite(t));
|
|
1920
|
+
}
|
|
1921
|
+
function lastTimes(file, n = 33, tail = 8192) {
|
|
1922
|
+
const st = statSync5(file, { throwIfNoEntry: false });
|
|
1923
|
+
if (!st || !st.size) return [];
|
|
1924
|
+
const len = Math.min(st.size, tail);
|
|
1925
|
+
const buf = Buffer.alloc(len);
|
|
1926
|
+
const fd = openSync3(file, "r");
|
|
1927
|
+
try {
|
|
1928
|
+
readSync3(fd, buf, 0, len, st.size - len);
|
|
1929
|
+
} finally {
|
|
1930
|
+
closeSync3(fd);
|
|
1931
|
+
}
|
|
1932
|
+
const text = buf.toString("utf8");
|
|
1933
|
+
return printedTimes(st.size > len ? text.slice(text.indexOf("\n") + 1) : text).slice(-n);
|
|
1934
|
+
}
|
|
1935
|
+
function pictureEnd(times) {
|
|
1936
|
+
if (!times.length) return null;
|
|
1937
|
+
const last = times[times.length - 1];
|
|
1938
|
+
let gap = Infinity;
|
|
1939
|
+
for (let i = 1; i < times.length; i++) if (times[i] > times[i - 1]) gap = Math.min(gap, times[i] - times[i - 1]);
|
|
1940
|
+
return last + (Number.isFinite(gap) ? gap : 0);
|
|
1941
|
+
}
|
|
1942
|
+
function cellsFor(picked, interval, cells) {
|
|
1943
|
+
const out = [];
|
|
1944
|
+
let j = 0;
|
|
1945
|
+
for (let k = 0; k < cells; k++) {
|
|
1946
|
+
const end = (k + 1) * interval - 1e-6;
|
|
1947
|
+
while (j + 1 < picked.length && picked[j + 1] < end) j++;
|
|
1948
|
+
out.push(j);
|
|
1949
|
+
}
|
|
1950
|
+
return out;
|
|
1951
|
+
}
|
|
1952
|
+
function storyboardOutput(thumbs, label = "[sb]") {
|
|
1953
|
+
return ["-map", label, "-fps_mode", "passthrough", "-f", "image2", `${thumbs}/t-%05d.png`];
|
|
1954
|
+
}
|
|
1955
|
+
function sheetArgs3(cells, out, plan, quality) {
|
|
1956
|
+
return [
|
|
1957
|
+
"-hide_banner",
|
|
1958
|
+
"-nostdin",
|
|
1959
|
+
"-y",
|
|
1960
|
+
"-loglevel",
|
|
1961
|
+
"error",
|
|
1962
|
+
"-framerate",
|
|
1963
|
+
"1",
|
|
1964
|
+
"-i",
|
|
1965
|
+
`${cells}/c-%05d.png`,
|
|
1966
|
+
// 4:2:0 in (what lossy WebP stores; libwebp would convert RGB itself), one still per sheet: libwebp, not FFmpeg's
|
|
1967
|
+
// default libwebp_anim for .webp (that writes every sheet as frames of ONE animated file). Best compression: the
|
|
1968
|
+
// sheets are tiny. A last, partial sheet is padded (tile flushes it at the end).
|
|
1969
|
+
"-vf",
|
|
1970
|
+
`tile=${plan.cols}x${plan.rows},format=yuv420p`,
|
|
1971
|
+
"-c:v",
|
|
1972
|
+
"libwebp",
|
|
1973
|
+
"-quality",
|
|
1974
|
+
String(quality),
|
|
1975
|
+
"-compression_level",
|
|
1976
|
+
"6",
|
|
1977
|
+
"-f",
|
|
1978
|
+
"image2",
|
|
1979
|
+
"-start_number",
|
|
1980
|
+
"1",
|
|
1981
|
+
`${out}/storyboard/sb-%03d.webp`
|
|
1982
|
+
];
|
|
1983
|
+
}
|
|
1984
|
+
function qualitySteps(s) {
|
|
1985
|
+
const out = [];
|
|
1986
|
+
for (let q = s.quality; q > s.minQuality; q -= 15) out.push(q);
|
|
1987
|
+
out.push(s.minQuality);
|
|
1988
|
+
return out;
|
|
1989
|
+
}
|
|
1990
|
+
async function finishStoryboard(j) {
|
|
1991
|
+
const { plan, settings } = j;
|
|
1992
|
+
const thumbs = readdirSync4(j.thumbs).filter((f) => /^t-\d{5}\.png$/.test(f)).sort();
|
|
1993
|
+
if (!thumbs.length) throw new StoryboardError("storyboard: no picture");
|
|
1994
|
+
if (thumbs.length > plan.maxFrames) throw new Error(`storyboard: ${thumbs.length} thumbnails, more than ${plan.maxFrames}`);
|
|
1995
|
+
const picked = printedTimes(statSync5(join5(j.thumbs, PICKED), { throwIfNoEntry: false }) ? readFileSync2(join5(j.thumbs, PICKED), "utf8") : "");
|
|
1996
|
+
if (picked.length !== thumbs.length) throw new Error(`storyboard: ${thumbs.length} thumbnails, ${picked.length} times`);
|
|
1997
|
+
const end = pictureEnd(lastTimes(join5(j.thumbs, FRAMES)));
|
|
1998
|
+
if (end === null) throw new Error("storyboard: no frame times written");
|
|
1999
|
+
const s = plan.seconds;
|
|
2000
|
+
if (s > 2 && end < s - j.shortfallAllowed(s)) throw new StoryboardError(`source damaged: the storyboard's picture ends at ${end.toFixed(1)} of ${s.toFixed(1)} s`);
|
|
2001
|
+
const count = Math.min(plan.maxFrames, Math.max(1, plan.expected, Math.ceil(end / plan.interval - 1e-6)));
|
|
2002
|
+
const cells = join5(j.thumbs, "cells");
|
|
2003
|
+
mkdirSync3(cells, { recursive: true });
|
|
2004
|
+
cellsFor(picked, plan.interval, count).forEach((t, k) => linkSync(join5(j.thumbs, thumbs[t]), join5(cells, `c-${String(k + 1).padStart(5, "0")}.png`)));
|
|
2005
|
+
const dir = join5(j.out, "storyboard");
|
|
2006
|
+
const want = sheetCount(plan, count);
|
|
2007
|
+
let made = null;
|
|
2008
|
+
for (const quality of qualitySteps(settings)) {
|
|
2009
|
+
for (const f of listSheets(dir)) rmSync4(join5(dir, f), { force: true });
|
|
2010
|
+
await j.ffmpeg(sheetArgs3(cells, j.out, plan, quality));
|
|
2011
|
+
const sheets = listSheets(dir);
|
|
2012
|
+
if (sheets.length !== want) throw new Error(`storyboard: ${sheets.length} sheets for ${count} thumbnails`);
|
|
2013
|
+
const sizes = sheets.map((f) => statSync5(join5(dir, f)).size);
|
|
2014
|
+
const bytes = sizes.reduce((a, b) => a + b, 0);
|
|
2015
|
+
if (Math.max(...sizes) <= settings.maxSheetBytes && bytes <= settings.maxBytes) {
|
|
2016
|
+
made = { bytes, quality };
|
|
2017
|
+
break;
|
|
2018
|
+
}
|
|
2019
|
+
}
|
|
2020
|
+
if (!made) throw new Error(`storyboard: over its byte budget even at quality ${settings.minQuality}`);
|
|
2021
|
+
rmSync4(j.thumbs, { recursive: true, force: true });
|
|
2022
|
+
writeFileSync4(join5(j.out, STORYBOARD_VTT), storyboardVtt(plan, count));
|
|
2023
|
+
writeFileSync4(join5(j.out, STORYBOARD_INDEX), JSON.stringify(storyboardIndex(plan, count, settings.version, made.bytes)));
|
|
2024
|
+
return { count, sheets: want, ...made };
|
|
2025
|
+
}
|
|
2026
|
+
var StoryboardError, FRAMES, PICKED, SAFE2, listSheets;
|
|
2027
|
+
var init_storyboard2 = __esm({
|
|
2028
|
+
"../media-worker/build/out/stage/src/storyboard/index.ts"() {
|
|
2029
|
+
"use strict";
|
|
2030
|
+
init_media();
|
|
2031
|
+
StoryboardError = class extends Error {
|
|
2032
|
+
code = "source";
|
|
2033
|
+
};
|
|
2034
|
+
FRAMES = "sb-frames.txt";
|
|
2035
|
+
PICKED = "sb-picked.txt";
|
|
2036
|
+
SAFE2 = /^[A-Za-z0-9/_.+-]+$/;
|
|
2037
|
+
listSheets = (dir) => readdirSync4(dir).filter((f) => /^sb-\d{3,4}\.webp$/.test(f)).sort();
|
|
2038
|
+
}
|
|
2039
|
+
});
|
|
2040
|
+
|
|
2041
|
+
// ../media-worker/build/out/stage/src/runner/job.ts
|
|
2042
|
+
import { spawn } from "node:child_process";
|
|
2043
|
+
import { closeSync as closeSync4, mkdirSync as mkdirSync4, openSync as openSync4, readSync as readSync4, readdirSync as readdirSync5, readFileSync as readFileSync3, rmSync as rmSync5, statSync as statSync6, statfsSync, writeFileSync as writeFileSync5 } from "node:fs";
|
|
2044
|
+
import { join as join6 } from "node:path";
|
|
2045
|
+
function freeBytesOf(dir) {
|
|
2046
|
+
try {
|
|
2047
|
+
const s = statfsSync(dir);
|
|
2048
|
+
return Number(s.bavail) * Number(s.bsize);
|
|
2049
|
+
} catch {
|
|
2050
|
+
return Infinity;
|
|
2051
|
+
}
|
|
2052
|
+
}
|
|
2053
|
+
function isFragmentedMp4(path, maxBoxes = 1024) {
|
|
2054
|
+
const fd = openSync4(path, "r");
|
|
2055
|
+
try {
|
|
2056
|
+
const size = statSync6(path).size;
|
|
2057
|
+
const h = Buffer.alloc(16);
|
|
2058
|
+
let at = 0;
|
|
2059
|
+
for (let i = 0; i < maxBoxes && at + 8 <= size; i++) {
|
|
2060
|
+
if (readSync4(fd, h, 0, 16, at) < 8) return false;
|
|
2061
|
+
if (h.toString("latin1", 4, 8) === "moof") return true;
|
|
2062
|
+
let len = h.readUInt32BE(0);
|
|
2063
|
+
if (len === 1) len = Number(h.readBigUInt64BE(8));
|
|
2064
|
+
if (len === 0 || len < 8 || at + len > size) return false;
|
|
2065
|
+
at += len;
|
|
2066
|
+
}
|
|
2067
|
+
return at + 8 <= size;
|
|
2068
|
+
} finally {
|
|
2069
|
+
closeSync4(fd);
|
|
2070
|
+
}
|
|
2071
|
+
}
|
|
2072
|
+
function readHead2(path, n = 64) {
|
|
2073
|
+
const fd = openSync4(path, "r");
|
|
2074
|
+
try {
|
|
2075
|
+
const b = Buffer.alloc(n);
|
|
2076
|
+
const got = readSync4(fd, b, 0, n, 0);
|
|
2077
|
+
return new Uint8Array(b.subarray(0, got));
|
|
2078
|
+
} finally {
|
|
2079
|
+
closeSync4(fd);
|
|
2080
|
+
}
|
|
2081
|
+
}
|
|
2082
|
+
function unsupportedReason(stderr) {
|
|
2083
|
+
if (/No such filter: '?zscale|zscale.*not found/i.test(stderr)) return "HDR: this FFmpeg has no zscale (zimg) to tone-map it to SDR";
|
|
2084
|
+
if (/Decoder \(codec [^)]*\) not found|Decoding requested, but no decoder found|No decoder for|Cannot determine format of input/i.test(stderr)) return "no decoder for the picture's codec";
|
|
2085
|
+
const tag = /Could not find tag for codec (\S+)/i.exec(stderr);
|
|
2086
|
+
if (tag) return `fast start: ${tag[1]} isn't playable in an MP4`;
|
|
2087
|
+
return null;
|
|
2088
|
+
}
|
|
2089
|
+
function ladderProblem(out, rungs, pictureS) {
|
|
2090
|
+
const masterPath = join6(out, "master.m3u8");
|
|
2091
|
+
if (!statSync6(masterPath, { throwIfNoEntry: false })) return "no master playlist";
|
|
2092
|
+
const master = readFileSync3(masterPath, "utf8");
|
|
2093
|
+
const listed = masterVariants(master);
|
|
2094
|
+
for (let i = 0; i < rungs; i++) {
|
|
2095
|
+
const rel = `v${i}/index.m3u8`;
|
|
2096
|
+
if (!listed.includes(rel)) return `the master doesn't list rung ${i} (${listed.length} of ${rungs} listed)`;
|
|
2097
|
+
const pl = join6(out, rel);
|
|
2098
|
+
if (!statSync6(pl, { throwIfNoEntry: false })) return `rung ${i} has no playlist`;
|
|
2099
|
+
const text = readFileSync3(pl, "utf8");
|
|
2100
|
+
const segs = text.split("\n").filter((l) => l && !l.startsWith("#")).map((l) => l.trim());
|
|
2101
|
+
if (!segs.length) return `rung ${i}'s playlist has no segment`;
|
|
2102
|
+
for (const f of segs) if (!statSync6(join6(out, `v${i}`, f), { throwIfNoEntry: false })) return `rung ${i}: ${f} is missing`;
|
|
2103
|
+
const init = /#EXT-X-MAP:URI="([^"]+)"/.exec(text)?.[1];
|
|
2104
|
+
if (!init || !statSync6(join6(out, `v${i}`, init), { throwIfNoEntry: false })) return `rung ${i} has no init segment`;
|
|
2105
|
+
const s = playlistSeconds(pl);
|
|
2106
|
+
if (!(s > 0)) return `rung ${i}'s playlist covers no time`;
|
|
2107
|
+
if (pictureS > 0 && s < pictureS - shortfallAllowed(pictureS)) return `rung ${i} covers ${s.toFixed(2)} of ${pictureS.toFixed(2)} s`;
|
|
2108
|
+
}
|
|
2109
|
+
if (listed.length !== rungs) return `the master lists ${listed.length} variants for ${rungs} rungs`;
|
|
2110
|
+
return null;
|
|
2111
|
+
}
|
|
2112
|
+
function avc1Of(init) {
|
|
2113
|
+
const b = readFileSync3(init);
|
|
2114
|
+
const i = b.indexOf("avcC");
|
|
2115
|
+
if (i < 0 || i + 8 > b.length) return null;
|
|
2116
|
+
const hex = (n) => n.toString(16).padStart(2, "0");
|
|
2117
|
+
return `avc1.${hex(b[i + 5])}${hex(b[i + 6])}${hex(b[i + 7])}`;
|
|
2118
|
+
}
|
|
2119
|
+
function writeMaster(out, rungs, hasAudio) {
|
|
2120
|
+
const lines = ["#EXTM3U", "#EXT-X-VERSION:7"];
|
|
2121
|
+
for (let i = rungs.length - 1; i >= 0; i--) {
|
|
2122
|
+
const pl = join6(out, `v${i}`, "index.m3u8");
|
|
2123
|
+
if (!statSync6(pl, { throwIfNoEntry: false })) return false;
|
|
2124
|
+
const text = readFileSync3(pl, "utf8");
|
|
2125
|
+
const init = /#EXT-X-MAP:URI="([^"]+)"/.exec(text)?.[1];
|
|
2126
|
+
const codec = init && statSync6(join6(out, `v${i}`, init), { throwIfNoEntry: false }) ? avc1Of(join6(out, `v${i}`, init)) : null;
|
|
2127
|
+
if (!codec) return false;
|
|
2128
|
+
let peak = 0;
|
|
2129
|
+
let bits = 0;
|
|
2130
|
+
let secs = 0;
|
|
2131
|
+
const ls = text.split("\n");
|
|
2132
|
+
for (let k = 0; k < ls.length; k++) {
|
|
2133
|
+
const m = /^#EXTINF:([\d.]+)/.exec(ls[k]);
|
|
2134
|
+
const seg = m ? ls.slice(k + 1).find((x) => x && !x.startsWith("#"))?.trim() : void 0;
|
|
2135
|
+
if (!m || !seg) continue;
|
|
2136
|
+
const size = statSync6(join6(out, `v${i}`, seg), { throwIfNoEntry: false })?.size;
|
|
2137
|
+
if (size === void 0) return false;
|
|
2138
|
+
const t = Math.max(Number(m[1]), 1e-3);
|
|
2139
|
+
peak = Math.max(peak, size * 8 / t);
|
|
2140
|
+
bits += size * 8;
|
|
2141
|
+
secs += Number(m[1]);
|
|
2142
|
+
}
|
|
2143
|
+
if (!secs) return false;
|
|
2144
|
+
const r = rungs[i];
|
|
2145
|
+
lines.push(`#EXT-X-STREAM-INF:BANDWIDTH=${Math.round(peak * 1.1)},AVERAGE-BANDWIDTH=${Math.round(bits / secs * 1.1)},RESOLUTION=${r.width}x${r.height},CODECS="${codec}${hasAudio ? ",mp4a.40.2" : ""}"`, `v${i}/index.m3u8`, "");
|
|
2146
|
+
}
|
|
2147
|
+
writeFileSync5(join6(out, "master.m3u8"), lines.join("\n"));
|
|
2148
|
+
return true;
|
|
2149
|
+
}
|
|
2150
|
+
function playlistSeconds(path) {
|
|
2151
|
+
if (!statSync6(path, { throwIfNoEntry: false })) return -1;
|
|
2152
|
+
let t = 0;
|
|
2153
|
+
for (const m of readFileSync3(path, "utf8").matchAll(/^#EXTINF:([\d.]+)/gm)) t += Number(m[1]);
|
|
2154
|
+
return t;
|
|
2155
|
+
}
|
|
2156
|
+
function listFiles2(dir, base = "") {
|
|
2157
|
+
const out = [];
|
|
2158
|
+
for (const d of readdirSync5(join6(dir, base), { withFileTypes: true })) {
|
|
2159
|
+
const rel = base ? `${base}/${d.name}` : d.name;
|
|
2160
|
+
if (d.isDirectory()) out.push(...listFiles2(dir, rel));
|
|
2161
|
+
else out.push(rel);
|
|
2162
|
+
}
|
|
2163
|
+
return out;
|
|
2164
|
+
}
|
|
2165
|
+
function decodesFrames(cmd, args, o) {
|
|
2166
|
+
if (o?.stdinFrom) return true;
|
|
2167
|
+
if (/ffprobe$/.test(cmd)) return args.includes("-show_frames") || args.includes("-skip_frame") || args.some((a) => a.startsWith("frame="));
|
|
2168
|
+
const inputs = args.filter((_, i) => args[i - 1] === "-i");
|
|
2169
|
+
if (inputs.length && inputs.every((f) => /%0\d+d\.(?:png|jpg)$/.test(f))) return false;
|
|
2170
|
+
return args.includes("-filter_complex") || args.some((a) => /^-c:v(:\d+)?$/.test(a) && args[args.indexOf(a) + 1] !== "copy");
|
|
2171
|
+
}
|
|
2172
|
+
async function runJob(spec, io, onStatus = () => {
|
|
2173
|
+
}) {
|
|
2174
|
+
let decodes = 0;
|
|
2175
|
+
let execs = 0;
|
|
2176
|
+
const base = io.exec ?? spawnExec;
|
|
2177
|
+
const exec = (cmd, args, o) => {
|
|
2178
|
+
execs++;
|
|
2179
|
+
if (decodesFrames(cmd, args, o)) decodes++;
|
|
2180
|
+
return base(cmd, args, o);
|
|
2181
|
+
};
|
|
2182
|
+
const ffmpeg = io.ffmpeg ?? "ffmpeg";
|
|
2183
|
+
const ffprobe = io.ffprobe ?? "ffprobe";
|
|
2184
|
+
const WORK = io.workDir;
|
|
2185
|
+
if (isEditJob(spec.steps)) {
|
|
2186
|
+
const ctx = { exec, ffmpeg, ffprobe, counts: () => ({ decodes, processes: execs }) };
|
|
2187
|
+
if (spec.steps.includes("render")) return (await Promise.resolve().then(() => (init_render(), render_exports))).runRender(spec, io, ctx, onStatus);
|
|
2188
|
+
return (await Promise.resolve().then(() => (init_prep(), prep_exports))).runPrep(spec, io, ctx, onStatus);
|
|
2189
|
+
}
|
|
2190
|
+
const steps = spec.steps;
|
|
2191
|
+
const l = spec.ladder;
|
|
2192
|
+
const ss = spec.stepSettings ?? DEFAULT_STEP_SETTINGS;
|
|
2193
|
+
const fastStart = steps.includes("faststart");
|
|
2194
|
+
const skippable = fastStart && steps.includes("ladder");
|
|
2195
|
+
let fastSkipped = null;
|
|
2196
|
+
const passSteps = fastStart ? steps.filter((x) => x !== "faststart") : steps;
|
|
2197
|
+
const expect = spec.expectDurationS && Number.isFinite(spec.expectDurationS) && spec.expectDurationS > 0 ? spec.expectDurationS : 0;
|
|
2198
|
+
rmSync5(WORK, { recursive: true, force: true });
|
|
2199
|
+
try {
|
|
2200
|
+
const out = join6(WORK, "out");
|
|
2201
|
+
mkdirSync4(out, { recursive: true });
|
|
2202
|
+
const thumbs = join6(WORK, "thumbs");
|
|
2203
|
+
if (steps.includes("storyboard")) {
|
|
2204
|
+
mkdirSync4(join6(out, "storyboard"), { recursive: true });
|
|
2205
|
+
mkdirSync4(thumbs, { recursive: true });
|
|
2206
|
+
}
|
|
2207
|
+
if (wantsAudio(steps)) mkdirSync4(join6(out, "audio"), { recursive: true });
|
|
2208
|
+
const src = join6(WORK, "source");
|
|
2209
|
+
const st = { state: "running", phase: "download", progress: 0, timings: {} };
|
|
2210
|
+
const emit = () => onStatus({ ...st, timings: { ...st.timings } });
|
|
2211
|
+
emit();
|
|
2212
|
+
let t = Date.now();
|
|
2213
|
+
const lap = (name) => {
|
|
2214
|
+
st.timings[name] = Math.round((Date.now() - t) / 100) / 10;
|
|
2215
|
+
t = Date.now();
|
|
2216
|
+
};
|
|
2217
|
+
const need = (spec.sourceBytes || 0) * (fastStart ? 2 : 1);
|
|
2218
|
+
const free = (io.freeBytes ?? freeBytesOf)(WORK);
|
|
2219
|
+
if (need > free) throw new JobError(`disk: the job needs at least ${Math.ceil(need / 1e6)} MB, the runner has ${Math.floor(free / 1e6)} MB free`, "disk");
|
|
2220
|
+
let got = 0;
|
|
2221
|
+
try {
|
|
2222
|
+
await io.download(src, (n) => {
|
|
2223
|
+
got += n;
|
|
2224
|
+
st.progress = spec.sourceBytes ? Math.min(0.1, got / spec.sourceBytes * 0.1) : 0;
|
|
2225
|
+
});
|
|
2226
|
+
} catch (e) {
|
|
2227
|
+
throw e instanceof JobError ? e : new JobError(e.message, "source");
|
|
2228
|
+
}
|
|
2229
|
+
if (spec.sourceBytes && statSync6(src).size !== spec.sourceBytes) throw new JobError(`source: got ${statSync6(src).size} of ${spec.sourceBytes} bytes`, "source");
|
|
2230
|
+
lap("download");
|
|
2231
|
+
st.phase = "probe";
|
|
2232
|
+
emit();
|
|
2233
|
+
let json = null;
|
|
2234
|
+
let probeErr = null;
|
|
2235
|
+
try {
|
|
2236
|
+
json = await exec(ffprobe, ["-v", "error", "-show_streams", "-show_format", "-of", "json", src]);
|
|
2237
|
+
} catch (e) {
|
|
2238
|
+
probeErr = e;
|
|
2239
|
+
}
|
|
2240
|
+
const header = json ? parseProbe(json, "") : null;
|
|
2241
|
+
const plugin = pickPlugin(spec.plugins, { head: readHead2(src), ffprobe: header, sourceBytes: spec.sourceBytes });
|
|
2242
|
+
let probe;
|
|
2243
|
+
let inputArgs;
|
|
2244
|
+
let feed;
|
|
2245
|
+
let scanned = 0;
|
|
2246
|
+
if (plugin) {
|
|
2247
|
+
if (fastStart) {
|
|
2248
|
+
const why = `faststart: ${plugin.name} sources aren't a container FFmpeg can remux`;
|
|
2249
|
+
if (!skippable) throw new JobError(why, "unsupported");
|
|
2250
|
+
fastSkipped = why;
|
|
2251
|
+
}
|
|
2252
|
+
const d = await plugin.open(src, { head: readHead2(src), ffprobe: header, sourceBytes: spec.sourceBytes });
|
|
2253
|
+
probe = probeOfDecoded(d);
|
|
2254
|
+
inputArgs = d.inputArgs;
|
|
2255
|
+
feed = { cmd: d.command, args: d.args };
|
|
2256
|
+
} else {
|
|
2257
|
+
if (probeErr) throw new JobError(probeErr.message, "unsupported");
|
|
2258
|
+
if (header && CAMERA_RAW_CODECS.has(header.videoCodec)) throw new JobError(`no decoder for the picture's codec (${header.videoCodec || "unknown"}: camera RAW without its plugin)`, "unsupported");
|
|
2259
|
+
if (header?.still) throw new JobError("unsupported: still picture (a photo, not a video)", "unsupported");
|
|
2260
|
+
if (header && fastStart && NOT_IN_MP4.has(header.videoCodec)) {
|
|
2261
|
+
const why = `fast start: ${header.videoCodec} isn't playable in an MP4`;
|
|
2262
|
+
if (!skippable) throw new JobError(why, "unsupported");
|
|
2263
|
+
fastSkipped = why;
|
|
2264
|
+
}
|
|
2265
|
+
const soundRunsOn = !!header && header.audioStart !== void 0 && !!header.audioDuration && header.videoStart !== void 0 && !!header.videoDuration && header.audioStart + header.audioDuration > header.videoStart + header.videoDuration + 0.1;
|
|
2266
|
+
const fragmented = !!header && (steps.includes("ladder") || steps.includes("storyboard")) && isFragmentedMp4(src);
|
|
2267
|
+
if (header && (needsKeyframes(header, l, steps, spec.maxShort) || header.sparseRate && steps.includes("ladder") || soundRunsOn && steps.includes("ladder") || header.vfr && steps.includes("ladder") || fragmented)) {
|
|
2268
|
+
scanned = 1;
|
|
2269
|
+
let errors = "";
|
|
2270
|
+
const facts = packetFacts(await exec(ffprobe, keyframeProbeArgs(src, header.video, fragmented), { onStderr: (x) => void (errors.length < 1e4 && (errors += x)) }));
|
|
2271
|
+
probe = parseProbe(json, facts.keyframes);
|
|
2272
|
+
if (probe) {
|
|
2273
|
+
if (facts.editList) probe.editList = true;
|
|
2274
|
+
if (facts.vfr) probe.vfr = true;
|
|
2275
|
+
if (errors.trim()) probe.packetErrors = true;
|
|
2276
|
+
if (facts.last !== void 0) probe = fragmented ? withPacketEnd(probe, { last: facts.last, end: facts.end }) : { ...probe, lastFrameAt: facts.last };
|
|
2277
|
+
if (probe.vfr && !probe.sparseRate && facts.maxGap !== void 0 && facts.maxGap > STILL_GAP && facts.minGap) probe.sparseRate = Math.min(60, Math.round(1e3 / facts.minGap) / 1e3);
|
|
2278
|
+
}
|
|
2279
|
+
} else probe = header;
|
|
2280
|
+
}
|
|
2281
|
+
if (!probe || !probe.width || !probe.height) throw new JobError("no video stream in the source", "unsupported");
|
|
2282
|
+
if (probe.audioUndecodable && (wantsAudio(steps) || steps.includes("ladder"))) throw new JobError("no decoder for the source's audio (e.g. APAC spatial audio)", "unsupported");
|
|
2283
|
+
const duration = probe.duration;
|
|
2284
|
+
lap("probe");
|
|
2285
|
+
st.phase = "encode";
|
|
2286
|
+
emit();
|
|
2287
|
+
const extra = hasExtraSteps(steps) ? await Promise.resolve().then(() => (init_runner(), runner_exports)) : null;
|
|
2288
|
+
const stepJob = { steps, hasAudio: probe.hasAudio, settings: ss, work: WORK, out };
|
|
2289
|
+
const stepArgsFor = (p) => extra?.stepArgs({ ...stepJob, ...p.audio ? { audioInput: audioIn(p) } : {} });
|
|
2290
|
+
const stepArgs2 = stepArgsFor(probe);
|
|
2291
|
+
const sbv2 = storyboardV2(steps, ss) ? await Promise.resolve().then(() => (init_storyboard2(), storyboard_exports)) : null;
|
|
2292
|
+
const sbPlan = sbv2?.planStoryboard({ ...shown(probe), seconds: pictureSeconds(probe) }, sbv2.storyboardSettings(ss.version, ss.storyboard));
|
|
2293
|
+
const storyboard = sbv2 && sbPlan ? { branch: (i) => sbv2.storyboardBranch(i, sbPlan, thumbs), outputs: sbv2.storyboardOutput(thumbs) } : void 0;
|
|
2294
|
+
let args = jobArgs(feed ? "pipe:0" : src, out, probe, { ladder: l, maxShort: spec.maxShort, steps: passSteps, inputArgs, thumbs, ...stepArgs2 ? { stepArgs: stepArgs2 } : {}, ...storyboard ? { storyboard } : {} });
|
|
2295
|
+
let outS = -1;
|
|
2296
|
+
let stderr = "";
|
|
2297
|
+
const onStderr = { onStderr: (x) => void (stderr.length < 1e6 && (stderr += x)) };
|
|
2298
|
+
let decodesBefore = decodes;
|
|
2299
|
+
if (ffmpegHasWork(passSteps, probe)) for (; ; ) try {
|
|
2300
|
+
decodesBefore = decodes;
|
|
2301
|
+
await exec(ffmpeg, args, {
|
|
2302
|
+
onStdout: (s) => {
|
|
2303
|
+
for (const m of s.matchAll(/out_time_us=(\d+)/g)) outS = Math.max(outS, Number(m[1]) / 1e6);
|
|
2304
|
+
if (outS >= 0 && duration > 0) st.progress = 0.1 + Math.min(0.8, outS / duration * 0.8);
|
|
2305
|
+
},
|
|
2306
|
+
...feed ? { stdinFrom: feed, earlyStopOk: passSteps.every((x) => x === "poster") } : {},
|
|
2307
|
+
...onStderr
|
|
2308
|
+
});
|
|
2309
|
+
break;
|
|
2310
|
+
} catch (e) {
|
|
2311
|
+
const why = feed ? null : unsupportedReason(e.message);
|
|
2312
|
+
const named = /no decoder found for:\s*([\w.-]+)/i.exec(e.message)?.[1];
|
|
2313
|
+
const next = probe.audioFallbacks?.[0];
|
|
2314
|
+
if (why?.startsWith("no decoder") && next !== void 0 && named && named !== probe.videoCodec) {
|
|
2315
|
+
const again = parseProbe(json, "", next);
|
|
2316
|
+
probe = { ...probe, hasAudio: !!again?.hasAudio, audio: next, audioFallbacks: again?.audioFallbacks, audioStart: again?.audioStart, audioDuration: again?.audioDuration };
|
|
2317
|
+
decodes = decodesBefore;
|
|
2318
|
+
const runsOn = probe.audioStart !== void 0 && !!probe.audioDuration && probe.videoStart !== void 0 && !!probe.videoDuration && probe.audioStart + probe.audioDuration > probe.videoStart + probe.videoDuration + 0.1;
|
|
2319
|
+
if (runsOn && probe.lastFrameAt === void 0 && steps.includes("ladder")) {
|
|
2320
|
+
scanned = 1;
|
|
2321
|
+
const facts = packetFacts(await exec(ffprobe, keyframeProbeArgs(src, probe.video)));
|
|
2322
|
+
if (facts.last !== void 0) probe = { ...probe, lastFrameAt: facts.last };
|
|
2323
|
+
}
|
|
2324
|
+
rmSync5(out, { recursive: true, force: true });
|
|
2325
|
+
rmSync5(thumbs, { recursive: true, force: true });
|
|
2326
|
+
mkdirSync4(out, { recursive: true });
|
|
2327
|
+
if (steps.includes("storyboard")) mkdirSync4(join6(out, "storyboard"), { recursive: true }), mkdirSync4(thumbs, { recursive: true });
|
|
2328
|
+
if (wantsAudio(steps)) mkdirSync4(join6(out, "audio"), { recursive: true });
|
|
2329
|
+
const sa = stepArgsFor(probe);
|
|
2330
|
+
args = jobArgs(feed ? "pipe:0" : src, out, probe, { ladder: l, maxShort: spec.maxShort, steps: passSteps, inputArgs, thumbs, ...sa ? { stepArgs: sa } : {}, ...storyboard ? { storyboard } : {} });
|
|
2331
|
+
stderr = "";
|
|
2332
|
+
continue;
|
|
2333
|
+
}
|
|
2334
|
+
throw why ? new JobError(named && named !== probe.videoCodec ? `no decoder for the ${named} audio` : why, "unsupported") : e;
|
|
2335
|
+
}
|
|
2336
|
+
lap("encode");
|
|
2337
|
+
for (const st2 of passSteps) {
|
|
2338
|
+
const f = st2 === "ladder" ? "master.m3u8" : st2 === "poster" ? "poster.jpg" : null;
|
|
2339
|
+
if (f && !statSync6(join6(out, f), { throwIfNoEntry: false })) throw new JobError(`missing output: ${f}`, "source");
|
|
2340
|
+
}
|
|
2341
|
+
const short = (made, what, of = duration) => {
|
|
2342
|
+
const want = Math.max(of, expect);
|
|
2343
|
+
if (made >= 0 && want > 2 && made < want - shortfallAllowed(want))
|
|
2344
|
+
throw new JobError(`source damaged: ${what} has ${made.toFixed(1)} of ${want.toFixed(1)} s${want > of ? " (the length the job expects)" : ""}`, "source");
|
|
2345
|
+
};
|
|
2346
|
+
if (steps.includes("ladder")) {
|
|
2347
|
+
const picture = pictureSeconds(probe);
|
|
2348
|
+
const d = displayed(probe);
|
|
2349
|
+
const ladderRungs = rungsFor(l.rungs, d.width, d.height, spec.maxShort);
|
|
2350
|
+
const masterPath = join6(out, "master.m3u8");
|
|
2351
|
+
if (statSync6(masterPath, { throwIfNoEntry: false }) && masterVariants(readFileSync3(masterPath, "utf8")).length < ladderRungs.length) writeMaster(out, ladderRungs, probe.hasAudio);
|
|
2352
|
+
short(playlistSeconds(join6(out, "v0", "index.m3u8")), "the ladder", picture);
|
|
2353
|
+
if (args.some((a, i) => /^-c:v:\d+$/.test(a) && args[i + 1] === "copy") && DAMAGED_DECODE.test(stderr)) {
|
|
2354
|
+
throw new JobError(`source damaged: decoding found damaged picture data (${DAMAGED_DECODE.exec(stderr)[0]}), which a copied rung would carry`, "source");
|
|
2355
|
+
}
|
|
2356
|
+
const problem = ladderProblem(out, ladderRungs.length, Math.max(picture, expect));
|
|
2357
|
+
if (problem) throw new Error(`ladder incomplete: ${problem}`);
|
|
2358
|
+
}
|
|
2359
|
+
if (extra) {
|
|
2360
|
+
try {
|
|
2361
|
+
extra.finishSteps({ ...stepJob, stderr });
|
|
2362
|
+
} catch (e) {
|
|
2363
|
+
throw new JobError(e.message, "source");
|
|
2364
|
+
}
|
|
2365
|
+
}
|
|
2366
|
+
if (fastStart && !fastSkipped) {
|
|
2367
|
+
const file = join6(out, "faststart.mp4");
|
|
2368
|
+
try {
|
|
2369
|
+
if (feed) throw new JobError("faststart: a plugin's source isn't a container FFmpeg can remux", "unsupported");
|
|
2370
|
+
try {
|
|
2371
|
+
await exec(ffmpeg, fastStartArgs(src, out, probe));
|
|
2372
|
+
} catch (e) {
|
|
2373
|
+
const why = unsupportedReason(e.message);
|
|
2374
|
+
throw why ? new JobError(why, "unsupported") : e;
|
|
2375
|
+
}
|
|
2376
|
+
if (!statSync6(file, { throwIfNoEntry: false })) throw new JobError("missing output: faststart.mp4", "source");
|
|
2377
|
+
short(Number(JSON.parse(await exec(ffprobe, ["-v", "error", "-show_format", "-of", "json", file])).format?.duration ?? 0), "the fast-start copy");
|
|
2378
|
+
} catch (e) {
|
|
2379
|
+
if (!skippable) throw e;
|
|
2380
|
+
fastSkipped = (e?.message || String(e)).slice(0, 500);
|
|
2381
|
+
rmSync5(file, { force: true });
|
|
2382
|
+
}
|
|
2383
|
+
}
|
|
2384
|
+
rmSync5(src, { force: true });
|
|
2385
|
+
if (steps.includes("ladder")) {
|
|
2386
|
+
const masterPath = join6(out, "master.m3u8");
|
|
2387
|
+
writeFileSync5(masterPath, bestFirst(readFileSync3(masterPath, "utf8")));
|
|
2388
|
+
}
|
|
2389
|
+
if (sbv2 && sbPlan) {
|
|
2390
|
+
try {
|
|
2391
|
+
await sbv2.finishStoryboard({ plan: sbPlan, settings: sbv2.storyboardSettings(ss.version, ss.storyboard), thumbs, out, ffmpeg: (a) => exec(ffmpeg, a), shortfallAllowed });
|
|
2392
|
+
} catch (e) {
|
|
2393
|
+
throw e instanceof sbv2.StoryboardError ? new JobError(e.message, "source") : e;
|
|
2394
|
+
}
|
|
2395
|
+
} else if (steps.includes("storyboard")) {
|
|
2396
|
+
const plan = storyboardPlan(probe);
|
|
2397
|
+
const count = readdirSync5(thumbs).filter((f) => f.endsWith(".jpg")).length;
|
|
2398
|
+
if (!count) throw new JobError("storyboard: no picture", "source");
|
|
2399
|
+
const seconds = pictureSeconds(probe);
|
|
2400
|
+
if (seconds > 2 && count * plan.interval < seconds - shortfallAllowed(seconds) - plan.interval) throw new JobError(`source damaged: the storyboard has ${count * plan.interval} of ${seconds.toFixed(1)} s`, "source");
|
|
2401
|
+
await exec(ffmpeg, sheetArgs(thumbs, out, plan));
|
|
2402
|
+
const sheets = readdirSync5(join6(out, "storyboard")).filter((f) => f.endsWith(".jpg")).sort();
|
|
2403
|
+
if (sheets.length !== Math.ceil(count / (plan.cols * plan.rows))) throw new Error(`storyboard: ${sheets.length} sheets for ${count} pictures`);
|
|
2404
|
+
writeFileSync5(join6(out, "storyboard", "storyboard.json"), JSON.stringify({ ...plan, count, sheets: sheets.map((f) => `storyboard/${f}`) }));
|
|
2405
|
+
}
|
|
2406
|
+
st.phase = "upload";
|
|
2407
|
+
emit();
|
|
2408
|
+
const last = completionFile(steps);
|
|
2409
|
+
const seg = [l.segmentName, ...l.legacySegmentNames ?? []];
|
|
2410
|
+
const files = listFiles2(out).filter((f) => f !== last);
|
|
2411
|
+
const unknown = files.filter((f) => !outputFile(f, steps, seg));
|
|
2412
|
+
if (unknown.length) throw new Error(`unexpected output: ${unknown.slice(0, 3).join(", ")}`);
|
|
2413
|
+
if (!statSync6(join6(out, last), { throwIfNoEntry: false })) throw new Error(`missing output: ${last}`);
|
|
2414
|
+
const isIndex = (f) => f.endsWith(".m3u8") || f === "storyboard/storyboard.json" || f === AUDIO_INDEX;
|
|
2415
|
+
files.sort((a, b) => a.localeCompare(b));
|
|
2416
|
+
const rungs = new Set([...files, last].map((f) => f.split("/")[0]).filter((d) => /^v\d/.test(d))).size;
|
|
2417
|
+
let doneCount = 0;
|
|
2418
|
+
const total = files.length + 1;
|
|
2419
|
+
const uploadAll = async (list) => {
|
|
2420
|
+
let i = 0;
|
|
2421
|
+
let failed = false;
|
|
2422
|
+
const next = async () => {
|
|
2423
|
+
while (!failed && i < list.length) {
|
|
2424
|
+
const f = list[i++];
|
|
2425
|
+
try {
|
|
2426
|
+
await io.upload(f, join6(out, f));
|
|
2427
|
+
} catch (e) {
|
|
2428
|
+
failed = true;
|
|
2429
|
+
throw e;
|
|
2430
|
+
}
|
|
2431
|
+
st.progress = 0.9 + 0.1 * (++doneCount / total);
|
|
2432
|
+
}
|
|
2433
|
+
};
|
|
2434
|
+
const settled = await Promise.allSettled(Array.from({ length: io.uploads ?? 4 }, next));
|
|
2435
|
+
const firstError = settled.find((x) => x.status === "rejected");
|
|
2436
|
+
if (firstError) throw firstError.reason;
|
|
2437
|
+
};
|
|
2438
|
+
await uploadAll(files.filter((f) => !isIndex(f)));
|
|
2439
|
+
await uploadAll(files.filter(isIndex));
|
|
2440
|
+
await io.upload(last, join6(out, last));
|
|
2441
|
+
lap("upload");
|
|
2442
|
+
const timings = {
|
|
2443
|
+
...st.timings,
|
|
2444
|
+
sourceMB: Math.round(spec.sourceBytes / 1e6),
|
|
2445
|
+
seconds: Math.round(duration),
|
|
2446
|
+
// Processes that decoded the source's frames (the plugin's, or ffmpeg's): one per job, none for a remux alone.
|
|
2447
|
+
decodes,
|
|
2448
|
+
processes: execs,
|
|
2449
|
+
keyframeScan: scanned,
|
|
2450
|
+
// Audio streams none of which decode here: the ladder has no sound (said, not hidden).
|
|
2451
|
+
...probe.audioUndecodable ? { undecodableAudio: 1 } : {}
|
|
2452
|
+
};
|
|
2453
|
+
const done = {
|
|
2454
|
+
state: "done",
|
|
2455
|
+
progress: 1,
|
|
2456
|
+
...steps.includes("ladder") ? { rungs } : {},
|
|
2457
|
+
ladder: jobVersion(l.version, steps, ss),
|
|
2458
|
+
timings,
|
|
2459
|
+
...fastStart ? { faststart: fastSkipped ? { state: "skipped", reason: fastSkipped } : { state: "done" } } : {},
|
|
2460
|
+
// Proof the expected length was enforced (an older image ignores the field and sends no echo).
|
|
2461
|
+
...spec.expectDurationS !== void 0 && expect ? { expectDurationS: spec.expectDurationS } : {}
|
|
2462
|
+
};
|
|
2463
|
+
onStatus(done);
|
|
2464
|
+
return done;
|
|
2465
|
+
} finally {
|
|
2466
|
+
rmSync5(WORK, { recursive: true, force: true });
|
|
2467
|
+
}
|
|
2468
|
+
}
|
|
2469
|
+
var JobError, spawnExec, shortfallAllowed, STILL_GAP, CAMERA_RAW_CODECS, DAMAGED_DECODE, NOT_IN_MP4, masterVariants;
|
|
2470
|
+
var init_job = __esm({
|
|
2471
|
+
"../media-worker/build/out/stage/src/runner/job.ts"() {
|
|
2472
|
+
"use strict";
|
|
2473
|
+
init_media();
|
|
2474
|
+
init_ffmpeg();
|
|
2475
|
+
init_plugins();
|
|
2476
|
+
JobError = class extends Error {
|
|
2477
|
+
code;
|
|
2478
|
+
constructor(message, code) {
|
|
2479
|
+
super(message);
|
|
2480
|
+
this.code = code;
|
|
2481
|
+
}
|
|
2482
|
+
};
|
|
2483
|
+
spawnExec = (cmd, args, o = {}) => new Promise((resolve, reject) => {
|
|
2484
|
+
const feeder = o.stdinFrom ? spawn(o.stdinFrom.cmd, o.stdinFrom.args, { stdio: ["ignore", "pipe", "pipe"] }) : null;
|
|
2485
|
+
const p = spawn(cmd, args, { stdio: [feeder ? "pipe" : "ignore", "pipe", "pipe"] });
|
|
2486
|
+
let feedErr = "";
|
|
2487
|
+
const feedDone = feeder ? new Promise((r) => feeder.on("close", (code, signal) => r({ code, signal }))) : Promise.resolve({ code: 0, signal: null });
|
|
2488
|
+
let feederRunning = !!feeder;
|
|
2489
|
+
void feedDone.then(() => feederRunning = false);
|
|
2490
|
+
if (feeder) {
|
|
2491
|
+
feeder.stdout.pipe(p.stdin);
|
|
2492
|
+
p.stdin.on("error", () => {
|
|
2493
|
+
});
|
|
2494
|
+
feeder.stderr.on("data", (d) => feedErr = (feedErr + String(d)).slice(-2e3));
|
|
2495
|
+
feeder.on("error", (e) => reject(e));
|
|
2496
|
+
}
|
|
2497
|
+
let out = "";
|
|
2498
|
+
let err = "";
|
|
2499
|
+
p.stdout.on("data", (d) => {
|
|
2500
|
+
const s = String(d);
|
|
2501
|
+
if (o.onStdout) o.onStdout(s);
|
|
2502
|
+
else out += s;
|
|
2503
|
+
});
|
|
2504
|
+
p.stderr.on("data", (d) => {
|
|
2505
|
+
o.onStderr?.(String(d));
|
|
2506
|
+
err = (err + String(d)).slice(-4e3);
|
|
2507
|
+
});
|
|
2508
|
+
p.on("error", reject);
|
|
2509
|
+
let timedOut = false;
|
|
2510
|
+
const timer = o.timeoutMs ? setTimeout(() => (timedOut = true, p.kill("SIGKILL"), feeder?.kill("SIGKILL")), o.timeoutMs) : null;
|
|
2511
|
+
p.on("close", async (code) => {
|
|
2512
|
+
if (timer) clearTimeout(timer);
|
|
2513
|
+
if (timedOut) return reject(new Error(`${cmd} took longer than ${Math.round(o.timeoutMs / 1e3)} s`));
|
|
2514
|
+
if (code !== 0) {
|
|
2515
|
+
feeder?.kill("SIGKILL");
|
|
2516
|
+
return reject(new Error(`${cmd} exited ${code}: ${err.trim().slice(-800)}`));
|
|
2517
|
+
}
|
|
2518
|
+
if (feeder && feederRunning && o.earlyStopOk) {
|
|
2519
|
+
feeder.stdout.destroy();
|
|
2520
|
+
feeder.kill("SIGKILL");
|
|
2521
|
+
return resolve(out);
|
|
2522
|
+
}
|
|
2523
|
+
if (feeder) {
|
|
2524
|
+
feeder.stdout.unpipe(p.stdin);
|
|
2525
|
+
feeder.stdout.destroy();
|
|
2526
|
+
const t = setTimeout(() => feeder.kill("SIGKILL"), 1e4);
|
|
2527
|
+
const f = await feedDone;
|
|
2528
|
+
clearTimeout(t);
|
|
2529
|
+
if (f.code !== 0) return reject(new Error(`${o.stdinFrom.cmd} ${f.signal ? `killed by ${f.signal}` : `exited ${f.code}`}: ${feedErr.trim().slice(-800)}`));
|
|
2530
|
+
}
|
|
2531
|
+
resolve(out);
|
|
2532
|
+
});
|
|
2533
|
+
});
|
|
2534
|
+
shortfallAllowed = (duration) => Math.max(1.5, duration * 0.01);
|
|
2535
|
+
STILL_GAP = 0.25;
|
|
2536
|
+
CAMERA_RAW_CODECS = /* @__PURE__ */ new Set(["", "unknown", "none", "prores_raw"]);
|
|
2537
|
+
DAMAGED_DECODE = /error while decoding MB|cabac decode of qscale diff failed|Invalid NAL unit size|corrupted macroblock|concealing \d+ DC, \d+ AC, \d+ MV errors|left block unavailable for requested intra/i;
|
|
2538
|
+
NOT_IN_MP4 = /* @__PURE__ */ new Set(["prores", "dnxhd", "dvvideo", "cfhd", "rawvideo", "v210", "ffv1", "huffyuv", "utvideo", "qtrle", "png", "tiff"]);
|
|
2539
|
+
masterVariants = (master) => master.split("\n").filter((l) => l && !l.startsWith("#")).map((l) => l.trim());
|
|
2540
|
+
}
|
|
2541
|
+
});
|
|
2542
|
+
|
|
2543
|
+
// ../media-worker/build/out/stage/src/runner/container.ts
|
|
2544
|
+
init_media();
|
|
2545
|
+
init_job();
|
|
2546
|
+
import { createReadStream, createWriteStream } from "node:fs";
|
|
2547
|
+
import { stat } from "node:fs/promises";
|
|
2548
|
+
import { createServer } from "node:http";
|
|
2549
|
+
import { Readable, Transform } from "node:stream";
|
|
2550
|
+
import { pipeline } from "node:stream/promises";
|
|
2551
|
+
async function fetchRetry(url, init, tries = 5, baseMs = 500) {
|
|
2552
|
+
let last;
|
|
2553
|
+
for (let i = 0; i < tries; i++) {
|
|
2554
|
+
try {
|
|
2555
|
+
const r = await fetch(url, typeof init === "function" ? init() : init);
|
|
2556
|
+
if (r.ok || r.status >= 400 && r.status < 500) return r;
|
|
2557
|
+
last = new Error(`${r.status} ${await r.text().catch(() => "")}`);
|
|
2558
|
+
} catch (e) {
|
|
2559
|
+
last = e;
|
|
2560
|
+
}
|
|
2561
|
+
await new Promise((res) => setTimeout(res, baseMs * 2 ** i));
|
|
2562
|
+
}
|
|
2563
|
+
throw last instanceof Error ? last : new Error(String(last));
|
|
2564
|
+
}
|
|
2565
|
+
function storeIO(store, version, o = {}) {
|
|
2566
|
+
const base = o.retryBaseMs ?? 500;
|
|
2567
|
+
const get = async (path, dest, onBytes) => {
|
|
2568
|
+
const r = await fetchRetry(`${store}${path}`, {}, 5, base).catch((e) => {
|
|
2569
|
+
throw new JobError(`source: ${e.message}`, "source");
|
|
2570
|
+
});
|
|
2571
|
+
if (!r.ok || !r.body) throw new JobError(`source: ${r.status}`, "source");
|
|
2572
|
+
const theirs = r.headers.get(VERSION_HEADER);
|
|
2573
|
+
if (theirs !== null && theirs !== version) {
|
|
2574
|
+
await r.body.cancel().catch(() => {
|
|
2575
|
+
});
|
|
2576
|
+
throw new JobError(`source: this image makes ${version}, the Worker stores ${theirs.slice(0, 80)}`, "stale-image");
|
|
2577
|
+
}
|
|
2578
|
+
const count = new Transform({
|
|
2579
|
+
transform(c, _e, cb) {
|
|
2580
|
+
onBytes(c.length);
|
|
2581
|
+
cb(null, c);
|
|
2582
|
+
}
|
|
2583
|
+
});
|
|
2584
|
+
await pipeline(Readable.fromWeb(r.body), count, createWriteStream(dest));
|
|
2585
|
+
};
|
|
2586
|
+
return {
|
|
2587
|
+
download: (dest, onBytes) => get("/source", dest, onBytes),
|
|
2588
|
+
downloadSource: (i, dest, onBytes) => get(`/source/${i}`, dest, onBytes),
|
|
2589
|
+
async upload(rel, file) {
|
|
2590
|
+
const size = (await stat(file)).size;
|
|
2591
|
+
const r = await fetchRetry(
|
|
2592
|
+
`${store}/out/${rel}`,
|
|
2593
|
+
() => ({ method: "PUT", body: Readable.toWeb(createReadStream(file)), duplex: "half", headers: { "content-length": String(size), [VERSION_HEADER]: version, ...o.expectDurationS !== void 0 ? { [EXPECT_HEADER]: String(o.expectDurationS) } : {} } }),
|
|
2594
|
+
5,
|
|
2595
|
+
base
|
|
2596
|
+
).catch((e) => {
|
|
2597
|
+
throw new Error(`upload ${rel}: ${e.message}`);
|
|
2598
|
+
});
|
|
2599
|
+
if (r.status === 409) throw new JobError(`upload ${rel}: ${r.status} ${await r.text().catch(() => "")}`, "stale-image");
|
|
2600
|
+
if (!r.ok) throw new Error(`upload ${rel}: ${r.status} ${await r.text().catch(() => "")}`);
|
|
2601
|
+
}
|
|
2602
|
+
};
|
|
2603
|
+
}
|
|
2604
|
+
function startContainer(o) {
|
|
2605
|
+
const STORE = o.store ?? (process.env.STORE_URL || "http://r2.internal");
|
|
2606
|
+
const WORK = o.workDir ?? (process.env.WORK_DIR || "/work");
|
|
2607
|
+
let st = { state: "idle", progress: 0 };
|
|
2608
|
+
const server2 = createServer(async (req, res) => {
|
|
2609
|
+
const send = (code, x) => {
|
|
2610
|
+
res.writeHead(code, { "content-type": "application/json" });
|
|
2611
|
+
res.end(JSON.stringify(x));
|
|
2612
|
+
};
|
|
2613
|
+
if (req.method === "GET" && (req.url === "/status" || req.url === "/ping")) return send(200, st);
|
|
2614
|
+
if (req.method === "POST" && req.url === "/run") {
|
|
2615
|
+
let raw = "";
|
|
2616
|
+
for await (const c of req) raw += c;
|
|
2617
|
+
if (st.state === "idle") {
|
|
2618
|
+
let b = {};
|
|
2619
|
+
try {
|
|
2620
|
+
b = JSON.parse(raw || "{}");
|
|
2621
|
+
} catch {
|
|
2622
|
+
}
|
|
2623
|
+
const steps = b.steps === void 0 ? o.defaultSteps : parseSteps(b.steps, ALL_STEPS);
|
|
2624
|
+
if (!steps) return send(400, { error: "bad steps" });
|
|
2625
|
+
const maxShort = Number.isSafeInteger(b.maxShort) && b.maxShort > 0 ? b.maxShort : void 0;
|
|
2626
|
+
st = { state: "running", phase: "download", progress: 0, timings: {} };
|
|
2627
|
+
runJob(
|
|
2628
|
+
{ sourceBytes: Number(b.sourceBytes) || 0, maxShort, steps, ladder: o.ladder, plugins: o.plugins, ...o.stepSettings ? { stepSettings: o.stepSettings } : {}, ...validExpectDuration(b.expectDurationS) ? { expectDurationS: b.expectDurationS } : {}, ...steps.includes("render") ? { render: b.render } : {} },
|
|
2629
|
+
// Stamped with the job's version: the ladder's own unless the job runs an optional step.
|
|
2630
|
+
{ workDir: WORK, ...storeIO(STORE, jobVersion(o.ladder.version, steps, o.stepSettings), validExpectDuration(b.expectDurationS) ? { expectDurationS: b.expectDurationS } : {}) },
|
|
2631
|
+
(s) => st = s
|
|
2632
|
+
).catch((e) => {
|
|
2633
|
+
st = { state: "error", progress: st.progress, error: (e?.message || String(e)).slice(0, 1e3), ...e instanceof JobError && e.code ? { code: e.code } : {} };
|
|
2634
|
+
});
|
|
2635
|
+
}
|
|
2636
|
+
return send(202, st);
|
|
2637
|
+
}
|
|
2638
|
+
send(404, { error: "not found" });
|
|
2639
|
+
});
|
|
2640
|
+
server2.listen(o.port ?? Number(process.env.PORT || 8080));
|
|
2641
|
+
if (o.exitOnSignal !== false) for (const sig of ["SIGTERM", "SIGINT"]) process.on(sig, () => process.exit(0));
|
|
2642
|
+
return server2;
|
|
2643
|
+
}
|
|
2644
|
+
|
|
2645
|
+
// ../media-worker/build/out/stage/build/entry/settings.ts
|
|
2646
|
+
var SETTINGS_ENV = "UPLOADFILM_MEDIA_SETTINGS";
|
|
2647
|
+
var PACKAGE_HEADER = "x-uploadfilm-media-package";
|
|
2648
|
+
function readSettings(raw, imagePkg) {
|
|
2649
|
+
if (!raw) return { error: `${SETTINGS_ENV} is not set: start this image from @uploadfilm/sdk/workers/media's Transcoder` };
|
|
2650
|
+
let s;
|
|
2651
|
+
try {
|
|
2652
|
+
s = JSON.parse(raw);
|
|
2653
|
+
} catch {
|
|
2654
|
+
return { error: `${SETTINGS_ENV} is not JSON` };
|
|
2655
|
+
}
|
|
2656
|
+
if (s.v !== 1 || typeof s.pkg !== "string" || !s.ladder || typeof s.ladder.version !== "string" || !Array.isArray(s.ladder.rungs) || !Array.isArray(s.defaultSteps))
|
|
2657
|
+
return { error: `${SETTINGS_ENV} has an unknown shape` };
|
|
2658
|
+
const ladder = s.pkg === imagePkg ? s.ladder : { ...s.ladder, version: `${s.ladder.version} (image ${imagePkg}, Worker ${s.pkg})` };
|
|
2659
|
+
return { ladder, defaultSteps: s.defaultSteps, ...s.stepSettings ? { stepSettings: s.stepSettings } : {} };
|
|
2660
|
+
}
|
|
2661
|
+
|
|
2662
|
+
// ../media-worker/build/out/stage/build/entry/program.ts
|
|
2663
|
+
var VERSION = true ? "0.2.0" : "0.0.0-dev";
|
|
2664
|
+
var wrapped = false;
|
|
2665
|
+
function startMediaContainer(o = {}) {
|
|
2666
|
+
const s = readSettings((o.env ?? process.env)[SETTINGS_ENV], VERSION);
|
|
2667
|
+
if ("error" in s) {
|
|
2668
|
+
console.error(`@uploadfilm/sdk/workers/media container: ${s.error}`);
|
|
2669
|
+
return process.exit(1);
|
|
2670
|
+
}
|
|
2671
|
+
if (!wrapped) {
|
|
2672
|
+
wrapped = true;
|
|
2673
|
+
const plainFetch = globalThis.fetch;
|
|
2674
|
+
globalThis.fetch = (input, init) => {
|
|
2675
|
+
const headers = new Headers(init?.headers ?? (input instanceof Request ? input.headers : void 0));
|
|
2676
|
+
headers.set(PACKAGE_HEADER, VERSION);
|
|
2677
|
+
return plainFetch(input, { ...init, headers });
|
|
2678
|
+
};
|
|
2679
|
+
}
|
|
2680
|
+
const { plugins, store, workDir, port } = o;
|
|
2681
|
+
const server2 = startContainer({ ladder: s.ladder, defaultSteps: s.defaultSteps, ...s.stepSettings ? { stepSettings: s.stepSettings } : {}, ...plugins ? { plugins } : {}, ...store ? { store } : {}, ...workDir ? { workDir } : {}, ...port ? { port } : {} });
|
|
2682
|
+
server2.prependListener("request", (_req, res) => res.setHeader(PACKAGE_HEADER, VERSION));
|
|
2683
|
+
return server2;
|
|
2684
|
+
}
|
|
2685
|
+
|
|
2686
|
+
// ../media-worker/build/out/stage/build/entry/container.ts
|
|
2687
|
+
var server = startMediaContainer();
|
|
2688
|
+
export {
|
|
2689
|
+
server
|
|
2690
|
+
};
|