How to use parallel offthread video? #5121
Replies: 1 comment
|
Drop the refs and the import React, {useCallback, useRef} from 'react';
import {AbsoluteFill, OffthreadVideo, useVideoConfig} from 'remotion';
import type {OnVideoFrame} from 'remotion';
export const MaskedVideo: React.FC<{
inputVideoSrc: string;
segmentationVideoSrc: string;
}> = ({inputVideoSrc, segmentationVideoSrc}) => {
const {width, height} = useVideoConfig();
const canvasRef = useRef<HTMLCanvasElement>(null);
const input = useRef<CanvasImageSource | null>(null);
const mask = useRef<CanvasImageSource | null>(null);
const draw = useCallback(() => {
if (!input.current || !mask.current || !canvasRef.current) {
return;
}
const ctx = canvasRef.current.getContext('2d', {willReadFrequently: true});
if (!ctx) {
return;
}
ctx.clearRect(0, 0, width, height);
ctx.drawImage(input.current, 0, 0, width, height);
const image = ctx.getImageData(0, 0, width, height);
ctx.clearRect(0, 0, width, height);
ctx.drawImage(mask.current, 0, 0, width, height);
const segmentation = ctx.getImageData(0, 0, width, height);
for (let i = 0; i < image.data.length; i += 4) {
image.data[i + 3] = segmentation.data[i] >= 128 ? 0 : 255;
}
ctx.putImageData(image, 0, 0);
}, [width, height]);
const onInputFrame: OnVideoFrame = useCallback(
(frame) => {
input.current = frame;
draw();
},
[draw],
);
const onMaskFrame: OnVideoFrame = useCallback(
(frame) => {
mask.current = frame;
draw();
},
[draw],
);
return (
<AbsoluteFill>
<AbsoluteFill style={{opacity: 0}}>
<OffthreadVideo src={inputVideoSrc} onVideoFrame={onInputFrame} />
<OffthreadVideo src={segmentationVideoSrc} onVideoFrame={onMaskFrame} />
</AbsoluteFill>
<AbsoluteFill>
<canvas ref={canvasRef} width={width} height={height} />
</AbsoluteFill>
</AbsoluteFill>
);
};why refs give you black on render both callbacks fire before the frame is captured because
no needs Remotion on current versions |
Uh oh!
There was an error while loading. Please reload this page.
I have written this component which is very slow due to the Video component I am assuming. How do I optimize this by using offthread video? Right now the video on preview looks good, but comes as black on render.
All reactions