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. 求pi

    参考自:https://www.zhihu.com/question/402311979 由 \[\frac{\pi^4}{90}={\textstyle \sum_{n=1}^{\infty }} ...

  2. appium之手机操作的方法

    Appium内置方法,来操作手机.在做app自动化时,可以内置方法的基本上,对一些常用的手机操作进行方法的封装. 常用的方法有: 获取手机分辨率: driver.get_window(size) # ...

  3. ES2016-ES2020

    参考:https://zhuanlan.zhihu.com/p/59096242 备注:可以使用ES6取代的10个Lodash特性 https://www.w3cplus.com/javascript ...

  4. Python从零到壹丨详解图像平滑的两种非线性滤波方法

    摘要:本文将详细讲解两种非线性滤波方法中值滤波和双边滤波. 本文分享自华为云社区<[Python从零到壹] 五十六.图像增强及运算篇之图像平滑(中值滤波.双边滤波)>,作者: eastmo ...

  5. sql处理重复的列,更好理清分组和分区

    一.分组统计.分区排名 1.语法和含义: 如果查询结果看得有疑惑,看第二部分-sql处理重复的列,更好理清分组和分区,有建表插入数据的sql语句 分组统计:GROUP BY 结合 统计/聚合函数一起使 ...

  6. MasaFramework入门第二篇,安装MasaFramework了解各个模板

    安装MasaFramework模板 执行以下命令安装最新Masa的模板 dotnet new --install Masa.Template 安装完成将出现四个模板 Masa Blazor App: ...

  7. python调用方法或者变量时出现未定义异常的原因,可能会是没有正确实例化

    当引用某个某块时 例如 Testpython import test class test(object): def __init__(): -- self.mimi = test def test1 ...

  8. 《golong入门教程📚》,从零开始入门❤️(建议收藏⭐️)

    Go语言学习笔记 本菜鸟的Go语言学习笔记,历时1个月,包含了Go语言大部分的基本语法(不敢说全部),学习期间参考了各种视频,阅读了各种文章,主要参考名单如下: 点击跳转到参考名单<( ̄︶ ̄)& ...

  9. 太坑了,我竟然从RocketMQ源码中扒出了7种导致消息重复消费的原因

    大家好,我是三友~~ 在众多关于MQ的面试八股文中有这么一道题,"如何保证MQ消息消费的幂等性". 为什么需要保证幂等性呢?是因为消息会重复消费. 为什么消息会重复消费? 明明已经 ...

  10. Solon v2.2.6 发布,助力信创国产化

    Solon 是一个高效的 Java 应用开发框架:更快.更小.更简单.它是一个有自己接口标准规范的开放生态,可为应用软件国产化提供支持,助力信创建设. 150来个生态插件,覆盖各种不同的应用开发场景: ...