Skip to content

πŸŽ₯ Make videos programmatically with React

sync progress

Jonny Burger39ccc8ffeefd
diff --git a/packages/cli/src/editor/components/CurrentCompositionSideEffects.tsx b/packages/cli/src/editor/components/CurrentCompositionSideEffects.tsx
index 195dd56e58..737c93279f 100644
--- a/packages/cli/src/editor/components/CurrentCompositionSideEffects.tsx
+++ b/packages/cli/src/editor/components/CurrentCompositionSideEffects.tsx
@@ -1,7 +1,9 @@
import type React from 'react';
-import {useCallback, useEffect} from 'react';
+import {useCallback, useContext, useEffect} from 'react';
import {Internals} from 'remotion';
+import {PreviewServerConnectionCtx} from '../helpers/client-id';
import {useKeybinding} from '../helpers/use-keybinding';
+import {sendErrorNotification} from './Notifications/NotificationCenter';
export const TitleUpdater: React.FC = () => {
const video = Internals.useVideo();
@@ -21,10 +23,16 @@ export const TitleUpdater: React.FC = () => {
export const CurrentCompositionKeybindings: React.FC = () => {
const keybindings = useKeybinding();
const video = Internals.useVideo();
+ const {type} = useContext(PreviewServerConnectionCtx);
const openRenderModal = useCallback(() => {
if (!video) {
- return null;
+ return;
+ }
+
+ if (type !== 'connected') {
+ sendErrorNotification('Preview server is offline');
+ return;
}
const renderButton = document.getElementById(
@@ -32,7 +40,7 @@ export const CurrentCompositionKeybindings: React.FC = () => {
) as HTMLDivElement;
renderButton.click();
- }, [video]);
+ }, [type, video]);
useEffect(() => {
const binding = keybindings.registerKeybinding({
diff --git a/packages/cli/src/editor/components/RenderModal/RenderProgressModal.tsx b/packages/cli/src/editor/components/RenderModal/RenderProgressModal.tsx
index 048f29a9c6..fb1c70d879 100644
--- a/packages/cli/src/editor/components/RenderModal/RenderProgressModal.tsx
+++ b/packages/cli/src/editor/components/RenderModal/RenderProgressModal.tsx
@@ -20,8 +20,8 @@ export const RenderProgressModal: React.FC<{jobId: string}> = ({jobId}) => {
const job = jobs.find((j) => j.id === jobId);
- console.log({jobs});
- if (!job || job.status === 'idle') {
+ // TODO: What if the job fails while the modal is open? Merge together?
+ if (!job || job.status === 'idle' || job.status === 'failed') {
throw new Error('should not have rendered this modal');
}
@@ -30,7 +30,7 @@ export const RenderProgressModal: React.FC<{jobId: string}> = ({jobId}) => {
<NewCompHeader title={`Render ${job.compositionId}`} />
<div style={container}>
<Spacing />
- {JSON.stringify(job, null, 2)}
+ {JSON.stringify(job.progress, null, 2)}
</div>
</ModalContainer>
);
diff --git a/packages/cli/src/preview-server/render-queue/job.ts b/packages/cli/src/preview-server/render-queue/job.ts
index 2f0abbe7dd..dcc1e6d1fe 100644
--- a/packages/cli/src/preview-server/render-queue/job.ts
+++ b/packages/cli/src/preview-server/render-queue/job.ts
@@ -21,6 +21,7 @@ export type GuiStillProgress = BaseRenderProgress & AggregateRenderProgress;
type RenderJobDynamicStatus<ProgressType extends BaseRenderProgress> =
| {
status: 'done';
+ progress: ProgressType;
}
| {
status: 'running';
diff --git a/packages/cli/src/preview-server/render-queue/queue.ts b/packages/cli/src/preview-server/render-queue/queue.ts
index c00e4e644b..1b77a1733b 100644
--- a/packages/cli/src/preview-server/render-queue/queue.ts
+++ b/packages/cli/src/preview-server/render-queue/queue.ts
@@ -2,6 +2,7 @@ import path from 'path';
import {chalk} from '../../chalk';
import {installFileWatcher} from '../../file-watcher';
import {Log} from '../../log';
+import type {AggregateRenderProgress} from '../../progress-types';
import {initialAggregateRenderProgress} from '../../progress-types';
import {waitForLiveEventsListener} from '../live-events';
import type {
@@ -157,12 +158,14 @@ export const processJobIfPossible = async ({
});
const startTime = Date.now();
Log.info(chalk.gray('╭─ Starting render '));
+ let lastProgress: AggregateRenderProgress | null = null;
await processJob({
job: nextJob,
entryPoint,
remotionRoot,
onProgress: (progress) => {
updateJob(nextJob.id, (job) => {
+ lastProgress = progress;
// Ignore late callbacks of progress updates after cancelling
if (job.status === 'failed' || job.status === 'done') {
return job;
@@ -211,11 +214,18 @@ export const processJobIfPossible = async ({
}
},
});
- updateJob(nextJob.id, (job) => ({
- ...job,
- status: 'done',
- cleanup: [...job.cleanup, unwatch],
- }));
+ updateJob(nextJob.id, (job) => {
+ if (!lastProgress) {
+ throw new Error('expected progress');
+ }
+
+ return {
+ ...job,
+ status: 'done',
+ cleanup: [...job.cleanup, unwatch],
+ progress: {message: 'Done', value: 1, ...lastProgress},
+ };
+ });
} catch (err) {
// TODO: Tell to look in preview to find the error
Log.error(chalk.gray('╰─ Render failed:'), err);
diff --git a/packages/cli/src/progress-types.ts b/packages/cli/src/progress-types.ts
index f61c9a7264..5bb0d29608 100644
--- a/packages/cli/src/progress-types.ts
+++ b/packages/cli/src/progress-types.ts
@@ -26,11 +26,16 @@ export type StitchingProgressInput = {
codec: Codec;
};
+export type BundlingProgress = {
+ progress: number;
+ message: string | null;
+};
+
export type AggregateRenderProgress = {
rendering: RenderingProgressInput | null;
stitching: StitchingProgressInput | null;
downloads: DownloadProgress[];
- bundling: {progress: number; message: string | null};
+ bundling: BundlingProgress;
};
export const initialAggregateRenderProgress = (): AggregateRenderProgress => ({
diff --git a/packages/cli/src/render-flows/render.ts b/packages/cli/src/render-flows/render.ts
index cf8e07af93..51e2610ebe 100644
--- a/packages/cli/src/render-flows/render.ts
+++ b/packages/cli/src/render-flows/render.ts
@@ -12,7 +12,6 @@ import type {
PixelFormat,
ProResProfile,
RenderMediaOnDownload,
- StitchingState,
VideoImageFormat,
} from '@remotion/renderer';
import {
@@ -46,7 +45,10 @@ import {
} from '../progress-bar';
import type {
AggregateRenderProgress,
+ BundlingProgress,
DownloadProgress,
+ RenderingProgressInput,
+ StitchingProgressInput,
} from '../progress-types';
import {bundleOnCliOrTakeServeUrl} from '../setup-cache';
import type {RenderStep} from '../step';
@@ -161,20 +163,53 @@ export const renderVideoFlow = async ({
indentationString: indent ? INDENT_TOKEN + ' ' : '',
});
+ const renderProgress = createOverwriteableCliOutput({
+ quiet,
+ cancelSignal,
+ });
+
const steps: RenderStep[] = [
RenderInternals.isServeUrl(fullEntryPoint) ? null : ('bundling' as const),
'rendering' as const,
shouldOutputImageSequence ? null : ('stitching' as const),
].filter(truthy);
+ const bundlingProgress: BundlingProgress = {
+ message: null,
+ progress: 0,
+ };
+
+ let renderingProgress: RenderingProgressInput | null = null;
+ let stitchingProgress: StitchingProgressInput | null = null;
+
+ const updateRenderProgress = () => {
+ const aggregateRenderProgress: AggregateRenderProgress = {
+ rendering: renderingProgress,
+ stitching: shouldOutputImageSequence ? null : stitchingProgress,
+ downloads,
+ bundling: bundlingProgress,
+ };
+
+ const {output, message, progress} = makeRenderingAndStitchingProgress(
+ aggregateRenderProgress,
+ indent
+ );
+ onProgress({message, value: progress, ...aggregateRenderProgress});
+
+ return renderProgress.update(output);
+ };
+
const {urlOrBundle, cleanup: cleanupBundle} = await bundleOnCliOrTakeServeUrl(
{
fullPath: fullEntryPoint,
remotionRoot,
steps,
publicDir,
- // TODO: Implement onProgress
- onProgress: () => undefined,
+ onProgress: ({message, progress}) => {
+ bundlingProgress.message = message;
+ bundlingProgress.progress = progress;
+ updateRenderProgress();
+ },
indentOutput: indent,
logLevel,
}
@@ -272,11 +307,6 @@ export const renderVideoFlow = async ({
Log.verboseAdvanced({indent, logLevel}, 'Output dir', outputDir);
- const renderProgress = createOverwriteableCliOutput({
- quiet,
- cancelSignal,
- });
-
const realFrameRange = RenderInternals.getRealFrameRange(
config.durationInFrames,
frameRange
@@ -285,51 +315,14 @@ export const renderVideoFlow = async ({
realFrameRange,
everyNthFrame
);
- let encodedFrames = 0;
- let renderedFrames = 0;
- let encodedDoneIn: number | null = null;
- let renderedDoneIn: number | null = null;
- let stitchStage: StitchingState = 'encoding';
-
const actualConcurrency = RenderInternals.getActualConcurrency(concurrency);
- const updateRenderProgress = () => {
- if (totalFrames.length === 0) {
- throw new Error('totalFrames should not be 0');
- }
-
- const aggregateRenderProgress: AggregateRenderProgress = {
- rendering: {
- frames: renderedFrames,
- totalFrames: totalFrames.length,
- concurrency: actualConcurrency,
- doneIn: renderedDoneIn,
- steps,
- },
- stitching: shouldOutputImageSequence
- ? null
- : {
- doneIn: encodedDoneIn,
- frames: encodedFrames,
- stage: stitchStage,
- steps,
- totalFrames: totalFrames.length,
- codec,
- },
- downloads,
- bundling: {
- message: 'Bundled',
- progress: 1,
- },
- };
-
- const {output, message, progress} = makeRenderingAndStitchingProgress(
- aggregateRenderProgress,
- indent
- );
- onProgress({message, value: progress, ...aggregateRenderProgress});
-
- return renderProgress.update(output);
+ renderingProgress = {
+ frames: 0,
+ totalFrames: totalFrames.length,
+ concurrency: actualConcurrency,
+ doneIn: null,
+ steps,
};
const imageFormat = getVideoImageFormat({
@@ -351,7 +344,7 @@ export const renderVideoFlow = async ({
imageFormat,
inputProps,
onFrameUpdate: (rendered) => {
- renderedFrames = rendered;
+ (renderingProgress as RenderingProgressInput).frames = rendered;
updateRenderProgress();
},
onStart: () => undefined,
@@ -392,8 +385,18 @@ export const renderVideoFlow = async ({
updateRenderProgress();
process.stdout.write('\n');
Log.infoAdvanced({indent, logLevel}, chalk.cyan(`β–Ά ${absoluteOutputFile}`));
+ return;
}
+ stitchingProgress = {
+ doneIn: null,
+ frames: 0,
+ stage: 'encoding',
+ steps,
+ totalFrames: totalFrames.length,
+ codec,
+ };
+
const {slowestFrames} = await renderMedia({
outputLocation: absoluteOutputFile,
composition: {
@@ -430,11 +433,15 @@ export const renderVideoFlow = async ({
audioBitrate,
videoBitrate,
onProgress: (update) => {
- encodedDoneIn = update.encodedDoneIn;
- encodedFrames = update.encodedFrames;
- renderedDoneIn = update.renderedDoneIn;
- stitchStage = update.stitchStage;
- renderedFrames = update.renderedFrames;
+ (stitchingProgress as StitchingProgressInput).doneIn =
+ update.encodedDoneIn;
+ (stitchingProgress as StitchingProgressInput).frames =
+ update.encodedFrames;
+ (stitchingProgress as StitchingProgressInput).stage = update.stitchStage;
+ (renderingProgress as RenderingProgressInput).doneIn =
+ update.renderedDoneIn;
+ (renderingProgress as RenderingProgressInput).frames =
+ update.renderedFrames;
updateRenderProgress();
},
puppeteerInstance,