tauri_plugin_use_ffmpeg/lib.rs
1//! # Tauri Plugin Use FFmpeg
2//!
3//! 一个为 Tauri v2 桌面应用提供使用 FFmpeg 功能的插件,支持自动下载和管理 FFmpeg 二进制文件。
4//!
5//! ## 功能特性
6//!
7//! - ✅ 无需预装 FFmpeg
8//! - ✅ 自动下载并解压 FFmpeg
9//! - ✅ 支持桌面平台:macOS、Windows、Linux
10//! - ✅ 实时下载进度监听
11//! - ✅ FFmpeg 可用性检查(包含路径和版本信息)
12//! - ✅ 执行任意 FFmpeg 命令
13//! - ✅ 删除已下载的 FFmpeg
14//! - ✅ 完整的 TypeScript 类型支持
15//!
16//! ## 使用方法
17//!
18//! ### 初始化插件
19//!
20//! ```rust,ignore
21//! use tauri_plugin_use_ffmpeg::init;
22//!
23//! fn main() {
24//! tauri::Builder::default()
25//! .plugin(init())
26//! .run(tauri::generate_context!())
27//! .expect("error while running tauri application");
28//! }
29//! ```
30//!
31//! ### 权限配置
32//!
33//! 在 `src-tauri/capabilities/default.json` 中添加:
34//!
35//! ```json
36//! {
37//! "permissions": [
38//! "core:default",
39//! "use-ffmpeg:default"
40//! ]
41//! }
42//! ```
43
44use tauri::{
45 plugin::{Builder, TauriPlugin},
46 Manager, Runtime,
47};
48
49pub use models::*;
50
51mod commands;
52mod desktop;
53mod error;
54mod models;
55
56pub use error::{Error, Result};
57
58use desktop::Ffmpeg;
59
60/// Extensions to [`tauri::App`], [`tauri::AppHandle`] and [`tauri::Window`] to access the ffmpeg APIs.
61pub trait FfmpegExt<R: Runtime> {
62 fn ffmpeg(&self) -> &Ffmpeg<R>;
63}
64
65impl<R: Runtime, T: Manager<R>> crate::FfmpegExt<R> for T {
66 fn ffmpeg(&self) -> &Ffmpeg<R> {
67 self.state::<Ffmpeg<R>>().inner()
68 }
69}
70
71/// Initializes the plugin.
72///
73/// # Example
74///
75/// ```rust,ignore
76/// use tauri_plugin_use_ffmpeg::init;
77///
78/// fn main() {
79/// tauri::Builder::default()
80/// .plugin(init())
81/// .run(tauri::generate_context!())
82/// .expect("error while running tauri application");
83/// }
84/// ```
85pub fn init<R: Runtime>() -> TauriPlugin<R> {
86 Builder::new("use-ffmpeg")
87 .invoke_handler(tauri::generate_handler![
88 commands::check,
89 commands::download,
90 commands::execute,
91 commands::remove
92 ])
93 .setup(|app, api| {
94 let ffmpeg = desktop::init(app, api)?;
95 app.manage(ffmpeg);
96 Ok(())
97 })
98 .build()
99}