🎥 Make videos programmatically with React
this looks good
Jonny Burger3f453c1a0da2
diff --git a/packages/compositor/rust/commands/mod.rs b/packages/compositor/rust/commands/mod.rsindex 9325615941..1c47935710 100644--- a/packages/compositor/rust/commands/mod.rs+++ b/packages/compositor/rust/commands/mod.rs@@ -5,7 +5,10 @@ use crate::payloads::payloads::CliInputCommandPayload;use crate::{ffmpeg, get_silent_parts, max_cache_size};use std::io::ErrorKind;-pub fn execute_command(opts: CliInputCommandPayload) -> Result<Vec<u8>, ErrorWithBacktrace> {+pub fn execute_command(+ opts: CliInputCommandPayload,+ thread_index: usize,+) -> Result<Vec<u8>, ErrorWithBacktrace> {let current_maximum_cache_size = max_cache_size::get_instance().lock().unwrap().get_value();if is_about_to_run_out_of_memory() && current_maximum_cache_size.is_some() {@@ -25,6 +28,7 @@ pub fn execute_command(opts: CliInputCommandPayload) -> Result<Vec<u8>, ErrorWitcommand.transparent,command.tone_mapped,max_cache_size::get_instance().lock().unwrap().get_value(),+ thread_index,)?;Ok(res)}@@ -39,6 +43,7 @@ pub fn execute_command(opts: CliInputCommandPayload) -> Result<Vec<u8>, ErrorWithi.unwrap();Ok(vec![])}+ CliInputCommandPayload::Eof(_) => Ok(vec![]),CliInputCommandPayload::FreeUpMemory(payload) => {ffmpeg::keep_only_latest_frames_and_close_videos(payload.remaining_bytes)?;Ok(vec![])diff --git a/packages/compositor/rust/ffmpeg.rs b/packages/compositor/rust/ffmpeg.rsindex 9e070c6e3d..f64278a64d 100644--- a/packages/compositor/rust/ffmpeg.rs+++ b/packages/compositor/rust/ffmpeg.rs@@ -62,11 +62,11 @@ pub fn extract_frame(transparent: bool,tone_mapped: bool,maximum_frame_cache_size_in_bytes: Option<u128>,+ thread_index: usize,) -> Result<Vec<u8>, ErrorWithBacktrace> {let manager = OpenedVideoManager::get_instance();let video_locked = manager.get_video(&src, &original_src, transparent)?;let mut vid = video_locked.lock()?;-// The requested position in the video.let position = calc_position(time, vid.time_base);@@ -114,8 +114,8 @@ pub fn extract_frame(// Seeking too far back in a stream is not efficient, rather open a new stream// 15 seconds was chosen arbitrarily- let max_stream_position = calc_position(time + 15.0, vid.time_base);- let min_stream_position = calc_position(time - 15.0, vid.time_base);+ let max_stream_position = calc_position(time + 5.0, vid.time_base);+ let min_stream_position = calc_position(time - 5.0, vid.time_base);for i in 0..open_stream_count {let stream = vid.opened_streams[i].lock()?;if stream.reached_eof {@@ -136,7 +136,7 @@ pub fn extract_frame(let stream_index = match suitable_open_stream {Some(index) => Ok(index),- None => vid.open_new_stream(transparent),+ None => vid.open_new_stream(transparent, thread_index),};let opened_stream = match vid.opened_streams.get(stream_index?) {diff --git a/packages/compositor/rust/main.rs b/packages/compositor/rust/main.rsindex 9ba3ab19fe..9d4d71305a 100644--- a/packages/compositor/rust/main.rs+++ b/packages/compositor/rust/main.rs@@ -21,9 +21,11 @@ use commands::execute_command;use errors::{error_to_json, ErrorWithBacktrace};use global_printer::{_print_verbose, set_verbose_logging};use memory::get_ideal_maximum_frame_cache_size;-use std::{env, thread};+use std::env;+use std::sync::mpsc;+use std::thread;-use payloads::payloads::{parse_cli, CliInputCommand, CliInputCommandPayload};+use payloads::payloads::{parse_cli, CliInputCommand, CliInputCommandPayload, Eof};extern crate png;@@ -57,7 +59,7 @@ fn mainfn() -> Result<(), ErrorWithBacktrace> {start_long_running_process(payload.concurrency, max_video_cache_size)?;}_ => {- let data = execute_command(opts.payload)?;+ let data = execute_command(opts.payload, 0)?;global_printer::synchronized_write_buf(0, &opts.nonce, &data)?;}}@@ -75,9 +77,37 @@ fn start_long_running_process(threads: usize,maximum_frame_cache_size_in_bytes: u128,) -> Result<(), ErrorWithBacktrace> {- let pool = rayon_core::ThreadPoolBuilder::new()- .num_threads(threads)- .build()?;+ let mut thread_handles = vec![];+ let mut send_handles = vec![];+ for thread_index in 0..threads {+ let (tx, rx) = mpsc::channel::<CliInputCommand>();++ let handle = thread::spawn(move || {+ while let Ok(message) = rx.recv() {+ match message.payload {+ CliInputCommandPayload::Eof(_) => {+ break;+ }+ _ => (),+ }+ match execute_command(message.payload, thread_index) {+ Ok(res) => {+ global_printer::synchronized_write_buf(0, &message.nonce, &res).unwrap()+ }+ Err(err) => global_printer::synchronized_write_buf(+ 1,+ &message.nonce,+ &error_to_json(err).unwrap().as_bytes(),+ )+ .unwrap(),+ };+ }++ // Thread work here+ });+ send_handles.push(tx);+ thread_handles.push(handle);+ }max_cache_size::get_instance().lock()@@ -95,39 +125,21 @@ fn start_long_running_process(input = matched.trim().to_string();if input == "EOF" {+ for handle in send_handles {+ handle.send(CliInputCommand {+ payload: CliInputCommandPayload::Eof(Eof {}),+ nonce: "".to_string(),+ });+ }break;}let opts: CliInputCommand = parse_cli(&input)?;+ // TODO: Find best thread to send to+ send_handles[0].send(opts).unwrap();+ }- if threads > 1 {- pool.install(move || {- let handle = thread::spawn(move || {- match execute_command(opts.payload) {- Ok(res) => {- global_printer::synchronized_write_buf(0, &opts.nonce, &res).unwrap()- }- Err(err) => global_printer::synchronized_write_buf(- 1,- &opts.nonce,- &error_to_json(err).unwrap().as_bytes(),- )- .unwrap(),- };- });-- handle.join().unwrap();- });- } else {- match execute_command(opts.payload) {- Ok(res) => global_printer::synchronized_write_buf(0, &opts.nonce, &res).unwrap(),- Err(err) => global_printer::synchronized_write_buf(- 1,- &opts.nonce,- &error_to_json(err).unwrap().as_bytes(),- )- .unwrap(),- };- }+ for handle in thread_handles {+ handle.join().unwrap();}Ok(())diff --git a/packages/compositor/rust/opened_video.rs b/packages/compositor/rust/opened_video.rsindex 389fc5f854..cfa40c6c78 100644--- a/packages/compositor/rust/opened_video.rs+++ b/packages/compositor/rust/opened_video.rs@@ -47,9 +47,14 @@ impl OpenedVideo {Ok(())}- pub fn open_new_stream(&mut self, transparent: bool) -> Result<usize, ErrorWithBacktrace> {+ pub fn open_new_stream(+ &mut self,+ transparent: bool,+ thread_index: usize,+ ) -> Result<usize, ErrorWithBacktrace> {let (opened_stream, _, _) =opened_stream::open_stream(&self.src, &self.original_src, transparent)?;+ _print_verbose(&format!("Opening new stream on thread {}", thread_index))?;let arc_mutex = Arc::new(Mutex::new(opened_stream));self.opened_streams.push(arc_mutex);return Ok(self.opened_streams.len() - 1);diff --git a/packages/compositor/rust/payloads.rs b/packages/compositor/rust/payloads.rsindex c659c5147f..465b6eed52 100644--- a/packages/compositor/rust/payloads.rs+++ b/packages/compositor/rust/payloads.rs@@ -244,6 +244,9 @@ pub mod payloads {pub output_path: String,}+ #[derive(Serialize, Deserialize, Debug)]+ pub struct Eof {}+#[derive(Serialize, Deserialize, Debug)]#[serde(tag = "type", content = "params")]pub enum CliInputCommandPayload {@@ -257,6 +260,7 @@ pub mod payloads {GetVideoMetadata(GetVideoMetadata),GetSilences(GetSilences),ExtractAudio(ExtractAudio),+ Eof(Eof),}#[derive(Serialize, Deserialize, Debug)]