|
| 1 | +const CRF_MAX_BY_FORMAT: Record<string, number> = { mp4: 51, webm: 63 }; |
| 2 | +const VIDEO_BITRATE = /^\d+(\.\d+)?[kKmMgG]?$/; |
| 3 | +const VIDEO_FRAME_FORMATS = ["auto", "jpg", "png"] as const; |
| 4 | +const HDR_MODES = ["auto", "force-hdr", "force-sdr"] as const; |
| 5 | + |
| 6 | +interface EncoderOptions { |
| 7 | + crf?: number; |
| 8 | + videoBitrate?: string; |
| 9 | + videoFrameFormat?: (typeof VIDEO_FRAME_FORMATS)[number]; |
| 10 | + hdrMode?: (typeof HDR_MODES)[number]; |
| 11 | +} |
| 12 | + |
| 13 | +export interface EncoderOptionsBody { |
| 14 | + crf?: unknown; |
| 15 | + videoBitrate?: unknown; |
| 16 | + videoFrameFormat?: unknown; |
| 17 | + hdrMode?: unknown; |
| 18 | +} |
| 19 | + |
| 20 | +type Parsed = { ok: true; options: EncoderOptions } | { ok: false; error: string }; |
| 21 | + |
| 22 | +const isSet = (value: unknown) => value !== undefined && value !== null; |
| 23 | + |
| 24 | +function oneOf<T extends string>(value: unknown, allowed: readonly T[]): value is T { |
| 25 | + return typeof value === "string" && (allowed as readonly string[]).includes(value); |
| 26 | +} |
| 27 | + |
| 28 | +function parseCrf(crf: unknown, format: string, max: number): Parsed { |
| 29 | + if (typeof crf !== "number" || !Number.isInteger(crf) || crf < 0 || crf > max) { |
| 30 | + return { ok: false, error: `crf must be an integer from 0 to ${max} for ${format}` }; |
| 31 | + } |
| 32 | + return { ok: true, options: { crf } }; |
| 33 | +} |
| 34 | + |
| 35 | +function parseVideoBitrate(videoBitrate: unknown): Parsed { |
| 36 | + if (typeof videoBitrate !== "string" || !VIDEO_BITRATE.test(videoBitrate)) { |
| 37 | + return { ok: false, error: 'videoBitrate must look like "10M", "2500k" or "8000000"' }; |
| 38 | + } |
| 39 | + return { ok: true, options: { videoBitrate } }; |
| 40 | +} |
| 41 | + |
| 42 | +function parseRateControl(body: EncoderOptionsBody, format: string): Parsed { |
| 43 | + const max = CRF_MAX_BY_FORMAT[format]; |
| 44 | + if (max === undefined) return { ok: true, options: {} }; |
| 45 | + if (isSet(body.crf) && isSet(body.videoBitrate)) { |
| 46 | + return { ok: false, error: "use either crf or videoBitrate, not both" }; |
| 47 | + } |
| 48 | + if (isSet(body.crf)) return parseCrf(body.crf, format, max); |
| 49 | + if (isSet(body.videoBitrate)) return parseVideoBitrate(body.videoBitrate); |
| 50 | + return { ok: true, options: {} }; |
| 51 | +} |
| 52 | + |
| 53 | +/** Explicit encoder overrides from a render request. MOV (ProRes) takes no rate control. */ |
| 54 | +export function parseEncoderOptions(body: EncoderOptionsBody, format: string): Parsed { |
| 55 | + const rate = parseRateControl(body, format); |
| 56 | + if (!rate.ok) return rate; |
| 57 | + const options: EncoderOptions = { ...rate.options }; |
| 58 | + if (isSet(body.videoFrameFormat)) { |
| 59 | + if (!oneOf(body.videoFrameFormat, VIDEO_FRAME_FORMATS)) { |
| 60 | + return { |
| 61 | + ok: false, |
| 62 | + error: `videoFrameFormat must be one of ${VIDEO_FRAME_FORMATS.join(", ")}`, |
| 63 | + }; |
| 64 | + } |
| 65 | + options.videoFrameFormat = body.videoFrameFormat; |
| 66 | + } |
| 67 | + if (isSet(body.hdrMode)) { |
| 68 | + if (!oneOf(body.hdrMode, HDR_MODES)) { |
| 69 | + return { ok: false, error: `hdrMode must be one of ${HDR_MODES.join(", ")}` }; |
| 70 | + } |
| 71 | + options.hdrMode = body.hdrMode; |
| 72 | + } |
| 73 | + return { ok: true, options }; |
| 74 | +} |
0 commit comments