Skip to content

🎥 Make videos programmatically with React

remotion.dev/convert: fixes

JonnyBurgera23891e6be6b
diff --git a/packages/convert/app/components/ConvertUi.tsx b/packages/convert/app/components/ConvertUi.tsx
index 46c3eea517..b8858e5e5b 100644
--- a/packages/convert/app/components/ConvertUi.tsx
+++ b/packages/convert/app/components/ConvertUi.tsx
@@ -236,141 +236,149 @@ const ConvertUI = ({
newName: filename,
});
- const conversion = await Conversion.init({
- input,
- output,
- video: (videoTrack) => {
- const operation = getActualVideoOperation({
- enableConvert,
- trackNumber: videoTrack.id,
- videoConfigIndexSelection: videoOperationSelection,
- operations:
- supportedConfigs.videoTrackOptions.find(
- (o) => o.trackId === videoTrack.id,
- )?.operations ?? [],
- });
+ try {
+ const conversion = await Conversion.init({
+ input,
+ output,
+ video: (videoTrack) => {
+ const operation = getActualVideoOperation({
+ enableConvert,
+ trackNumber: videoTrack.id,
+ videoConfigIndexSelection: videoOperationSelection,
+ operations:
+ supportedConfigs.videoTrackOptions.find(
+ (o) => o.trackId === videoTrack.id,
+ )?.operations ?? [],
+ });
+
+ if (operation.type === 'drop') {
+ return {discard: true};
+ }
- if (operation.type === 'drop') {
- return {discard: true};
- }
+ if (operation.type === 'copy') {
+ progress.hasVideo = true;
+ return {};
+ }
+
+ if (operation.type === 'fail') {
+ throw new Error('videoOperation.type === "fail"');
+ }
- if (operation.type === 'copy') {
progress.hasVideo = true;
- return {};
- }
+ return {
+ height: Math.min(videoTrack.displayHeight, 1080),
+ process(sample) {
+ const flipped = flipVideoFrame({
+ frame: sample.toVideoFrame(),
+ horizontal:
+ flipHorizontal && enableRotateOrMirror === 'mirror',
+ vertical: flipVertical && enableRotateOrMirror === 'mirror',
+ });
+ if (videoFrames % 15 === 0) {
+ convertProgressRef.current?.draw(flipped);
+ }
+
+ progress.millisecondsWritten = sample.timestamp * 1000;
+
+ videoFrames++;
+ return flipped;
+ },
+ rotate: userRotation as Rotation,
+ forceTranscode: true,
+ ...calculateMediabunnyResizeOption(
+ resizeOperation,
+ dimensions ?? null,
+ ),
+ codec: operation.videoCodec,
+ };
+ },
+ audio: (audioTrack) => {
+ const operations =
+ supportedConfigs.audioTrackOptions.find((o) => {
+ return o.trackId === audioTrack.id;
+ })?.operations ?? [];
+ const operation = getActualAudioOperation({
+ audioConfigIndexSelection: audioOperationSelection,
+ enableConvert,
+ operations,
+ trackNumber: audioTrack.id,
+ });
- if (operation.type === 'fail') {
- throw new Error('videoOperation.type === "fail"');
- }
+ if (operation.type === 'fail') {
+ throw new Error('audioOperation.type === "fail"');
+ }
- progress.hasVideo = true;
- return {
- height: Math.min(videoTrack.displayHeight, 1080),
- process(sample) {
- const flipped = flipVideoFrame({
- frame: sample.toVideoFrame(),
- horizontal: flipHorizontal && enableRotateOrMirror === 'mirror',
- vertical: flipVertical && enableRotateOrMirror === 'mirror',
- });
- if (videoFrames % 15 === 0) {
- convertProgressRef.current?.draw(flipped);
- }
-
- progress.millisecondsWritten = sample.timestamp * 1000;
-
- videoFrames++;
- return flipped;
- },
- rotate: userRotation as Rotation,
- forceTranscode: true,
- ...calculateMediabunnyResizeOption(
- resizeOperation,
- dimensions ?? null,
- ),
- codec: operation.videoCodec,
- };
- },
- audio: (audioTrack) => {
- const operations =
- supportedConfigs.audioTrackOptions.find((o) => {
- return o.trackId === audioTrack.id;
- })?.operations ?? [];
- const operation = getActualAudioOperation({
- audioConfigIndexSelection: audioOperationSelection,
- enableConvert,
- operations,
- trackNumber: audioTrack.id,
- });
+ if (operation.type === 'copy') {
+ return {};
+ }
- if (operation.type === 'fail') {
- throw new Error('audioOperation.type === "fail"');
- }
+ if (operation.type === 'drop') {
+ return {discard: true};
+ }
- if (operation.type === 'copy') {
- return {};
- }
+ return {
+ sampleRate: operation.sampleRate ?? undefined,
+ process(sample) {
+ if (!progress.hasVideo) {
+ waveform.add(sample.toAudioBuffer());
+ }
+
+ return sample;
+ },
+ };
+ },
+ });
- if (operation.type === 'drop') {
- return {discard: true};
- }
+ if (stopped) {
+ return;
+ }
- return {
- sampleRate: operation.sampleRate ?? undefined,
- process(sample) {
- if (!progress.hasVideo) {
- waveform.add(sample.toAudioBuffer());
- }
+ cancelConversion = () => {
+ conversion.cancel();
- return sample;
- },
- };
- },
- });
+ setState({
+ type: 'idle',
+ });
+ };
- if (stopped) {
- return;
- }
+ if (input.disposed) {
+ throw new Error('Input disposed');
+ }
- cancelConversion = () => {
- conversion.cancel();
+ conversion.onProgress = (newProg) => {
+ progress.overallProgress = newProg;
+ progress.bytesWritten = getWrittenByteCount();
- setState({
- type: 'idle',
- });
- };
+ setState({
+ type: 'in-progress',
+ onAbort: cancelConversion,
+ state: progress,
+ startTime,
+ newName: filename,
+ });
+ };
- if (input.disposed) {
- throw new Error('Input disposed');
- }
+ await conversion.execute();
- conversion.onProgress = (newProg) => {
- progress.overallProgress = newProg;
- progress.bytesWritten = getWrittenByteCount();
+ close();
setState({
- type: 'in-progress',
- onAbort: cancelConversion,
+ type: 'done',
+ download: async () => {
+ const blob = await getBlob();
+ return blob;
+ },
state: progress,
startTime,
+ completedTime: Date.now(),
newName: filename,
});
- };
-
- await conversion.execute();
-
- close();
-
- setState({
- type: 'done',
- download: async () => {
- const blob = await getBlob();
- return blob;
- },
- state: progress,
- startTime,
- completedTime: Date.now(),
- newName: filename,
- });
+ } catch (error) {
+ setState({
+ type: 'error',
+ error: error as Error,
+ });
+ }
};
run();
diff --git a/packages/convert/app/lib/mediabunny-calculate-resize-option.ts b/packages/convert/app/lib/mediabunny-calculate-resize-option.ts
index 96f51a84a7..a60a4648b0 100644
--- a/packages/convert/app/lib/mediabunny-calculate-resize-option.ts
+++ b/packages/convert/app/lib/mediabunny-calculate-resize-option.ts
@@ -29,7 +29,7 @@ export const calculateMediabunnyResizeOption = (
if (resizeOperation.mode === 'scale') {
return {
- height: rotatedDimensions.height * resizeOperation.scale,
+ height: Math.round(rotatedDimensions.height * resizeOperation.scale),
};
}