Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(enc-ffmpeg): let encoders write packets to any packet sink

Richie McIlroy1aab3b5066f2
diff --git a/crates/enc-ffmpeg/src/audio/aac.rs b/crates/enc-ffmpeg/src/audio/aac.rs
index 89ce84fcd..c1fb74a6e 100644
--- a/crates/enc-ffmpeg/src/audio/aac.rs
+++ b/crates/enc-ffmpeg/src/audio/aac.rs
@@ -123,6 +123,10 @@ impl AACEncoder {
) -> Result<(), ffmpeg::Error> {
self.base.send_frame(frame, timestamp, sink)
}
+
+ pub fn flush_to(&mut self, sink: &mut impl PacketSink) -> Result<(), ffmpeg::Error> {
+ self.base.flush(sink)
+ }
}
impl AudioEncoder for AACEncoder {
diff --git a/crates/enc-ffmpeg/src/audio/opus.rs b/crates/enc-ffmpeg/src/audio/opus.rs
index 3bcfb974c..5182c2968 100644
--- a/crates/enc-ffmpeg/src/audio/opus.rs
+++ b/crates/enc-ffmpeg/src/audio/opus.rs
@@ -106,6 +106,19 @@ impl OpusEncoder {
pub fn flush(&mut self, output: &mut format::context::Output) -> Result<(), ffmpeg::Error> {
self.base.flush(output)
}
+
+ pub fn queue_frame_to(
+ &mut self,
+ frame: frame::Audio,
+ timestamp: Duration,
+ sink: &mut impl crate::PacketSink,
+ ) -> Result<(), ffmpeg::Error> {
+ self.base.send_frame(frame, timestamp, sink)
+ }
+
+ pub fn flush_to(&mut self, sink: &mut impl crate::PacketSink) -> Result<(), ffmpeg::Error> {
+ self.base.flush(sink)
+ }
}
fn select_output_rate(input_rate: i32, supported_rates: &[i32]) -> Option<i32> {
diff --git a/crates/enc-ffmpeg/src/video/h264.rs b/crates/enc-ffmpeg/src/video/h264.rs
index 6ad7b80bf..446a2f4cd 100644
--- a/crates/enc-ffmpeg/src/video/h264.rs
+++ b/crates/enc-ffmpeg/src/video/h264.rs
@@ -11,7 +11,7 @@ use ffmpeg::{
};
use tracing::{debug, error, info, trace, warn};
-use crate::base::EncoderBase;
+use crate::base::{EncoderBase, PacketSink};
use crate::video::h264_packet::H264PacketEncoder;
#[cfg(target_os = "macos")]
use crate::video::videotoolbox_hw::VideoToolboxHwFrames;
@@ -924,9 +924,18 @@ impl H264Encoder {
pub fn queue_frame(
&mut self,
- mut frame: frame::Video,
+ frame: frame::Video,
timestamp: Duration,
output: &mut format::context::Output,
+ ) -> Result<(), QueueFrameError> {
+ self.queue_frame_to(frame, timestamp, output)
+ }
+
+ pub fn queue_frame_to<S: PacketSink + ?Sized>(
+ &mut self,
+ mut frame: frame::Video,
+ timestamp: Duration,
+ output: &mut S,
) -> Result<(), QueueFrameError> {
self.base
.update_pts(&mut frame, timestamp, &mut self.encoder);
@@ -1010,6 +1019,28 @@ impl H264Encoder {
self.base.process_eof(output, &mut self.encoder)
}
+ pub fn flush_to<S: PacketSink + ?Sized>(
+ &mut self,
+ output: &mut S,
+ ) -> Result<(), ffmpeg::Error> {
+ self.base.process_eof(output, &mut self.encoder)
+ }
+
+ /// Configured target bitrate in bits per second, estimated from the
+ /// output geometry when the encoder runs without a bitrate target.
+ pub fn bit_rate(&self) -> u64 {
+ let configured = unsafe { (*self.encoder.as_ptr()).bit_rate };
+ if configured > 0 {
+ return configured as u64;
+ }
+ get_bitrate(
+ self.output_width,
+ self.output_height,
+ 30.0,
+ H264EncoderBuilder::QUALITY_BPP,
+ ) as u64
+ }
+
pub fn set_packet_stats(&mut self, stats: Arc<crate::EncodedPacketStats>) {
self.base.set_packet_stats(stats);
}