Skip to main content

VideoEncoder

Struct VideoEncoder 

Source
pub struct VideoEncoder { /* private fields */ }
Expand description

Video encoder.

Encodes video frames to a file using FFmpeg.

§Examples

use ff_encode::{VideoEncoder, VideoCodec};
use ff_format::VideoFrame;

let mut encoder = VideoEncoder::create("output.mp4")
    .expect("Failed to create encoder")
    .video(1920, 1080, 30.0)
    .video_codec(VideoCodec::H264)
    .build()
    .expect("Failed to build encoder");

// Push frames
let frames = vec![]; // Your video frames here
for frame in frames {
    encoder.push_video(&frame).expect("Failed to push frame");
}

encoder.finish().expect("Failed to finish encoding");

Implementations§

Source§

impl VideoEncoder

Source

pub fn create<P: AsRef<Path>>(path: P) -> Result<EncoderBuilder, EncodeError>

Create a new encoder builder with the given output path.

§Arguments
  • path - Output file path
§Returns

Returns an EncoderBuilder for configuring the encoder.

§Examples
use ff_encode::VideoEncoder;

let builder = VideoEncoder::create("output.mp4")?;
Source

pub fn actual_video_codec(&self) -> &str

Get the actual video codec being used.

Returns the name of the FFmpeg encoder (e.g., “h264_nvenc”, “libx264”).

Source

pub fn actual_audio_codec(&self) -> &str

Get the actual audio codec being used.

Returns the name of the FFmpeg encoder (e.g., “aac”, “libopus”).

Source

pub fn hardware_encoder(&self) -> HardwareEncoder

Get the hardware encoder actually being used.

Returns the hardware encoder type that is actually being used for encoding. This may differ from what was requested if the requested encoder is not available.

§Examples
use ff_encode::{VideoEncoder, VideoCodec, HardwareEncoder};

let encoder = VideoEncoder::create("output.mp4")?
    .video(1920, 1080, 30.0)
    .video_codec(VideoCodec::H264)
    .hardware_encoder(HardwareEncoder::Auto)
    .build()?;

println!("Using hardware encoder: {:?}", encoder.hardware_encoder());
Source

pub fn is_hardware_encoding(&self) -> bool

Check if hardware encoding is being used.

Returns true if the encoder is using hardware acceleration, false if using software encoding.

§Examples
use ff_encode::{VideoEncoder, VideoCodec, HardwareEncoder};

let encoder = VideoEncoder::create("output.mp4")?
    .video(1920, 1080, 30.0)
    .video_codec(VideoCodec::H264)
    .hardware_encoder(HardwareEncoder::Auto)
    .build()?;

if encoder.is_hardware_encoding() {
    println!("Using hardware encoder: {}", encoder.actual_video_codec());
} else {
    println!("Using software encoder: {}", encoder.actual_video_codec());
}
Source

pub fn is_lgpl_compliant(&self) -> bool

Check if the actually selected video encoder is LGPL-compliant.

Returns true if the encoder is safe for commercial use without licensing fees. Returns false for GPL encoders that require licensing.

§LGPL-Compatible Encoders (Commercial Use OK)
  • Hardware encoders: h264_nvenc, h264_qsv, h264_amf, h264_videotoolbox, h264_vaapi
  • Royalty-free codecs: libvpx-vp9, libaom-av1, libsvtav1
  • Professional codecs: prores_ks, dnxhd
§GPL Encoders (Licensing Required)
  • libx264: Requires MPEG LA H.264 license for commercial distribution
  • libx265: Requires MPEG LA H.265 license for commercial distribution
§Examples
use ff_encode::{VideoEncoder, VideoCodec, HardwareEncoder};

// Default: Will use hardware encoder or VP9 fallback (LGPL-compliant)
let encoder = VideoEncoder::create("output.mp4")?
    .video(1920, 1080, 30.0)
    .video_codec(VideoCodec::H264)
    .build()?;

if encoder.is_lgpl_compliant() {
    println!("✓ Safe for commercial use: {}", encoder.actual_video_codec());
} else {
    println!("⚠ GPL encoder (requires licensing): {}", encoder.actual_video_codec());
}
§Note

By default (without gpl feature), this will always return true because the encoder automatically selects LGPL-compatible alternatives.

Source

pub fn push_video(&mut self, frame: &VideoFrame) -> Result<(), EncodeError>

Push a video frame for encoding.

§Arguments
  • frame - The video frame to encode
§Errors

Returns an error if encoding fails or the encoder is not initialized. Returns EncodeError::Cancelled if the progress callback requested cancellation.

Source

pub fn push_audio(&mut self, frame: &AudioFrame) -> Result<(), EncodeError>

Push an audio frame for encoding.

§Arguments
  • frame - The audio frame to encode
§Errors

Returns an error if encoding fails or the encoder is not initialized. Returns EncodeError::Cancelled if the progress callback requested cancellation.

§Examples
use ff_encode::{VideoEncoder, AudioCodec};
use ff_format::AudioFrame;

let mut encoder = VideoEncoder::create("output.mp4")?
    .video(1920, 1080, 30.0)
    .audio(48000, 2)
    .audio_codec(AudioCodec::Aac)
    .build()?;

// Push audio frames
let frame = AudioFrame::empty(1024, 2, 48000, ff_format::SampleFormat::F32)?;
encoder.push_audio(&frame)?;
Source

pub fn finish(self) -> Result<(), EncodeError>

Finish encoding and write the file trailer.

This method must be called to properly finalize the output file. It flushes any remaining encoded frames and writes the file trailer.

§Errors

Returns an error if finalizing fails.

Trait Implementations§

Source§

impl Drop for VideoEncoder

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.