🎥 Make videos programmatically with React
move open_stream() to opened_stream
Jonny Burger2796a78fce7f
diff --git a/packages/compositor-darwin-arm64/compositor b/packages/compositor-darwin-arm64/compositorindex f1f98f1241..6ac10e3a5b 100755Binary files a/packages/compositor-darwin-arm64/compositor and b/packages/compositor-darwin-arm64/compositor differdiff --git a/packages/renderer/rust/opened_stream.rs b/packages/renderer/rust/opened_stream.rsindex 8c39613a43..f53bee65e6 100644--- a/packages/renderer/rust/opened_stream.rs+++ b/packages/renderer/rust/opened_stream.rs@@ -5,7 +5,7 @@ use std::{};use ffmpeg_next::Rational;-use remotionffmpeg::{format::Pixel, frame::Video, media::Type, StreamMut};+use remotionffmpeg::{codec::Id, format::Pixel, frame::Video, media::Type, Dictionary, StreamMut};extern crate ffmpeg_next as remotionffmpeg;use std::time::UNIX_EPOCH;@@ -283,3 +283,104 @@ pub fn get_display_aspect_ratio(mut_stream: &StreamMut) -> Rational {return Rational::new(asp.numerator(), asp.denominator());}}++pub fn open_stream(+ src: &str,+ transparent: bool,+) -> Result<(OpenedStream, Rational, Rational), ErrorWithBacktrace> {+ let mut dictionary = Dictionary::new();+ dictionary.set("fflags", "+genpts");+ let mut input = remotionffmpeg::format::input_with_dictionary(&src, dictionary)?;++ // TODO: Don't open stream and stream_mut, might need to adapt rust-ffmpeg for it+ let stream = match input+ .streams()+ .find(|s| s.parameters().medium() == Type::Video)+ {+ Some(stream) => stream,+ None => {+ return Err(ErrorWithBacktrace::from(+ "No video stream found in input file",+ ));+ }+ };++ let stream_index = stream.index();++ drop(stream);++ let mut_stream = input+ .stream_mut(stream_index)+ .ok_or(ErrorWithBacktrace::from(+ "No video stream found in input file",+ ))?;+ let duration_or_zero = mut_stream.duration().max(0);++ let time_base = mut_stream.time_base();+ let parameters = mut_stream.parameters();++ let mut parameters_cloned = parameters.clone();+ let is_vp8_or_vp9_and_transparent = match transparent {+ true => unsafe {+ let codec_id = (*(*(mut_stream).as_ptr()).codecpar).codec_id;+ let is_vp8 = codec_id == remotionffmpeg::codec::id::get_av_codec_id(Id::VP8);+ let is_vp9 = codec_id == remotionffmpeg::codec::id::get_av_codec_id(Id::VP9);++ if is_vp8 || is_vp9 {+ (*parameters_cloned.as_mut_ptr()).format =+ remotionffmpeg::util::format::pixel::to_av_pixel_format(Pixel::YUVA420P) as i32;+ }++ if is_vp8 {+ Some("vp8")+ } else if is_vp9 {+ Some("vp9")+ } else {+ None+ }+ },+ false => None,+ };++ let video = remotionffmpeg::codec::context::Context::from_parameters(parameters_cloned)?;++ let decoder = match is_vp8_or_vp9_and_transparent {+ Some("vp8") => video.decoder().video_with_codec("libvpx")?,+ Some("vp9") => video.decoder().video_with_codec("libvpx-vp9")?,+ Some(_) => unreachable!(),+ None => video.decoder().video()?,+ };++ let format = decoder.format();++ let original_width = decoder.width();+ let original_height = decoder.height();+ let fps = mut_stream.avg_frame_rate();++ let aspect_ratio = get_display_aspect_ratio(&mut_stream);++ let (scaled_width, scaled_height) = calculate_display_video_size(+ aspect_ratio.0,+ aspect_ratio.1,+ original_width,+ original_height,+ );++ let opened_stream = OpenedStream {+ stream_index,+ original_height,+ original_width,+ scaled_height,+ scaled_width,+ format,+ video: decoder,+ src: src.to_string(),+ input,+ last_position: 0,+ duration_or_zero,+ reached_eof: false,+ transparent,+ };++ Ok((opened_stream, fps, time_base))+}diff --git a/packages/renderer/rust/opened_video.rs b/packages/renderer/rust/opened_video.rsindex 8978fa5aaf..8d778ac64c 100644--- a/packages/renderer/rust/opened_video.rs+++ b/packages/renderer/rust/opened_video.rs@@ -1,10 +1,10 @@-use remotionffmpeg::{codec::Id, format::Pixel, media::Type, Dictionary, Rational};+use remotionffmpeg::Rational;use std::sync::{Arc, Mutex};use crate::{errors::ErrorWithBacktrace,frame_cache::FrameCache,- opened_stream::{calculate_display_video_size, get_display_aspect_ratio, OpenedStream},+ opened_stream::{self, OpenedStream},};extern crate ffmpeg_next as remotionffmpeg;@@ -18,7 +18,7 @@ pub struct OpenedVideo {}pub fn open_video(src: &str, transparent: bool) -> Result<OpenedVideo, ErrorWithBacktrace> {- let (opened_stream, fps, time_base) = open_stream(src, transparent)?;+ let (opened_stream, fps, time_base) = opened_stream::open_stream(src, transparent)?;let opened_video = OpenedVideo {opened_streams: vec![(Arc::new(Mutex::new(opened_stream)))],@@ -32,107 +32,6 @@ pub fn open_video(src: &str, transparent: bool) -> Result<OpenedVideo, ErrorWithOk(opened_video)}-fn open_stream(- src: &str,- transparent: bool,-) -> Result<(OpenedStream, Rational, Rational), ErrorWithBacktrace> {- let mut dictionary = Dictionary::new();- dictionary.set("fflags", "+genpts");- let mut input = remotionffmpeg::format::input_with_dictionary(&src, dictionary)?;-- // TODO: Don't open stream and stream_mut, might need to adapt rust-ffmpeg for it- let stream = match input- .streams()- .find(|s| s.parameters().medium() == Type::Video)- {- Some(stream) => stream,- None => {- return Err(ErrorWithBacktrace::from(- "No video stream found in input file",- ));- }- };-- let stream_index = stream.index();-- drop(stream);-- let mut_stream = input- .stream_mut(stream_index)- .ok_or(ErrorWithBacktrace::from(- "No video stream found in input file",- ))?;- let duration_or_zero = mut_stream.duration().max(0);-- let time_base = mut_stream.time_base();- let parameters = mut_stream.parameters();-- let mut parameters_cloned = parameters.clone();- let is_vp8_or_vp9_and_transparent = match transparent {- true => unsafe {- let codec_id = (*(*(mut_stream).as_ptr()).codecpar).codec_id;- let is_vp8 = codec_id == remotionffmpeg::codec::id::get_av_codec_id(Id::VP8);- let is_vp9 = codec_id == remotionffmpeg::codec::id::get_av_codec_id(Id::VP9);-- if is_vp8 || is_vp9 {- (*parameters_cloned.as_mut_ptr()).format =- remotionffmpeg::util::format::pixel::to_av_pixel_format(Pixel::YUVA420P) as i32;- }-- if is_vp8 {- Some("vp8")- } else if is_vp9 {- Some("vp9")- } else {- None- }- },- false => None,- };-- let video = remotionffmpeg::codec::context::Context::from_parameters(parameters_cloned)?;-- let decoder = match is_vp8_or_vp9_and_transparent {- Some("vp8") => video.decoder().video_with_codec("libvpx")?,- Some("vp9") => video.decoder().video_with_codec("libvpx-vp9")?,- Some(_) => unreachable!(),- None => video.decoder().video()?,- };-- let format = decoder.format();-- let original_width = decoder.width();- let original_height = decoder.height();- let fps = mut_stream.avg_frame_rate();-- let aspect_ratio = get_display_aspect_ratio(&mut_stream);-- let (scaled_width, scaled_height) = calculate_display_video_size(- aspect_ratio.0,- aspect_ratio.1,- original_width,- original_height,- );-- let opened_stream = OpenedStream {- stream_index,- original_height,- original_width,- scaled_height,- scaled_width,- format,- video: decoder,- src: src.to_string(),- input,- last_position: 0,- duration_or_zero,- reached_eof: false,- transparent,- };-- Ok((opened_stream, fps, time_base))-}-impl OpenedVideo {pub fn close(&mut self) -> Result<(), ErrorWithBacktrace> {self.opened_streams = vec![];@@ -140,7 +39,7 @@ impl OpenedVideo {Ok(())}pub fn open_new_stream(&mut self, transparent: bool) -> Result<usize, ErrorWithBacktrace> {- let (opened_stream, _, _) = open_stream(&self.src, transparent)?;+ let (opened_stream, _, _) = opened_stream::open_stream(&self.src, transparent)?;let arc_mutex = Arc::new(Mutex::new(opened_stream));self.opened_streams.push(arc_mutex);return Ok(self.opened_streams.len() - 1);