网站首页 > 技术教程 正文
2023-04-21:用go语言重写ffmpeg的metadata.c示例。
答案2023-04-21:
这段 Go 代码演示了如何使用 `ffmpeg-go` 库中的函数来读取多媒体文件元数据,包括视频、音频等信息。它的大体过程如下:
1. 设置环境变量以加载 FFmpeg 动态链接库
这里将 FFmpeg 库中的各个动态链接库路径添加到环境变量 `PATH` 中,以便在程序运行时能够自动加载。同时,通过 `ffcommon` 包中提供的函数设置各个库的路径。
2. 创建一个输出目录
如果指定的输出目录不存在,则创建一个新的目录用于存储输出文件。
3. 打开输入文件并查找流信息
使用 `libavformat.AvformatOpenInput()` 函数打开用户指定的输入文件,并将返回的 `AVFormatContext` 结构体指针赋值给 `fmt_ctx` 变量。然后调用 `fmt_ctx.AvformatFindStreamInfo(nil)` 函数查找输入文件中的流信息。
4. 遍历元数据并输出
使用 `fmt_ctx.Metadata.AvDictGet()` 函数获取输入文件中的元数据。该函数返回指向 `AVDictionaryEntry` 结构体的指针,其中包含键值对形式的元数据信息。使用 `for` 循环遍历所有元数据,并使用 `fmt.Printf()` 函数输出每个元数据的键值对。
5. 关闭输入文件
使用 `libavformat.AvformatCloseInput(&fmt_ctx)` 函数关闭输入文件并释放内存。
使用github/moonfdd/ffmpeg-go库。
# 命令如下:
go run ./examples/internalexamples/metadata/main.go ./resources/big_buck_bunny.mp4
# golang代码如下:
package main
import (
"fmt"
"os"
"github.com/moonfdd/ffmpeg-go/ffcommon"
"github.com/moonfdd/ffmpeg-go/libavformat"
"github.com/moonfdd/ffmpeg-go/libavutil"
)
func main() {
// go run ./examples/internalexamples/metadata/main.go ./resources/big_buck_bunny.mp4
os.Setenv("Path", os.Getenv("Path")+";./lib")
ffcommon.SetAvutilPath("./lib/avutil-56.dll")
ffcommon.SetAvcodecPath("./lib/avcodec-58.dll")
ffcommon.SetAvdevicePath("./lib/avdevice-58.dll")
ffcommon.SetAvfilterPath("./lib/avfilter-56.dll")
ffcommon.SetAvformatPath("./lib/avformat-58.dll")
ffcommon.SetAvpostprocPath("./lib/postproc-55.dll")
ffcommon.SetAvswresamplePath("./lib/swresample-3.dll")
ffcommon.SetAvswscalePath("./lib/swscale-5.dll")
genDir := "./out"
_, err := os.Stat(genDir)
if err != nil {
if os.IsNotExist(err) {
os.Mkdir(genDir, 0777) // Everyone can read write and execute
}
}
main0()
}
func main0() (ret ffcommon.FInt) {
var fmt_ctx *libavformat.AVFormatContext
var tag *libavutil.AVDictionaryEntry
if len(os.Args) != 2 {
fmt.Printf("usage: %s <input_file>\nexample program to demonstrate the use of the libavformat metadata API.\n\n", os.Args[0])
return 1
}
ret = libavformat.AvformatOpenInput(&fmt_ctx, os.Args[1], nil, nil)
if ret != 0 {
return ret
}
ret = fmt_ctx.AvformatFindStreamInfo(nil)
if ret < 0 {
libavutil.AvLog(uintptr(0), libavutil.AV_LOG_ERROR, "Cannot find stream information\n")
return ret
}
tag = fmt_ctx.Metadata.AvDictGet("", tag, libavutil.AV_DICT_IGNORE_SUFFIX)
for tag != nil {
fmt.Printf("%s=%s\n", ffcommon.StringFromPtr(tag.Key), ffcommon.StringFromPtr(tag.Value))
tag = fmt_ctx.Metadata.AvDictGet("", tag, libavutil.AV_DICT_IGNORE_SUFFIX)
}
libavformat.AvformatCloseInput(&fmt_ctx)
return 0
}
猜你喜欢
- 2024-11-24 Ubuntu20编译FFmpeg4(非常简单)
- 2024-11-24 基于 FFmpeg 的播放器 demo
- 2024-11-24 2023-02-22:请用go语言调用ffmpeg,保存mp4文件的视频帧,每帧
- 2024-11-24 2023-02-25:请用go语言调用ffmpeg,解码mp4文件并保存为YUV420S
- 2024-11-24 2023-02-24:请用go语言调用ffmpeg,解码mp4文件并保存为YUV420S
- 2024-11-24 2023-02-21:请用go语言调用ffmpeg,解码mp4文件,输出视频信息
- 2024-11-24 搭载Android11的ZidooM6预览版
- 2024-11-24 2023-03-21:音视频解混合(demuxer)为MP3和H264,用go语言编写
- 2024-11-24 3分钟快速实现Flutter当中播放视频方案 支持Android iOS 通用
- 2024-11-24 2023-03-17:使用Go语言和FFmpeg库实现音频重采样解码,并将其保
你 发表评论:
欢迎- 08-06linux 和 windows文件格式互相转换
- 08-06谷歌 ChromeOS 已支持 7z、iso、tar 文件格式
- 08-06Linux下比较文件内容的6种方法
- 08-06文件格式及功能汇总
- 08-0610个Linux文件内容查看命令的实用示例
- 08-06Linux-如何区分不同文件类型
- 08-06Zabbix技术分享——监控windows进程资源使用情况
- 08-06Linux系统卡顿?学会ps命令这三招,轻松定位问题进程
- 最近发表
- 标签列表
-
- 下划线是什么 (87)
- 精美网站 (58)
- qq登录界面 (90)
- nginx 命令 (82)
- nginx .http (73)
- nginx lua (70)
- nginx 重定向 (68)
- Nginx超时 (65)
- nginx 监控 (57)
- odbc (59)
- rar密码破解工具 (62)
- annotation (71)
- 红黑树 (57)
- 智力题 (62)
- php空间申请 (61)
- 按键精灵 注册码 (69)
- 软件测试报告 (59)
- ntcreatefile (64)
- 闪动文字 (56)
- guid (66)
- abap (63)
- mpeg 2 (65)
- column (63)
- dreamweaver教程 (57)
- excel行列转换 (56)
本文暂时没有评论,来添加一个吧(●'◡'●)