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
impl EncoderBuilder
Sourcepub fn new(path: PathBuf) -> Result<Self, EncodeError>
pub fn new(path: PathBuf) -> Result<Self, EncodeError>
Sourcepub fn video(self, width: u32, height: u32, fps: f64) -> Self
pub fn video(self, width: u32, height: u32, fps: f64) -> Self
Configure video stream settings.
§Arguments
width- Video width in pixelsheight- Video height in pixelsfps- Frame rate in frames per second
Sourcepub fn video_codec(self, codec: VideoCodec) -> Self
pub fn video_codec(self, codec: VideoCodec) -> Self
Sourcepub fn video_bitrate(self, bitrate: u64) -> Self
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_000for 8 Mbps)
Sourcepub fn video_quality(self, crf: u32) -> Self
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.
Sourcepub fn hardware_encoder(self, hw: HardwareEncoder) -> Self
pub fn hardware_encoder(self, hw: HardwareEncoder) -> Self
Sourcepub fn audio(self, sample_rate: u32, channels: u32) -> Self
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)
Sourcepub fn audio_codec(self, codec: AudioCodec) -> Self
pub fn audio_codec(self, codec: AudioCodec) -> Self
Sourcepub fn audio_bitrate(self, bitrate: u64) -> Self
pub fn audio_bitrate(self, bitrate: u64) -> Self
Set audio bitrate in bits per second.
§Arguments
bitrate- Target bitrate in bps (e.g.,192_000for 192 kbps)
Sourcepub fn container(self, container: Container) -> Self
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.
Sourcepub fn on_progress<F>(self, callback: F) -> Self
pub fn on_progress<F>(self, callback: F) -> Self
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()?;Sourcepub fn progress_callback<C: ProgressCallback + 'static>(
self,
callback: C,
) -> Self
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()?;Sourcepub fn build(self) -> Result<VideoEncoder, EncodeError>
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
Sourcepub fn build_audio(self) -> Result<AudioEncoder, EncodeError>
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.