Skip to main content

EncoderBuilder

Struct EncoderBuilder 

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

Builder for constructing a VideoEncoder or AudioEncoder.

Provides a fluent API for configuring encoding parameters before creating the actual encoder instance.

§Examples

use ff_encode::{VideoEncoder, VideoCodec, AudioCodec, Preset};

let encoder = VideoEncoder::create("output.mp4")?
    .video(1920, 1080, 30.0)
    .video_codec(VideoCodec::H264)
    .video_bitrate(8_000_000)
    .preset(Preset::Medium)
    .audio(48000, 2)
    .audio_codec(AudioCodec::Aac)
    .audio_bitrate(192_000)
    .build()?;

Implementations§

Source§

impl EncoderBuilder

Source

pub fn new(path: PathBuf) -> Result<Self, EncodeError>

Create a new encoder builder with the given output path.

§Arguments
  • path - Output file path
§Errors

Returns an error if the output path is invalid.

Source

pub fn video(self, width: u32, height: u32, fps: f64) -> Self

Configure video stream settings.

§Arguments
  • width - Video width in pixels
  • height - Video height in pixels
  • fps - Frame rate in frames per second
Source

pub fn video_codec(self, codec: VideoCodec) -> Self

Set video codec.

§Arguments
  • codec - Video codec to use
Source

pub fn video_bitrate(self, bitrate: u64) -> Self

Set video bitrate in bits per second.

§Arguments
  • bitrate - Target bitrate in bps (e.g., 8_000_000 for 8 Mbps)
Source

pub fn video_quality(self, crf: u32) -> Self

Set video quality using CRF (Constant Rate Factor).

§Arguments
  • crf - Quality value (0-51, lower is higher quality)

Note: CRF mode typically produces better quality than constant bitrate for the same file size, but the final file size is less predictable.

Source

pub fn preset(self, preset: Preset) -> Self

Set encoding preset (speed vs quality tradeoff).

§Arguments
  • preset - Encoding preset
Source

pub fn hardware_encoder(self, hw: HardwareEncoder) -> Self

Set hardware encoder.

§Arguments
  • hw - Hardware encoder to use
Source

pub fn audio(self, sample_rate: u32, channels: u32) -> Self

Configure audio stream settings.

§Arguments
  • sample_rate - Sample rate in Hz (e.g., 48000)
  • channels - Number of channels (1 = mono, 2 = stereo)
Source

pub fn audio_codec(self, codec: AudioCodec) -> Self

Set audio codec.

§Arguments
  • codec - Audio codec to use
Source

pub fn audio_bitrate(self, bitrate: u64) -> Self

Set audio bitrate in bits per second.

§Arguments
  • bitrate - Target bitrate in bps (e.g., 192_000 for 192 kbps)
Source

pub fn container(self, container: Container) -> Self

Set container format explicitly.

§Arguments
  • container - Container format

Note: Usually not needed as the format is auto-detected from file extension.

Source

pub fn on_progress<F>(self, callback: F) -> Self
where F: FnMut(&Progress) + Send + 'static,

Set progress callback using a closure or function.

This is a convenience method for simple progress callbacks. For cancellation support, use progress_callback() instead.

§Arguments
  • callback - Closure or function to call with progress updates
§Examples
use ff_encode::VideoEncoder;

let encoder = VideoEncoder::create("output.mp4")?
    .video(1920, 1080, 30.0)
    .on_progress(|progress| {
        println!("Progress: {:.1}%", progress.percent());
    })
    .build()?;
Source

pub fn progress_callback<C: ProgressCallback + 'static>( self, callback: C, ) -> Self

Set progress callback using a trait object.

Use this method when you need cancellation support or want to use a custom struct implementing ProgressCallback.

§Arguments
  • callback - Progress callback handler
§Examples
use ff_encode::{VideoEncoder, ProgressCallback, Progress};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};

struct CancellableProgress {
    cancelled: Arc<AtomicBool>,
}

impl ProgressCallback for CancellableProgress {
    fn on_progress(&mut self, progress: &Progress) {
        println!("Progress: {:.1}%", progress.percent());
    }

    fn should_cancel(&self) -> bool {
        self.cancelled.load(Ordering::Relaxed)
    }
}

let cancelled = Arc::new(AtomicBool::new(false));
let encoder = VideoEncoder::create("output.mp4")?
    .video(1920, 1080, 30.0)
    .progress_callback(CancellableProgress {
        cancelled: cancelled.clone()
    })
    .build()?;
Source

pub fn build(self) -> Result<VideoEncoder, EncodeError>

Build the encoder.

§Errors

Returns an error if:

  • The configuration is invalid
  • The output file cannot be created
  • No suitable encoder is found for the requested codec
Source

pub fn build_audio(self) -> Result<AudioEncoder, EncodeError>

Build an audio-only encoder with the configured settings.

§Errors

Returns an error if the configuration is invalid or encoder creation fails.

Trait Implementations§

Source§

impl Debug for EncoderBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. 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.