Skip to content

πŸŽ₯ Make videos programmatically with React

`@remotion/studio`: Fix timeline media flicker while trimming

JonnyBurger72834441ccc2
diff --git a/packages/studio/src/components/AudioWaveform.tsx b/packages/studio/src/components/AudioWaveform.tsx
index 973e2e04d2..287dbd8213 100644
--- a/packages/studio/src/components/AudioWaveform.tsx
+++ b/packages/studio/src/components/AudioWaveform.tsx
@@ -8,6 +8,7 @@ import {
import React, {useLayoutEffect, useMemo, useRef, useState} from 'react';
import type {LoopDisplay} from 'remotion';
import {Internals} from 'remotion';
+import {alignCanvasToDevicePixels} from '../helpers/align-canvas-to-device-pixels';
import {BLACK_ALPHA_60, WHITE_ALPHA_60} from '../helpers/colors';
import {TIMELINE_FRAME_WIDTH_AT_MAX_ZOOM} from '../helpers/get-timeline-max-zoom';
import {resolveStudioColor} from '../helpers/resolve-studio-color';
@@ -155,15 +156,14 @@ const AudioWaveformInner: React.FC<{
}
const pixelRatio = getStudioPixelRatio();
- const h = Math.ceil(height * pixelRatio);
- const w = Math.ceil(visualizationWidth * pixelRatio);
+ const {horizontalOffset} = alignCanvasToDevicePixels({
+ canvas: canvasElement,
+ cssHeight: height,
+ cssWidth: visualizationWidth,
+ pixelRatio,
+ });
const drawingWidth = visualizationWidth * pixelRatio;
- canvasElement.width = w;
- canvasElement.height = h;
- canvasElement.style.width = w / pixelRatio + 'px';
- canvasElement.style.height = h / pixelRatio + 'px';
-
drawBars({
canvas: canvasElement,
peaks: portionPeaks ?? EMPTY_PEAKS,
@@ -173,6 +173,7 @@ const AudioWaveformInner: React.FC<{
),
volume: visibleVolume,
width: drawingWidth,
+ horizontalOffset,
});
}, [height, portionPeaks, visibleVolume, visualizationWidth]);
@@ -187,19 +188,22 @@ const AudioWaveformInner: React.FC<{
}
const pixelRatio = getStudioPixelRatio();
- const h = Math.ceil(height * pixelRatio);
- const w = Math.ceil(visualizationWidth * pixelRatio);
+ const {
+ height: h,
+ horizontalOffset,
+ width: w,
+ } = alignCanvasToDevicePixels({
+ canvas: volumeCanvasElement,
+ cssHeight: height,
+ cssWidth: visualizationWidth,
+ pixelRatio,
+ });
const drawingWidth = visualizationWidth * pixelRatio;
const context = volumeCanvasElement.getContext('2d');
if (!context) {
return;
}
- volumeCanvasElement.width = w;
- volumeCanvasElement.height = h;
- volumeCanvasElement.style.width = w / pixelRatio + 'px';
- volumeCanvasElement.style.height = h / pixelRatio + 'px';
-
context.clearRect(0, 0, w, h);
if (
!Array.isArray(visibleVolume) ||
@@ -210,6 +214,7 @@ const AudioWaveformInner: React.FC<{
return;
}
+ context.setTransform(1, 0, 0, 1, horizontalOffset, 0);
context.beginPath();
context.moveTo(0, 0);
// The canvas only spans the virtualized range. Sampling by its physical
diff --git a/packages/studio/src/components/Timeline/TimelineVideoInfo.tsx b/packages/studio/src/components/Timeline/TimelineVideoInfo.tsx
index 6a05e6e9d5..eaabb9ced1 100644
--- a/packages/studio/src/components/Timeline/TimelineVideoInfo.tsx
+++ b/packages/studio/src/components/Timeline/TimelineVideoInfo.tsx
@@ -18,6 +18,7 @@ import {
import React, {useLayoutEffect, useMemo, useRef, useState} from 'react';
import type {LoopDisplay} from 'remotion';
import {Internals, useVideoConfig} from 'remotion';
+import {alignCanvasToDevicePixels} from '../../helpers/align-canvas-to-device-pixels';
import {BLACK_ALPHA_30} from '../../helpers/colors';
import {getStudioPixelRatio} from '../../helpers/studio-pixel-ratio';
import {
@@ -116,18 +117,30 @@ const TimelineVideoInfoSegment: React.FC<{
const controller = new AbortController();
const pixelRatio = getStudioPixelRatio();
+ const {horizontalOffset} = alignCanvasToDevicePixels({
+ canvas,
+ cssHeight: TIMELINE_LAYER_FILMSTRIP_HEIGHT,
+ cssWidth: visualizationWidth,
+ pixelRatio,
+ });
+ ctx.setTransform(1, 0, 0, 1, horizontalOffset, 0);
- canvas.width = Math.ceil(visualizationWidth * pixelRatio);
- canvas.height = Math.ceil(TIMELINE_LAYER_FILMSTRIP_HEIGHT * pixelRatio);
- canvas.style.width = canvas.width / pixelRatio + 'px';
- canvas.style.height = canvas.height / pixelRatio + 'px';
+ const clearCanvas = () => {
+ ctx.save();
+ ctx.resetTransform();
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
+ ctx.restore();
+ };
const drawRepeatedFrame = (frame: VideoFrame) => {
const thumbnailWidth = Math.max(1, frame.displayWidth);
- ctx.clearRect(0, 0, canvas.width, canvas.height);
+ clearCanvas();
for (let x = 0; x < canvas.width; x += thumbnailWidth) {
- ctx.drawImage(frame, x, 0, thumbnailWidth, canvas.height);
+ const left = Math.floor(x + horizontalOffset) - horizontalOffset;
+ const right =
+ Math.floor(x + thumbnailWidth + horizontalOffset) - horizontalOffset;
+ ctx.drawImage(frame, left, 0, right - left, canvas.height);
}
};
@@ -235,10 +248,14 @@ const TimelineVideoInfoSegment: React.FC<{
}
const targetCanvas = tiledLoop ? document.createElement('canvas') : canvas;
- targetCanvas.width = tiledLoop
- ? Math.max(1, Math.ceil(tiledLoop.loopWidth * pixelRatio))
- : canvas.width;
- targetCanvas.height = canvas.height;
+ if (tiledLoop) {
+ targetCanvas.width = Math.max(
+ 1,
+ Math.ceil(tiledLoop.loopWidth * pixelRatio),
+ );
+ targetCanvas.height = canvas.height;
+ }
+
const targetCtx = tiledLoop ? targetCanvas.getContext('2d') : ctx;
if (!targetCtx) {
return;
@@ -246,6 +263,7 @@ const TimelineVideoInfoSegment: React.FC<{
// desired-timestamp -> filled-timestamp
const filledSlots = new Map<number, number | undefined>();
+ const frameDrawOffset = tiledLoop ? 0 : horizontalOffset;
const {fromSeconds, toSeconds} = times;
// Keep the time-to-pixel scale independent of the integer canvas backing size.
@@ -278,7 +296,7 @@ const TimelineVideoInfoSegment: React.FC<{
0,
]),
);
- ctx.clearRect(0, 0, canvas.width, canvas.height);
+ clearCanvas();
ctx.fillStyle = pattern;
ctx.fillRect(0, 0, canvas.width, canvas.height);
};
@@ -302,6 +320,7 @@ const TimelineVideoInfoSegment: React.FC<{
fromSeconds,
devicePixelRatio: 1,
frameHeight: canvas.height,
+ horizontalOffset: frameDrawOffset,
});
repeatTarget();
const unfilled = Array.from(filledSlots.keys()).filter(
@@ -341,6 +360,7 @@ const TimelineVideoInfoSegment: React.FC<{
fromSeconds,
devicePixelRatio: 1,
frameHeight: canvas.height,
+ horizontalOffset: frameDrawOffset,
});
repeatTarget();
@@ -390,6 +410,7 @@ const TimelineVideoInfoSegment: React.FC<{
fromSeconds,
devicePixelRatio: 1,
frameHeight: canvas.height,
+ horizontalOffset: frameDrawOffset,
});
repeatTarget();
} catch (e) {
@@ -418,6 +439,7 @@ const TimelineVideoInfoSegment: React.FC<{
fromSeconds,
devicePixelRatio: 1,
frameHeight: canvas.height,
+ horizontalOffset: frameDrawOffset,
});
repeatTarget();
})
diff --git a/packages/studio/src/helpers/align-canvas-to-device-pixels.ts b/packages/studio/src/helpers/align-canvas-to-device-pixels.ts
new file mode 100644
index 0000000000..4d7941e018
--- /dev/null
+++ b/packages/studio/src/helpers/align-canvas-to-device-pixels.ts
@@ -0,0 +1,40 @@
+export const alignCanvasToDevicePixels = ({
+ canvas,
+ cssWidth,
+ cssHeight,
+ pixelRatio,
+}: {
+ readonly canvas: HTMLCanvasElement;
+ readonly cssWidth: number;
+ readonly cssHeight: number;
+ readonly pixelRatio: number;
+}) => {
+ const containerLeft =
+ canvas.parentElement?.getBoundingClientRect().left ??
+ canvas.getBoundingClientRect().left;
+ const containerLeftInPixels = containerLeft * pixelRatio;
+ // Keep the canvas bitmap on the device-pixel grid. The returned offset lets
+ // callers preserve the exact timeline position in their drawing coordinates.
+ const horizontalOffset =
+ cssWidth === 0
+ ? 0
+ : containerLeftInPixels - Math.floor(containerLeftInPixels);
+ const width = Math.ceil(cssWidth * pixelRatio + horizontalOffset);
+ const height = Math.ceil(cssHeight * pixelRatio);
+
+ canvas.width = width;
+ canvas.height = height;
+ canvas.style.width = width / pixelRatio + 'px';
+ canvas.style.height = height / pixelRatio + 'px';
+ if (canvas.style.position === '') {
+ canvas.style.position = 'relative';
+ }
+
+ canvas.style.left = -horizontalOffset / pixelRatio + 'px';
+
+ return {
+ height,
+ horizontalOffset,
+ width,
+ };
+};
diff --git a/packages/timeline-utils/src/audio-waveform/draw-peaks.ts b/packages/timeline-utils/src/audio-waveform/draw-peaks.ts
index 4a4d441aea..481c7a2ce6 100644
--- a/packages/timeline-utils/src/audio-waveform/draw-peaks.ts
+++ b/packages/timeline-utils/src/audio-waveform/draw-peaks.ts
@@ -38,12 +38,14 @@ export const drawBars = ({
peaks,
volume,
width,
+ horizontalOffset = 0,
}: {
readonly canvas: HTMLCanvasElement | OffscreenCanvas;
readonly peaks: Float32Array;
readonly color: string;
readonly volume: WaveformVolume;
readonly width: number;
+ readonly horizontalOffset?: number;
}) => {
const ctx = canvas.getContext('2d');
@@ -58,7 +60,7 @@ export const drawBars = ({
// `createImageData(0, h)` / `(w, 0)` throws a DOMException, which
// surfaces in Studio's console for compositions with many audio
// sequences β€” some segments are 0 px wide at certain zoom levels.
- if (w === 0 || height === 0) {
+ if (w === 0 || height === 0 || width <= 0) {
return;
}
@@ -72,9 +74,16 @@ export const drawBars = ({
const numBars = width;
const fullScaleHalfBar = (height * WAVEFORM_HEIGHT_SCALE) / 2;
- for (let barIndex = 0; barIndex < numBars; barIndex++) {
- const x = barIndex;
- if (x >= w) break;
+ for (let x = 0; x < w; x++) {
+ // The canvas may start before its fractionally positioned container so its
+ // bitmap stays device-pixel aligned. Sample from the exact container origin.
+ const barIndex = Math.max(
+ 0,
+ Math.min(
+ numBars - Number.EPSILON * Math.max(1, numBars),
+ x - horizontalOffset,
+ ),
+ );
const peakIndex = Math.floor((barIndex / numBars) * peaks.length);
const peak = peaks[peakIndex] || 0;
diff --git a/packages/timeline-utils/src/render-frame-strip.ts b/packages/timeline-utils/src/render-frame-strip.ts
index b0cfd0dee3..7a5aa3e4a8 100644
--- a/packages/timeline-utils/src/render-frame-strip.ts
+++ b/packages/timeline-utils/src/render-frame-strip.ts
@@ -111,6 +111,7 @@ export const drawSlot = ({
fromSeconds,
devicePixelRatio,
frameHeight,
+ horizontalOffset,
}: {
frame: VideoFrame;
ctx: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D;
@@ -121,6 +122,7 @@ export const drawSlot = ({
fromSeconds: number;
devicePixelRatio: number;
frameHeight: number;
+ horizontalOffset?: number;
}) => {
const durationOfOneFrame = getDurationOfOneFrame({
visualizationWidth,
@@ -132,13 +134,24 @@ export const drawSlot = ({
const relativeTimestamp = timestamp - fromSeconds * WEBCODECS_TIMESCALE;
const frameIndex = relativeTimestamp / durationOfOneFrame;
const thumbnailWidth = frame.displayWidth / devicePixelRatio;
- const left = frameIndex * thumbnailWidth;
+ const idealLeft = frameIndex * thumbnailWidth;
+ const idealRight = idealLeft + thumbnailWidth;
+ // When the canvas is aligned to the device-pixel grid, use the same absolute
+ // grid for both edges so adjacent thumbnails neither shimmer nor leave seams.
+ const left =
+ horizontalOffset === undefined
+ ? idealLeft
+ : Math.floor(idealLeft + horizontalOffset) - horizontalOffset;
+ const right =
+ horizontalOffset === undefined
+ ? idealRight
+ : Math.floor(idealRight + horizontalOffset) - horizontalOffset;
ctx.drawImage(
frame,
left,
0,
- thumbnailWidth,
+ right - left,
frame.displayHeight / devicePixelRatio,
);
filledSlots.set(timestamp, frame.timestamp);
@@ -153,6 +166,7 @@ export const fillWithCachedFrames = ({
fromSeconds,
devicePixelRatio,
frameHeight,
+ horizontalOffset,
}: {
ctx: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D;
naturalWidth: number;
@@ -162,6 +176,7 @@ export const fillWithCachedFrames = ({
fromSeconds: number;
devicePixelRatio: number;
frameHeight: number;
+ horizontalOffset?: number;
}) => {
const prefix = getFrameDatabaseKeyPrefix(src);
const keys = Array.from(frameDatabase.keys()).filter((k) =>
@@ -212,6 +227,7 @@ export const fillWithCachedFrames = ({
fromSeconds,
devicePixelRatio,
frameHeight,
+ horizontalOffset,
});
}
};
@@ -225,6 +241,7 @@ export const fillFrameWhereItFits = ({
fromSeconds,
devicePixelRatio,
frameHeight,
+ horizontalOffset,
}: {
frame: VideoFrame;
filledSlots: Map<number, number | undefined>;
@@ -234,6 +251,7 @@ export const fillFrameWhereItFits = ({
fromSeconds: number;
devicePixelRatio: number;
frameHeight: number;
+ horizontalOffset?: number;
}) => {
const slots = Array.from(filledSlots.keys());
@@ -261,6 +279,7 @@ export const fillFrameWhereItFits = ({
fromSeconds,
devicePixelRatio,
frameHeight,
+ horizontalOffset,
});
}
};