FFMpeg

Struct FFMpeg 

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

Main interface into FFMPEG

This struct holds a child ffmpeg process that you can send frames into. Remember to call FFMpeg::finalize when you’re done. Dropping this struct basically calls finalize anyway but it ignores errors, so it’s better to call finalize explicitly

Implementations§

Source§

impl FFMpeg

Source

pub fn start( out_file: impl AsRef<OsStr>, width: usize, height: usize, fps: u32, ) -> Result<FFMpeg>

Start the FFMPEG rendering

Starts the FFMPEG rendering.

Source

pub fn width(&self) -> usize

Get the render width

Source

pub fn height(&self) -> usize

Get the render height

Source

pub fn fps(&self) -> u32

Get the render FPS

Source

pub fn resolution(&self) -> (usize, usize)

Get the render resolution

Source

pub fn send_frame(&mut self, pixels: &[Color]) -> Result<()>

Send a frame to the FFMPEG process

Send a frame to the FFMPEG process. pixels.len() must be equal to ffmpeg.width() * ffmpeg.height()

Examples found in repository?
examples/bouncing-square.rs (line 37)
23fn main() -> ffmpeg::Result<()> {
24    let mut pixels: [Color; PIXEL_COUNT] = [0; PIXEL_COUNT];
25
26    let mut ffmpeg = ffmpeg::start("out.mp4", WIDTH, HEIGHT, FPS)?;
27
28    let mut x = 0;
29    let mut y = 0;
30    let mut dx = 1;
31    let mut dy = 1;
32    let w = 50;
33    let h = 50;
34    for _ in 0..(60 * FPS) {
35        pixels.fill(0);
36        draw_rect(&mut pixels, WIDTH, x, y, w, h, 0xFF00FF00);
37        ffmpeg.send_frame(&pixels)?;
38
39        let new_x = x + dx;
40        if (0..WIDTH as i32 - w).contains(&new_x) {
41            x = new_x;
42        } else {
43            dx *= -1;
44        }
45
46        let new_y = y + dy;
47        if (0..HEIGHT as i32 - h).contains(&new_y) {
48            y = new_y;
49        } else {
50            dy *= -1;
51        }
52    }
53
54    ffmpeg.finalize()?;
55
56    Ok(())
57}
Source

pub fn finalize(self) -> Result<()>

Finalize the FFMPEG rendering

If this method isn’t called directly or indirectly (such as if std::mem::forget is called on FFMpeg), the final video may not be complete

Examples found in repository?
examples/bouncing-square.rs (line 54)
23fn main() -> ffmpeg::Result<()> {
24    let mut pixels: [Color; PIXEL_COUNT] = [0; PIXEL_COUNT];
25
26    let mut ffmpeg = ffmpeg::start("out.mp4", WIDTH, HEIGHT, FPS)?;
27
28    let mut x = 0;
29    let mut y = 0;
30    let mut dx = 1;
31    let mut dy = 1;
32    let w = 50;
33    let h = 50;
34    for _ in 0..(60 * FPS) {
35        pixels.fill(0);
36        draw_rect(&mut pixels, WIDTH, x, y, w, h, 0xFF00FF00);
37        ffmpeg.send_frame(&pixels)?;
38
39        let new_x = x + dx;
40        if (0..WIDTH as i32 - w).contains(&new_x) {
41            x = new_x;
42        } else {
43            dx *= -1;
44        }
45
46        let new_y = y + dy;
47        if (0..HEIGHT as i32 - h).contains(&new_y) {
48            y = new_y;
49        } else {
50            dy *= -1;
51        }
52    }
53
54    ffmpeg.finalize()?;
55
56    Ok(())
57}

Trait Implementations§

Source§

impl Drop for FFMpeg

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl Freeze for FFMpeg

§

impl RefUnwindSafe for FFMpeg

§

impl Send for FFMpeg

§

impl Sync for FFMpeg

§

impl Unpin for FFMpeg

§

impl UnwindSafe for FFMpeg

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.