Skip to content

🎥 Make videos programmatically with React

Fix video filmstrip flicker while trimming

JonnyBurger039eb3129b5d
diff --git a/packages/studio/src/components/Timeline/TimelineVideoInfo.tsx b/packages/studio/src/components/Timeline/TimelineVideoInfo.tsx
index d4850f2f7b..47ef89503c 100644
--- a/packages/studio/src/components/Timeline/TimelineVideoInfo.tsx
+++ b/packages/studio/src/components/Timeline/TimelineVideoInfo.tsx
@@ -15,7 +15,7 @@ import {
type WaveformVolume,
WEBCODECS_TIMESCALE,
} from '@remotion/timeline-utils';
-import React, {useEffect, useMemo, useRef, useState} from 'react';
+import React, {useLayoutEffect, useMemo, useRef, useState} from 'react';
import type {LoopDisplay} from 'remotion';
import {Internals, useVideoConfig} from 'remotion';
import {BLACK_ALPHA_30} from '../../helpers/colors';
@@ -84,7 +84,7 @@ const TimelineVideoInfoSegment: React.FC<{
}) => {
const {fps} = useVideoConfig();
const resolvedSrc = Internals.usePreload(src);
- const ref = useRef<HTMLDivElement>(null);
+ const ref = useRef<HTMLCanvasElement>(null);
const [error, setError] = useState<Error | null>(null);
const aspectRatio = useRef<number | null>(getAspectRatioFromCache(src));
const mediaStartFrame =
@@ -97,7 +97,7 @@ const TimelineVideoInfoSegment: React.FC<{
sourceOffsetInFrames * playbackRate;
// for rendering frames
- useEffect(() => {
+ useLayoutEffect(() => {
if (error) {
return;
}
@@ -110,7 +110,7 @@ const TimelineVideoInfoSegment: React.FC<{
const controller = new AbortController();
const pixelRatio = getStudioPixelRatio();
- const canvas = document.createElement('canvas');
+ const canvas = current;
canvas.width = Math.ceil(visualizationWidth * pixelRatio);
canvas.height = Math.ceil(TIMELINE_LAYER_FILMSTRIP_HEIGHT * pixelRatio);
canvas.style.width = canvas.width / pixelRatio + 'px';
@@ -120,8 +120,6 @@ const TimelineVideoInfoSegment: React.FC<{
return;
}
- current.appendChild(canvas);
-
const drawRepeatedFrame = (frame: VideoFrame) => {
const thumbnailWidth = Math.max(1, frame.displayWidth);
@@ -174,10 +172,7 @@ const TimelineVideoInfoSegment: React.FC<{
if (cachedFrame) {
drawRepeatedFrame(cachedFrame);
-
- return () => {
- current.removeChild(canvas);
- };
+ return;
}
extractFrames({
@@ -234,7 +229,6 @@ const TimelineVideoInfoSegment: React.FC<{
return () => {
controller.abort();
- current.removeChild(canvas);
};
}
@@ -245,7 +239,6 @@ const TimelineVideoInfoSegment: React.FC<{
targetCanvas.height = canvas.height;
const targetCtx = tiledLoop ? targetCanvas.getContext('2d') : ctx;
if (!targetCtx) {
- current.removeChild(canvas);
return;
}
@@ -315,9 +308,7 @@ const TimelineVideoInfoSegment: React.FC<{
// Don't extract frames if all slots are filled
if (unfilled.length === 0) {
- return () => {
- current.removeChild(canvas);
- };
+ return;
}
}
@@ -434,7 +425,6 @@ const TimelineVideoInfoSegment: React.FC<{
return () => {
controller.abort();
- current.removeChild(canvas);
};
}, [
durationInFrames,
@@ -475,7 +465,9 @@ const TimelineVideoInfoSegment: React.FC<{
return (
<div style={segmentStyle}>
- <div ref={ref} style={filmstripStyle} />
+ <div style={filmstripStyle}>
+ <canvas ref={ref} />
+ </div>
<div style={audioStyle}>
<AudioWaveform
src={src}