Struct Encoder

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

Encodes video.

Implementations§

Source§

impl Encoder

Source

pub fn builder() -> Setup

Creates a new builder with default options.

For more options see Setup::new.

Examples found in repository?
examples/fade.rs (line 13)
7fn main() {
8    const WIDTH: usize = 480;
9    const HEIGHT: usize = 360;
10
11    // Initialize things.
12
13    let mut encoder = Encoder::builder()
14        .fps(60, 1)
15        .build(Colorspace::RGB, WIDTH as _, HEIGHT as _)
16        .unwrap();
17    let mut file = File::create("fade.h264").unwrap();
18    let mut canvas = vec![0; WIDTH * HEIGHT * 3];
19
20    println!("Initialized!");
21
22    // Write the headers.
23
24    {
25        let headers = encoder.headers().unwrap();
26        file.write_all(headers.entirety()).unwrap();
27    }
28
29    // Queue each frame.
30
31    for i in 0..300 {
32        frame(i as f64 / 300.0, &mut canvas);
33        let image = Image::rgb(WIDTH as _, HEIGHT as _, &canvas);
34        let (data, _) = encoder.encode((60 * i) as _, image).unwrap();
35        file.write_all(data.entirety()).unwrap();
36    }
37
38    // Finally, flush any delayed frames.
39
40    {
41        let mut flush = encoder.flush();
42        while let Some(result) = flush.next() {
43            let (data, _) = result.unwrap();
44            file.write_all(data.entirety()).unwrap();
45        }
46    }
47
48    println!("Done! The output is at `fade.h264`.");
49    println!("Good luck finding a H.264 viewer, though! ;)");
50}
Source

pub fn encode( &mut self, pts: i64, image: Image<'_>, ) -> Result<(Data<'_>, Picture)>

Feeds a frame to the encoder.

§Panics

Panics if there is a mismatch between the image and the encoder regarding width, height or colorspace.

Examples found in repository?
examples/fade.rs (line 34)
7fn main() {
8    const WIDTH: usize = 480;
9    const HEIGHT: usize = 360;
10
11    // Initialize things.
12
13    let mut encoder = Encoder::builder()
14        .fps(60, 1)
15        .build(Colorspace::RGB, WIDTH as _, HEIGHT as _)
16        .unwrap();
17    let mut file = File::create("fade.h264").unwrap();
18    let mut canvas = vec![0; WIDTH * HEIGHT * 3];
19
20    println!("Initialized!");
21
22    // Write the headers.
23
24    {
25        let headers = encoder.headers().unwrap();
26        file.write_all(headers.entirety()).unwrap();
27    }
28
29    // Queue each frame.
30
31    for i in 0..300 {
32        frame(i as f64 / 300.0, &mut canvas);
33        let image = Image::rgb(WIDTH as _, HEIGHT as _, &canvas);
34        let (data, _) = encoder.encode((60 * i) as _, image).unwrap();
35        file.write_all(data.entirety()).unwrap();
36    }
37
38    // Finally, flush any delayed frames.
39
40    {
41        let mut flush = encoder.flush();
42        while let Some(result) = flush.next() {
43            let (data, _) = result.unwrap();
44            file.write_all(data.entirety()).unwrap();
45        }
46    }
47
48    println!("Done! The output is at `fade.h264`.");
49    println!("Good luck finding a H.264 viewer, though! ;)");
50}
Source

pub unsafe fn encode_unchecked( &mut self, pts: i64, image: Image<'_>, ) -> Result<(Data<'_>, Picture)>

Feeds a frame to the encoder.

§Unsafety

The caller must ensure that the width, height and colorspace of the image are the same as that of the encoder.

Source

pub fn headers(&mut self) -> Result<Data<'_>>

Gets the video headers, which should be sent first.

Examples found in repository?
examples/fade.rs (line 25)
7fn main() {
8    const WIDTH: usize = 480;
9    const HEIGHT: usize = 360;
10
11    // Initialize things.
12
13    let mut encoder = Encoder::builder()
14        .fps(60, 1)
15        .build(Colorspace::RGB, WIDTH as _, HEIGHT as _)
16        .unwrap();
17    let mut file = File::create("fade.h264").unwrap();
18    let mut canvas = vec![0; WIDTH * HEIGHT * 3];
19
20    println!("Initialized!");
21
22    // Write the headers.
23
24    {
25        let headers = encoder.headers().unwrap();
26        file.write_all(headers.entirety()).unwrap();
27    }
28
29    // Queue each frame.
30
31    for i in 0..300 {
32        frame(i as f64 / 300.0, &mut canvas);
33        let image = Image::rgb(WIDTH as _, HEIGHT as _, &canvas);
34        let (data, _) = encoder.encode((60 * i) as _, image).unwrap();
35        file.write_all(data.entirety()).unwrap();
36    }
37
38    // Finally, flush any delayed frames.
39
40    {
41        let mut flush = encoder.flush();
42        while let Some(result) = flush.next() {
43            let (data, _) = result.unwrap();
44            file.write_all(data.entirety()).unwrap();
45        }
46    }
47
48    println!("Done! The output is at `fade.h264`.");
49    println!("Good luck finding a H.264 viewer, though! ;)");
50}
Source

pub fn flush(self) -> Flush

Begins flushing the encoder, to handle any delayed frames.

let mut flush = encoder.flush();

while let Some(result) = flush.next() {
    if let Ok((data, picture)) = result {
        // Handle data.
    }
}
Examples found in repository?
examples/fade.rs (line 41)
7fn main() {
8    const WIDTH: usize = 480;
9    const HEIGHT: usize = 360;
10
11    // Initialize things.
12
13    let mut encoder = Encoder::builder()
14        .fps(60, 1)
15        .build(Colorspace::RGB, WIDTH as _, HEIGHT as _)
16        .unwrap();
17    let mut file = File::create("fade.h264").unwrap();
18    let mut canvas = vec![0; WIDTH * HEIGHT * 3];
19
20    println!("Initialized!");
21
22    // Write the headers.
23
24    {
25        let headers = encoder.headers().unwrap();
26        file.write_all(headers.entirety()).unwrap();
27    }
28
29    // Queue each frame.
30
31    for i in 0..300 {
32        frame(i as f64 / 300.0, &mut canvas);
33        let image = Image::rgb(WIDTH as _, HEIGHT as _, &canvas);
34        let (data, _) = encoder.encode((60 * i) as _, image).unwrap();
35        file.write_all(data.entirety()).unwrap();
36    }
37
38    // Finally, flush any delayed frames.
39
40    {
41        let mut flush = encoder.flush();
42        while let Some(result) = flush.next() {
43            let (data, _) = result.unwrap();
44            file.write_all(data.entirety()).unwrap();
45        }
46    }
47
48    println!("Done! The output is at `fade.h264`.");
49    println!("Good luck finding a H.264 viewer, though! ;)");
50}
Source

pub fn width(&self) -> i32

The width required of any input images.

Source

pub fn height(&self) -> i32

The height required of any input images.

Source

pub fn encoding(&self) -> Encoding

The encoding required of any input images.

Trait Implementations§

Source§

impl Drop for Encoder

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.