import { existsSync, openSync, readSync, closeSync } from 'node:fs' import { execFile } from 'node:child_process' import { promisify } from 'node:util' import { getClip, getStill, clipVideoPath, stillPath } from '../library' import { imageDimensions } from '../resolution' import { resolveSize } from '~/shared/studio2/input.mjs' const exec = promisify(execFile) function readImageHeader(path: string, maxBytes = 65536) { const fd = openSync(path, 'r') try { const buf = Buffer.alloc(maxBytes) const n = readSync(fd, buf, 0, maxBytes, 0) return buf.subarray(0, n) } finally { closeSync(fd) } } export async function resolveRequestSize(owner: string, request: any) { const video = ['video', 'extend'].includes(request.mode) let dimensions: any if (video && request.startClipId) { const clip = getClip(owner, request.startClipId) if (clip.width && clip.height) { dimensions = { width: clip.width, height: clip.height } } else { const result = await exec('ffprobe', ['-v', 'error', '-select_streams', 'v:0', '-show_entries', 'stream=width,height', '-of', 'json', clipVideoPath(owner, request.startClipId)], { windowsHide: true, timeout: 15000 }) dimensions = JSON.parse(result.stdout).streams?.[0] } } else { const id = request.imageAId || request.identityStillId if (id) { const still = getStill(owner, id) // Catalog size wins — never re-probe a saved still as an upload format check. if (Number(still.width) > 0 && Number(still.height) > 0) { dimensions = { width: Number(still.width), height: Number(still.height) } } else { const path = stillPath(owner, id) if (!existsSync(path)) { throw createError({ statusCode: 404, statusMessage: 'That still is missing from the library' }) } try { dimensions = imageDimensions(readImageHeader(path)) } catch { dimensions = null } // Header probe failed — fall through to bench aspect via resolveSize. } } } if (video && request.startClipId && !dimensions?.width) throw new Error('Cannot measure the previous clip.') Object.assign(request.settings, resolveSize(request.settings.aspect, dimensions, undefined, request.engine)) }