From b226e011db3290ba4f62fb7b484ce662721df687 Mon Sep 17 00:00:00 2001 From: kieran Date: Mon, 18 Nov 2024 14:24:14 +0000 Subject: [PATCH] fix: assign encoder timebase from sample rate --- src/audio_fifo.rs | 2 +- src/encode.rs | 17 ++++++++++++----- src/mux.rs | 2 +- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/audio_fifo.rs b/src/audio_fifo.rs index eee00e1..b912cd4 100644 --- a/src/audio_fifo.rs +++ b/src/audio_fifo.rs @@ -81,7 +81,7 @@ mod tests { let mut enc = Encoder::new_with_name("libfdk_aac")? .with_sample_format(AVSampleFormat::AV_SAMPLE_FMT_S16) - .with_sample_rate(48_000) + .with_sample_rate(48_000)? .with_default_channel_layout(2) .open(None)?; diff --git a/src/encode.rs b/src/encode.rs index e08b708..3b58831 100644 --- a/src/encode.rs +++ b/src/encode.rs @@ -108,9 +108,13 @@ impl Encoder { } /// Set the encoder sample rate (audio) - pub unsafe fn with_sample_rate(self, fmt: i32) -> Self { - (*self.ctx).sample_rate = fmt; - self + pub unsafe fn with_sample_rate(self, rate: i32) -> Result { + if (*self.ctx).time_base.num != 1 || (*self.ctx).time_base.den != 1 { + bail!("Cannot assign sample_rate for a video encoder") + } + (*self.ctx).sample_rate = rate; + (*self.ctx).time_base = AVRational { num: 1, den: rate }; + Ok(self) } /// Set the encoder width in pixels @@ -138,11 +142,14 @@ impl Encoder { } /// Set the encoder framerate - pub unsafe fn with_framerate(self, fps: f32) -> Self { + pub unsafe fn with_framerate(self, fps: f32) -> Result { + if (*self.ctx).time_base.num != 1 || (*self.ctx).time_base.den != 1 { + bail!("Cannot assign framerate for an audio encoder") + } let q = av_d2q(fps as f64, 90_000); (*self.ctx).framerate = q; (*self.ctx).time_base = av_inv_q(q); - self + Ok(self) } /// Set the encoder pixel format diff --git a/src/mux.rs b/src/mux.rs index c5faf4c..68707fd 100644 --- a/src/mux.rs +++ b/src/mux.rs @@ -400,7 +400,7 @@ mod tests { .with_height((*frame).height) .with_pix_fmt(AV_PIX_FMT_YUV420P) .with_bitrate(1_000_000) - .with_framerate(30.0) + .with_framerate(30.0)? .with_profile(AV_PROFILE_H264_MAIN) .with_level(50) .open(None)?;