GO调用FFmpeg动态库

作者:罗上文,微信:Loken1,公众号:FFmpeg弦外之音

GO 可以用过 cgo 的方式调用 FFmpeg 的动态库,具体用法如下:

package main

/*
#cgo CFLAGS: -I/usr/include -I./build64/ffmpeg-n4.4.1/include
#cgo LDFLAGS: -L/usr/lib -L./build64/ffmpeg-n4.4.1/lib -lavformat -lavcodec -lavutil -lswresample
#include <stdio.h>
#include "libavformat/avformat.h"
*/
import "C"

import (
"fmt"
)

func main() {
    ver := C.avformat_version()
    fmt.Println("ffmpeg libavformat version is ",ver)
}

就是在 import "C" 前面加入一段编译链接的代码就行了,这段代码虽然是用 /**/ 注释包起来的,但他不是真正意义上的注释,go 编译器会识别到这段代码,然后应用的。

完整的项目代码,读者可以在 GitHub 里找到,这个项目代码,请在 Ubuntu22 系统上进行编译。编译命令如下:

备注:项目目录:

E:\0-博客\ffmpeg_principle_volume1\source\code\ffmpeg_go
go build main.go

编译完成之后,要设置一下环境变量 LD_LIBRARY_PATH,这样 main 运行的时候才能找到对应的动态库。

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:./build64/ffmpeg-n4.4.1/lib

运行效果如下图:

1-1

思考:如何把 go 的函数作为一个指针传递给 FFmpeg 调用?


参考资料:

1,《Go语言高级编程》- 2.2 章节

版权所属 xianwanzhiyin.net 罗上文 2024 all right reserved,powered by Gitbook该文件修订时间: 2024-07-04 00:08:26

results matching ""

    No results matching ""