refactor: move muxer options to open

This commit is contained in:
kieran 2024-11-14 11:12:02 +00:00
parent aae40f0fba
commit 38180396b7
No known key found for this signature in database
GPG Key ID: DE71CEB3925BE941
2 changed files with 22 additions and 26 deletions

View File

@ -118,7 +118,6 @@ impl MuxerBuilder {
&mut self, &mut self,
dst: Option<&str>, dst: Option<&str>,
format: Option<&str>, format: Option<&str>,
options: Option<HashMap<String, String>>,
) -> Result<()> { ) -> Result<()> {
if !self.ctx.is_null() { if !self.ctx.is_null() {
bail!("context already open"); bail!("context already open");
@ -144,11 +143,6 @@ impl MuxerBuilder {
if (*(*self.ctx).oformat).flags & AVFMT_GLOBALHEADER != 0 { if (*(*self.ctx).oformat).flags & AVFMT_GLOBALHEADER != 0 {
(*self.ctx).flags |= AV_CODEC_FLAG_GLOBAL_HEADER as libc::c_int; (*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(()) Ok(())
} }
@ -157,13 +151,12 @@ impl MuxerBuilder {
mut self, mut self,
dst: T, dst: T,
format: Option<&'a str>, format: Option<&'a str>,
options: Option<HashMap<String, String>>,
) -> Result<Self> ) -> Result<Self>
where where
T: Into<&'a str>, T: Into<&'a str>,
{ {
let path_str = dst.into(); 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()); self.output = MuxerOutput::Url(path_str.to_string());
Ok(self) Ok(self)
} }
@ -174,12 +167,11 @@ impl MuxerBuilder {
mut self, mut self,
writer: W, writer: W,
format: Option<&str>, format: Option<&str>,
options: Option<HashMap<String, String>>,
) -> Result<Self> ) -> Result<Self>
where where
W: WriteSeek + 'static, W: WriteSeek + 'static,
{ {
self.init_ctx(None, format, options)?; self.init_ctx(None, format)?;
self.output = MuxerOutput::WriterSeeker(Some(slimbox_unsize!(writer))); self.output = MuxerOutput::WriterSeeker(Some(slimbox_unsize!(writer)));
Ok(self) Ok(self)
} }
@ -189,12 +181,11 @@ impl MuxerBuilder {
mut self, mut self,
writer: W, writer: W,
format: Option<&str>, format: Option<&str>,
options: Option<HashMap<String, String>>,
) -> Result<Self> ) -> Result<Self>
where where
W: Write + 'static, W: Write + 'static,
{ {
self.init_ctx(None, format, options)?; self.init_ctx(None, format)?;
self.output = MuxerOutput::Writer(Some(slimbox_unsize!(writer))); self.output = MuxerOutput::Writer(Some(slimbox_unsize!(writer)));
Ok(self) Ok(self)
} }
@ -290,7 +281,12 @@ impl Muxer {
} }
/// Open the output to start sending packets /// Open the output to start sending packets
pub unsafe fn open(&mut self) -> Result<()> { pub unsafe fn open(&mut self, options: Option<HashMap<String, String>>) -> 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 { if (*(*self.ctx).oformat).flags & AVFMT_NOFILE == 0 {
(*self.ctx).pb = (&mut self.output).try_into()?; (*self.ctx).pb = (&mut self.output).try_into()?;
// if pb is still null, open with ctx.url // if pb is still null, open with ctx.url
@ -410,10 +406,10 @@ mod tests {
let (frame, encoder) = setup_encoder()?; let (frame, encoder) = setup_encoder()?;
let mut muxer = Muxer::builder() 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)? .with_stream_encoder(&encoder)?
.build()?; .build()?;
muxer.open()?; muxer.open(None)?;
write_frames(muxer, encoder, frame)?; write_frames(muxer, encoder, frame)?;
} }
Ok(()) Ok(())
@ -428,10 +424,10 @@ mod tests {
let fout = std::fs::File::create(path)?; let fout = std::fs::File::create(path)?;
let mut muxer = Muxer::builder() let mut muxer = Muxer::builder()
.with_output_write_seek(fout, Some("mp4"), None)? .with_output_write_seek(fout, Some("mp4"))?
.with_stream_encoder(&encoder)? .with_stream_encoder(&encoder)?
.build()?; .build()?;
muxer.open()?; muxer.open(None)?;
write_frames(muxer, encoder, frame)?; write_frames(muxer, encoder, frame)?;
} }
Ok(()) Ok(())
@ -446,10 +442,10 @@ mod tests {
let fout = std::fs::File::create(path)?; let fout = std::fs::File::create(path)?;
let mut muxer = Muxer::builder() let mut muxer = Muxer::builder()
.with_output_write(fout, Some("mpegts"), None)? .with_output_write(fout, Some("mpegts"))?
.with_stream_encoder(&encoder)? .with_stream_encoder(&encoder)?
.build()?; .build()?;
muxer.open()?; muxer.open(None)?;
write_frames(muxer, encoder, frame)?; write_frames(muxer, encoder, frame)?;
} }
Ok(()) Ok(())

View File

@ -21,7 +21,7 @@ pub struct Transcoder {
impl Transcoder { impl Transcoder {
pub unsafe fn new(input: &str, output: &str) -> Result<Self> { pub unsafe fn new(input: &str, output: &str) -> Result<Self> {
let muxer = Muxer::builder() let muxer = Muxer::builder()
.with_output_path(output, None, None)? .with_output_path(output, None)?
.build()?; .build()?;
Ok(Self { Ok(Self {
@ -54,8 +54,8 @@ impl Transcoder {
// Setup scaler if the size/format is different from what the codec expects // Setup scaler if the size/format is different from what the codec expects
if in_stream.stream_type == StreamType::Video if in_stream.stream_type == StreamType::Video
&& (in_stream.width != (*out_ctx).width as usize && (in_stream.width != (*out_ctx).width as usize
|| in_stream.height != (*out_ctx).height as usize || in_stream.height != (*out_ctx).height as usize
|| in_stream.format != (*out_ctx).pix_fmt as isize) || in_stream.format != (*out_ctx).pix_fmt as isize)
{ {
self.scalers.insert(src_index, Scaler::new()); self.scalers.insert(src_index, Scaler::new());
} }
@ -63,7 +63,7 @@ impl Transcoder {
// Setup resampler for audio // Setup resampler for audio
if in_stream.stream_type == StreamType::Audio if in_stream.stream_type == StreamType::Audio
&& (in_stream.format != (*out_ctx).sample_fmt as isize && (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( let r = Resample::new(
(*out_ctx).sample_fmt, (*out_ctx).sample_fmt,
@ -153,8 +153,8 @@ impl Transcoder {
} }
/// Run the transcoder /// Run the transcoder
pub unsafe fn run(mut self) -> Result<()> { pub unsafe fn run(mut self, mux_options: Option<HashMap<String, String>>) -> Result<()> {
self.muxer.open()?; self.muxer.open(mux_options)?;
while !self.next()? { while !self.next()? {
// nothing here // nothing here
} }
@ -178,7 +178,7 @@ mod tests {
for c in info.streams { for c in info.streams {
transcoder.copy_stream(c)?; transcoder.copy_stream(c)?;
} }
transcoder.run()?; transcoder.run(None)?;
Ok(()) Ok(())
} }