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