fix(studio-server): share background removal job runner

This commit is contained in:
ukimsanov
2026-07-06 13:27:45 -07:00
parent a3bf7eb995
commit 413ee07da5
6 changed files with 200 additions and 226 deletions
+10 -64
View File
@@ -23,11 +23,12 @@ import {
createStudioManualEditsRenderBodyScript,
createStudioApi,
createProjectSignature,
createBackgroundRemovalJob,
getMimeType,
type StudioApiAdapter,
type ResolvedProject,
type RenderJobState,
type MediaProcessingJobState,
type BackgroundRemovalRender,
} from "@hyperframes/studio-server";
import { getElementScreenshotClip } from "@hyperframes/studio-server/screenshot-clip";
import type { ScreenshotClip } from "@hyperframes/studio-server/screenshot-clip";
@@ -428,69 +429,13 @@ export function createStudioServer(options: StudioServerOptions): StudioServer {
},
startBackgroundRemoval(opts) {
const state: MediaProcessingJobState = {
id: opts.jobId,
status: "processing",
progress: 0,
stage: "Preparing background removal",
inputAssetPath: opts.inputAssetPath,
outputAssetPath: opts.outputAssetPath,
outputPath: opts.outputPath,
...(opts.backgroundOutputPath ? { backgroundOutputPath: opts.backgroundOutputPath } : {}),
...(opts.backgroundOutputAssetPath
? { backgroundOutputAssetPath: opts.backgroundOutputAssetPath }
: {}),
};
(async () => {
try {
const sourcePipelinePath = "../background-removal/pipeline.ts";
const pipeline = (await import("../background-removal/pipeline.js").catch(
() => import(sourcePipelinePath),
)) as typeof import("../background-removal/pipeline.js");
const { render } = pipeline;
const result = await render({
inputPath: opts.inputPath,
outputPath: opts.outputPath,
backgroundOutputPath: opts.backgroundOutputPath,
device: opts.device,
quality: opts.quality,
onProgress: (event) => {
if (event.kind === "info") {
state.stage = event.message;
return;
}
if (event.kind === "metadata") {
state.stage = `Source ${event.width}×${event.height}`;
state.progress = 2;
return;
}
const pct = event.total
? Math.min(99, Math.floor((event.index / event.total) * 100))
: 0;
state.progress = pct;
state.stage = event.total
? `Removing background ${event.index}/${event.total}`
: `Removing background frame ${event.index}`;
state.framesProcessed = event.index;
state.avgMsPerFrame = event.avgMsPerFrame;
},
});
state.status = "complete";
state.progress = 100;
state.stage = "Complete";
state.provider = result.provider;
state.framesProcessed = result.framesProcessed;
state.durationSeconds = result.durationSeconds;
state.avgMsPerFrame = result.avgMsPerFrame;
} catch (err) {
state.status = "failed";
state.error = err instanceof Error ? err.message : String(err);
state.stage = "Failed";
}
})();
return state;
return createBackgroundRemovalJob(opts, async (renderOpts) => {
const sourcePipelinePath = "../background-removal/pipeline.ts";
const pipeline = (await import("../background-removal/pipeline.js").catch(
() => import(sourcePipelinePath),
)) as { render: BackgroundRemovalRender };
return pipeline.render(renderOpts);
});
},
async generateThumbnail(opts): Promise<Buffer | null> {
@@ -515,6 +460,7 @@ export function createStudioServer(options: StudioServerOptions): StudioServer {
{ timeout: 5000 },
)
.catch(() => {});
// fallow-ignore-next-line code-duplication
await page.evaluate((t: number) => {
const w = window as Window & {
__player?: { seek?: (time: number) => void };