Skip to content

🎥 Make videos programmatically with React

getLottieMetadaa() function

Jonny Burger985397c6773b
diff --git a/packages/lottie/src/Lottie.tsx b/packages/lottie/src/Lottie.tsx
index 99c3ebbcbd..5eb4a37451 100644
--- a/packages/lottie/src/Lottie.tsx
+++ b/packages/lottie/src/Lottie.tsx
@@ -3,10 +3,9 @@ import lottie from 'lottie-web';
import type {CSSProperties} from 'react';
import {useEffect, useRef, useState} from 'react';
import {continueRender, delayRender, useCurrentFrame} from 'remotion';
+import type {LottieAnimationData} from './types';
import {getNextFrame} from './utils';
-export type LottieAnimationData = Record<string | number | symbol, unknown>;
-
export interface LottieProps {
/**
* JSON object with the animation data.
diff --git a/packages/lottie/src/get-lottie-metadata.ts b/packages/lottie/src/get-lottie-metadata.ts
new file mode 100644
index 0000000000..b428e7be04
--- /dev/null
+++ b/packages/lottie/src/get-lottie-metadata.ts
@@ -0,0 +1,30 @@
+import type {LottieAnimationData} from './types';
+
+type LottieMetadata = {
+ fps: number;
+ durationInSeconds: number;
+ durationInFrames: number;
+ width: number;
+ height: number;
+};
+
+export const getLottieMetadata = (
+ animationData: LottieAnimationData
+): LottieMetadata | null => {
+ const width = animationData.w;
+ const height = animationData.h;
+ const framerate = animationData.fr;
+ const durationInFrames = animationData.op;
+
+ if (![width, height, framerate, durationInFrames].every(Boolean)) {
+ return null;
+ }
+
+ return {
+ durationInFrames: Math.floor(durationInFrames),
+ durationInSeconds: durationInFrames / framerate,
+ fps: framerate,
+ height,
+ width,
+ };
+};
diff --git a/packages/lottie/src/index.ts b/packages/lottie/src/index.ts
index 0984168d5b..e390871bbe 100644
--- a/packages/lottie/src/index.ts
+++ b/packages/lottie/src/index.ts
@@ -1 +1,3 @@
-export {Lottie, LottieAnimationData, LottieProps} from './Lottie';
+export {getLottieMetadata} from './get-lottie-metadata';
+export {Lottie, LottieProps} from './Lottie';
+export type {LottieAnimationData} from './types';
diff --git a/packages/lottie/src/test/example.json b/packages/lottie/src/test/example.json
new file mode 100644
index 0000000000..70b112ed84
--- /dev/null
+++ b/packages/lottie/src/test/example.json
@@ -0,0 +1,141 @@
+{
+ "v": "5.9.6",
+ "fr": 29.9700012207031,
+ "ip": 0,
+ "op": 90.0000036657751,
+ "w": 1920,
+ "h": 1080,
+ "nm": "Comp 1",
+ "ddd": 0,
+ "assets": [],
+ "layers": [
+ {
+ "ddd": 0,
+ "ind": 1,
+ "ty": 4,
+ "nm": "Shape Layer 1",
+ "sr": 1,
+ "ks": {
+ "o": {"a": 0, "k": 100, "ix": 11},
+ "r": {
+ "a": 1,
+ "k": [
+ {
+ "i": {"x": [0.833], "y": [0.833]},
+ "o": {"x": [0.167], "y": [0.167]},
+ "t": 0,
+ "s": [360]
+ },
+ {"t": 58.0000023623884, "s": [0]}
+ ],
+ "ix": 10
+ },
+ "p": {
+ "a": 1,
+ "k": [
+ {
+ "i": {"x": 0.833, "y": 0.833},
+ "o": {"x": 0.167, "y": 0.167},
+ "t": 0,
+ "s": [979.401, 1368, 0],
+ "to": [0, -138, 0],
+ "ti": [0, 138, 0]
+ },
+ {"t": 58.0000023623884, "s": [979.401, 540, 0]}
+ ],
+ "ix": 2,
+ "l": 2
+ },
+ "a": {"a": 0, "k": [517.365, 112.096, 0], "ix": 1, "l": 2},
+ "s": {"a": 0, "k": [100, 100, 100], "ix": 6, "l": 2}
+ },
+ "ao": 0,
+ "shapes": [
+ {
+ "ty": "gr",
+ "it": [
+ {
+ "ty": "rc",
+ "d": 1,
+ "s": {"a": 0, "k": [425.883, 425.883], "ix": 2},
+ "p": {"a": 0, "k": [0, 0], "ix": 3},
+ "r": {"a": 0, "k": 98, "ix": 4},
+ "nm": "Rectangle Path 1",
+ "mn": "ADBE Vector Shape - Rect",
+ "hd": false
+ },
+ {
+ "ty": "st",
+ "c": {"a": 0, "k": [1, 1, 1, 1], "ix": 3},
+ "o": {"a": 0, "k": 100, "ix": 4},
+ "w": {"a": 0, "k": 2, "ix": 5},
+ "lc": 1,
+ "lj": 1,
+ "ml": 4,
+ "bm": 0,
+ "nm": "Stroke 1",
+ "mn": "ADBE Vector Graphic - Stroke",
+ "hd": false
+ },
+ {
+ "ty": "fl",
+ "c": {"a": 0, "k": [0, 0.468933612108, 1, 1], "ix": 4},
+ "o": {"a": 0, "k": 100, "ix": 5},
+ "r": 1,
+ "bm": 0,
+ "nm": "Fill 1",
+ "mn": "ADBE Vector Graphic - Fill",
+ "hd": false
+ },
+ {
+ "ty": "tr",
+ "p": {"a": 0, "k": [494.618, 123.481], "ix": 2},
+ "a": {"a": 0, "k": [0, 0], "ix": 1},
+ "s": {"a": 0, "k": [100, 100], "ix": 3},
+ "r": {"a": 0, "k": 0, "ix": 6},
+ "o": {"a": 0, "k": 100, "ix": 7},
+ "sk": {"a": 0, "k": 0, "ix": 4},
+ "sa": {"a": 0, "k": 0, "ix": 5},
+ "nm": "Transform"
+ }
+ ],
+ "nm": "Rectangle 2",
+ "np": 3,
+ "cix": 2,
+ "bm": 0,
+ "ix": 1,
+ "mn": "ADBE Vector Group",
+ "hd": false
+ }
+ ],
+ "ip": 0,
+ "op": 90.0000036657751,
+ "st": 0,
+ "ct": 1,
+ "bm": 0
+ },
+ {
+ "ddd": 0,
+ "ind": 2,
+ "ty": 1,
+ "nm": "White Solid 1",
+ "sr": 1,
+ "ks": {
+ "o": {"a": 0, "k": 100, "ix": 11},
+ "r": {"a": 0, "k": 0, "ix": 10},
+ "p": {"a": 0, "k": [960, 540, 0], "ix": 2, "l": 2},
+ "a": {"a": 0, "k": [960, 540, 0], "ix": 1, "l": 2},
+ "s": {"a": 0, "k": [100, 100, 100], "ix": 6, "l": 2}
+ },
+ "ao": 0,
+ "sw": 1920,
+ "sh": 1080,
+ "sc": "#ffffff",
+ "ip": 0,
+ "op": 90.0000036657751,
+ "st": 0,
+ "bm": 0
+ }
+ ],
+ "markers": []
+}
diff --git a/packages/lottie/src/test/get-lottie-metadata.test.ts b/packages/lottie/src/test/get-lottie-metadata.test.ts
new file mode 100644
index 0000000000..a4620a5319
--- /dev/null
+++ b/packages/lottie/src/test/get-lottie-metadata.test.ts
@@ -0,0 +1,17 @@
+import fs from 'fs';
+import path from 'path';
+import {getLottieMetadata} from '../get-lottie-metadata';
+
+test('Should be able to get Lottie metadata', () => {
+ const file = fs.readFileSync(path.join(__dirname, 'example.json'), 'utf-8');
+
+ const parsed = JSON.parse(file);
+
+ expect(getLottieMetadata(parsed)).toEqual({
+ durationInFrames: 90,
+ durationInSeconds: 3.0030030030030037,
+ fps: 29.9700012207031,
+ height: 1080,
+ width: 1920,
+ });
+});
diff --git a/packages/lottie/src/types.ts b/packages/lottie/src/types.ts
new file mode 100644
index 0000000000..0e8df6f49e
--- /dev/null
+++ b/packages/lottie/src/types.ts
@@ -0,0 +1,6 @@
+export type LottieAnimationData = {
+ fr: number;
+ w: number;
+ h: number;
+ op: number;
+} & Record<string | number | symbol, unknown>;