🎥 Make videos programmatically with React
calculate assets position
yuta08012e815e375f84
diff --git a/packages/renderer/src/assets.ts b/packages/renderer/src/assets.tsnew file mode 100644index 0000000000..e6a6c0161d--- /dev/null+++ b/packages/renderer/src/assets.ts@@ -0,0 +1,49 @@+type AssetMetadata = {+ type: 'audio';+ src: string;+};++type Asset = AssetMetadata & {+ from: number;+ duration?: number;+};++const areEqual = (a: AssetMetadata, b: AssetMetadata) => {+ return a.type === b.type && a.src === b.src;+};++export const calculateAssetsPosition = (frames: AssetMetadata[][]) => {+ const assets: Asset[] = [];++ for (let frame = 0; frame < frames.length; frame++) {+ const prev = (frames[frame - 1] ?? []).slice();+ const current = frames[frame];+ const next = (frames[frame + 1] ?? []).slice();++ for (const asset of current) {+ const findFrom = (target: AssetMetadata[]) => {+ const index = target.findIndex((a) => areEqual(a, asset));+ if (index === -1) {+ return false;+ } else {+ target.splice(index, 1);+ return true;+ }+ };++ if (!findFrom(prev)) {+ assets.push({...asset, from: frame});+ }++ if (!findFrom(next)) {+ const found = assets.find(+ (a) => a.duration == null && areEqual(a, asset)+ );+ if (!found) throw new Error('something wrong');+ found.duration = frame - found.from - 1;+ }+ }+ }++ return assets;+};diff --git a/packages/renderer/src/render.ts b/packages/renderer/src/render.tsindex 67405341dc..eb5f35b429 100644--- a/packages/renderer/src/render.ts+++ b/packages/renderer/src/render.ts@@ -1,6 +1,7 @@import path from 'path';import {VideoConfig} from 'remotion';import {openBrowser, provideScreenshot} from '.';+import {calculateAssetsPosition} from './assets';import {getActualConcurrency} from './get-concurrency';import {DEFAULT_IMAGE_FORMAT, ImageFormat} from './image-format';import {Pool} from './pool';@@ -59,7 +60,7 @@ export const renderFrames = async ({const filePadLength = String(frames).length;let framesRendered = 0;onStart();- const allAssets = await Promise.all(+ const assetsFrames = await Promise.all(new Array(frames).fill(Boolean).map((x, i) => i)@@ -95,7 +96,8 @@ export const renderFrames = async ({}));- console.log(allAssets);+ const assets = calculateAssetsPosition(assetsFrames);+ console.log(assets);await browser.close();};