pub struct Encoder { /* private fields */ }
Expand description
Encodes video.
Implementations§
Source§impl Encoder
impl Encoder
Sourcepub fn builder() -> Setup
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}
Sourcepub fn encode(
&mut self,
pts: i64,
image: Image<'_>,
) -> Result<(Data<'_>, Picture)>
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}
Sourcepub unsafe fn encode_unchecked(
&mut self,
pts: i64,
image: Image<'_>,
) -> Result<(Data<'_>, Picture)>
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.
Sourcepub fn headers(&mut self) -> Result<Data<'_>>
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}
Sourcepub fn flush(self) -> Flush
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}
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Encoder
impl RefUnwindSafe for Encoder
impl !Send for Encoder
impl !Sync for Encoder
impl Unpin for Encoder
impl UnwindSafe for Encoder
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
Mutably borrows from an owned value. Read more