🎥 Make videos programmatically with React
`@remotion/renderer`: Fix fractional OffthreadVideo frame selection (#10090)
Jonny Burgere1e2c50408f6
diff --git a/packages/compositor-darwin-arm64/remotion b/packages/compositor-darwin-arm64/remotionindex cc3a3cbc4c..32a4b4754b 100755Binary files a/packages/compositor-darwin-arm64/remotion and b/packages/compositor-darwin-arm64/remotion differdiff --git a/packages/compositor-darwin-x64/remotion b/packages/compositor-darwin-x64/remotionindex 24b584bb35..96dc42783a 100755Binary files a/packages/compositor-darwin-x64/remotion and b/packages/compositor-darwin-x64/remotion differdiff --git a/packages/compositor-linux-arm64-gnu/remotion b/packages/compositor-linux-arm64-gnu/remotionindex 4e7d1c2b78..76ed7e8ded 100755Binary files a/packages/compositor-linux-arm64-gnu/remotion and b/packages/compositor-linux-arm64-gnu/remotion differdiff --git a/packages/compositor-linux-arm64-musl/remotion b/packages/compositor-linux-arm64-musl/remotionindex 111a6f8269..e48d40c655 100755Binary files a/packages/compositor-linux-arm64-musl/remotion and b/packages/compositor-linux-arm64-musl/remotion differdiff --git a/packages/compositor-linux-x64-gnu/remotion b/packages/compositor-linux-x64-gnu/remotionindex d59712ed3e..030c58c99f 100755Binary files a/packages/compositor-linux-x64-gnu/remotion and b/packages/compositor-linux-x64-gnu/remotion differdiff --git a/packages/compositor-linux-x64-musl/remotion b/packages/compositor-linux-x64-musl/remotionindex 56a2ab5fd7..b9451f615b 100755Binary files a/packages/compositor-linux-x64-musl/remotion and b/packages/compositor-linux-x64-musl/remotion differdiff --git a/packages/compositor-win32-x64-msvc/remotion.exe b/packages/compositor-win32-x64-msvc/remotion.exeindex b8ce75dd58..62a2180f58 100755Binary files a/packages/compositor-win32-x64-msvc/remotion.exe and b/packages/compositor-win32-x64-msvc/remotion.exe differdiff --git a/packages/compositor/rust/ffmpeg.rs b/packages/compositor/rust/ffmpeg.rsindex eccb70ef9d..758b854ba0 100644--- a/packages/compositor/rust/ffmpeg.rs+++ b/packages/compositor/rust/ffmpeg.rs@@ -30,7 +30,8 @@ pub fn extract_frame(frame_cache_manager: &mut FrameCacheManager,max_cache_size: u64,) -> Result<Vec<u8>, ErrorWithBacktrace> {- // Don't allow previous frame, but allow for some flexibility+ // Do not return a previous frame until a frame after the requested timestamp+ // is cached as well, so its presentation interval is known.let cache_item = match manager.get_position_and_threshold_of_video(time, &src) {Ok(Some((position, threshold))) => frame_cache_manager.get_cache_item_id(&src,diff --git a/packages/compositor/rust/frame_cache.rs b/packages/compositor/rust/frame_cache.rsindex e8bd9e1467..418e87ae01 100644--- a/packages/compositor/rust/frame_cache.rs+++ b/packages/compositor/rust/frame_cache.rs@@ -226,16 +226,34 @@ impl FrameCache {}}+ // A video frame is displayed from its PTS until the PTS of the next frame.+ // Therefore, select the latest frame at or before the requested timestamp,+ // instead of the frame with the nearest PTS. If the request is before the+ // first frame, fall back to the earliest frame after the timestamp.for i in 0..self.items.len() {- let distance = (self.items[i].resolved_pts - time as i64).abs();- // LTE: IF multiple items have the same distance, we take the one with the last timestamp.- // This is because the last frame is more likely to have been decoded- if distance <= best_distance as i64 {+ if self.items[i].resolved_pts > time {+ continue;+ }++ let distance = time - self.items[i].resolved_pts;+ // LTE: If multiple items have the same PTS, take the most recently+ // decoded one.+ if distance <= best_distance {best_distance = distance;best_item = Some(i);}}+ if best_item.is_none() {+ for i in 0..self.items.len() {+ let distance = self.items[i].resolved_pts - time;+ if distance >= 0 && distance <= best_distance {+ best_distance = distance;+ best_item = Some(i);+ }+ }+ }+let has_pts_after = self.items.iter().any(|item| item.resolved_pts >= time);// If this happens, then `last_frame` has not worked correctly. Do not matchdiff --git a/packages/compositor/rust/opened_stream.rs b/packages/compositor/rust/opened_stream.rsindex f4289aa92d..afe0bcb310 100644--- a/packages/compositor/rust/opened_stream.rs+++ b/packages/compositor/rust/opened_stream.rs@@ -43,7 +43,9 @@ pub struct LastFrameInfo {}pub fn calc_position(time: f64, time_base: Rational) -> i64 {- (time * time_base.1 as f64 / time_base.0 as f64) as i64+ // Container timestamps use integer time-base ticks. Use the nearest tick so+ // floating-point imprecision does not put an exact frame time one tick early.+ (time * time_base.1 as f64 / time_base.0 as f64).round() as i64}pub fn get_time() -> u128 {diff --git a/packages/example/src/VideoTesting/index.tsx b/packages/example/src/VideoTesting/index.tsxindex 0869622245..cd3a81b70d 100644--- a/packages/example/src/VideoTesting/index.tsx+++ b/packages/example/src/VideoTesting/index.tsx@@ -10,7 +10,8 @@ import {export const VideoTesting: React.FC<{codec: 'mp4' | 'webm';type?: 'normal' | 'offthread' | 'codec';-}> = ({codec, type = 'normal'}) => {+ trimBefore?: number;+}> = ({codec, type = 'normal', trimBefore}) => {const {durationInFrames} = useVideoConfig();const videoMp4 = staticFile('framermp4withoutfileextension');const videoWebm = staticFile('framer.webm');@@ -27,7 +28,10 @@ export const VideoTesting: React.FC<{return (<div><Sequence durationInFrames={durationInFrames}>- <Comp src={codec === 'mp4' ? videoMp4 : videoWebm} />+ <Comp+ src={codec === 'mp4' ? videoMp4 : videoWebm}+ trimBefore={trimBefore}+ /></Sequence></div>);diff --git a/packages/it-tests/src/rendering/frame-accuracy.test.ts b/packages/it-tests/src/rendering/frame-accuracy.test.tsindex f3279bdf3b..3e360a550b 100644--- a/packages/it-tests/src/rendering/frame-accuracy.test.ts+++ b/packages/it-tests/src/rendering/frame-accuracy.test.ts@@ -1,5 +1,8 @@import {expect, test} from 'bun:test';-import {getMissedFramesforCodec} from './test-utils';+import {+ getMissedFramesforCodec,+ getMissedFramesWithFractionalTrimApplied,+} from './test-utils';test('should render correct frames from embedded videos - WebM onthread',@@ -36,3 +39,12 @@ test(},{retry: 3},);++test(+ 'should select the containing OffthreadVideo frame for a fractional trim',+ async () => {+ const missedFrames = await getMissedFramesWithFractionalTrimApplied();+ expect(missedFrames).toBe(0);+ },+ {retry: 3},+);diff --git a/packages/it-tests/src/rendering/test-utils.ts b/packages/it-tests/src/rendering/test-utils.tsindex 0ddf3af982..d2b1a809c5 100644--- a/packages/it-tests/src/rendering/test-utils.ts+++ b/packages/it-tests/src/rendering/test-utils.ts@@ -131,6 +131,53 @@ export const getMissedFramesWithTrimApplied = async (return missedFrames;};+export const getMissedFramesWithFractionalTrimApplied = async () => {+ const outputPath = await fs.promises.mkdtemp(+ path.join(os.tmpdir(), 'remotion-'),+ );+ const filename = path.join(outputPath, 'fractional-trim.png');++ await execa(+ 'node_modules/.bin/remotion',+ [+ 'still',+ './build',+ 'video-testing-mp4-offthread',+ filename,+ '--frame',+ '0',+ '--props',+ JSON.stringify({trimBefore: 20.75}),+ ],+ {+ cwd: path.join(process.cwd(), '..', 'example'),+ },+ );++ const img = await sharp(filename).raw().toBuffer();+ const actualColor = {+ red: img.readUInt8(0),+ green: img.readUInt8(1),+ blue: img.readUInt8(2),+ };+ const expectedColor = {+ red: selectColor('red', 20),+ green: selectColor('green', 20),+ blue: selectColor('blue', 20),+ };++ const missedFrames = missedFrameChecker(+ expectedColor,+ actualColor,+ 0,+ 20,+ filename,+ );+ RenderInternals.deleteDirectory(outputPath);++ return missedFrames;+};+function selectColor(color: string, frame: number) {return Math.floor((random(`${color}-${frame}`) * 255) % 255);}