How do I use segmentation videos to segment object in video? #5090
Answered
by
JonnyBurger
gajraj-gan
asked this question in
Q&A
-
Hello there, import React, { useEffect, useRef, useState } from 'react';
import {
AbsoluteFill,
continueRender,
delayRender,
useCurrentFrame,
useVideoConfig,
Video
} from 'remotion';
type CustomOffthreadVideoProps = {
inputVideoSrc: string;
segmentationVideoSrc: string;
};
export const CustomOffthreadVideo: React.FC<CustomOffthreadVideoProps> = ({
inputVideoSrc,
segmentationVideoSrc
}) => {
const inputVideoRef = useRef<HTMLVideoElement>(null);
const segmentationVideoRef = useRef<HTMLVideoElement>(null);
const canvasRef = useRef<HTMLCanvasElement>(null);
const frame = useCurrentFrame();
const { width, height } = useVideoConfig();
const [handle] = useState(() => delayRender('Fetching dimensions...'));
const [video1Loaded, setVideo1Loaded] = useState(false);
const [video2Loaded, setVideo2Loaded] = useState(false);
useEffect(() => {
if (video1Loaded && video2Loaded) {
continueRender(handle);
}
}, [video1Loaded, video2Loaded]);
useEffect(() => {
const inputVideo = inputVideoRef.current;
const segmentationVideo = segmentationVideoRef.current;
const canvas = canvasRef.current;
if (!video1Loaded || !video2Loaded || !inputVideo || !segmentationVideo || !canvas) {
return;
}
const context = canvas.getContext('2d');
if (!context) {
return;
}
const renderFrame = () => {
if (inputVideo.readyState >= 2 && segmentationVideo.readyState >= 2) {
context.clearRect(0, 0, width, height);
context.drawImage(inputVideo, 0, 0, width, height);
const imageData = context.getImageData(0, 0, width, height);
context.drawImage(segmentationVideo, 0, 0, width, height);
const segmentationData = context.getImageData(0, 0, width, height);
for (let i = 0; i < imageData.data.length; i += 4) {
const alpha = segmentationData.data[i] / 255;
imageData.data[i + 3] = alpha * 255;
}
context.putImageData(imageData, 0, 0);
}
};
renderFrame();
}, [frame, width, height, video1Loaded, video2Loaded]);
return (
<AbsoluteFill>
<AbsoluteFill>
<Video
onLoad={() => setVideo1Loaded(true)}
ref={inputVideoRef}
src={inputVideoSrc}
style={{ opacity: 0 }}
/>
<Video
onLoad={() => setVideo2Loaded(true)}
ref={segmentationVideoRef}
src={segmentationVideoSrc}
style={{ opacity: 0 }}
/>
</AbsoluteFill>
<AbsoluteFill>
<canvas ref={canvasRef} width={width} height={height} />
</AbsoluteFill>
</AbsoluteFill>
);
}; |
Beta Was this translation helpful? Give feedback.
Answered by
JonnyBurger
Apr 2, 2025
Replies: 1 comment 7 replies
-
Good question, probably you want to base it off the pixel manipulation example: https://www.remotion.dev/docs/video-manipulation Where exactly does it stop working? |
Beta Was this translation helpful? Give feedback.
7 replies
Answer selected by
gajraj-gan
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good question, probably you want to base it off the pixel manipulation example: https://www.remotion.dev/docs/video-manipulation
Where exactly does it stop working?