Skip to content

🎥 Make videos programmatically with React

expanded dimensions

Jonny Burgerade7eb385ce9
diff --git a/packages/media-parser/src/boxes/iso-base-media/tkhd.ts b/packages/media-parser/src/boxes/iso-base-media/tkhd.ts
index 0c792b130d..d8cc2dd5db 100644
--- a/packages/media-parser/src/boxes/iso-base-media/tkhd.ts
+++ b/packages/media-parser/src/boxes/iso-base-media/tkhd.ts
@@ -16,6 +16,27 @@ export interface TkhdBox extends BaseBox {
matrix: ThreeDMatrix;
width: number;
height: number;
+ unrotatedWidth: number;
+ unrotatedHeight: number;
+ rotation: number;
+}
+
+type Matrix2x2 = readonly [number, number, number, number];
+
+function getRotationAngleFromMatrix(matrix: Matrix2x2): number {
+ // Extract elements from the matrix
+ const [a, b, c, d] = matrix;
+
+ // Check if the matrix is a valid rotation matrix
+ if (Math.round(a * a + b * b) !== 1 || Math.round(c * c + d * d) !== 1) {
+ throw new Error('The provided matrix is not a valid rotation matrix.');
+ }
+
+ // Calculate the angle using the atan2 function
+ const angleRadians = Math.atan2(c, a); // atan2(sin(θ), cos(θ))
+ const angleDegrees = angleRadians * (180 / Math.PI); // Convert radians to degrees
+
+ return angleDegrees;
}
const applyRotation = ({
@@ -23,7 +44,7 @@ const applyRotation = ({
width,
height,
}: {
- matrix: readonly [number, number, number, number];
+ matrix: Matrix2x2;
width: number;
height: number;
}) => {
@@ -107,6 +128,8 @@ export const parseTkhd = ({
height: heightWithoutRotationApplied,
});
+ const rotation = getRotationAngleFromMatrix(rotationMatrix);
+
return {
offset,
boxSize: size,
@@ -122,5 +145,8 @@ export const parseTkhd = ({
width,
height,
version,
+ rotation,
+ unrotatedWidth: widthWithoutRotationApplied,
+ unrotatedHeight: heightWithoutRotationApplied,
};
};
diff --git a/packages/media-parser/src/get-dimensions.ts b/packages/media-parser/src/get-dimensions.ts
index d3429c34e2..2f3da51edc 100644
--- a/packages/media-parser/src/get-dimensions.ts
+++ b/packages/media-parser/src/get-dimensions.ts
@@ -8,7 +8,15 @@ export type Dimensions = {
height: number;
};
-const getDimensionsFromMatroska = (segments: MainSegment): Dimensions => {
+export type ExpandedDimensions = Dimensions & {
+ rotation: number;
+ unrotatedWidth: number;
+ unrotatedHeight: number;
+};
+
+const getDimensionsFromMatroska = (
+ segments: MainSegment,
+): ExpandedDimensions => {
const tracksSegment = segments.children.find(
(b) => b.type === 'tracks-segment',
);
@@ -55,10 +63,13 @@ const getDimensionsFromMatroska = (segments: MainSegment): Dimensions => {
return {
width: widthSegment.width,
height: heightSegment.height,
+ rotation: 0,
+ unrotatedHeight: heightSegment.height,
+ unrotatedWidth: widthSegment.width,
};
};
-export const getDimensions = (boxes: AnySegment[]): Dimensions => {
+export const getDimensions = (boxes: AnySegment[]): ExpandedDimensions => {
const matroskaBox = boxes.find((b) => b.type === 'main-segment');
if (matroskaBox && matroskaBox.type === 'main-segment') {
return getDimensionsFromMatroska(matroskaBox);
@@ -80,6 +91,9 @@ export const getDimensions = (boxes: AnySegment[]): Dimensions => {
return {
width: tkhdBox.width,
height: tkhdBox.height,
+ rotation: tkhdBox.rotation,
+ unrotatedHeight: tkhdBox.unrotatedHeight,
+ unrotatedWidth: tkhdBox.unrotatedWidth,
};
}
@@ -94,12 +108,18 @@ export const getDimensions = (boxes: AnySegment[]): Dimensions => {
throw new Error('Has no video stream');
}
- const [firstTrack] = videoSamples;
- if (firstTrack.type !== 'video') {
+ const [firstSample] = videoSamples;
+ if (firstSample.type !== 'video') {
throw new Error('Expected video track');
}
- return {width: firstTrack.width, height: firstTrack.height};
+ return {
+ width: firstSample.width,
+ height: firstSample.height,
+ rotation: 0,
+ unrotatedHeight: firstSample.height,
+ unrotatedWidth: firstSample.width,
+ };
};
// TODO: An audio track should return 'hasDimensions' = true on an audio file
diff --git a/packages/media-parser/src/test/tkhd.test.ts b/packages/media-parser/src/test/tkhd.test.ts
index 97639e0ae6..daf9314267 100644
--- a/packages/media-parser/src/test/tkhd.test.ts
+++ b/packages/media-parser/src/test/tkhd.test.ts
@@ -80,5 +80,8 @@ test('Should be able to parse a TKHD box', () => {
matrix: [1, 0, 0, 0, 1, 0, 0, 0, 1],
width: 0,
height: 0,
+ rotation: 0,
+ unrotatedWidth: 0,
+ unrotatedHeight: 0,
});
});