Skip to content

🎥 Make videos programmatically with React

return rotation

Jonny Burger680fcfdba30b
diff --git a/packages/media-parser/src/has-all-info.ts b/packages/media-parser/src/has-all-info.ts
index 372c73fff5..562a620794 100644
--- a/packages/media-parser/src/has-all-info.ts
+++ b/packages/media-parser/src/has-all-info.ts
@@ -28,6 +28,7 @@ export const hasAllInfo = (
true,
true,
true,
+ true,
true
>)[];
@@ -40,7 +41,7 @@ export const hasAllInfo = (
return hasDuration(parseResult.segments);
}
- if (key === 'dimensions') {
+ if (key === 'dimensions' || key === 'rotation') {
return hasDimensions(parseResult.segments);
}
diff --git a/packages/media-parser/src/options.ts b/packages/media-parser/src/options.ts
index 146ae2d05d..faf6e39089 100644
--- a/packages/media-parser/src/options.ts
+++ b/packages/media-parser/src/options.ts
@@ -31,6 +31,7 @@ export type Options<
EnableVideoCodec extends boolean,
EnableAudioCodec extends boolean,
EnableTracks extends boolean,
+ EnableRotation extends boolean,
> = {
dimensions?: EnableDimensions;
durationInSeconds?: EnableDuration;
@@ -39,6 +40,7 @@ export type Options<
videoCodec?: EnableVideoCodec;
audioCodec?: EnableAudioCodec;
tracks?: EnableTracks;
+ rotation?: EnableRotation;
};
export type Metadata<
@@ -49,6 +51,7 @@ export type Metadata<
EnableVideoCodec extends boolean,
EnableAudioCodec extends boolean,
EnableTracks extends boolean,
+ EnableRotation extends boolean,
> = (EnableDimensions extends true ? {dimensions: Dimensions} : {}) &
(EnableDuration extends true ? {durationInSeconds: number | null} : {}) &
(EnableBoxes extends true ? {boxes: AnySegment[]} : {}) &
@@ -57,7 +60,8 @@ export type Metadata<
(EnableAudioCodec extends true ? {audioCodec: KnownAudioCodecs | null} : {}) &
(EnableTracks extends true
? {videoTracks: VideoTrack[]; audioTracks: AudioTrack[]}
- : {});
+ : {}) &
+ (EnableRotation extends true ? {rotation: number | null} : {});
export type ParseMedia = <
EnableDimensions extends boolean,
@@ -67,6 +71,7 @@ export type ParseMedia = <
EnableVideoCodec extends boolean,
EnableAudioCodec extends boolean,
EnableTracks extends boolean,
+ EnableRotation extends boolean,
>(options: {
src: string | File;
fields: Options<
@@ -76,7 +81,8 @@ export type ParseMedia = <
EnableFps,
EnableVideoCodec,
EnableAudioCodec,
- EnableTracks
+ EnableTracks,
+ EnableRotation
>;
reader?: ReaderInterface;
onAudioSample?: OnAudioSample;
@@ -89,6 +95,7 @@ export type ParseMedia = <
EnableFps,
EnableVideoCodec,
EnableAudioCodec,
- EnableTracks
+ EnableTracks,
+ EnableRotation
>
>;
diff --git a/packages/media-parser/src/parse-media.ts b/packages/media-parser/src/parse-media.ts
index b0471f7738..d65ac3a34c 100644
--- a/packages/media-parser/src/parse-media.ts
+++ b/packages/media-parser/src/parse-media.ts
@@ -22,7 +22,16 @@ export const parseMedia: ParseMedia = async ({
const {reader, contentLength} = await readerInterface.read(src, null);
let currentReader = reader;
- const returnValue = {} as Metadata<true, true, true, true, true, true, true>;
+ const returnValue = {} as Metadata<
+ true,
+ true,
+ true,
+ true,
+ true,
+ true,
+ true,
+ true
+ >;
let iterator: BufferIterator | null = null;
let parseResult: ParseResult | null = null;
@@ -86,7 +95,16 @@ export const parseMedia: ParseMedia = async ({
}
if (fields.dimensions) {
- returnValue.dimensions = getDimensions(parseResult.segments);
+ const dimensions = getDimensions(parseResult.segments);
+ returnValue.dimensions = {
+ width: dimensions.width,
+ height: dimensions.height,
+ };
+ }
+
+ if (fields.rotation) {
+ const dimensions = getDimensions(parseResult.segments);
+ returnValue.rotation = dimensions.rotation;
}
if (fields.durationInSeconds) {
diff --git a/packages/media-parser/src/test/stream-local.test.ts b/packages/media-parser/src/test/stream-local.test.ts
index 9fbb0af7c4..2dccfa79ec 100644
--- a/packages/media-parser/src/test/stream-local.test.ts
+++ b/packages/media-parser/src/test/stream-local.test.ts
@@ -13,6 +13,7 @@ test('Should stream ISO base media', async () => {
audioCodec: true,
tracks: true,
dimensions: true,
+ rotation: true,
},
reader: nodeReader,
});
@@ -26,6 +27,7 @@ test('Should stream ISO base media', async () => {
expect(result.audioCodec).toBe('aac');
expect(result.videoTracks.length).toBe(1);
expect(result.videoTracks[0].codecString).toBe('hvc1.2.4.L150.b0');
+ expect(result.rotation).toBe(-90);
});
test('Should stream WebM with no duration', async () => {
@@ -37,6 +39,7 @@ test('Should stream WebM with no duration', async () => {
dimensions: true,
videoCodec: true,
audioCodec: true,
+ rotation: true,
// TODO: Return WebM tracks
},
reader: nodeReader,
@@ -49,6 +52,7 @@ test('Should stream WebM with no duration', async () => {
expect(result.fps).toBe(null);
expect(result.videoCodec).toBe('vp8');
expect(result.audioCodec).toBe(null);
+ expect(result.rotation).toBe(0);
});
test('Should stream AV1 with no duration', async () => {
@@ -60,6 +64,7 @@ test('Should stream AV1 with no duration', async () => {
fps: true,
videoCodec: true,
audioCodec: true,
+ rotation: true,
// TODO: Return WebM tracks
},
reader: nodeReader,
@@ -73,6 +78,7 @@ test('Should stream AV1 with no duration', async () => {
});
expect(parsed.videoCodec).toBe('av1');
expect(parsed.audioCodec).toBe(null);
+ expect(parsed.rotation).toBe(0);
});
test('Should stream corrupted video', async () => {
@@ -85,6 +91,7 @@ test('Should stream corrupted video', async () => {
videoCodec: true,
audioCodec: true,
tracks: true,
+ rotation: true,
},
reader: nodeReader,
});
@@ -99,6 +106,7 @@ test('Should stream corrupted video', async () => {
expect(parsed.audioCodec).toBe('aac');
expect(parsed.videoTracks.length).toEqual(1);
expect(parsed.videoTracks[0].codecString).toBe('avc1.640028');
+ expect(parsed.rotation).toBe(0);
});
test('Should stream screen recording video', async () => {
@@ -111,6 +119,7 @@ test('Should stream screen recording video', async () => {
videoCodec: true,
audioCodec: true,
tracks: true,
+ rotation: true,
},
reader: nodeReader,
});
@@ -125,6 +134,7 @@ test('Should stream screen recording video', async () => {
expect(parsed.audioCodec).toBe(null);
expect(parsed.videoTracks.length).toEqual(1);
expect(parsed.videoTracks[0].codecString).toBe('avc1.4d0033');
+ expect(parsed.rotation).toBe(0);
});
test('Should stream ProRes video', async () => {
@@ -137,6 +147,7 @@ test('Should stream ProRes video', async () => {
videoCodec: true,
audioCodec: true,
tracks: true,
+ rotation: true,
},
reader: nodeReader,
});
@@ -149,6 +160,7 @@ test('Should stream ProRes video', async () => {
expect(parsed.audioCodec).toBe('aiff');
expect(parsed.videoTracks.length).toEqual(1);
expect(parsed.videoTracks[0].codecString).toBe('ap4h');
+ expect(parsed.rotation).toBe(0);
});
test('Should stream variable fps video', async () => {
@@ -160,6 +172,7 @@ test('Should stream variable fps video', async () => {
durationInSeconds: true,
videoCodec: true,
audioCodec: true,
+ rotation: true,
},
reader: nodeReader,
});
@@ -169,6 +182,7 @@ test('Should stream variable fps video', async () => {
expect(parsed.durationInSeconds).toBe(22.901);
expect(parsed.videoCodec).toBe('vp8');
expect(parsed.audioCodec).toBe('opus');
+ expect(parsed.rotation).toBe(0);
});
test('Should stream MKV video', async () => {
@@ -180,6 +194,7 @@ test('Should stream MKV video', async () => {
durationInSeconds: true,
videoCodec: true,
audioCodec: true,
+ rotation: true,
},
reader: nodeReader,
});
@@ -189,6 +204,7 @@ test('Should stream MKV video', async () => {
expect(parsed.durationInSeconds).toBe(0.333);
expect(parsed.videoCodec).toBe('h264');
expect(parsed.audioCodec).toBe('pcm');
+ expect(parsed.rotation).toBe(0);
});
test('Should stream MP3 in MP4 video', async () => {
@@ -201,6 +217,7 @@ test('Should stream MP3 in MP4 video', async () => {
videoCodec: true,
audioCodec: true,
tracks: true,
+ rotation: true,
},
reader: nodeReader,
});
@@ -214,4 +231,5 @@ test('Should stream MP3 in MP4 video', async () => {
expect(parsed.videoTracks[0].codecString).toBe('avc1.640020');
expect(parsed.audioTracks.length).toEqual(1);
expect(parsed.audioTracks[0].codecString).toBe('mp4a.6b');
+ expect(parsed.rotation).toBe(0);
});