Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(enc-ffmpeg): enforce monotonic DTS in encoder output

Richie McIlroy490a06790ed4
diff --git a/crates/enc-ffmpeg/src/base.rs b/crates/enc-ffmpeg/src/base.rs
index 22c92384f..c0be140b3 100644
--- a/crates/enc-ffmpeg/src/base.rs
+++ b/crates/enc-ffmpeg/src/base.rs
@@ -11,6 +11,7 @@ pub struct EncoderBase {
packet: ffmpeg::Packet,
stream_index: usize,
first_pts: Option<i64>,
+ last_written_dts: Option<i64>,
}
impl EncoderBase {
@@ -19,6 +20,7 @@ impl EncoderBase {
packet: Packet::empty(),
first_pts: None,
stream_index,
+ last_written_dts: None,
}
}
@@ -70,6 +72,20 @@ impl EncoderBase {
encoder.time_base(),
output.stream(self.stream_index).unwrap().time_base(),
);
+
+ if let (Some(dts), Some(last_dts)) = (self.packet.dts(), self.last_written_dts)
+ && dts <= last_dts
+ {
+ let fixed_dts = last_dts + 1;
+ self.packet.set_dts(Some(fixed_dts));
+ if let Some(pts) = self.packet.pts()
+ && pts < fixed_dts
+ {
+ self.packet.set_pts(Some(fixed_dts));
+ }
+ }
+
+ self.last_written_dts = self.packet.dts();
self.packet.write_interleaved(output)?;
}