Skip to content

🎥 Make videos programmatically with React

frame cache

Jonny Burger3e5ca396a7f0
diff --git a/packages/renderer/rust/frame_cache.rs b/packages/renderer/rust/frame_cache.rs
index 9154128f13..4879a0fd25 100644
--- a/packages/renderer/rust/frame_cache.rs
+++ b/packages/renderer/rust/frame_cache.rs
@@ -1,24 +1,58 @@
+use crate::global_printer::_print_debug;
+
extern crate ffmpeg_next as remotionffmpeg;
pub struct FrameCacheItem {
pub bitmap: Vec<u8>,
pub time: i64,
+ pub next_time: i64,
+ pub has_next_time: bool,
}
pub struct FrameCache {
pub items: Vec<FrameCacheItem>,
+ pub last_frame: Option<FrameCacheItem>,
}
impl FrameCache {
pub fn new() -> Self {
- Self { items: Vec::new() }
+ Self {
+ items: Vec::new(),
+ last_frame: None,
+ }
}
pub fn add_item(&mut self, item: FrameCacheItem) {
- let exists = self.items.iter().any(|i| i.time == item.time);
+ // TODO: This is weird with shifted PTS
+ let exists = self
+ .items
+ .iter()
+ .any(|i| i.time == item.time && i.next_time == item.next_time);
if exists {
return;
}
+ if item.has_next_time == false {
+ self.last_frame = Some(item);
+ return;
+ }
+
self.items.push(item);
}
+
+ pub fn get_item(&self, time: i64) -> Option<Vec<u8>> {
+ if self.last_frame.is_some() {
+ let last_frame = self.last_frame.as_ref().unwrap();
+ if last_frame.time < time {
+ return Some(last_frame.bitmap.clone());
+ }
+ }
+
+ for i in 0..self.items.len() {
+ let item = &self.items[i];
+ if item.time <= time && item.next_time > time {
+ return Some(item.bitmap.clone());
+ }
+ }
+ return None;
+ }
}
diff --git a/packages/renderer/rust/opened_video.rs b/packages/renderer/rust/opened_video.rs
index 8a20f74c57..5c730db03c 100644
--- a/packages/renderer/rust/opened_video.rs
+++ b/packages/renderer/rust/opened_video.rs
@@ -1,6 +1,5 @@
use std::io::ErrorKind;
-use ffmpeg_next::Dictionary;
use ffmpeg_next::Rational;
use remotionffmpeg::{
format::Pixel,
@@ -13,6 +12,7 @@ extern crate ffmpeg_next as remotionffmpeg;
use crate::{
errors::PossibleErrors,
frame_cache::{FrameCache, FrameCacheItem},
+ global_printer::_print_debug,
};
pub struct OpenedVideo {
@@ -49,10 +49,6 @@ impl OpenedVideo {
let new_bitmap =
scale_and_make_bitmap(frame, self.format, self.width, self.height)?;
- self.frame_cache.add_item(FrameCacheItem {
- time: timestamp,
- bitmap: new_bitmap.clone(),
- });
self.last_seek = timestamp;
self.packets_opened += 1;
Ok(Some(new_bitmap))
@@ -65,6 +61,11 @@ impl OpenedVideo {
let min_position =
((time as f64 - 1.0) * self.time_base.1 as f64 / self.time_base.0 as f64) as i64;
+ let cache_item = self.frame_cache.get_item(position);
+ if cache_item.is_some() {
+ return Ok(cache_item.unwrap());
+ }
+
if position < self.last_seek || self.last_seek < min_position {
self.input.seek(
self.stream_index as i32,
@@ -86,6 +87,13 @@ impl OpenedVideo {
let result = self.receive_frame(self.last_seek);
match result {
Ok(Some(data)) => {
+ self.frame_cache.add_item(FrameCacheItem {
+ time: self.last_seek,
+ bitmap: bitmap.clone(),
+ next_time: 0,
+ has_next_time: false,
+ });
+
bitmap = data;
}
Ok(None) => {
@@ -105,6 +113,14 @@ impl OpenedVideo {
continue;
}
+ _print_debug(&format!(
+ "packet.pts {} {} {} {}",
+ packet.pts().unwrap(),
+ position,
+ self.time_base.0,
+ self.time_base.1
+ ));
+
// -1 because uf 67 and we want to process 66.66 -> rounding error
if (packet.pts().unwrap() - 1) > position {
break;
@@ -115,7 +131,14 @@ impl OpenedVideo {
let result = self.receive_frame(packet.dts().unwrap());
match result {
Ok(Some(data)) => {
+ self.frame_cache.add_item(FrameCacheItem {
+ time: self.last_seek,
+ bitmap: bitmap.clone(),
+ next_time: packet.dts().unwrap(),
+ has_next_time: true,
+ });
bitmap = data;
+
break;
}
Ok(None) => {