2023-03-23:音视频解混合(demuxer)为PCM和YUV420P,用go语言编写。

答案2023-03-23:

大体步骤如下:

1.打开媒体文件,并获取音频和视频流。

2.对于每个流,找到对应的解码器、创建解码上下文并打开解码器。

3.一帧一帧读取压缩的音频或视频数据AVPacket,并调用对应的解码器进行解码。

4.对于音频:重采样成16bit 44100 PCM格式,并将数据写入输出文件或缓冲区。

5.对于视频:转换成YUV420P格式,并将数据写入输出文件或缓冲区。

6.清理已分配的资源。

代码见github/moonfdd/ffmpeg-go库。

执行命令:

go run ./examples/a23.video_demuxer_mp42yuvpcm/main.go

代码参考23:音视频解混合(demuxer)为PCM和YUV420P,代码如下:

// https://feater.top/ffmpeg/ffmpeg-demuxer-video-to-pcm-and-yuv420
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/libavdevice"
"github.com/moonfdd/ffmpeg-go/libavformat"
"github.com/moonfdd/ffmpeg-go/libavutil"
"github.com/moonfdd/ffmpeg-go/libswresample"
"github.com/moonfdd/ffmpeg-go/libswscale"
) /*
* 音频播放命令:ffplay -ar 44100 -ac 1 -f s16le -i out.pcm
*/
const i44100 = 22050 func avcodec_save_audio_file(pFormatCtx *libavformat.AVFormatContext, streamIndex ffcommon.FInt, fileName string) ffcommon.FInt {
var pCodec *libavcodec.AVCodec
var pCodecCtx *libavcodec.AVCodecContext
codecpar := pFormatCtx.GetStream(uint32(streamIndex)).Codecpar //4.获取解码器(一):音频
//根据索引拿到对应的流
pCodec = libavcodec.AvcodecFindDecoder(codecpar.CodecId)
if pCodec == nil {
fmt.Printf("can't decoder audio\n")
return -1
}
//申请一个解码上下文
pCodecCtx = pCodec.AvcodecAllocContext3()
if pCodecCtx == nil {
fmt.Printf("can't allocate a audio decoding context\n")
return -1
} //用流解码信息初始化编码参数
pCodecCtx.AvcodecParametersToContext(codecpar) //没有此句会出现:Could not update timestamps for skipped samples
pCodecCtx.PktTimebase = pFormatCtx.GetStream(uint32(streamIndex)).TimeBase //5.打开解码器
if pCodecCtx.AvcodecOpen2(pCodec, nil) < 0 {
fmt.Printf("can't open codec\n")
return -1
} // printf("--------------- File Information ----------------\n");
// av_dump_format(pFormatCtx, 0, fileName, 0);
// printf("-------------------------------------------------\n"); //编码数据
packet := new(libavcodec.AVPacket)
//解压缩数据
frame := libavutil.AvFrameAlloc() //frame->16bit 44100 PCM统一音频采样格式与采样率
swrCtx := libswresample.SwrAlloc()
//重采样设置选项-----------------------------------------------------------start
//输入的采样格式
inSampleFmt := pCodecCtx.SampleFmt
//输出的采样格式
var outSampleFmt libswresample.AVSampleFormat = libavutil.AV_SAMPLE_FMT_S16
//输入的采样率
inSampleRate := pCodecCtx.SampleRate
//输出的采样率
var outSampleRate ffcommon.FInt = i44100
//输入的声道布局
var inChannelLayout ffcommon.FUint64T = pCodecCtx.ChannelLayout
//输出的声道布局:CHANNEL_IN_MONO为单声道,CHANNEL_IN_STEREO为双声道
var outChannelLayout ffcommon.FUint64T = libavutil.AV_CH_LAYOUT_MONO fmt.Printf("inSampleFmt = %d, inSampleRate = %d, inChannelLayout = %d, name = %s\n", inSampleFmt, inSampleRate,
inChannelLayout, ffcommon.StringFromPtr(pCodec.Name)) swrCtx.SwrAllocSetOpts(int64(outChannelLayout), outSampleFmt, outSampleRate,
int64(inChannelLayout), inSampleFmt, inSampleRate, 0, uintptr(0))
swrCtx.SwrInit()
//重采样设置选项-----------------------------------------------------------end //获取输出的声道个数
outChannelNb := libavutil.AvGetChannelLayoutNbChannels(outChannelLayout)
fmt.Printf("outChannelNb = %d\n", outChannelNb) // //存储PCM数据
outBuffer := (*byte)(unsafe.Pointer(libavutil.AvMalloc(2 * i44100))) // FILE *fp = fopen(fileName, "wb");
fp, _ := os.Create(fileName) //回到流的初始位置
pFormatCtx.AvSeekFrame(streamIndex, 0, libavformat.AVSEEK_FLAG_BACKWARD) //6.一帧一帧读取压缩的音频数据AVPacket
for pFormatCtx.AvReadFrame(packet) >= 0 {
if packet.StreamIndex == uint32(streamIndex) {
//解码AVPacket --> AVFrame
ret := pCodecCtx.AvcodecSendPacket(packet)
if ret < 0 {
fmt.Printf("Decode error\n")
break
} if pCodecCtx.AvcodecReceiveFrame(frame) >= 0 {
swrCtx.SwrConvert(&outBuffer, 2*i44100, (**byte)(unsafe.Pointer(&frame.Data)), frame.NbSamples)
//获取sample的size
outBufferSize := libavutil.AvSamplesGetBufferSize(nil, outChannelNb, frame.NbSamples, outSampleFmt, 1)
//写入文件
fp.Write(ffcommon.ByteSliceFromByteP(outBuffer, int(outBufferSize)))
}
} packet.AvPacketUnref()
} fp.Close()
libavutil.AvFrameFree(&frame)
libavutil.AvFree(uintptr(unsafe.Pointer(outBuffer)))
libswresample.SwrFree(&swrCtx)
pCodecCtx.AvcodecClose() return 0
} /*
* 视频播放命令:ffplay -video_size 654x368 -i out.yuv
*/
func avcodec_save_video_file(pFormatCtx *libavformat.AVFormatContext, streamIndex ffcommon.FInt, fileName string) ffcommon.FInt {
var pCodec *libavcodec.AVCodec
var pCodecCtx *libavcodec.AVCodecContext
codecpar := pFormatCtx.GetStream(uint32(streamIndex)).Codecpar //4.获取解码器(一):音频
//根据索引拿到对应的流
pCodec = libavcodec.AvcodecFindDecoder(codecpar.CodecId)
if pCodec == nil {
fmt.Printf("can't decoder audio\n")
return -1
}
//申请一个解码上下文
pCodecCtx = pCodec.AvcodecAllocContext3()
if pCodecCtx == nil {
fmt.Printf("can't allocate a audio decoding context\n")
return -1
} //用流解码信息初始化编码参数
pCodecCtx.AvcodecParametersToContext(codecpar) //没有此句会出现:Could not update timestamps for skipped samples
pCodecCtx.PktTimebase = pFormatCtx.GetStream(uint32(streamIndex)).TimeBase //5.打开解码器
if pCodecCtx.AvcodecOpen2(pCodec, nil) < 0 {
fmt.Printf("can't open codec\n")
return -1
} // printf("--------------- File Information ----------------\n");
// av_dump_format(pFormatCtx, 0, fileName, 0);
// printf("-------------------------------------------------\n"); //编码数据
pPacket := new(libavcodec.AVPacket)
//解压缩数据
pFrame := libavutil.AvFrameAlloc()
pFrameYUV := libavutil.AvFrameAlloc() outBuffer := (*byte)(unsafe.Pointer(libavutil.AvMalloc(
uint64(libavutil.AvImageGetBufferSize(libavutil.AV_PIX_FMT_YUV420P, pCodecCtx.Width, pCodecCtx.Height, 1)))))
libavutil.AvImageFillArrays((*[4]*byte)(unsafe.Pointer(&pFrameYUV.Data)), (*[4]int32)(unsafe.Pointer(&pFrameYUV.Linesize)), outBuffer,
libavutil.AV_PIX_FMT_YUV420P, pCodecCtx.Width,
pCodecCtx.Height, 1) pImgConvertCtx := libswscale.SwsGetContext(pCodecCtx.Width, pCodecCtx.Height, pCodecCtx.PixFmt,
pCodecCtx.Width, pCodecCtx.Height, libavutil.AV_PIX_FMT_YUV420P, libswscale.SWS_BICUBIC, nil, nil, nil) fmt.Printf("width = %d, height = %d, name = %s\n", pCodecCtx.Width, pCodecCtx.Height, ffcommon.StringFromPtr(pCodec.Name)) fp, _ := os.Create(fileName) //回到流的初始位置
pFormatCtx.AvSeekFrame(streamIndex, 0, libavformat.AVSEEK_FLAG_BACKWARD) //6.一帧一帧读取压缩的视频数据AVPacket
for pFormatCtx.AvReadFrame(pPacket) >= 0 {
if pPacket.StreamIndex == uint32(streamIndex) {
//解码AVPacket --> AVFrame
ret := pCodecCtx.AvcodecSendPacket(pPacket)
if ret < 0 {
fmt.Printf("Decode error\n")
break
} if pCodecCtx.AvcodecReceiveFrame(pFrame) >= 0 {
pImgConvertCtx.SwsScale((**byte)(unsafe.Pointer(&pFrame.Data)), (*int32)(unsafe.Pointer(&pFrame.Linesize)), 0,
uint32(pCodecCtx.Height), (**byte)(unsafe.Pointer(&pFrameYUV.Data)), (*int32)(unsafe.Pointer(&pFrameYUV.Linesize))) y_size := pCodecCtx.Width * pCodecCtx.Height
fp.Write(ffcommon.ByteSliceFromByteP(pFrameYUV.Data[0], int(y_size))) //Y
fp.Write(ffcommon.ByteSliceFromByteP(pFrameYUV.Data[1], int(y_size/4))) //U
fp.Write(ffcommon.ByteSliceFromByteP(pFrameYUV.Data[2], int(y_size/4))) //V
}
} pPacket.AvPacketUnref()
} fp.Close()
libavutil.AvFrameFree(&pFrame)
libavutil.AvFrameFree(&pFrameYUV)
libavutil.AvFree(uintptr(unsafe.Pointer(outBuffer)))
pCodecCtx.AvcodecClose() return 0
} 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
}
} inputFile := "./resources/big_buck_bunny.mp4"
outAudioFile := "./out/a23.pcm"
outVideoFile := "./out/a23.yuv" var videoStreamIndex ffcommon.FInt = -1
var audioStreamIndex ffcommon.FInt = -1
var i ffcommon.FUnsignedInt = 0
var pFormatCtx *libavformat.AVFormatContext //1.注册组件
libavdevice.AvdeviceRegisterAll() //封装格式上下文
pFormatCtx = libavformat.AvformatAllocContext() //2.打开输入文件
if libavformat.AvformatOpenInput(&pFormatCtx, inputFile, nil, nil) != 0 {
fmt.Printf("can't open input file\n")
return
} //3.获取音视频信息
if pFormatCtx.AvformatFindStreamInfo(nil) < 0 {
fmt.Printf("can't find stream info\n")
return
} //音视频编码,找到对应的音视频流的索引位置
//找到音频流的索引
for i = 0; i < pFormatCtx.NbStreams; i++ {
if pFormatCtx.GetStream(i).Codecpar.CodecType == libavutil.AVMEDIA_TYPE_AUDIO {
audioStreamIndex = int32(i)
break
}
} //找到视频流的索引
for i = 0; i < pFormatCtx.NbStreams; i++ {
if pFormatCtx.GetStream(i).Codecpar.CodecType == libavutil.AVMEDIA_TYPE_VIDEO {
videoStreamIndex = int32(i)
break
}
} fmt.Printf("audioStreamIndex = %d, videoStreamIndex = %d\n", audioStreamIndex, videoStreamIndex) if audioStreamIndex == -1 {
fmt.Printf("can't find a audio stream\n")
} else {
fmt.Printf("try to save audio stream\n")
avcodec_save_audio_file(pFormatCtx, audioStreamIndex, outAudioFile)
} if videoStreamIndex == -1 {
fmt.Printf("can't find a video stream\n")
} else {
fmt.Printf("try to save video stream\n")
avcodec_save_video_file(pFormatCtx, videoStreamIndex, outVideoFile)
} libavformat.AvformatCloseInput(&pFormatCtx) fmt.Println("-----------------------------------------")
go func() {
_, err = exec.Command("./lib/ffplay.exe", "-ar", "22050", "-ac", "1", "-f", "s16le", "-i", outAudioFile).Output()
if err != nil {
fmt.Println("play err = ", err)
}
}()
_, err = exec.Command("./lib/ffplay.exe", "-video_size", "640*360", "-i", outVideoFile).Output()
if err != nil {
fmt.Println("play err = ", err)
}
}

2023-03-23:音视频解混合(demuxer)为PCM和YUV420P,用go语言编写。的更多相关文章

  1. FFmpeg音视频解封装

    一 . 解封装用到的函数和结构体 1.av_register_all() : open 一次就调用一次 2.avformat_network_init() : 网络模块初始化 3.avformat_o ...

  2. 全志Tina_dolphin播放音视频裸流(h264,pcm)验证

    最近在验证tina对裸流音视频的支持,主要指h264视频裸流及pcm音频裸流. 在原始sdk中有针对很多video和audio类型的parser,但就是没有找到pcm和h264的parser,所以需要 ...

  3. 音视频编解码: YUV存储格式中的YUV420P,YUV420SP,NV12, NV21理解(转)

    概述  之前介绍了YUV码流的采样格式,下面分析下YUV码流的存储格式,YUV码流的存储格式与采样格式息息相关.总的来讲,YUV存储格式主要分为两种: planar 平面格式 指先连续存储所有像素点的 ...

  4. Android 音视频深入 二 AudioTrack播放pcm(附源码下载)

    本篇项目地址,名字是录音和播放PCM,求starhttps://github.com/979451341/Audio-and-video-learning-materials 1.AudioTrack ...

  5. Android硬编码——音频编码、视频编码及音视频混合

    视频编解码对许多Android程序员来说都是Android中比较难的一个知识点.在Android 4.1以前,Android并没有提供硬编硬解的API,所以之前基本上都是采用FFMpeg来做视频软件编 ...

  6. Android 音视频开发(七): 音视频录制流程总结

    在前面我们学习和使用了AudioRecord.AudioTrack.Camera.MediaExtractor.MediaMuxer API.MediaCodec. 学习和使用了上述的API之后,相信 ...

  7. moviepy音视频剪辑:视频剪辑基类VideoClip的属性及方法详解

    ☞ ░ 前往老猿Python博文目录 ░ 一.概述 在<moviepy音视频剪辑:moviepy中的剪辑基类Clip详解>和<moviepy音视频剪辑:moviepy中的剪辑基类Cl ...

  8. Android 音视频开发(六): MediaCodec API 详解

    在学习了Android 音视频的基本的相关知识,并整理了相关的API之后,我们应该对基本的音视频有一定的轮廓了. 下面开始接触一个Android音视频中相当重要的一个API: MediaCodec.通 ...

  9. 音视频入门-11-PNG文件格式详解

    * 音视频入门文章目录 * PNG 文件格式解析 PNG 图像格式文件由一个 8 字节的 PNG 文件署名域和 3 个以上的后续数据块(IHDR.IDAT.IEND)组成. PNG 文件包括 8 字节 ...

  10. 音视频入门-14-JPEG文件格式详解

    * 音视频入门文章目录 * JPEG 文件格式解析 JPEG 文件使用的数据存储方式有多种.最常用的格式称为 JPEG 文件交换格式(JPEG File Interchange Format,JFIF ...

随机推荐

  1. C++ accumulate()函数的用法

    accumulate定义在 numeric 中,作用有两个,一个是累加求和,另一个是自定义类型数据的处理. 头文件 #include <numeric> 原型 默认求累加和 templat ...

  2. 经典面试题:UDP和TCP的区别?

    相信测试这行的同道朋友们,经常会被问到这个问题,这里我用自己的语言总结了几点: UDP 和 TCP的区别: 连接方面:tcp面向连接,三次握手,四次挥手 udp无连接,即发送数据之前不需要建立连接 安 ...

  3. @Value属性值读取

    1.在父类定义属性DQ,并通过配置初始化 @Configuration public class DQConfig { public static String DQ; @Value("${ ...

  4. OSPF之路由撤销

  5. 【Unity3D】基于粒子系统实现烟花特效

    1 需求实现 ​ 粒子系统ParticleSystem 中介绍了粒子初始化.粒子发射.发射器形状.渲染器.碰撞.子发射器.拖尾等粒子系统的基本用法,本节将基于粒子系统实现烟花特效. ​ 实现需求如下( ...

  6. ThreadLocal部分源码分析和应用场景

    结构演进 早起JDK版本中,ThreadLocal内部结构是一个Map,线程为key,线程在"线程本地变量"中绑定的值为Value.每一个ThreadLocal实例拥有一个Map实 ...

  7. Go语言:一文看懂什么是DI依赖注入(dependency injection)设计模式

    前言: 本文主要介绍的是Goalng中关于 DI 的部分,前一部分会先通过典型的面向对象语言Java引入DI这个概念 仅供初学者理解使用,文章如有纰漏敬请指出 本文涉及到的知识面较为零散,其中包含面向 ...

  8. pcm音频的录制、播放及转换

    操作系统 :Windows10_x64 pcm格式为原始音频数据,有时候会遇到需要录制.播放及转换的情况,这里记录下. 一.录制pcm音频 这里演示下使用Audacity进行pcm音频录音的过程. A ...

  9. 智能且集成的端到端移动应用程序安全解决方案——Quixxi简介

    移动应用程序安全变得简单快捷 Quixxi 是一种智能且集成的端到端移动应用程序安全解决方案.这个强大的工具可供开发人员在几分钟内保护和监控任何移动应用程序. Quixxi Security 评估应用 ...

  10. pysimplegui之popup弹出框

    弹出框其实跟信息框差不多,在写界面的时候经常用,具体如下 "高级呼叫"是以"弹出"开头的呼叫.它们是与用户沟通的最基本形式.它们以它们创建的窗口类型命名,即弹出 ...