From 38180396b7a7cc3aa5a3e96851d4f116c68c98cb Mon Sep 17 00:00:00 2001 From: kieran Date: Thu, 14 Nov 2024 11:12:02 +0000 Subject: [PATCH] refactor: move muxer options to open --- src/mux.rs | 34 +++++++++++++++------------------- src/transcode.rs | 14 +++++++------- 2 files changed, 22 insertions(+), 26 deletions(-) diff --git a/src/mux.rs b/src/mux.rs index 62d593d..a8158bc 100644 --- a/src/mux.rs +++ b/src/mux.rs @@ -118,7 +118,6 @@ impl MuxerBuilder { &mut self, dst: Option<&str>, format: Option<&str>, - options: Option>, ) -> Result<()> { if !self.ctx.is_null() { bail!("context already open"); @@ -144,11 +143,6 @@ impl MuxerBuilder { if (*(*self.ctx).oformat).flags & AVFMT_GLOBALHEADER != 0 { (*self.ctx).flags |= AV_CODEC_FLAG_GLOBAL_HEADER as libc::c_int; } - - // Set options on ctx - if let Some(opts) = options { - set_opts((*self.ctx).priv_data, opts)?; - } Ok(()) } @@ -157,13 +151,12 @@ impl MuxerBuilder { mut self, dst: T, format: Option<&'a str>, - options: Option>, ) -> Result where T: Into<&'a str>, { let path_str = dst.into(); - self.init_ctx(Some(path_str), format, options)?; + self.init_ctx(Some(path_str), format)?; self.output = MuxerOutput::Url(path_str.to_string()); Ok(self) } @@ -174,12 +167,11 @@ impl MuxerBuilder { mut self, writer: W, format: Option<&str>, - options: Option>, ) -> Result where W: WriteSeek + 'static, { - self.init_ctx(None, format, options)?; + self.init_ctx(None, format)?; self.output = MuxerOutput::WriterSeeker(Some(slimbox_unsize!(writer))); Ok(self) } @@ -189,12 +181,11 @@ impl MuxerBuilder { mut self, writer: W, format: Option<&str>, - options: Option>, ) -> Result where W: Write + 'static, { - self.init_ctx(None, format, options)?; + self.init_ctx(None, format)?; self.output = MuxerOutput::Writer(Some(slimbox_unsize!(writer))); Ok(self) } @@ -290,7 +281,12 @@ impl Muxer { } /// Open the output to start sending packets - pub unsafe fn open(&mut self) -> Result<()> { + pub unsafe fn open(&mut self, options: Option>) -> Result<()> { + // Set options on ctx + if let Some(opts) = options { + set_opts((*self.ctx).priv_data, opts)?; + } + if (*(*self.ctx).oformat).flags & AVFMT_NOFILE == 0 { (*self.ctx).pb = (&mut self.output).try_into()?; // if pb is still null, open with ctx.url @@ -410,10 +406,10 @@ mod tests { let (frame, encoder) = setup_encoder()?; let mut muxer = Muxer::builder() - .with_output_path(path.to_str().unwrap(), None, None)? + .with_output_path(path.to_str().unwrap(), None)? .with_stream_encoder(&encoder)? .build()?; - muxer.open()?; + muxer.open(None)?; write_frames(muxer, encoder, frame)?; } Ok(()) @@ -428,10 +424,10 @@ mod tests { let fout = std::fs::File::create(path)?; let mut muxer = Muxer::builder() - .with_output_write_seek(fout, Some("mp4"), None)? + .with_output_write_seek(fout, Some("mp4"))? .with_stream_encoder(&encoder)? .build()?; - muxer.open()?; + muxer.open(None)?; write_frames(muxer, encoder, frame)?; } Ok(()) @@ -446,10 +442,10 @@ mod tests { let fout = std::fs::File::create(path)?; let mut muxer = Muxer::builder() - .with_output_write(fout, Some("mpegts"), None)? + .with_output_write(fout, Some("mpegts"))? .with_stream_encoder(&encoder)? .build()?; - muxer.open()?; + muxer.open(None)?; write_frames(muxer, encoder, frame)?; } Ok(()) diff --git a/src/transcode.rs b/src/transcode.rs index 1ce5a2e..89f7517 100644 --- a/src/transcode.rs +++ b/src/transcode.rs @@ -21,7 +21,7 @@ pub struct Transcoder { impl Transcoder { pub unsafe fn new(input: &str, output: &str) -> Result { let muxer = Muxer::builder() - .with_output_path(output, None, None)? + .with_output_path(output, None)? .build()?; Ok(Self { @@ -54,8 +54,8 @@ impl Transcoder { // Setup scaler if the size/format is different from what the codec expects if in_stream.stream_type == StreamType::Video && (in_stream.width != (*out_ctx).width as usize - || in_stream.height != (*out_ctx).height as usize - || in_stream.format != (*out_ctx).pix_fmt as isize) + || in_stream.height != (*out_ctx).height as usize + || in_stream.format != (*out_ctx).pix_fmt as isize) { self.scalers.insert(src_index, Scaler::new()); } @@ -63,7 +63,7 @@ impl Transcoder { // Setup resampler for audio if in_stream.stream_type == StreamType::Audio && (in_stream.format != (*out_ctx).sample_fmt as isize - || in_stream.sample_rate != (*out_ctx).sample_rate as usize) + || in_stream.sample_rate != (*out_ctx).sample_rate as usize) { let r = Resample::new( (*out_ctx).sample_fmt, @@ -153,8 +153,8 @@ impl Transcoder { } /// Run the transcoder - pub unsafe fn run(mut self) -> Result<()> { - self.muxer.open()?; + pub unsafe fn run(mut self, mux_options: Option>) -> Result<()> { + self.muxer.open(mux_options)?; while !self.next()? { // nothing here } @@ -178,7 +178,7 @@ mod tests { for c in info.streams { transcoder.copy_stream(c)?; } - transcoder.run()?; + transcoder.run(None)?; Ok(()) }