🎥 Make videos programmatically with React
fix dar / sar parsing
Jonny Burger4cc8306deca2
diff --git a/packages/renderer/src/calculate-sar-dar-pixels.ts b/packages/renderer/src/calculate-sar-dar-pixels.tsnew file mode 100644index 0000000000..47e78ef47f--- /dev/null+++ b/packages/renderer/src/calculate-sar-dar-pixels.ts@@ -0,0 +1,29 @@+// https://superuser.com/questions/907933/correct-aspect-ratio-without-re-encoding-video-file++export const calculateDisplayVideoSize = ({+ darX,+ darY,+ x,+ y,+}: {+ x: number;+ y: number;+ darX: number;+ darY: number;+}): {width: number; height: number} => {+ // We know two equations:+ // newWidth / newHeight = darX / darY+ // and:+ // x * y = (newWidth * newHeight)++ // I solved it then on pen and paper and simplified the formula:++ const dimensions = x * y;+ const newWidth = Math.sqrt(dimensions * (darX / darY));+ const newHeight = dimensions / newWidth;++ return {+ height: Math.round(newHeight),+ width: Math.round(newWidth),+ };+};diff --git a/packages/renderer/src/get-video-info.ts b/packages/renderer/src/get-video-info.tsindex 19071d0a5a..bdc604ca2f 100644--- a/packages/renderer/src/get-video-info.ts+++ b/packages/renderer/src/get-video-info.ts@@ -1,5 +1,6 @@import execa from 'execa';import type {FfmpegExecutable} from 'remotion';+import {calculateDisplayVideoSize} from './calculate-sar-dar-pixels';import {pLimit} from './p-limit';type Result = {@@ -40,8 +41,13 @@ async function getVideoInfoUnlimited(const darWidth = parseInt(dar[1], 10);const darHeight = parseInt(dar[2], 10);- const actualWidth = Math.round(width * (darWidth / darHeight));- const actualHeight = Math.round(height * (darHeight / darWidth));+ const {width: actualWidth, height: actualHeight} =+ calculateDisplayVideoSize({+ darX: darWidth,+ darY: darHeight,+ x: width,+ y: height,+ });if (actualWidth !== width || actualHeight !== height) {needsResize = [actualWidth, actualHeight];diff --git a/packages/renderer/src/test/sar-dar.test.ts b/packages/renderer/src/test/sar-dar.test.tsnew file mode 100644index 0000000000..0d1ffa4f99--- /dev/null+++ b/packages/renderer/src/test/sar-dar.test.ts@@ -0,0 +1,27 @@+import {calculateDisplayVideoSize} from '../calculate-sar-dar-pixels';++test('Should parse display dimensions based on SAR and DAR correctly', () => {+ const size = calculateDisplayVideoSize({+ x: 1280,+ y: 720,+ darX: 896,+ darY: 1593,+ });++ expect(size).toEqual({+ width: 720,+ height: 1280,+ });++ const size2 = calculateDisplayVideoSize({+ x: 1920,+ y: 1080,+ darX: 16,+ darY: 9,+ });++ expect(size2).toEqual({+ width: 1920,+ height: 1080,+ });+});