VideoConcat/wails/resources/ffmpeg/平台特定嵌入说明.md
2026-01-08 13:25:06 +08:00

150 lines
4.3 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 平台特定 FFmpeg 嵌入说明
## 概述
项目使用 **Go 构建标签Build Tags** 实现平台特定的 FFmpeg 嵌入,确保每个平台的构建只包含对应平台的二进制文件。
## 工作原理
### 构建标签文件
项目包含以下平台特定文件:
- `ffmpeg_darwin.go` - macOS 平台(`//go:build darwin`
- `ffmpeg_windows.go` - Windows 平台(`//go:build windows`
- `ffmpeg_linux.go` - Linux 平台(`//go:build linux`
- `ffmpeg_default.go` - 其他平台(向后兼容)
### 编译时的行为
Go 编译器会根据当前编译目标平台(`GOOS`)自动选择匹配的文件:
| 编译平台 | 使用文件 | 嵌入目录 | 结果 |
|---------|---------|---------|------|
| `GOOS=darwin` | `ffmpeg_darwin.go` | `resources/ffmpeg/darwin/*` | 只包含 macOS 版本 |
| `GOOS=windows` | `ffmpeg_windows.go` | `resources/ffmpeg/windows/*` | 只包含 Windows 版本 |
| `GOOS=linux` | `ffmpeg_linux.go` | `resources/ffmpeg/linux/*` | 只包含 Linux 版本 |
## 目录结构
```
wails/
├── main.go # 主文件(声明 getEmbeddedFFmpeg 函数)
├── ffmpeg_darwin.go # macOS 实现
├── ffmpeg_windows.go # Windows 实现
├── ffmpeg_linux.go # Linux 实现
├── ffmpeg_default.go # 默认实现(向后兼容)
└── resources/
└── ffmpeg/
├── darwin/ # macOS 二进制文件
│ ├── ffmpeg
│ └── ffprobe
├── windows/ # Windows 二进制文件
│ ├── ffmpeg.exe
│ └── ffprobe.exe
└── linux/ # Linux 二进制文件
├── ffmpeg
└── ffprobe
```
## 使用方法
### 1. 准备二进制文件
将各平台的 FFmpeg 二进制文件放置到对应目录:
```bash
# macOS
cp /opt/homebrew/bin/ffmpeg wails/resources/ffmpeg/darwin/ffmpeg
cp /opt/homebrew/bin/ffprobe wails/resources/ffmpeg/darwin/ffprobe
# Windows
copy ffmpeg.exe wails\resources\ffmpeg\windows\ffmpeg.exe
copy ffprobe.exe wails\resources\ffmpeg\windows\ffprobe.exe
# Linux
cp ffmpeg wails/resources/ffmpeg/linux/ffmpeg
cp ffprobe wails/resources/ffmpeg/linux/ffprobe
```
### 2. 编译不同平台
```bash
# macOS 版本(只嵌入 darwin/ 目录)
GOOS=darwin go build -o VideoConcat-mac
# Windows 版本(只嵌入 windows/ 目录)
GOOS=windows GOARCH=amd64 go build -o VideoConcat.exe
# Linux 版本(只嵌入 linux/ 目录)
GOOS=linux GOARCH=amd64 go build -o VideoConcat-linux
```
### 3. 验证嵌入内容
编译后,可以使用以下方法验证:
```bash
# macOS/Linux
strings VideoConcat | grep -i ffmpeg | head -5
# Windows (使用 PowerShell)
Select-String -Path VideoConcat.exe -Pattern "ffmpeg" | Select-Object -First 5
```
## 优势
### 1. 减小文件体积
- 只嵌入当前平台的二进制文件
- 减少不必要的文件大小增加
- 示例:如果每个平台 FFmpeg 是 50MB使用平台特定嵌入后
- 之前:所有平台 = 150MB三个平台都包含
- 现在:每个平台 = 50MB只包含对应平台
### 2. 避免交叉编译问题
- 编译 Windows 版本时不会误嵌入 macOS 版本
- 编译 Linux 版本时不会误嵌入 Windows 版本
- 避免运行时找不到正确文件的问题
### 3. 清晰的目录组织
- 各平台文件分开管理
- 易于维护和更新
- 符合 Go 的最佳实践
## 故障排查
### 问题:编译时找不到文件
**原因**:对应平台的目录中没有二进制文件
**解决**
```bash
# 检查文件是否存在
ls -la wails/resources/ffmpeg/darwin/
ls -la wails/resources/ffmpeg/windows/
ls -la wails/resources/ffmpeg/linux/
```
### 问题:交叉编译时仍嵌入所有文件
**原因**:使用了旧的目录结构(根目录下的文件)
**解决**:确保使用平台特定目录(`darwin/`, `windows/`, `linux/`
### 问题:运行时找不到 FFmpeg
**检查**
1. 查看应用日志,确认 FFmpeg 查找路径
2. 检查临时目录中是否成功提取
3. 验证二进制文件是否有执行权限
## 向后兼容
如果不想使用平台特定目录,仍然可以使用根目录放置文件(`resources/ffmpeg/ffmpeg`),但这样会:
- 嵌入所有平台的二进制文件
- 增加不必要的文件大小
- 不推荐使用