mirror of
https://github.com/webadderallorg/Recordly.git
synced 2026-10-05 12:07:45 +00:00
69 lines
2.3 KiB
TypeScript
69 lines
2.3 KiB
TypeScript
import type { ExportQuality } from "@/lib/exporter";
|
|
import { type AspectRatio, getAspectRatioValue } from "@/utils/aspectRatioUtils";
|
|
|
|
function normalizeEvenDimension(value: number): number {
|
|
return Math.max(2, Math.floor(value / 2) * 2);
|
|
}
|
|
|
|
function fitAspectRatioWithinBounds(
|
|
maxWidth: number,
|
|
maxHeight: number,
|
|
aspectRatioValue: number,
|
|
): { width: number; height: number } {
|
|
const safeMaxWidth = normalizeEvenDimension(maxWidth);
|
|
const safeMaxHeight = normalizeEvenDimension(maxHeight);
|
|
const safeAspectRatio =
|
|
Number.isFinite(aspectRatioValue) && aspectRatioValue > 0 ? aspectRatioValue : 16 / 9;
|
|
|
|
if (safeMaxWidth / safeMaxHeight > safeAspectRatio) {
|
|
const height = safeMaxHeight;
|
|
const width = normalizeEvenDimension(height * safeAspectRatio);
|
|
return { width: Math.min(width, safeMaxWidth), height };
|
|
}
|
|
|
|
const width = safeMaxWidth;
|
|
const height = normalizeEvenDimension(width / safeAspectRatio);
|
|
return { width, height: Math.min(height, safeMaxHeight) };
|
|
}
|
|
|
|
export function calculateMp4SourceDimensions(
|
|
sourceWidth: number,
|
|
sourceHeight: number,
|
|
aspectRatio: AspectRatio,
|
|
): { width: number; height: number } {
|
|
const safeSourceWidth = normalizeEvenDimension(sourceWidth);
|
|
const safeSourceHeight = normalizeEvenDimension(sourceHeight);
|
|
const sourceAspectRatio = safeSourceHeight > 0 ? safeSourceWidth / safeSourceHeight : 16 / 9;
|
|
const aspectRatioValue = getAspectRatioValue(aspectRatio, sourceAspectRatio);
|
|
|
|
if (aspectRatio === "native") {
|
|
return { width: safeSourceWidth, height: safeSourceHeight };
|
|
}
|
|
|
|
const longSide = Math.max(safeSourceWidth, safeSourceHeight);
|
|
const shortSide = Math.min(safeSourceWidth, safeSourceHeight);
|
|
const maxWidth = aspectRatioValue >= 1 ? longSide : shortSide;
|
|
const maxHeight = aspectRatioValue >= 1 ? shortSide : longSide;
|
|
|
|
return fitAspectRatioWithinBounds(maxWidth, maxHeight, aspectRatioValue);
|
|
}
|
|
|
|
export function calculateMp4ExportDimensions(
|
|
baseWidth: number,
|
|
baseHeight: number,
|
|
quality: ExportQuality,
|
|
): { width: number; height: number } {
|
|
if (quality === "source") {
|
|
return {
|
|
width: normalizeEvenDimension(baseWidth),
|
|
height: normalizeEvenDimension(baseHeight),
|
|
};
|
|
}
|
|
|
|
const qualityScale = quality === "medium" ? 0.6 : quality === "good" ? 0.75 : 0.9;
|
|
return {
|
|
width: normalizeEvenDimension(baseWidth * qualityScale),
|
|
height: normalizeEvenDimension(baseHeight * qualityScale),
|
|
};
|
|
}
|