Code Monkey home page Code Monkey logo

ffmpeg-sidecar's Introduction

FFmpeg Sidecar ๐Ÿ

Github | Crates.io | Docs.rs

Wrap a standalone FFmpeg binary in an intuitive Iterator interface.

Features

  • โœจ Minimal dependencies
  • โšก Automatic FFmpeg CLI download (if needed)
  • ๐Ÿค— Support for Windows, MacOS, and Linux
  • ๐Ÿงช Thoroughly unit tested

๐Ÿ‘‰ Jump to Getting Started ๐Ÿ‘ˆ

Motivation

The core goal of this project is to provide a method of interacting with any video as if it were an array of raw RGB frames.

Of course, that's what video is, fundamentally, but there is a whole pandora's box of complexity in terms of receiving and decoding video before you get there.

Using FFmpeg as the core engine provides interoperability between a massive range of formats, containers, extensions, protocols, encoders, decoders, hardware accelerations, and more.

Why CLI?

One method of using FFmpeg is low-level bindings to the code used inside the CLI itself -- there are good crates in the Rust ecosystem that do this.

Low level bindings have drawbacks, though:

  • Difficult, time-consuming build, toolchain, and dependencies, especially on Windows
  • Complexity, especially for beginners
  • You end up manually re-implementing a lot of the standard conversions you need from scratch

By wrapping the CLI, this crate avoids those downsides, and also solves some of the pain points that you would encounter if you were to use the CLI directly on its own:

  • Raw data can easily move in and out of FFmpeg instances, or pipe between them. Under the hood they are moving across stdin and stdout.
  • Rich semantic information is recovered from the FFmpeg stderr logs, including:
    • Progress updates (frame #, timestamp, speed, bitrate, ...)
    • Input/output metadata and stream mappings
    • Warnings & errors
  • Argument presets and aliases with discoverable names through Intellisense/autocomplete

The only remaining downside is the size of the FFmpeg binary itself, but it's less than 100MB when zipped. It can be automatically downloaded by the crate, so you may choose to not even ship it with your own application and instead download it at runtime.

Getting Started

1. Cargo Install

On the Rust side, it has zero Cargo dependencies! ๐ŸŽ‰

cargo add ffmpeg-sidecar

2. Download FFmpeg

To automatically download & install a FFmpeg binary for your platform (Windows, MacOS, and Linux), call this function anywhere in your program:

ffmpeg_sidecar::download::auto_download().unwrap();

You can do this once to set up your dev environment, or include it as a feature of your client application.

To customize or extend the download, see /examples/download_ffmpeg.rs.

Examples

Hello world ๐Ÿ‘‹

Read raw video frames.

use ffmpeg_sidecar::{command::FfmpegCommand, error::Result, event::FfmpegEvent};

fn main() -> Result<()> {
  FfmpegCommand::new() // <- Builder API like `std::process::Command`
    .testsrc()  // <- Discoverable aliases for FFmpeg args
    .rawvideo() // <- Convenient argument presets
    .spawn()?   // <- Uses an ordinary `std::process::Child`
    .iter()?    // <- Iterator over all log messages and video output
    .for_each(|event: FfmpegEvent| {
      match event {
        FfmpegEvent::OutputFrame(frame) => {
          println!("frame: {}x{}", frame.width, frame.height);
          let _pixels: Vec<u8> = frame.data; // <- raw RGB pixels! ๐ŸŽจ
        }
        FfmpegEvent::Progress(progress) => {
          eprintln!("Current speed: {}x", progress.speed); // <- parsed progress updates
        }
        FfmpegEvent::Log(_level, msg) => {
          eprintln!("[ffmpeg] {}", msg); // <- granular log message from stderr
        }
        _ => {}
      }
    });
  Ok(())
}

Source: /examples/hello_world.rs

cargo run --example hello-world

H265 Transcoding

Decode H265, modify the decoded frames, and then write back to H265.

Source: /examples/h265_transcode.rs

cargo run --example h265_transcode

FFplay

Pipe an FFmpeg instance to FFplay for debugging purposes.

Source: /examples/ffplay_preview.rs

cargo run --example ffplay_preview

Others

For a myriad of other examples, check any of the unit tests in /src/test.rs in this repo.

Todo

  • Add /examples
  • Take input from stdin, and pipe between iterators
  • Pipe directly to ffplay for debugging
  • Idiomatic error type instead of Result<_, String>
  • Handle indeterminate output formats like H264/H265
    • Currently these formats are mutually exclusive with using iter() since they require consuming stdout directly

See also

Inspired loosely by Node.js fluent-ffmpeg, which does something similar in Javascript.

Uses setup-ffmpeg for Github Actions and as a reference for the auto-download behavior.

๐Ÿ“ฃ Pull Requests Welcome ๐Ÿ“ฃ

ffmpeg-sidecar's People

Contributors

nathanbabcock avatar avsaase avatar emirror-de avatar odilf avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.