fix: assign encoder timebase from sample rate

This commit is contained in:
kieran 2024-11-18 14:24:14 +00:00
parent 497234f4b2
commit b226e011db
No known key found for this signature in database
GPG Key ID: DE71CEB3925BE941
3 changed files with 14 additions and 7 deletions

View File

@ -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)?;

View File

@ -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<Self> {
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<Self> {
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

View File

@ -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)?;