2023-02-21:请用go语言调用ffmpeg,解码mp4文件,输出视频信息和总帧数。
2023-06-13 09:17:08 时间
2023-02-21:请用go语言调用ffmpeg,解码mp4文件,输出视频信息和总帧数。
答案2023-02-21:
使用 github.com/moonfdd/ffmpeg-go 库,这个库比goav还好用。
代码根据05:解码视频流过程用golang编写。
执行命令:
go run ./examples/a05video_decode_flow/main.go
代码用golang编写。代码如下:
package main
import (
"fmt"
"os"
"github.com/moonfdd/ffmpeg-go/ffcommon"
"github.com/moonfdd/ffmpeg-go/libavcodec"
"github.com/moonfdd/ffmpeg-go/libavformat"
"github.com/moonfdd/ffmpeg-go/libavutil"
)
func main() {
os.Setenv("Path", os.Getenv("Path")+";./lib")
ffcommon.SetAvutilPath("./lib/avutil-56.dll")
ffcommon.SetAvcodecPath("./lib/avcodec-58.dll")
ffcommon.SetAvdevicePath("./lib/avdevice-56.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")
filePath := "./resources/big_buck_bunny.mp4" //文件地址
videoStreamIndex := -1 //视频流所在流序列中的索引
ret := int32(0) //默认返回值
//需要的变量名并初始化
var fmtCtx *libavformat.AVFormatContext
var pkt *libavcodec.AVPacket
var codecCtx *libavcodec.AVCodecContext
var avCodecPara *libavcodec.AVCodecParameters
var codec *libavcodec.AVCodec
libavformat.AvformatNetworkInit()
for {
//=========================== 创建AVFormatContext结构体 ===============================//
//分配一个AVFormatContext,FFMPEG所有的操作都要通过这个AVFormatContext来进行
fmtCtx = libavformat.AvformatAllocContext()
//==================================== 打开文件 ======================================//
ret = libavformat.AvformatOpenInput(&fmtCtx, filePath, nil, nil)
if ret != 0 {
fmt.Printf("cannot open video file\n")
break
}
//=================================== 获取视频流信息 ===================================//
ret = fmtCtx.AvformatFindStreamInfo(nil)
if ret < 0 {
fmt.Printf("cannot retrive video info\n")
break
}
//循环查找视频中包含的流信息,直到找到视频类型的流
//便将其记录下来 保存到videoStreamIndex变量中
for i := uint32(0); i < fmtCtx.NbStreams; i++ {
if fmtCtx.GetStream(i).Codecpar.CodecType == libavutil.AVMEDIA_TYPE_VIDEO {
videoStreamIndex = int(i)
break //找到视频流就退出
}
}
//如果videoStream为-1 说明没有找到视频流
if videoStreamIndex == -1 {
fmt.Printf("cannot find video stream\n")
break
}
//打印输入和输出信息:长度 比特率 流格式等
fmtCtx.AvDumpFormat(0, filePath, 0)
//================================= 查找解码器 ===================================//
avCodecPara = fmtCtx.GetStream(uint32(videoStreamIndex)).Codecpar
codec = libavcodec.AvcodecFindDecoder(avCodecPara.CodecId)
if codec == nil {
fmt.Printf("cannot find decoder\n")
break
}
//根据解码器参数来创建解码器内容
codecCtx = codec.AvcodecAllocContext3()
codecCtx.AvcodecParametersToContext(avCodecPara)
if codecCtx == nil {
fmt.Printf("Cannot alloc context.")
break
}
//================================ 打开解码器 ===================================//
ret = codecCtx.AvcodecOpen2(codec, nil)
if ret < 0 { // 具体采用什么解码器ffmpeg经过封装 我们无须知道
fmt.Printf("cannot open decoder\n")
break
}
//=========================== 分配AVPacket结构体 ===============================//
i := 0 //用于帧计数
pkt = libavcodec.AvPacketAlloc() //分配一个packet
pkt.AvNewPacket(codecCtx.Width * codecCtx.Height) //调整packet的数据
//=========================== 读取视频信息 ===============================//
for fmtCtx.AvReadFrame(pkt) >= 0 { //读取的是一帧视频 数据存入一个AVPacket的结构中
if pkt.StreamIndex == uint32(videoStreamIndex) {
i++ //只计算视频帧
}
pkt.AvPacketUnref() //重置pkt的内容
}
fmt.Printf("There are %d frames int total.\n", i)
break
}
//===========================释放所有指针===============================//
libavcodec.AvPacketFree(&pkt)
codecCtx.AvcodecClose()
libavformat.AvformatCloseInput(&fmtCtx)
fmtCtx.AvformatFreeContext()
}
相关文章
- Golang的strings.go源码解析 - Rabin-Karp了解一下?
- Go语言基础语法之import别名(七)
- Web框架的设计方案和Go源码实现
- 2022-10-26:以下go语言代码输出什么?A:1 3 2;B:1 2 3;C:3 1 2;D:3 2 1。 package main import “fm
- 2023-03-05:ffmpeg推送本地视频至lal流媒体服务器(以RTMP为例),请用go语言编写。
- Go-包管理-go install
- Go语言go mod包依赖管理工具使用详解
- Go语言文件的写入、追加、读取、复制操作
- go clean命令——清除编译文件
- go fmt命令——格式化代码文件
- Go语言圣经–Channel习题详解编程语言
- Go语言圣经-指针对象的方法-bit数组习题详解编程语言
- go语言圣经-声明,变量,赋值,类型,包和文件习题详解编程语言
- go语言之goto语句和函数和defer语句详解编程语言
- Go语言连接Oracle数据库实战(go连接oracle)
- 如何用 GVM 管理 Go 项目
- 从2份专利文件,一窥Amazon Go到底藏了什么黑科技?
- 便捷使用Go编程连接Redis(go连接redis)
- 徒手用Go编写Redis迈向新技术世界的旅程(徒手用go写个redis)
- Go语言链接Oracle数据库的实践记录(go 链接oracle)
- Go语言操作Oracle轻松实现数据库编程(go语言访问oracle)