Skip to content

🎥 Make videos programmatically with React

revert to working

Jonny Burger724a9ebe6440
diff --git a/packages/renderer/rust/frame_cache.rs b/packages/renderer/rust/frame_cache.rs
index b2c534c660..3bd6964913 100644
--- a/packages/renderer/rust/frame_cache.rs
+++ b/packages/renderer/rust/frame_cache.rs
@@ -4,8 +4,6 @@ extern crate ffmpeg_next as remotionffmpeg;
pub struct FrameCacheItem {
pub frame: Video,
pub time: i64,
- pub next_time: i64,
- pub has_next: bool,
}
pub struct FrameCache {
@@ -16,7 +14,6 @@ impl FrameCache {
pub fn new() -> Self {
Self { items: Vec::new() }
}
-
pub fn add_item(&mut self, item: FrameCacheItem) {
let exists = self.items.iter().any(|i| i.time == item.time);
if exists {
@@ -25,10 +22,4 @@ impl FrameCache {
self.items.push(item);
}
-
- pub fn get_item(&self, time: i64) -> Option<&FrameCacheItem> {
- self.items
- .iter()
- .find(|i| time >= i.time && time < i.next_time)
- }
}
diff --git a/packages/renderer/rust/opened_video.rs b/packages/renderer/rust/opened_video.rs
index 77fd845bba..b7fb9465b2 100644
--- a/packages/renderer/rust/opened_video.rs
+++ b/packages/renderer/rust/opened_video.rs
@@ -30,17 +30,6 @@ pub struct OpenedVideo {
impl OpenedVideo {
pub fn get_frame(&mut self, time: f64) -> Result<Vec<u8>, PossibleErrors> {
- let frame_cache_item = self.frame_cache.get_item(time as i64);
- if frame_cache_item.is_some() {
- return scale_and_make_bitmap(
- // TODO: Clone is avoidable
- frame_cache_item.unwrap().frame.clone(),
- self.format,
- self.width,
- self.height,
- );
- }
-
let position = (time as f64 * self.time_base.1 as f64 / self.time_base.0 as f64) as i64;
let min_position =
((time as f64 - 1.0) * self.time_base.1 as f64 / self.time_base.0 as f64) as i64;
@@ -63,15 +52,6 @@ impl OpenedVideo {
loop {
let (stream, packet) = match self.input.get_next_packet() {
None => {
- if !is_frame_empty(&mut frame) {
- self.frame_cache.add_item(FrameCacheItem {
- time: self.last_seek,
- frame: frame.clone(),
- has_next: false,
- next_time: 0,
- });
- }
-
break;
}
Some(packet) => packet,
@@ -80,17 +60,8 @@ impl OpenedVideo {
continue;
}
- let timestamp = packet.dts().unwrap();
-
- self.frame_cache.add_item(FrameCacheItem {
- time: self.last_seek,
- frame: frame.clone(),
- has_next: true,
- next_time: timestamp,
- });
-
// -1 because uf 67 and we want to process 66.66 -> rounding error
- if (timestamp - 1) > position {
+ if (packet.dts().unwrap() - 1) > position {
break;
}
loop {
@@ -106,16 +77,20 @@ impl OpenedVideo {
}
}
Ok(_) => {
- self.last_seek = timestamp;
+ self.frame_cache.add_item(FrameCacheItem {
+ time: packet.dts().unwrap(),
+ frame: frame.clone(),
+ });
+ _print_debug(&format!("Got frame {}", packet.dts().unwrap(),))?;
+ self.last_seek = packet.dts().unwrap();
break;
}
}
}
}
if is_frame_empty(&mut frame) {
- return Err(std::io::Error::new(ErrorKind::Other, "No frame was found"))?;
+ return Err(std::io::Error::new(ErrorKind::Other, "No frame found"))?;
}
-
scale_and_make_bitmap(frame, self.format, self.width, self.height)
}
}
@@ -137,33 +112,12 @@ pub fn scale_and_make_bitmap(
Flags::BILINEAR,
)?;
- let planes = frame.planes();
- let output_planes = Vec::new();
- for i in 0..planes {
- let mut buffer = vec![0_u8; (frame.width() * frame.height() * 3).try_into()?];
- output_planes.push(buffer.as_mut_ptr());
- }
-
- let output_stride = width * 3;
+ let mut scaled = Video::empty();
+ scaler.run(&frame, &mut scaled)?;
- unsafe {
- scaler.run(
- (*frame.as_ptr()).data.as_ptr(),
- (*frame.as_ptr()).linesize.as_ptr(),
- output_planes.as_ptr(),
- (width * 3).try_into().unwrap(),
- (height).try_into().unwrap(),
- )?;
- }
-
- let bmp = create_bmp_image_from_frame(
- width,
- height,
- (output_stride).try_into().unwrap(),
- &output_planes,
- );
+ let bmp = create_bmp_image_from_frame(&mut scaled);
_print_debug(&format!(
- "Scaling and making bitmap took {} microsseconds",
+ "Scaling and making bitmap took {}microsseconds",
start_time.elapsed().as_micros()
))?;
return Ok(bmp);
@@ -213,11 +167,14 @@ fn is_frame_empty(frame: &mut Video) -> bool {
return false;
}
-fn create_bmp_image_from_frame(width: u32, height: u32, stride: usize, data: &[u8]) -> Vec<u8> {
+fn create_bmp_image_from_frame(rgb_frame: &mut Video) -> Vec<u8> {
+ let width = rgb_frame.width() as u32;
+ let height = rgb_frame.height() as u32;
let row_size = (width * 3 + 3) & !3;
let row_padding = row_size - width * 3;
let image_size = row_size * height;
let header_size = 54;
+ let stride = rgb_frame.stride(0);
let mut bmp_data = Vec::with_capacity(header_size as usize + image_size as usize);
@@ -242,7 +199,7 @@ fn create_bmp_image_from_frame(width: u32, height: u32, stride: usize, data: &[u
for y in (0..height).rev() {
let row_start = (y as usize) * stride;
let row_end = row_start + (width * 3) as usize;
- bmp_data.extend_from_slice(&data[row_start..row_end]);
+ bmp_data.extend_from_slice(&rgb_frame.data(0)[row_start..row_end]);
for _ in 0..row_padding {
bmp_data.push(0);
}