pub struct VideoEncoder { /* private fields */ }Expand description
Encodes video frames (and optional audio) and writes them to a file or stream.
Frames are provided as Direct3D surfaces or raw BGRA buffers. Audio can be pushed as interleaved PCM bytes.
- Use
VideoEncoder::newfor file output orVideoEncoder::new_from_streamfor stream output. - Push frames with
VideoEncoder::send_frameorVideoEncoder::send_frame_buffer. - Optionally push audio with
VideoEncoder::send_audio_bufferor useVideoEncoder::send_frame_with_audio. - Call
VideoEncoder::finishto finalize the container.
ยงExample
use windows_capture::encoder::{
AudioSettingsBuilder, ContainerSettingsBuilder, VideoEncoder, VideoSettingsBuilder,
};
// Create an encoder that outputs H.265 in an MP4 container
let mut encoder = VideoEncoder::new(
VideoSettingsBuilder::new(1920, 1080),
AudioSettingsBuilder::new().disabled(true),
ContainerSettingsBuilder::new(),
"capture.mp4",
)
.unwrap();
// In your capture loop, push frames:
// encoder.send_frame(&frame).unwrap();
// When done:
// encoder.finish().unwrap();Implementationsยง
Sourceยงimpl VideoEncoder
impl VideoEncoder
Sourcepub fn new<P: AsRef<Path>>(
video_settings: VideoSettingsBuilder,
audio_settings: AudioSettingsBuilder,
container_settings: ContainerSettingsBuilder,
path: P,
) -> Result<Self, VideoEncoderError>
pub fn new<P: AsRef<Path>>( video_settings: VideoSettingsBuilder, audio_settings: AudioSettingsBuilder, container_settings: ContainerSettingsBuilder, path: P, ) -> Result<Self, VideoEncoderError>
Constructs a new VideoEncoder that writes to a file path.
Sourcepub fn new_from_stream(
video_settings: VideoSettingsBuilder,
audio_settings: AudioSettingsBuilder,
container_settings: ContainerSettingsBuilder,
stream: IRandomAccessStream,
) -> Result<Self, VideoEncoderError>
pub fn new_from_stream( video_settings: VideoSettingsBuilder, audio_settings: AudioSettingsBuilder, container_settings: ContainerSettingsBuilder, stream: IRandomAccessStream, ) -> Result<Self, VideoEncoderError>
Constructs a new VideoEncoder that writes to the given stream.
Unlike VideoEncoder::new, which writes directly to a file, this constructor writes
encoded output into any [IRandomAccessStream]. Use [InMemoryRandomAccessStream] to
keep the encoded video in memory (e.g., for network streaming or further processing).
ยงExample
use windows::Storage::Streams::InMemoryRandomAccessStream;
use windows::core::Interface;
use windows_capture::encoder::{
AudioSettingsBuilder, ContainerSettingsBuilder, VideoEncoder, VideoSettingsBuilder,
};
let stream = InMemoryRandomAccessStream::new().unwrap();
let encoder = VideoEncoder::new_from_stream(
VideoSettingsBuilder::new(1920, 1080),
AudioSettingsBuilder::new().disabled(true),
ContainerSettingsBuilder::new(),
stream.cast().unwrap(),
)
.unwrap();Sourcepub fn send_frame(&mut self, frame: &Frame<'_>) -> Result<(), VideoEncoderError>
pub fn send_frame(&mut self, frame: &Frame<'_>) -> Result<(), VideoEncoderError>
Sends a video frame (DirectX). Returns immediately.
Sourcepub fn send_frame_with_audio(
&mut self,
frame: &mut Frame<'_>,
audio_buffer: &[u8],
) -> Result<(), VideoEncoderError>
pub fn send_frame_with_audio( &mut self, frame: &mut Frame<'_>, audio_buffer: &[u8], ) -> Result<(), VideoEncoderError>
Sends a video frame and an audio buffer (owned). Returns immediately. Audio timestamp is derived from total samples sent so far (monotonic).
Sourcepub fn send_frame_buffer(
&mut self,
buffer: &[u8],
timestamp: i64,
) -> Result<(), VideoEncoderError>
pub fn send_frame_buffer( &mut self, buffer: &[u8], timestamp: i64, ) -> Result<(), VideoEncoderError>
Sends a raw frame buffer (owned inside). Returns immediately. Windows expects BGRA and bottom-to-top layout for this path.
Sourcepub fn send_audio_buffer(
&mut self,
buffer: &[u8],
_timestamp: i64,
) -> Result<(), VideoEncoderError>
pub fn send_audio_buffer( &mut self, buffer: &[u8], _timestamp: i64, ) -> Result<(), VideoEncoderError>
Sends an audio buffer (owned inside). Returns immediately.
NOTE: The provided timestamp is ignored; we use a monotonic audio clock.
Sourcepub fn finish(self) -> Result<(), VideoEncoderError>
pub fn finish(self) -> Result<(), VideoEncoderError>
Finishes the encoding and performs any necessary cleanup.
Trait Implementationsยง
Sourceยงimpl Drop for VideoEncoder
impl Drop for VideoEncoder
impl Send for VideoEncoder
Auto Trait Implementationsยง
impl Freeze for VideoEncoder
impl !RefUnwindSafe for VideoEncoder
impl !Sync for VideoEncoder
impl Unpin for VideoEncoder
impl UnsafeUnpin for VideoEncoder
impl !UnwindSafe for VideoEncoder
Blanket Implementationsยง
Sourceยงimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Sourceยงfn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Sourceยงimpl<T> IntoEither for T
impl<T> IntoEither for T
Sourceยงfn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSourceยงfn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more