2023-03-30:用Go语言改写FFmpeg示例decode_audio.c,实现高效音频解码。
2023-03-30:用Go语言改写FFmpeg示例decode_audio.c,实现高效音频解码。
答案2023-03-30:
这个程序的主要功能是将 MP2 音频文件解码为 PCM 格式,并输出到指定的输出文件中。下面是该程序的详细步骤:
1.导入所需的包
通过import语句导入了一些第三方库和FFmpeg相关的包。
2.定义变量
定义了一些必要的变量和常量,如输入和输出文件名、音频编解码器、编解码器上下文、音频解析器上下文、解析缓冲区、音频数据帧、采样格式等。
3.解析命令行参数
读取命令行传入的输入文件名和输出文件名。
4.初始化解析器和编码器
通过 AVCodecFindDecoder() 函数查找 MPEG 音频解码器并得到其指针,如果为空则表示未找到对应的解码器。接着调用 AVParserInit() 函数初始化一个解析器,用于从输入文件中解析出音频数据帧。同时也需要分配一个编解码器上下文(AVCodecContext)对象,并调用 AVCodecOpen2() 函数打开编解码器。
5.打开输入文件和输出文件
使用 os.Open() 函数打开输入文件,如果失败则退出程序。使用 os.Create() 函数创建输出文件,如果失败则需要释放相关资源并退出程序。
6.逐帧解码
循环读取输入文件,每次读取 AUDIO_INBUF_SIZE 大小的数据,然后使用 AVParserParse2() 函数将数据解析成音频数据帧 AVPacket,并调用解码函数 decode() 进行解码,将解码后的 PCM 数据输出到输出文件中。读取结束时需要调用 AVCodecSendPacket() 函数和 AVCodecReceiveFrame() 函数进行“flush”,以确保所有剩余的音频数据帧都被解码。
7.输出 PCM 文件信息
在程序结束前,输出 PCM 文件的格式信息(包括采样率、声道数、采样格式等),以供用户使用 ffplay 命令播放该文件。
8.释放资源
关闭输入文件和输出文件,释放编解码器上下文、解析器上下文、解析缓冲区、音频数据帧以及 AVPacket 等资源。
总体来说,这个程序通过FFmpeg库提供的API从输入文件中逐帧解码音频数据,并将解码后的PCM数据输出到指定的输出文件中。此外,它还提供了一些基本的错误处理和输出格式信息的功能。
执行命令:
./lib/ffmpeg -i ./resources/big_buck_bunny.mp4 -c:a mp2 ./out/big_buck_bunny.mp2
go run ./examples/internalexamples/decode_audio/main.go ./out/big_buck_bunny.mp2 ./out/big_buck_bunny.pcm
./lib/ffplay -f s16le -ac 2 -ar 22050 ./out/big_buck_bunny.pcm
golang代码如下:
package main
import (
"fmt"
"os"
"unsafe"
"github.com/moonfdd/ffmpeg-go/ffcommon"
"github.com/moonfdd/ffmpeg-go/libavcodec"
"github.com/moonfdd/ffmpeg-go/libavutil"
)
func main0() (ret ffcommon.FInt) {
// ./lib/ffmpeg -i ./resources/big_buck_bunny.mp4 -c:a mp2 ./out/big_buck_bunny.mp2
// go run ./examples/internalexamples/decode_audio/main.go ./out/big_buck_bunny.mp2 ./out/big_buck_bunny.pcm
// ./lib/ffplay -f s16le -ac 2 -ar 22050 ./out/big_buck_bunny.pcm
var outfilename, filename string
var codec *libavcodec.AVCodec
var c *libavcodec.AVCodecContext
var parser *libavcodec.AVCodecParserContext
var len0 ffcommon.FInt
var f, outfile *os.File
var inbuf [AUDIO_INBUF_SIZE + libavcodec.AV_INPUT_BUFFER_PADDING_SIZE]ffcommon.FUint8T
var data *ffcommon.FUint8T
var data_size ffcommon.FSizeT
var pkt *libavcodec.AVPacket
var decoded_frame *libavutil.AVFrame
var sfmt libavutil.AVSampleFormat
var n_channels ffcommon.FInt = 0
var fmt0 string
if len(os.Args) <= 2 {
fmt.Printf("Usage: %s <input file> <output file>\n", os.Args[0])
os.Exit(0)
}
filename = os.Args[1]
outfilename = os.Args[2]
pkt = libavcodec.AvPacketAlloc()
/* find the MPEG audio decoder */
codec = libavcodec.AvcodecFindDecoder(libavcodec.AV_CODEC_ID_MP2)
if codec == nil {
fmt.Printf("Codec not found\n")
os.Exit(1)
}
parser = libavcodec.AvParserInit(int32(codec.Id))
if parser == nil {
fmt.Printf("Parser not found\n")
os.Exit(1)
}
c = codec.AvcodecAllocContext3()
if c == nil {
fmt.Printf("Could not allocate audio codec context\n")
os.Exit(1)
}
/* open it */
if c.AvcodecOpen2(codec, nil) < 0 {
fmt.Printf("Could not open codec\n")
os.Exit(1)
}
var err error
f, err = os.Open(filename)
if err != nil {
fmt.Printf("Could not open %s\n", filename)
os.Exit(1)
}
outfile, err = os.Create(outfilename)
if err != nil {
libavutil.AvFree(uintptr(unsafe.Pointer(c)))
os.Exit(1)
}
/* decode until eof */
data = (*byte)(unsafe.Pointer(&inbuf))
var n int
n, _ = f.Read(inbuf[0:AUDIO_INBUF_SIZE])
data_size = uint64(n)
for data_size > 0 {
if decoded_frame == nil {
decoded_frame = libavutil.AvFrameAlloc()
if decoded_frame == nil {
fmt.Printf("Could not allocate audio frame\n")
os.Exit(1)
}
}
ret = parser.AvParserParse2(c, &pkt.Data, (*int32)(unsafe.Pointer(&pkt.Size)),
data, int32(data_size),
libavutil.AV_NOPTS_VALUE, libavutil.AV_NOPTS_VALUE, 0)
if ret < 0 {
fmt.Printf("Error while parsing\n")
os.Exit(1)
}
data = (*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(data)) + uintptr(ret)))
data_size -= uint64(ret)
if pkt.Size != 0 {
decode(c, pkt, decoded_frame, outfile)
}
if data_size < AUDIO_REFILL_THRESH {
for i := uint64(0); i < data_size; i++ {
inbuf[i] = *(*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(data)) + uintptr(i)))
}
data = (*byte)(unsafe.Pointer(&inbuf))
n, _ = f.Read(inbuf[data_size:AUDIO_INBUF_SIZE])
len0 = int32(n)
if len0 > 0 {
data_size += uint64(len0)
}
}
}
/* flush the decoder */
pkt.Data = nil
pkt.Size = 0
decode(c, pkt, decoded_frame, outfile)
/* print output pcm infomations, because there have no metadata of pcm */
sfmt = c.SampleFmt
if libavutil.AvSampleFmtIsPlanar(sfmt) != 0 {
packed := libavutil.AvGetSampleFmtName(sfmt)
pa := ""
if packed == "" {
pa = "?"
} else {
pa = packed
}
fmt.Printf("Warning: the sample format the decoder produced is planar (%s). This example will output the first channel only.\n", pa)
sfmt = libavutil.AvGetPackedSampleFmt(sfmt)
}
n_channels = c.Channels
for {
ret = get_format_from_sample_fmt(&fmt0, sfmt)
if ret < 0 {
break
}
fmt.Printf("Play the output audio file with the command:\nffplay -f %s -ac %d -ar %d %s\n",
fmt0, n_channels, c.SampleRate,
outfilename)
break
}
// end:
outfile.Close()
f.Close()
libavcodec.AvcodecFreeContext(&c)
parser.AvParserClose()
libavutil.AvFrameFree(&decoded_frame)
libavcodec.AvPacketFree(&pkt)
return 0
}
const AUDIO_INBUF_SIZE = 20480
const AUDIO_REFILL_THRESH = 4096
func get_format_from_sample_fmt(fmt0 *string, sample_fmt libavutil.AVSampleFormat) (ret ffcommon.FInt) {
switch sample_fmt {
case libavutil.AV_SAMPLE_FMT_U8:
*fmt0 = "u8"
case libavutil.AV_SAMPLE_FMT_S16:
*fmt0 = "s16le"
case libavutil.AV_SAMPLE_FMT_S32:
*fmt0 = "s32le"
case libavutil.AV_SAMPLE_FMT_FLT:
*fmt0 = "f32le"
case libavutil.AV_SAMPLE_FMT_DBL:
*fmt0 = "f64le"
default:
fmt.Printf("sample format %s is not supported as output format\n",
libavutil.AvGetSampleFmtName(sample_fmt))
ret = -1
}
return
}
func decode(dec_ctx *libavcodec.AVCodecContext, pkt *libavcodec.AVPacket, frame *libavutil.AVFrame, outfile *os.File) {
var i, ch ffcommon.FInt
var ret, data_size ffcommon.FInt
/* send the packet with the compressed data to the decoder */
ret = dec_ctx.AvcodecSendPacket(pkt)
if ret < 0 {
fmt.Printf("Error submitting the packet to the decoder\n")
os.Exit(1)
}
/* read all the output frames (in general there may be any number of them */
for ret >= 0 {
ret = dec_ctx.AvcodecReceiveFrame(frame)
if ret == -libavutil.EAGAIN || ret == libavutil.AVERROR_EOF {
return
} else if ret < 0 {
fmt.Printf("Error during decoding\n")
os.Exit(1)
}
data_size = libavutil.AvGetBytesPerSample(dec_ctx.SampleFmt)
if data_size < 0 {
/* This should not occur, checking just for paranoia */
fmt.Printf("Failed to calculate data size\n")
os.Exit(1)
}
bytes := []byte{}
for i = 0; i < frame.NbSamples; i++ {
for ch = 0; ch < dec_ctx.Channels; ch++ {
ptr := uintptr(unsafe.Pointer(frame.Data[ch])) + uintptr(data_size*i)
for k := int32(0); k < data_size; k++ {
bytes = append(bytes, *(*byte)(unsafe.Pointer(ptr)))
ptr++
}
}
}
outfile.Write(bytes)
}
}
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-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()
}

2023-03-30:用Go语言改写FFmpeg示例decode_audio.c,实现高效音频解码。的更多相关文章
- FFmpeg简易播放器的实现-音频播放
本文为作者原创,转载请注明出处:https://www.cnblogs.com/leisure_chn/p/10068490.html 基于FFmpeg和SDL实现的简易视频播放器,主要分为读取视频文 ...
- Qt与FFmpeg联合开发指南(一)——解码(1):功能实现
前言:对于从未接触过音视频编解码的同学来说,使用FFmpeg的学习曲线恐怕略显陡峭.本人由于工作需要,正好需要在项目中使用.因此特地将开发过程总结下来.只当提供给有兴趣的同学参考和学习. 由于FFmp ...
- EasyDarwin开源音频解码项目EasyAudioDecoder:基于ffmpeg的安卓音频(AAC、G726)解码库(第一部分,ffmpeg-android的编译)
ffmpeg是一套开源的,完整的流媒体解决方案.基于它可以很轻松构建一些强大的应用程序.对于流媒体这个行业,ffmpeg就像圣经一样的存在.为了表达敬意,在这里把ffmpeg官网的一段简介搬过来,ff ...
- 【项目分析】利用C#改写JAVA中的Base64.DecodeBase64以及Inflater解码
原文:[项目分析]利用C#改写JAVA中的Base64.DecodeBase64以及Inflater解码 最近正在进行项目服务的移植工作,即将JAVA服务的程序移植到DotNet平台中. 在JAVA程 ...
- FFmpeg示例程序合集-批量编译脚本
此前做了一系列有关FFmpeg的示例程序,组成了<最简单的FFmpeg示例程序合集>,其中包含了如下项目:simplest ffmpeg player: ...
- FFmpeg示例程序合集-Git批量获取脚本
此前做了一系列有关FFmpeg的示例程序,组成了<FFmpeg示例程序合集>,其中包含了如下项目:simplest ffmpeg player: 最简单的 ...
- FFmpeg 裁剪——音频解码
配置ffmpeg,只留下某些音频的配置: ./configure --enable-shared --disable-yasm --enable-memalign-hack --enable-gpl ...
- ffmpeg学习笔记-音频解码
在之前的文章已经初步对视频解码有个初步的认识了,接下来来看一看音频解码 音频解码步骤 音频解码与视频解码一样,有者固有的步骤,只要按照步骤来,就能顺利的解码音频 以上是ffmpeg的解码流程图,可以看 ...
- 你可能不知道的 30 个 Python 语言的特点技巧
列表按难度排序,常用的语言特征和技巧放在前面. 1.1 分拆 >>> a, b, c = 1, 2, 3>>> a, b, c(1, 2, 3)> ...
- 你可能不知道的30个Python语言的特点技巧
1 介绍 从我开始学习Python时我就决定维护一个经常使用的“窍门”列表.不论何时当我看到一段让我觉得“酷,这样也行!”的代码时(在一个例子中.在StackOverflow.在开源码软件中,等等), ...
随机推荐
- Maven 切换JDK版本
欢迎访问我的个人博客:xie-kang.com 查看Maven安装目录的conf目录可以看到有 settings.xml\toolchains.xml文件.settings.xml主要是设置切换Mav ...
- postman 8.7.0 下的cookie 禁用
简介:以下过程描述在postman8.7.0中,如何禁用掉cookie,使每次请求都带空cookie去请求服务器. 有一个简单投票场景.投票连接是一个get请求, 类似如http://domain/t ...
- 修改/编辑jar包
替换或者导入jar包时,jar包被自动压缩,springboot规定嵌套的jar包不能在被压缩的情况下存储. 解决(本文以升级ojdbc包为例): 使用jar命令解压jar包,在压缩包外重新替换jar ...
- Jquery中,$(this)的少许获取问题
这是原始代码,结果为解禁提示框中$(this)获取不到当前元素. 如上图,在提示方法外面声明一下$(this)并赋给$this,下面调用$this,即可获取当前元素.
- 1 关于win10原生系统下 OCRmyPDF安装使用
win10原生系统下 OCRmyPDF安装使用长期以来一直在代替freepic2pdf的工具,因为在图片转化PDF时,如果没有勾选该软件 添加OCR层 选项,印象中事后无法挂OCR层上去.福昕风腾,A ...
- 打开CMD方式
打开CMD的方式 win+r 输入cmd 常用的Dos命令 1.#盘符切换2.#查看当前文件目录下的所有文件 dir3.#切换目录 cd change directory4.#cd .. 返回上级5. ...
- 理解Map-构建关联数组
理解Map 要更深入理解map,学习如何构建关联数组是很有帮助的,以下是简单实现 package org.example.onjava.senior.example03collection.map; ...
- 初认Spring
官网地址:https://spring.io/ Spring Framework的系统架构 1.Core Contiainer:核心容器 2.AOP:面向切片编程 3.Aspects:AOP思想实现 ...
- 在CentOS中安装和使用nginx
概述 本文简单讲述一下,如何快速将一个内网的Web服务通过nginx提供给外网访问,并且启用HTTPS.例如我们部署了一个kubesphere,地址为192.168.202.151:30880,需要通 ...
- 使用 zeromq与cppzmq 程序退出遇到的坑
在使用zeromq 退出的时候还遇到一点坑,对于服务deaman(守护进程)化的进程可能会遇到这个问题. 现象: 这个问题导致的现象是服务一旦关闭(stop),就会 core dump,core du ...