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,实现高效音频解码。的更多相关文章

  1. FFmpeg简易播放器的实现-音频播放

    本文为作者原创,转载请注明出处:https://www.cnblogs.com/leisure_chn/p/10068490.html 基于FFmpeg和SDL实现的简易视频播放器,主要分为读取视频文 ...

  2. Qt与FFmpeg联合开发指南(一)——解码(1):功能实现

    前言:对于从未接触过音视频编解码的同学来说,使用FFmpeg的学习曲线恐怕略显陡峭.本人由于工作需要,正好需要在项目中使用.因此特地将开发过程总结下来.只当提供给有兴趣的同学参考和学习. 由于FFmp ...

  3. EasyDarwin开源音频解码项目EasyAudioDecoder:基于ffmpeg的安卓音频(AAC、G726)解码库(第一部分,ffmpeg-android的编译)

    ffmpeg是一套开源的,完整的流媒体解决方案.基于它可以很轻松构建一些强大的应用程序.对于流媒体这个行业,ffmpeg就像圣经一样的存在.为了表达敬意,在这里把ffmpeg官网的一段简介搬过来,ff ...

  4. 【项目分析】利用C#改写JAVA中的Base64.DecodeBase64以及Inflater解码

    原文:[项目分析]利用C#改写JAVA中的Base64.DecodeBase64以及Inflater解码 最近正在进行项目服务的移植工作,即将JAVA服务的程序移植到DotNet平台中. 在JAVA程 ...

  5. FFmpeg示例程序合集-批量编译脚本

    此前做了一系列有关FFmpeg的示例程序,组成了<最简单的FFmpeg示例程序合集>,其中包含了如下项目:simplest ffmpeg player:                   ...

  6. FFmpeg示例程序合集-Git批量获取脚本

    此前做了一系列有关FFmpeg的示例程序,组成了<FFmpeg示例程序合集>,其中包含了如下项目:simplest ffmpeg player:                  最简单的 ...

  7. FFmpeg 裁剪——音频解码

    配置ffmpeg,只留下某些音频的配置: ./configure --enable-shared --disable-yasm --enable-memalign-hack --enable-gpl ...

  8. ffmpeg学习笔记-音频解码

    在之前的文章已经初步对视频解码有个初步的认识了,接下来来看一看音频解码 音频解码步骤 音频解码与视频解码一样,有者固有的步骤,只要按照步骤来,就能顺利的解码音频 以上是ffmpeg的解码流程图,可以看 ...

  9. 你可能不知道的 30 个 Python 语言的特点技巧

        列表按难度排序,常用的语言特征和技巧放在前面. 1.1   分拆 >>> a, b, c = 1, 2, 3>>> a, b, c(1, 2, 3)> ...

  10. 你可能不知道的30个Python语言的特点技巧

    1 介绍 从我开始学习Python时我就决定维护一个经常使用的“窍门”列表.不论何时当我看到一段让我觉得“酷,这样也行!”的代码时(在一个例子中.在StackOverflow.在开源码软件中,等等), ...

随机推荐

  1. Python第七章实验报告

    一.实验名称:<零基础学Python>第7章 面向对象程序设计 二.实验环境:IDLE Shell 3.9.7 三.实验内容:5道实例.4道实战 四.实验过程: 实例01 创建大雁类并定义 ...

  2. linux 命令行下适配 nvidia 驱动 - 搬运

    linux 命令行下适配 nvidia 驱动 转自:https://www.cnblogs.com/chua-n/p/13208398.html 1. 下载相应驱动 在官网这里选择适应你 GPU 的驱 ...

  3. mininet配置命令

    Mininet实验手册 一.安装 1.  直接使用带有完整软件的VM(略) 2.  源码安装 1)  下载 git clone git://github.com/mininet/mininet 2)  ...

  4. (转)解决ubuntu下拼音输入法出错的问题

    一.如果仅在中文输入法下出现按键映射错位而英文输入下正常的情况,则在终端下输入以下命令即可: ibus-daemon -drx 二.如果拼音正常但打出来显示 "[Invalid UTF-8] ...

  5. VUE+.NET应用系统的国际化-多语言词条服务

    上篇文章我们介绍了 VUE+.NET应用系统的国际化-整体设计思路 系统国际化改造整体设计思路如下: 提供一个工具,识别前后端代码中的中文,形成多语言词条,按语言.界面.模块统一管理多有的多语言词条 ...

  6. Android笔记--SQL

    SQL基本语法 基本语法在Python和Java那里都已经重复过了,这里就不再重复了 SQLiteDatabase--SQLite的数据库管理类 主要分为以下3类: 管理类 相关实现: 事务类 数据库 ...

  7. 【NLP 系列】Bert 词向量的空间分布

    作者:京东零售 彭馨 1. 背景 我们知道Bert 预训练模型针对分词.ner.文本分类等下游任务取得了很好的效果,但在语义相似度任务上,表现相较于 Word2Vec.Glove 等并没有明显的提升. ...

  8. STL常用容器用法总结

    vector 变长数组,倍增的思想 size() 返回元素个数 empty() 返回是否为空 clear() 清空 front()/back() push_back()/pop_back() begi ...

  9. 西瓜播放器api的坑 直播设置自动播放

    我们先看一下官方DEMO let player = new HlsPlayer({ id: 'mse', url: '//sf1-cdn-tos.huoshanstatic.com/obj/media ...

  10. 白嫖一个月的ES,完成了与MySQL的联动

    前言 <腾讯云 x Elasticsearch三周年>活动来了.文章写之前的思路是:在腾讯云服务器使用docker搭建ES.但是理想很丰满,显示很骨感,在操作过程中一波三折,最后还是含着泪 ...