将 ffmpeg 和 ffprobe 静态二进制文件嵌入到你的 Go 程序中,无需预安装即可使用。
本项目灵感来自 ffmpeg-static,使用 jellyfin-ffmpeg的 portable 版本。
使用 Go embed 将 ffmpeg/ffprobe 二进制文件作为字节嵌入到 Go 程序中。程序运行时会自动将可执行文件释放到临时目录。
通过此包,您可以在无需预装 ffmpeg/ffprobe 的情况下使用它们,这对于在无服务器平台(如 Lambda/Vercel)上运行程序非常有用。
直接使用统一的包,它会根据您的操作系统和架构自动选择合适的二进制文件:
go get -u cnb.cool/zishuo/ffstatic
如果需要,也可以单独安装特定平台的包:
Darwin (macOS)
go get -u cnb.cool/zishuo/ffstatic/darwin-amd64
go get -u cnb.cool/zishuo/ffstatic/darwin-arm64
Linux
go get -u cnb.cool/zishuo/ffstatic/linux-amd64
go get -u cnb.cool/zishuo/ffstatic/linux-arm64
Windows
go get -u cnb.cool/zishuo/ffstatic/windows-amd64
go get -u cnb.cool/zishuo/ffstatic/windows-arm64
package main
import (
"fmt"
"os/exec"
"cnb.cool/zishuo/ffstatic"
)
func main() {
// 获取 ffmpeg 和 ffprobe 的路径
ffmpegPath := ffstatic.FFmpegPath()
ffprobePath := ffstatic.FFprobePath()
fmt.Println("FFmpeg path:", ffmpegPath)
fmt.Println("FFprobe path:", ffprobePath)
// 使用 ffmpeg
cmd := exec.Command(ffmpegPath, "-version")
output, err := cmd.CombinedOutput()
if err != nil {
panic(err)
}
fmt.Println(string(output))
}
使用统一入口包时,无需任何额外配置即可实现跨平台支持。包会自动根据编译目标平台选择正确的二进制文件。
例如,在 macOS 上开发,部署到 Linux:
# 本地开发(macOS)
go run main.go
# 编译为 Linux 可执行文件
GOOS=linux GOARCH=amd64 go build -o app main.go
# 编译为 Windows 可执行文件
GOOS=windows GOARCH=amd64 go build -o app.exe main.go
您也可以配合这些 ffmpeg 封装库使用:
MIT