rsmpeg Documentation

repository·master·Indexed 21 days ago

https://github.com/larksuite/rsmpeg

A safe Rust wrapper around FFmpeg's inner APIs designed for robust multimedia processing. rsmpeg provides a thin layer over FFmpeg's Rust bindings, supporting FFmpeg versions 6.*, 7.*, and 8.*. It includes comprehensive guides for linking against system-wide FFmpeg installations, managing dependencies via vcpkg, and building FFmpeg from source using provided utility scripts.

Tokens
12.4K
Snippets
44
Records
68
Agent score
72%

What's inside rsmpeg

  1. Overview of rsmpeg

    master
    rsmpeg is a thin and safe Rust layer built on top of FFmpeg's Rust bindings. Its primary purpose is to safely expose FFmpeg's inner APIs within Rust, allowing developers to build robust multimedia projects using Rust's safety guarantees instead of interacting directly with the FFmpeg C API.
  2. Configure rsmpeg with FFmpeg via vcpkg

    master

    To use rsmpeg with FFmpeg managed by vcpkg, follow these configuration steps in your Cargo.toml:

    1. Add rsmpeg with the link_vcpkg_ffmpeg feature: Choose the feature flag that matches your target FFmpeg version (ffmpeg6, ffmpeg7, or ffmpeg8). You must set default-features = false.
    2. Configure [package.metadata.vcpkg]: Define the vcpkg git repository, the specific dependencies (including the triplet), and the branch (e.g., master).
    3. Build dependencies: Run cargo vcpkg build to compile the FFmpeg dependencies.
    [dependencies]
    # Choose one based on your FFmpeg version requirement
    rsmpeg = { version = "0.18", default-features = false, features = ["ffmpeg7", "link_vcpkg_ffmpeg"] }
    
    [package.metadata.vcpkg]
    git = "https://github.com/microsoft/vcpkg"
    dependencies = ["ffmpeg:x64-windows-static-md"]
    branch = "master"
    # Use --verbose to help identify errors if the build fails
    cargo vcpkg --verbose build
  3. Cross-compile FFmpeg for Windows from Linux or WSL

    master
    For production environments on Windows, it is highly recommended to cross-compile FFmpeg from source on a Linux machine or Windows Subsystem for Linux (WSL). This method allows for greater control over configure script options. Refer to the source build tutorial for details.
  4. Configure FFMPEG_PKG_CONFIG_PATH

    master

    To allow cargo to find your FFmpeg installation during compilation, you must set the FFMPEG_PKG_CONFIG_PATH environment variable to the absolute path of your FFmpeg pkgconfig directory.

    If you used the utils scripts, the directory is typically located at {PWD}/tmp/ffmpeg_build/lib/pkgconfig.

    # macOS & Linux
    export FFMPEG_PKG_CONFIG_PATH=${PWD}/tmp/ffmpeg_build/lib/pkgconfig
    
    # Windows (CMD)
    set FFMPEG_PKG_CONFIG_PATH="%CD%\tmp\ffmpeg_build\lib\pkgconfig"
    
    # Windows (PowerShell)
    $env:FFMPEG_PKG_CONFIG_PATH="$(($PWD).path)\tmp\ffmpeg_build\lib\pkgconfig"
  5. Install system-wide FFmpeg for rsmpeg

    master

    To avoid compiling FFmpeg from source, you can link rsmpeg against a system-wide FFmpeg installation. You must install the FFmpeg libraries, development headers, and pkg-config.

    Debian/Ubuntu:

    sudo apt update
    sudo apt install libavcodec-dev libavdevice-dev libavfilter-dev libavformat-dev libavutil-dev libpostproc-dev libswresample-dev libswscale-dev pkg-config

    macOS (Homebrew):

    brew install ffmpeg pkg-config

    Verify that pkg-config can locate your installation by running:

    pkg-config --modversion libavcodec
    pkg-config --modversion libavcodec
  6. Install FFmpeg on Windows using BtbN's builds via Winget

    master
    You can install pre-built FFmpeg binaries from BtbN using winget. This is the easiest way to use BtbN's feature-rich builds. Refer to the winget tutorial for the specific command and setup steps.
  7. Configure Windows build target for rsmpeg

    master

    If your build target is Windows, you must create a .cargo/config.toml file in your project workspace to link the necessary system libraries required by FFmpeg.

    [target.x86_64-pc-windows-msvc]
    rustflags = [
        "-C", "link-arg=Mfplat.lib",
        "-C", "link-arg=Strmiids.lib",
        "-C", "link-arg=Mfuuid.lib",
        "-C", "link-arg=Bcrypt.lib",
        "-C", "link-arg=Secur32.lib",
        "-C", "link-arg=Ole32.lib",
        "-C", "link-arg=User32.lib"
    ]
  8. Add rsmpeg to your Cargo project

    master

    Add rsmpeg to your Cargo.toml. You must select a feature flag that matches the version of FFmpeg you have compiled.

    • For FFmpeg 6.*: Use features = ["ffmpeg6"] and disable default features.
    • For FFmpeg 7.*: Use features = ["ffmpeg7"] and disable default features.
    • For FFmpeg 8.*: This is the default behavior; no extra flags are required.
    # FFmpeg 6.*
    rsmpeg = { version = "0.18", default-features = false, features = ["ffmpeg6"] }
    
    # FFmpeg 7.*
    rsmpeg = { version = "0.18", default-features = false, features = ["ffmpeg7"] }
    
    # FFmpeg 8.* (feature `ffmpeg8` is enabled by default)
    rsmpeg = "0.18"