Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(enc-ffmpeg): use encoder_time_base in H264PacketEncoder PTS to match muxer init

ManthanNimodiya2e0872f97566
diff --git a/crates/enc-ffmpeg/src/video/h264_packet.rs b/crates/enc-ffmpeg/src/video/h264_packet.rs
index d207fe5df..cc7c9f02b 100644
--- a/crates/enc-ffmpeg/src/video/h264_packet.rs
+++ b/crates/enc-ffmpeg/src/video/h264_packet.rs
@@ -198,8 +198,12 @@ impl H264PacketEncoder {
}
fn update_pts(&mut self, frame: &mut frame::Video, timestamp: Duration) {
+ // Use encoder_time_base (the value sent to the muxer subprocess via InitVideo),
+ // NOT self.encoder.time_base() which may be overridden by the codec after open.
+ // Mismatching these causes the muxer to rescale PTS using the wrong time base,
+ // producing wrong playback speed (observed as 4-8x too fast with h264_videotoolbox).
+ let tb = self.encoder_time_base;
if timestamp != Duration::MAX {
- let tb = self.encoder.time_base();
let rate = tb.denominator() as f64 / tb.numerator() as f64;
let pts = (timestamp.as_secs_f64() * rate).round() as i64;
let first_pts = *self.first_pts.get_or_insert(pts);
@@ -207,7 +211,7 @@ impl H264PacketEncoder {
pts - first_pts,
self.last_frame_pts,
tb,
- self.encoder.frame_rate(),
+ self.frame_rate,
);
self.last_frame_pts = Some(pts);
frame.set_pts(Some(pts));
@@ -216,8 +220,8 @@ impl H264PacketEncoder {
let pts = normalize_input_pts(
pts - first_pts,
self.last_frame_pts,
- self.encoder.time_base(),
- self.encoder.frame_rate(),
+ tb,
+ self.frame_rate,
);
self.last_frame_pts = Some(pts);
frame.set_pts(Some(pts));