Cross platform FFI bindings for FFmpeg internal libraries. This is a crate that:
- Links FFmpeg libraries for you.
- Generates Rust binding for FFmpeg libraries.
This create requires configuration before you can start using it. To quickly get started, follow the steps below.
For more advanced usage, consult the reference section below.
Before beginning to build FFmpeg, you should install LLVM first. This will enable Rusty FFmpeg to generate Rust bindings from the C header files. The bindgen documentation has good instructions on how to install prebuilt LLVM binaries for your platform. You can also build LLVM from source, such as using vcpkg, but this can take hours on all but the most well-equipped computers.
If you have FFmpeg installed with your system package manager, import rusty_ffmpeg with feature link_system_ffmpeg and the feature for your installed FFmpeg version. The build script will use pkg-config to probe for the system-installed FFmpeg.
If you'd like to use an FFmpeg that you built from source, set FFMPEG_PKG_CONFIG_PATH to the path of the generated pkgconfig directory. The build script will use pkg-config to probe in that directory. You may also use vcpkg, as described below: the instructions should be similar to Windows.
One of the easiest ways to get started on Windows is to build FFmpeg using vcpkg, which provides a port for FFmpeg.
- Install vcpkg according to the instructions (the first step of cloning and bootstrapping).
- Build and install ffmpeg:
vcpkg install ffmpeg --triplet=<value>, wheretripletis set to an appropriate value for your project (see below). - Check documentation of the vcpkg crate for the environment variables to set.
- Import
rusty_ffmpegwith featurelink_vcpkg_ffmpegand the feature corresponding to the FFmpeg version that you built.
You may want to look into cargo-vcpkg, which can automate these steps.
When using manifest mode, you create a vcpkg.json file in your repository that declares what vcpkg ports your project requires. vcpkg then builds and installs the packages in a vcpkg_installed directory that is local to your repository.
- Create a
vcpkg.jsonfile for your project by following instructions in vcpkg's documentation. In addition to FFmpeg, make sure you also install thepkgconfport as a host tool. - Build and install your manifest:
vcpkg install --triplet=<value>, wheretripletis set to an appropriate value for your project (see below). - Set the
PKG_CONFIGenvironment variable to the vcpkg-builtpkgconf.exetool within the builttools/directory. Set thePKG_CONFIG_PATHto thepkgconfigdirectory that was installed within the builtlib/directory. - Import
rusty_ffmpegwith the feature corresponding to the FFmpeg version that you built.
Basic usage of Rusty FFmpeg requires that you:
- Set the feature for the FFmpeg version that you are using (see Features, below).
- Instruct the build script how to find FFmpeg.
There are several ways that FFmpeg can be found and configured using the environment variables and features. There is no default method for finding FFmpeg: you must choose one or more of these methods to utilize.
Unless the FFMPEG_DLL_PATH environment variable is set, static linking will be done.
When the build script is in static linking mode, it will try to statically link with FFmpeg in the following order:
- First, if
FFMPEG_PKG_CONFIG_PATHis set, pkg-config or pkgconf is used to probe for FFmpeg by using the.pcfiles in the given directory. ThePKG_CONFIGandFFMPEG_BINDING_PATHenvironment variables will also be used if they are set. - If
FFMPEG_LIBS_DIRis set, then FFmpeg libraries from that directory will be used. No probing with pkg-config will take place. You must also set theFFMPEG_INCLUDE_DIRor theFFMPEG_BINDING_PATHenvironment variable in this case. - If the
link_system_ffmpegfeature is enabled, then pkg-config will be used to probe for an FFmpeg package installed by your system package manager. - If the
link_vcpkg_ffmpegfeature is enabled, then the vcpkg crate will be used to probe for FFmpeg.- Note that as of this writing, the published crate does not support using vcpkg in manifest mode, so you may want to consider setting
PKG_CONFIGandFFMPEG_PKG_CONFIG_PATHto the corresponding file/directories installed by vcpkg in that case.
- Note that as of this writing, the published crate does not support using vcpkg in manifest mode, so you may want to consider setting
Both link_system_ffmpeg and link_vcpkg_ffmpeg features can be enabled simultaneously. If system pkg-config probing fails, then it will fallback to vcpkg probing.
At this time, the build script locates a dynamically-linked FFmpeg using only one way. Probing using pkg-config or vcpkg is not currently supported.
To dynamically link with FFmpeg, the following environment variables must be set:
FFMPEG_DLL_PATHFFMPEG_INCLUDE_DIRorFFMPEG_BINDING_PATH
At this time, more advanced search methods such as those used for static linking are not supported.
Several aspects of the build are controlled by environment variables:
| Variable | Purpose |
|---|---|
FFMPEG_BINDING_PATH |
Path to a pre-built binding.rs file so as to avoid repeatedly regenerating the same binding file. Include files will be ignored in this case; the variable takes precedence over FFMPEG_INCLUDE_DIR or any include files discovered using pkg-config. It can be copied from the OUT_DIR of a previous build that generated the bindings. |
FFMPEG_DLL_PATH |
Path to the FFmpeg dynamically linked library file. The lib file is expected to be alongside it. |
FFMPEG_INCLUDE_DIR |
Path to directory containing FFmpeg include files. Bindings will be generated using bindgen. |
FFMPEG_LIBS_DIR |
Path to directory containing FFmpeg library files. |
FFMPEG_PKG_CONFIG_PATH |
Path to directory containing .pc files to probe with pkg-config. |
PKG_CONFIG |
Path to pkg-config executable file, if pkg-config is not in your PATH. The pkgconf tool can also be used. |
This crate has several features which can be configured:
| Feature | Purpose |
|---|---|
ffmpeg5 |
Compile with support for FFmpeg 5.* |
ffmpeg6 |
Compile with support for FFmpeg 6.* |
ffmpeg7 |
Compile with support for FFmpeg 7.* |
link_system_ffmpeg |
Enable probing for FFmpeg installed by your system package manager using pkg-config. |
link_vcpkg_ffmpeg |
Enable probing for FFmpeg using the vcpkg crate. |
None of the features are enabled by default. If no ffmpeg* feature is chosen, then support for FFmpeg 4.* will be compiled.
The vcpkg triplet controls the CPU architecture, operating system, runtime library, etc. It is very important that you choose a triplet that is known to be compatible with your Rust toolchain targets and configuration in order to avoid undefined behavior.
This table provides a quick reference for what vcpkg triplet to choose:
| Rust toolchain target | FFmpeg linking | CRT linking | vcpkg triplet |
|---|---|---|---|
x86_64-pc-windows-msvc |
Static | Static | x64-windows-static |
x86_64-pc-windows-msvc |
Static | Dynamic | x64-windows-static-md |
x86_64-pc-windows-msvc |
Dynamic | Dynamic | x64-windows |
x86_64-unknown-linux-gnu |
Static | Dynamic | x64-linux |
x86_64-unknown-linux-gnu |
Dynamic | Dynamic | x64-linux-dynamic |
aarch64-apple-darwin |
Static | Dynamic | arm64-osx |
aarch64-apple-darwin |
Dynamic | Dynamic | arm64-osx-dynamic |
- Rust toolchain target: View Rust toolchain targets using
rustup target list. - CRT linking: The C runtime can be dynamically or statically linked to the code compiled by Rust. The
crt-statictarget feature controls this; the default varies by target. On Windows, macOS, and Linux with glibc, dynamic linking is the Rust default. - vcpkg triplet: The vcpkg triplet that you should use for the given Rust target and CRT linking. Typically set using the
--tripletparameter.
FFI is not that easy, especially when you are dealing with a big old C project. Don't get discouraged if you encounter some problems. The CI check already has some typical ffmpeg compilation and use cases for you to check. File an issue if you still have any problems.