Skip to content

🎥 Make videos programmatically with React

fix audio only exports

Jonny Burgerd6312bd065b1
diff --git a/packages/convert/app/components/ConvertForm.tsx b/packages/convert/app/components/ConvertForm.tsx
index 155985925a..c21421cbf1 100644
--- a/packages/convert/app/components/ConvertForm.tsx
+++ b/packages/convert/app/components/ConvertForm.tsx
@@ -46,8 +46,6 @@ export const ConvertForm: React.FC<{
}) => {
const [showAdvanced, setShowAdvanced] = React.useState(false);
- console.log(supportedConfigs);
-
return (
<div className="gap-4 grid">
<div>
diff --git a/packages/convert/app/components/ConvertUi.tsx b/packages/convert/app/components/ConvertUi.tsx
index 89f23376f3..5681129499 100644
--- a/packages/convert/app/components/ConvertUi.tsx
+++ b/packages/convert/app/components/ConvertUi.tsx
@@ -1,5 +1,6 @@
import {Button} from '@/components/ui/button';
import {CardTitle} from '@/components/ui/card';
+import {MediaParserInternals} from '@remotion/media-parser';
import {fetchReader} from '@remotion/media-parser/fetch';
import {webFileReader} from '@remotion/media-parser/web-file';
import {convertMedia, ConvertMediaContainer} from '@remotion/webcodecs';
@@ -94,6 +95,50 @@ export default function ConvertUI({
_n = n;
setName(n);
},
+ onAudioTrack: ({track}) => {
+ const options = supportedConfigs?.audioTrackOptions.find((trk) => {
+ return trk.trackId === track.trackId;
+ });
+ if (!options) {
+ throw new Error('Found no options for audio track');
+ }
+
+ const configIndex = audioConfigIndex[track.trackId] ?? 0;
+
+ const operation = options.operations[configIndex ?? 0];
+ if (!operation) {
+ throw new Error('Found no operation');
+ }
+
+ MediaParserInternals.Log.info(
+ 'info',
+ `Selected operation for audio track ${track.trackId}`,
+ operation,
+ );
+
+ return operation;
+ },
+ onVideoTrack: ({track}) => {
+ const options = supportedConfigs?.videoTrackOptions.find((trk) => {
+ return trk.trackId === track.trackId;
+ });
+ if (!options) {
+ throw new Error('Found no options for video track');
+ }
+
+ const configIndex = videoConfigIndex[track.trackId] ?? 0;
+
+ const operation = options.operations[configIndex ?? 0];
+ if (!operation) {
+ throw new Error('Found no operation');
+ }
+ MediaParserInternals.Log.info(
+ 'info',
+ `Selected operation for video track ${track.trackId}`,
+ operation,
+ );
+ return operation;
+ },
})
.then(({save}) => {
// TODO: When to remove?
@@ -132,7 +177,15 @@ export default function ConvertUI({
return () => {
abortController.abort();
};
- }, [src, container, flipHorizontal, flipVertical]);
+ }, [
+ src,
+ container,
+ flipHorizontal,
+ flipVertical,
+ supportedConfigs,
+ audioConfigIndex,
+ videoConfigIndex,
+ ]);
const cancel = useCallback(() => {
if (state.type !== 'in-progress') {
diff --git a/packages/media-parser/src/create/cluster.ts b/packages/media-parser/src/create/cluster.ts
index 72b4ee7187..e896089564 100644
--- a/packages/media-parser/src/create/cluster.ts
+++ b/packages/media-parser/src/create/cluster.ts
@@ -16,8 +16,28 @@ export const timestampToClusterTimestamp = (timestamp: number) => {
return Math.round((timestamp / CREATE_TIME_SCALE) * 1000);
};
-export const makeCluster = async (w: Writer, timestamp: number) => {
- const cluster = createClusterSegment(timestampToClusterTimestamp(timestamp));
+export const canFitInCluster = ({
+ clusterStartTimestamp,
+ chunk,
+}: {
+ clusterStartTimestamp: number;
+ chunk: AudioOrVideoSample;
+}) => {
+ const timecodeRelativeToCluster =
+ timestampToClusterTimestamp(chunk.timestamp) -
+ timestampToClusterTimestamp(clusterStartTimestamp);
+
+ if (timecodeRelativeToCluster < 0) {
+ throw new Error(`timecodeRelativeToCluster is negative`);
+ }
+
+ return timecodeRelativeToCluster <= maxClusterTimestamp;
+};
+
+export const makeCluster = async (w: Writer, clusterStartTimestamp: number) => {
+ const cluster = createClusterSegment(
+ timestampToClusterTimestamp(clusterStartTimestamp),
+ );
const clusterVIntPosition =
w.getWrittenByteCount() +
cluster.offsets.offset +
@@ -32,14 +52,9 @@ export const makeCluster = async (w: Writer, timestamp: number) => {
const addSample = async (chunk: AudioOrVideoSample, trackNumber: number) => {
const timecodeRelativeToCluster =
timestampToClusterTimestamp(chunk.timestamp) -
- timestampToClusterTimestamp(timestamp);
- if (timecodeRelativeToCluster < 0) {
- throw new Error(
- `timecodeRelativeToCluster is negative (track ${trackNumber})`,
- );
- }
+ timestampToClusterTimestamp(clusterStartTimestamp);
- if (timecodeRelativeToCluster > maxClusterTimestamp) {
+ if (!canFitInCluster({clusterStartTimestamp, chunk})) {
throw new Error(
`timecodeRelativeToCluster is too big: ${timecodeRelativeToCluster} > ${maxClusterTimestamp}`,
);
@@ -66,18 +81,36 @@ export const makeCluster = async (w: Writer, timestamp: number) => {
const shouldMakeNewCluster = ({
isVideo,
- keyframe,
+ chunk,
newT,
}: {
newT: number;
- keyframe: boolean;
+ chunk: AudioOrVideoSample;
isVideo: boolean;
}) => {
const newTimestamp = timestampToClusterTimestamp(newT);
- const oldTimestamp = timestampToClusterTimestamp(timestamp);
+ const oldTimestamp = timestampToClusterTimestamp(clusterStartTimestamp);
+ const canFit = canFitInCluster({
+ chunk,
+ clusterStartTimestamp,
+ });
+
+ if (!canFit) {
+ // We must create a new cluster
+ // This is for example if we have an audio-only file
+ return true;
+ }
+
+ const keyframe = chunk.type === 'key';
+
+ // TODO: Timestamp falls apart when video only
return newTimestamp - oldTimestamp >= 2000 && keyframe && isVideo;
};
- return {addSample, shouldMakeNewCluster};
+ return {
+ addSample,
+ shouldMakeNewCluster,
+ startTimestamp: clusterStartTimestamp,
+ };
};
diff --git a/packages/media-parser/src/create/create-media.ts b/packages/media-parser/src/create/create-media.ts
index 192f1e23b8..69fb681968 100644
--- a/packages/media-parser/src/create/create-media.ts
+++ b/packages/media-parser/src/create/create-media.ts
@@ -152,11 +152,12 @@ export const createMedia = async ({
isVideo: boolean;
}) => {
const smallestProgress = Math.min(...Object.values(trackNumberProgresses));
+
if (
!currentCluster.shouldMakeNewCluster({
newT: smallestProgress,
- keyframe: chunk.type === 'key',
isVideo,
+ chunk,
})
) {
return {cluster: currentCluster, isNew: false, smallestProgress};