网站首页 > 技术教程 正文
2023-03-17:使用Go语言和FFmpeg库实现音频重采样解码,并将其保存为PCM格式的文件。
答案2023-03-17:
在音视频处理领域,常常需要对音频进行重采样和解码,以便于后续的处理和分析。本文将介绍如何使用Go语言及FFmpeg库实现音频重采样解码为PCM数据的过程。
# 1.前置知识和背景介绍
在介绍音频重采样解码之前,我们需要了解几个基本概念:
音频采样率:指音频信号每秒钟采样的次数,通常用赫兹(Hz)表示。常见的采样率有44100Hz、48000Hz等。
音频编码格式:指把声音转成数字信号后所采用的编码格式,常见的编码格式有MP3、AAC、WAV等。
音频重采样:指改变音频采样率的过程,也可以理解为对音频做插值运算,使得原来采样率与目标采样率不一致的音频能够适配到目标采样率上。
音频解码:指把已经编码压缩的音频文件解码成原始的音频数据流的过程。
# 2.实现步骤
实现音频重采样解码为PCM数据的具体步骤如下:
## 2.1.导入所需的FFmpeg库和Go语言包
首先,我们需要导入一些必要的FFmpeg库和Go语言包,以便后续代码中能够正常调用相关接口和方法。代码示例如下:
import (
"fmt"
"os"
"unsafe"
"github.com/moonfdd/ffmpeg-go/ffcommon"
"github.com/moonfdd/ffmpeg-go/libavcodec"
"github.com/moonfdd/ffmpeg-go/libavformat"
"github.com/moonfdd/ffmpeg-go/libavutil"
"github.com/moonfdd/ffmpeg-go/libswresample"
)
## 2.2.打开输入音频文件
需要打开输入音频文件,并检查是否打开成功。若无法打开,则应该返回错误信息。
fmtCtx := libavformat.AvformatAllocContext()
if libavformat.AvformatOpenInput(&fmtCtx, inFileName, nil, nil) < 0 {
fmt.Printf("Cannot open input file.\n")
return
}
其中,inFileName是输入音频文件名。
## 2.3.获取音频流信息
获取音频流信息,包括音频流的相关参数(采样率、声道数、采样格式等),并检查是否获取成功。若无法获取成功,则应该返回错误信息。
if fmtCtx.AvformatFindStreamInfo(nil) < 0 {
fmt.Printf("Cannot find stream info in input file.\n")
return
}
aStreamIndex := -1
for i := uint32(0); i < fmtCtx.NbStreams; i++ {
if fmtCtx.GetStream(i).Codecpar.CodecType == libavutil.AVMEDIA_TYPE_AUDIO {
aStreamIndex = int(i)
break
}
}
if aStreamIndex == -1 {
fmt.Printf("Cannot find audio stream.\n")
return
}
aCodecPara := fmtCtx.GetStream(uint32(aStreamIndex)).Codecpar
其中,aCodecPara是音频流的参数。
## 2.4.查找音频解码器并打开音频解码器
根据音频流的参数,查找对应的音频解码器,并打开解码器。在打开解码器时,需要将音频流的参数设置为解码器的参数。
codec := libavcodec.AvcodecFindDecoder(aCodecPara.CodecId)
if codec == nil {
fmt.Printf("Cannot find any codec for audio.\n")
return
}
codecCtx = codec.AvcodecAllocContext3()
if codecCtx.AvcodecParametersToContext(aCodecPara) < 0 {
fmt.Printf("Cannot alloc codec context.\n")
return
}
if codecCtx.AvcodecOpen2(codec, nil) < 0 {
fmt.Printf("Cannot open audio codec.\n")
return
}
其中,codecCtx是解码器的上下文。
## 2.5.计算重采样参数
计算重采样后的采样率、声道数和采样格式等参数。
out_channel_layout := codecCtx.ChannelLayout
out_sample_fmt := libavutil.AV_SAMPLE_FMT_S16
out_sample_rate := int32(44100)
out_channels := libavutil.AvGetChannelLayoutNbChannels(out_channel_layout)
## 2.6.初始化重采样上下文
根据输入和输出参数,初始化重采样上下文
swrCtx := libswresample.SwrAllocSetOpts(
nil,
out_channel_layout, out_sample_fmt, out_sample_rate,
codecCtx.ChannelLayout, codecCtx.SampleFmt(), int(codecCtx.SampleRate()), 0, nil,
)
if swrCtx == nil {
fmt.Printf("Cannot allocate resampler context.\n")
return
}
if swrCtx.SwrInit() < 0 {
fmt.Printf("Cannot initialize resampling context.\n")
return
}
其中,swrCtx是重采样上下文。
## 2.7.分配AVPacket和AVFrame
分别分配AVPacket和AVFrame,用于从输入音频流中读取数据、向解码器传递数据和从解码器接收数据等操作。
pkt := libavcodec.AvPacketAlloc()
defer pkt.AvPacketFree()
frame := libavutil.AvFrameAlloc()
defer frame.AvFrameFree()
## 2.8.从输入音频流中读取数据,并将其送入解码器进行解码
循环从输入音频流中读取数据,并将数据送入解码器进行解码。若读取到的数据为空,则跳出循环。
for {
ret := fmtCtx.AvReadFrame(pkt)
if ret < 0 {
break
}
if pkt.StreamIndex() != int32(aStreamIndex) {
continue
}
ret = codecCtx.AvCodecSendPacket(pkt)
if ret < 0 {
fmt.Printf("Error sending a packet for decoding.\n")
break
}
for {
ret = codecCtx.AvCodecReceiveFrame(frame)
if ret < 0 {
break
}
// 进行重采样
data := make([]*uint8, 0)
for i := 0; i < int(frame.NbSamples()); i++ {
data = append(data, (*uint8)(frame.ExtendedData(0)[i]))
}
out_nb_samples := swrCtx.SwrGetDelay(int64(codecCtx.SampleRate())) + int(frame.NbSamples())
out_samples_per_channel := out_nb_samples * out_channels
out_buffer := libavutil.AvMalloc(uintptr(out_samples_per_channel) * uintptr(libavutil.AvGetBytesPerSample(out_sample_fmt)))
defer libavutil.AvFree(out_buffer)
out_data := (**uint8)(unsafe.Pointer(&out_buffer))
swrCtx.SwrConvert(out_data, out_nb_samples, data, int(frame.NbSamples()))
}
pkt.AvPacketUnref()
}
注意,在解码时需要根据每个AVPacket的stream index判断是否是目标音频流。
## 2.9.编写PCM数据到文件中
将重采样后的PCM数据写入输出文件中。
outFile, err := os.Create(outFileName)
if err != nil {
fmt.Printf("Can not create output file.\n")
return
}
defer outFile.Close()
samples_size := libavutil.AvGetBytesPerSample(out_sample_fmt)
for i := 0; i < out_samples_per_channel; i++ {
for j := 0; j < out_channels; j++ {
sample_value := *(*int16)(unsafe.Pointer(uintptr(out_buffer) + uintptr(i*out_channels+j)*uintptr(samples_size)))
binary.Write(outFile, binary.LittleEndian, &sample_value)
}
}
其中,outFileName是输出音频文件名。
# 3.go语言完整代码
// https://feater.top/ffmpeg/ffmpeg-audio-resample-decode-mp3-to-pcm-with-cpu
package main
import (
"fmt"
"os"
"os/exec"
"unsafe"
"github.com/moonfdd/ffmpeg-go/ffcommon"
"github.com/moonfdd/ffmpeg-go/libavcodec"
"github.com/moonfdd/ffmpeg-go/libavformat"
"github.com/moonfdd/ffmpeg-go/libavutil"
"github.com/moonfdd/ffmpeg-go/libswresample"
)
const MAX_AUDIO_FRAME_SIZE = 192000
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
}
}
inVFileName := "./out/test.mp3"
outFileName := "./out/test16.pcm"
// ./lib/ffmpeg -i ./resources/big_buck_bunny.mp4 -acodec libmp3lame -vn ./out/test.mp3
//是否存在mp3文件
_, err = os.Stat(inVFileName)
if err != nil {
if os.IsNotExist(err) {
fmt.Println("create mp3 file")
exec.Command("./lib/ffmpeg", "-i", "./resources/big_buck_bunny.mp4", "-acodec", "libmp3lame", "-vn", inVFileName, "-y").CombinedOutput()
}
}
os.Remove(outFileName)
f, err := os.OpenFile(outFileName, os.O_CREATE|os.O_RDWR, 0777)
if err != nil {
fmt.Println("open file failed,err:", err)
return
}
fmtCtx := libavformat.AvformatAllocContext()
var codecCtx *libavcodec.AVCodecContext
pkt := libavcodec.AvPacketAlloc()
frame := libavutil.AvFrameAlloc()
aStreamIndex := -1
for {
if libavformat.AvformatOpenInput(&fmtCtx, inVFileName, nil, nil) < 0 {
fmt.Printf("Cannot open input file.\n")
break
}
if fmtCtx.AvformatFindStreamInfo(nil) < 0 {
fmt.Printf("Cannot find stream info in input file.\n")
break
}
fmtCtx.AvDumpFormat(0, inVFileName, 0)
//查找视频流在文件中的位置
for i := uint32(0); i < fmtCtx.NbStreams; i++ {
if fmtCtx.GetStream(i).Codecpar.CodecType == libavutil.AVMEDIA_TYPE_AUDIO {
aStreamIndex = int(i)
break
}
}
if aStreamIndex == -1 {
fmt.Printf("Cannot find audio stream.\n")
return
}
aCodecPara := fmtCtx.GetStream(uint32(aStreamIndex)).Codecpar
codec := libavcodec.AvcodecFindDecoder(aCodecPara.CodecId)
if codec == nil {
fmt.Printf("Cannot find any codec for audio.\n")
return
}
codecCtx = codec.AvcodecAllocContext3()
if codecCtx.AvcodecParametersToContext(aCodecPara) < 0 {
fmt.Printf("Cannot alloc codec context.\n")
return
}
codecCtx.PktTimebase = fmtCtx.GetStream(uint32(aStreamIndex)).TimeBase
if codecCtx.AvcodecOpen2(codec, nil) < 0 {
fmt.Printf("Cannot open audio codec.\n")
return
}
//设置转码参数
out_channel_layout := codecCtx.ChannelLayout
out_sample_fmt := libavutil.AV_SAMPLE_FMT_S16
out_sample_rate := int32(44100) //codecCtx.SampleRate
out_channels := libavutil.AvGetChannelLayoutNbChannels(out_channel_layout)
audio_out_buffer := libavutil.AvMalloc(MAX_AUDIO_FRAME_SIZE * 2)
var swr_ctx *libswresample.SwrContext
swr_ctx = swr_ctx.SwrAllocSetOpts(int64(out_channel_layout),
libavutil.AVSampleFormat(out_sample_fmt),
out_sample_rate,
int64(codecCtx.ChannelLayout),
codecCtx.SampleFmt,
codecCtx.SampleRate,
0, uintptr(0))
swr_ctx.SwrInit()
for (fmtCtx.AvReadFrame(pkt)) >= 0 {
if pkt.StreamIndex == uint32(aStreamIndex) {
if codecCtx.AvcodecSendPacket(pkt) >= 0 {
for codecCtx.AvcodecReceiveFrame(frame) >= 0 {
/*
Planar(平面),其数据格式排列方式为 (特别记住,该处是以点nb_samples采样点来交错,不是以字节交错):
LLLLLLRRRRRRLLLLLLRRRRRRLLLLLLRRRRRRL...(每个LLLLLLRRRRRR为一个音频帧)
而不带P的数据格式(即交错排列)排列方式为:
LRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRL...(每个LR为一个音频样本)
*/
if libavutil.AvSampleFmtIsPlanar(codecCtx.SampleFmt) != 0 {
len0 := swr_ctx.SwrConvert((**byte)(unsafe.Pointer(&audio_out_buffer)),
MAX_AUDIO_FRAME_SIZE*2,
(**byte)(unsafe.Pointer(&frame.Data)),
frame.NbSamples)
if len0 < 0 {
continue
}
dst_bufsize := libavutil.AvSamplesGetBufferSize(nil,
out_channels,
len0,
libavutil.AVSampleFormat(out_sample_fmt),
1)
bytes := []byte{}
ptr := audio_out_buffer
for i := int32(0); i < dst_bufsize; i++ {
bytes = append(bytes, *(*byte)(unsafe.Pointer(ptr)))
ptr++
}
f.Write(bytes)
}
}
}
}
pkt.AvPacketUnref()
}
break
}
libavutil.AvFrameFree(&frame)
libavcodec.AvPacketFree(&pkt)
codecCtx.AvcodecClose()
libavcodec.AvcodecFreeContext(&codecCtx)
fmtCtx.AvformatFreeContext()
f.Close()
fmt.Println("-----------------------------------------")
// ./lib/ffplay -ar 44100 -ac 2 -f s16le -i ./out/test.pcm
_, err = exec.Command("./lib/ffplay.exe", "-ar", "44100", "-ac", "2", "-f", "s16le", "-i", "./out/test16.pcm").Output()
if err != nil {
fmt.Println("play err = ", err)
}
}
# 4.测试和演示结果
go run ./examples/a16.audio_decode_swr_mp32pcm/main.go
# 5.结论
通过调用Go语言和FFmpeg库提供的接口和方法,我们可以轻松实现音频重采样解码,并将其保存为PCM格式的文件。这对于音视频应用开发和研究等领域非常有帮助。在实际工作中,我们可以根据具体需求和场景,进一步优化和扩展相关功能。
猜你喜欢
- 2024-11-24 2023-04-21:用go语言重写ffmpeg的metadata.c示例。
- 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 通用
你 发表评论:
欢迎- 最近发表
-
- Win10 TH2正式版官方ESD映像转换ISO镜像方法详解
- 使用iso镜像升级到Windows 10的步骤
- macOS Ventura 13.2 (22D49) Boot ISO 原版可引导镜像
- 安利一个用ISO镜像文件制作引导U盘的的小工具RUFUS
- CentOS 7使用ISO镜像配置本地yum源
- 用于x86平台的安卓9.0 ISO镜像发布下载:通吃I/A/N、完全免费
- AlmaLinux 9.6发布:升级工具、初步支持IBM Power虚拟化技术
- Rufus写入工具简洁介绍与教程(写入模式)
- 新硬件也能安装使用了,Edge版Linux Mint 21.3镜像发布
- 开源工程师:Ubuntu应该抛弃32位ISO镜像
- 标签列表
-
- 下划线是什么 (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)
本文暂时没有评论,来添加一个吧(●'◡'●)