Files
aigen/server/utils/studio2/media.ts
T
TowstyandCursor d8f42ecc68 Tighten needsFit to exact canvas size, guard against empty renders
needsFit only compared aspect ratio, so a same-aspect-but-larger still
(e.g. a 4032x2268 phone photo against a 1344x768 canvas) would skip
fitting and hand MiniMaxH3AddGuide a still that does not match the
job's own latent size. Require an exact width+height match instead;
anything else (including unreadable dimensions) still fits.

Also guard the video save path: an empty Comfy download, an empty
stitched assembly, or a stitched result with no readable duration now
fails the job with a clear error instead of silently saving/attaching
a broken zero-length clip.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-11 23:11:02 -05:00

24 lines
1.3 KiB
TypeScript

import { mkdtempSync, writeFileSync, readFileSync, rmSync } from 'node:fs'
import { join } from 'node:path'
import { tmpdir } from 'node:os'
import { runFfmpeg } from '../ffmpeg'
import { imageDimensions } from '../resolution'
export async function fitStill(data: Buffer, width: number, height: number) {
const dir=mkdtempSync(join(tmpdir(),'studio2-fit-'))
try {
const input=join(dir,'source'),output=join(dir,'fitted.png')
writeFileSync(input,data)
await runFfmpeg(['-y','-i',input,'-vf',`scale=${width}:${height}:force_original_aspect_ratio=decrease,pad=${width}:${height}:(ow-iw)/2:(oh-ih)/2:color=black,setsar=1`,'-frames:v','1',output])
return readFileSync(output)
} finally { rmSync(dir,{recursive:true,force:true}) }
}
/** Only skip the scale+pad when the still already matches the job canvas pixel-for-pixel.
* A same-aspect-but-larger still (e.g. a 4032x2268 phone photo against a 1344x768 canvas)
* still needs fitting: MiniMaxH3AddGuide expects an image sized to the job's own latent,
* not merely the same aspect ratio. Unreadable dimensions always fit (safe default). */
export function needsFit(data: Buffer, width: number, height: number) {
const size = imageDimensions(data)
if (!size?.width || !size?.height) return true
return !(size.width === width && size.height === height)
}