🎥 Make videos programmatically with React
mutex
Jonny Burgerfea3e5f9c64a
diff --git a/packages/renderer/rust/commands/mod.rs b/packages/renderer/rust/commands/mod.rsindex 9b2b750f1d..3b2f83afe0 100644--- a/packages/renderer/rust/commands/mod.rs+++ b/packages/renderer/rust/commands/mod.rs@@ -10,8 +10,7 @@ pub fn execute_command(opts: CliInputCommand) -> Result<(), PossibleErrors> {match opts {CliInputCommand::ExtractFrame(command) => {let start = std::time::Instant::now();- let _result = ffmpeg::extract_frame(command.input, command.time)?;- global_printer::synchronized_write_buf(&command.nonce, &_result)?;+ _print_debug("msg");_print_debug(&format!("Extracted frame in {:?}", start.elapsed()))?;}CliInputCommand::StartLongRunningProcess(_command) => {diff --git a/packages/renderer/rust/ffmpeg.rs b/packages/renderer/rust/ffmpeg.rsindex 1ab907de1b..f69a1346cb 100644--- a/packages/renderer/rust/ffmpeg.rs+++ b/packages/renderer/rust/ffmpeg.rs@@ -1,3 +1,4 @@+use lazy_static::lazy_static;use remotionffmepg::media::Type;use remotionffmepg::Rational;@@ -7,21 +8,78 @@ use remotionffmepg::format::Pixel;use remotionffmepg::frame::Video;use remotionffmepg::software::scaling::Context;use remotionffmepg::software::scaling::Flags;+use std::collections::HashMap;use std::io::ErrorKind;+use std::sync::Arc;+use std::sync::Mutex;+use std::sync::RwLock;use std::time::Instant;extern crate ffmpeg_next as remotionffmepg;pub struct OpenedVideo {- pub input: remotionffmepg::format::context::Input,pub stream_index: usize,pub time_base: Rational,pub width: u32,pub height: u32,pub format: Pixel,pub video: remotionffmepg::codec::decoder::Video,+ pub src: String,+}+pub struct OpenedInput {+ pub input: remotionffmepg::format::context::Input,+}++pub struct OpenedVideoManager {+ videos: RwLock<HashMap<String, Arc<Mutex<OpenedVideo>>>>,+ inputs: RwLock<HashMap<String, Arc<Mutex<OpenedInput>>>>,+}++impl OpenedVideoManager {+ pub fn get_instance() -> &'static OpenedVideoManager {+ lazy_static! {+ static ref INSTANCE: OpenedVideoManager = OpenedVideoManager {+ videos: RwLock::new(HashMap::new()),+ inputs: RwLock::new(HashMap::new())+ };+ }+ &INSTANCE+ }++ pub fn get_video(+ &self,+ src: &str,+ ) -> Result<(Arc<Mutex<OpenedVideo>>, Arc<Mutex<OpenedInput>>), PossibleErrors> {+ let videos_read = self.videos.read().unwrap();+ let mut videos_write = self.videos.write().unwrap();++ let inputs_read = self.inputs.read().unwrap();+ let mut inputs_write = self.inputs.write().unwrap();++ if videos_read.contains_key(src) {+ return Ok((+ videos_read.get(src).unwrap().clone(),+ inputs_read.get(src).unwrap().clone(),+ ));+ } else {+ let (video, input) = open_video(src)?;+ inputs_write.insert(src.to_string(), Arc::new(Mutex::new(input)));+ videos_write.insert(src.to_string(), Arc::new(Mutex::new(video)));+ return Ok((+ videos_read.get(src).unwrap().clone(),+ inputs_read.get(src).unwrap().clone(),+ ));+ }+ }++ pub fn remove_video(&self, src: String) {+ let mut videos = self.videos.write().unwrap();+ videos.remove(&src);+ let mut inputs = self.inputs.write().unwrap();+ inputs.remove(&src);+ }}-pub fn open_video(src: String) -> Result<OpenedVideo, PossibleErrors> {+pub fn open_video(src: &str) -> Result<(OpenedVideo, OpenedInput), PossibleErrors> {remotionffmepg::init()?;let mut input = remotionffmepg::format::input(&src)?;@@ -31,6 +89,8 @@ pub fn open_video(src: String) -> Result<OpenedVideo, PossibleErrors> {.unwrap().index();+ _print_debug("msg: \"Opening video\"")?;+let mut_stream = input.stream_mut(stream_index).unwrap();let time_base = mut_stream.time_base();let parameters = mut_stream.parameters();@@ -43,20 +103,27 @@ pub fn open_video(src: String) -> Result<OpenedVideo, PossibleErrors> {let height = video.height();let opened_video = OpenedVideo {- input,stream_index,time_base,width,height,format,video,+ src: src.to_string(),};- Ok(opened_video)+ let opened_input = OpenedInput { input };++ Ok((opened_video, opened_input))}pub fn extract_frame(src: String, time: f64) -> Result<Vec<u8>, PossibleErrors> {- let mut vid = open_video(src)?;+ _print_debug("extract");+ let manager = OpenedVideoManager::get_instance();++ let video_locked = manager.get_video(&src)?;+ let mut vid = video_locked.0.lock().unwrap();+ let mut input = video_locked.1.lock().unwrap();let mut scaler = Context::get(vid.format,@@ -70,19 +137,19 @@ pub fn extract_frame(src: String, time: f64) -> Result<Vec<u8>, PossibleErrors>let position = (time as f64 * vid.time_base.1 as f64 / vid.time_base.0 as f64) as i64;- vid.input.seek(- vid.stream_index as i32,- position - 1000,- position,- position,- 0,- )?;+ let stream_index = vid.stream_index.clone();++ input+ .input+ .seek(stream_index as i32, position - 1000, position, position, 0)?;_print_debug(&format!("position {}", position))?;let mut frame = Video::empty();- for (stream, packet) in vid.input.packets() {+ let packets = input.input.packets();++ for (stream, packet) in packets {if stream.parameters().medium() != Type::Video {continue;}