MPV plugins in Rust
Bindings for libmpv client API that allow you to create plugins for MPV in Rust.
⚠️ About this Fork: This is a maintained fork of the original TheCactusVert/mpv-client.
Key improvements in this fork:
- Windows Support: via MPV_CPLUGIN_DYNAMIC_SYM linkage-to-libmpv.
- Android Support: via MPV_CPLUGIN_DYNAMIC_SYM linkage-to-libmpv.
- No LLVM/Clang required: Uses pregenerated bindings by default, meaning you don't need
bindgenor a local LLVM installation duringcargo build.- Ergonomic
#[mpv_client::main]Macro: Removes C-FFI boilerplate by wrapping your main function and automatically generating the underlyingmpv_open_cpluginentry point.- Built-in Configuration Parsing: Includes a seamless
read_options()helper withserdesupport to automatically merge and parse options from both configuration files (~~/script-opts/) and MPV command-line arguments.- Logging: The macro or
mp.init_loggermethod automatically initializes a custom globallogimplementation (MpvLogger) that prints colored messages to stderr and seamlessly matches/synchronizes its timestamps with MPV's native--log-filetimeline.
Example
Init plugin
Here is an example for your Cargo.toml:
[]
= "mpv-plugin"
= "0.1.0"
= "2024"
[]
= "mpv_plugin"
= ["cdylib"]
[]
= { = "2.0", = "mpv-client-cross" }
And then the code src/lib.rs:
use ;
extern "C"
or
= { = "2.0", = "mpv-client-cross", = ["macros"] }
use ;
⚠️ Important Warning: #[mpv_client::main] macro automatically initializes global logger internally. If your code already sets up a third-party logger (for example, via env_logger::init(), log4rs, or similar crates), make sure to remove or disable its initialization. Attempting to initialize a global logger twice will result in a runtime error (application panic).
Read config options
First, define your configuration structure using serde deserialization. It is highly recommended to use #[serde(default)] so the plugin can gracefully fall back to default values if options are missing from the configuration files.
use Deserialize;
use ;
You can call read_options() directly on your Handle instance. The method automatically infers the plugin name and looks up settings from both configuration files and the command line.