Skip to main content

不同片段以不同速度

如果你有一个视频,并且想以不同的速度展示视频的不同部分,请使用以下代码片段。

🌐 If you have a video and want to show different sections of the video at different speeds, use the following snippet.

SegmentSpeeds.tsx
import {OffthreadVideo, Sequence, staticFile, useCurrentFrame} from 'remotion'; const segments = [ { duration: 100, speed: 0.5, }, { duration: 100, speed: 1, }, { duration: 200, speed: 2, }, { duration: 400, speed: 4, }, ]; type AccumulatedSegment = { start: number; passedVideoTime: number; end: number; speed: number; }; export const accumulateSegments = () => { const accumulatedSegments: AccumulatedSegment[] = []; let accumulatedDuration = 0; let accumulatedPassedVideoTime = 0; for (const segment of segments) { const duration = segment.duration / segment.speed; accumulatedSegments.push({ end: accumulatedDuration + duration, speed: segment.speed, start: accumulatedDuration, passedVideoTime: accumulatedPassedVideoTime, }); accumulatedPassedVideoTime += segment.duration; accumulatedDuration += duration; } return accumulatedSegments; }; export const SpeedSegments = () => { const frame = useCurrentFrame(); const accumulated = accumulateSegments(); const currentSegment = accumulated.find((segment) => frame > segment.start && frame <= segment.end); if (!currentSegment) { return; } return ( <Sequence from={currentSegment.start}> <OffthreadVideo pauseWhenBuffering trimBefore={currentSegment.passedVideoTime} // Remotion will automatically add a time fragment to the end of the video URL // based on `trimBefore`. Opt out of this by adding one yourself. // https://www.remotion.dev/docs/media-fragments src={`${staticFile('bigbuckbunny.mp4')}#t=0,`} playbackRate={currentSegment.speed} /> </Sequence> ); };

另请参阅

🌐 See also