2023-04-04:使用 Golang 和 ffmpeg-go 库实现 demuxing_decoding.c,轻松掌握音视频分离解码技巧。

答案2023-04-05:

使用github/moonfdd/ffmpeg-go库。

代码使用FFmpeg库打开一个音视频文件,提取其中的视频和音频流,并解码每一帧数据。它将解码后的视频和音频帧写入不同的输出文件中。代码中使用了libavformat、libavcodec和libavutil库提供的函数。

大体过程如下:

1.设置FFmpeg库的路径。

2.打开音视频文件并分配AVFormatContext结构体。

3.获取音频和视频流的信息,并选择合适的解码器进行解码。

4.对于视频流:
分配AVCodecContext结构体。
设置解码器参数并打开解码器。
读取视频帧并进行解码。
将解码后的视频帧写入输出文件。

5.对于音频流:
分配AVCodecContext结构体。
设置解码器参数并打开解码器。
读取音频帧并进行解码。
将解码后的音频帧写入输出文件。

6.关闭解码器和输入输出文件句柄,释放内存。

命令如下:

go run ./examples/internalexamples/demuxing_decoding/main.go ./resources/big_buck_bunny.mp4 ./out/big_buck_bunny.yuv ./out/big_buck_bunny.pcm

./lib/ffplay -f rawvideo -pix_fmt yuv420p -video_size 640x360 ./out/big_buck_bunny.yuv

./lib/ffplay -f f32le -ac 1 -ar 22050 .\out\big_buck_bunny.pcm

代码如下:

package main

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"
) func main0() (ret ffcommon.FInt) { if len(os.Args) != 4 {
fmt.Printf("usage: %s input_file video_output_file audio_output_file\nAPI example program to show how to read frames from an input file.\nThis program reads frames from a file, decodes them, and writes decoded\nvideo frames to a rawvideo file named video_output_file, and decoded\naudio frames to a rawaudio file named audio_output_file.\n",
os.Args[0])
os.Exit(1)
}
src_filename = os.Args[1]
video_dst_filename = os.Args[2]
audio_dst_filename = os.Args[3] /* open input file, and allocate format context */
if libavformat.AvformatOpenInput(&fmt_ctx, src_filename, nil, nil) < 0 {
fmt.Printf("Could not open source file %s\n", src_filename)
os.Exit(1)
} /* retrieve stream information */
if fmt_ctx.AvformatFindStreamInfo(nil) < 0 {
fmt.Printf("Could not find stream information\n")
os.Exit(1)
} for {
if open_codec_context(&video_stream_idx, &video_dec_ctx, fmt_ctx, libavutil.AVMEDIA_TYPE_VIDEO) >= 0 {
video_stream = fmt_ctx.GetStream(uint32(video_stream_idx)) video_dst_file, _ = os.Create(video_dst_filename)
if video_dst_file == nil {
fmt.Printf("Could not open destination file %s\n", video_dst_filename)
ret = 1
break
} /* allocate image where the decoded image will be put */
width = video_dec_ctx.Width
height = video_dec_ctx.Height
pix_fmt = video_dec_ctx.PixFmt
ret = libavutil.AvImageAlloc(&video_dst_data, &video_dst_linesize,
width, height, pix_fmt, 1)
if ret < 0 {
fmt.Printf("Could not allocate raw video buffer\n")
break
}
video_dst_bufsize = ret
} if open_codec_context(&audio_stream_idx, &audio_dec_ctx, fmt_ctx, libavutil.AVMEDIA_TYPE_AUDIO) >= 0 {
audio_stream = fmt_ctx.GetStream(uint32(audio_stream_idx))
audio_dst_file, _ = os.Create(audio_dst_filename)
if audio_dst_file == nil {
fmt.Printf("Could not open destination file %s\n", audio_dst_filename)
ret = 1
break
}
} /* dump input information to stderr */
fmt_ctx.AvDumpFormat(0, src_filename, 0) if audio_stream == nil && video_stream == nil {
fmt.Printf("Could not find audio or video stream in the input, aborting\n")
ret = 1
break
} frame = libavutil.AvFrameAlloc()
if frame == nil {
fmt.Printf("Could not allocate frame\n")
ret = -libavutil.ENOMEM
break
} pkt = libavcodec.AvPacketAlloc()
if pkt == nil {
fmt.Printf("Could not allocate packet\n")
ret = -libavutil.ENOMEM
break
} if video_stream != nil {
fmt.Printf("Demuxing video from file '%s' into '%s'\n", src_filename, video_dst_filename)
}
if audio_stream != nil {
fmt.Printf("Demuxing audio from file '%s' into '%s'\n", src_filename, audio_dst_filename)
} /* read frames from the file */
for fmt_ctx.AvReadFrame(pkt) >= 0 {
// check if the packet belongs to a stream we are interested in, otherwise
// skip it
if pkt.StreamIndex == uint32(video_stream_idx) {
ret = decode_packet(video_dec_ctx, pkt)
} else if pkt.StreamIndex == uint32(audio_stream_idx) {
ret = decode_packet(audio_dec_ctx, pkt)
}
pkt.AvPacketUnref()
if ret < 0 {
break
}
} /* flush the decoders */
if video_dec_ctx != nil {
decode_packet(video_dec_ctx, nil)
}
if audio_dec_ctx != nil {
decode_packet(audio_dec_ctx, nil)
} fmt.Printf("Demuxing succeeded.\n") if video_stream != nil {
fmt.Printf("Play the output video file with the command:\nffplay -f rawvideo -pix_fmt %s -video_size %dx%d %s\n",
libavutil.AvGetPixFmtName(pix_fmt), width, height,
video_dst_filename)
} if audio_stream != nil {
sfmt := audio_dec_ctx.SampleFmt
n_channels := audio_dec_ctx.Channels
var fmt0 string if libavutil.AvSampleFmtIsPlanar(sfmt) != 0 {
packed := libavutil.AvGetSampleFmtName(sfmt)
if packed == "" {
packed = "?"
}
fmt.Printf("Warning: the sample format the decoder produced is planar (%s). This example will output the first channel only.\n",
packed)
sfmt = libavutil.AvGetPackedSampleFmt(sfmt)
n_channels = 1
} 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, audio_dec_ctx.SampleRate,
audio_dst_filename)
}
break
}
// end:
libavcodec.AvcodecFreeContext(&video_dec_ctx)
libavcodec.AvcodecFreeContext(&audio_dec_ctx)
libavformat.AvformatCloseInput(&fmt_ctx)
if video_dst_file != nil {
video_dst_file.Close()
}
if audio_dst_file != nil {
audio_dst_file.Close()
}
libavcodec.AvPacketFree(&pkt)
libavutil.AvFrameFree(&frame)
libavutil.AvFree(uintptr(unsafe.Pointer(video_dst_data[0]))) if ret < 0 {
return 1
} else {
return 0
}
} var fmt_ctx *libavformat.AVFormatContext
var video_dec_ctx, audio_dec_ctx *libavcodec.AVCodecContext
var width, height ffcommon.FInt
var pix_fmt libavutil.AVPixelFormat
var video_stream, audio_stream *libavformat.AVStream
var src_filename string
var video_dst_filename string
var audio_dst_filename string
var video_dst_file *os.File
var audio_dst_file *os.File
var video_dst_data [4]*ffcommon.FUint8T
var video_dst_linesize [4]ffcommon.FInt
var video_dst_bufsize ffcommon.FInt
var video_stream_idx, audio_stream_idx ffcommon.FInt = -1, -1
var frame *libavutil.AVFrame
var pkt *libavcodec.AVPacket
var video_frame_count ffcommon.FInt
var audio_frame_count ffcommon.FInt func output_video_frame(frame *libavutil.AVFrame) ffcommon.FInt {
if frame.Width != width || frame.Height != height ||
frame.Format != pix_fmt {
/* To handle this change, one could call av_image_alloc again and
* decode the following frames into another rawvideo file. */
fmt.Printf("Error: Width, height and pixel format have to be constant in a rawvideo file, but the width, height or pixel format of the input video changed:\nold: width = %d, height = %d, format = %s\nnew: width = %d, height = %d, format = %s\n",
width, height, libavutil.AvGetPixFmtName(pix_fmt),
frame.Width, frame.Height,
libavutil.AvGetPixFmtName(frame.Format))
return -1
} fmt.Printf("video_frame n:%d coded_n:%d\n",
video_frame_count, frame.CodedPictureNumber)
video_frame_count++ /* copy decoded frame to destination buffer:
* this is required since rawvideo expects non aligned data */
libavutil.AvImageCopy(&video_dst_data, &video_dst_linesize,
(*[4]*byte)(unsafe.Pointer(&frame.Data)), (*[4]int32)(unsafe.Pointer(&frame.Linesize)),
pix_fmt, width, height) /* write to rawvideo file */
video_dst_file.Write(ffcommon.ByteSliceFromByteP(video_dst_data[0], int(video_dst_bufsize)))
return 0
} func output_audio_frame(frame *libavutil.AVFrame) ffcommon.FInt {
unpadded_linesize := ffcommon.FSizeT(frame.NbSamples * libavutil.AvGetBytesPerSample(libavutil.AVSampleFormat(frame.Format)))
fmt.Printf("audio_frame n:%d nb_samples:%d pts:%s\n",
audio_frame_count, frame.NbSamples,
libavutil.AvTs2timestr(frame.Pts, &audio_dec_ctx.TimeBase))
audio_frame_count++
/* Write the raw audio data samples of the first plane. This works
* fine for packed formats (e.g. AV_SAMPLE_FMT_S16). However,
* most audio decoders output planar audio, which uses a separate
* plane of audio samples for each channel (e.g. AV_SAMPLE_FMT_S16P).
* In other words, this code will write only the first audio channel
* in these cases.
* You should use libswresample or libavfilter to convert the frame
* to packed data. */
audio_dst_file.Write(ffcommon.ByteSliceFromByteP(*frame.ExtendedData, int(unpadded_linesize))) return 0
} func decode_packet(dec *libavcodec.AVCodecContext, pkt *libavcodec.AVPacket) ffcommon.FInt {
ret := ffcommon.FInt(0) // submit the packet to the decoder
ret = dec.AvcodecSendPacket(pkt)
if ret < 0 {
fmt.Printf("Error submitting a packet for decoding (%s)\n", libavutil.AvErr2str(ret))
return ret
} // get all the available frames from the decoder
for ret >= 0 {
ret = dec.AvcodecReceiveFrame(frame)
if ret < 0 {
// those two return values are special and mean there is no output
// frame available, but there were no errors during decoding
if ret == libavutil.AVERROR_EOF || ret == -libavutil.EAGAIN {
return 0
} fmt.Printf("Error during decoding (%s)%d\n", libavutil.AvErr2str(ret), ret)
return ret
} // write the frame data to output file
if dec.Codec.Type == libavutil.AVMEDIA_TYPE_VIDEO {
ret = output_video_frame(frame)
} else {
ret = output_audio_frame(frame)
} frame.AvFrameUnref()
if ret < 0 {
return ret
}
} return 0
} func open_codec_context(stream_idx *ffcommon.FInt,
dec_ctx **libavcodec.AVCodecContext, fmt_ctx *libavformat.AVFormatContext, type0 libavutil.AVMediaType) ffcommon.FInt {
var ret, stream_index ffcommon.FInt
var st *libavformat.AVStream
var dec *libavcodec.AVCodec
var opts *libavutil.AVDictionary ret = fmt_ctx.AvFindBestStream(type0, -1, -1, nil, 0)
if ret < 0 {
fmt.Printf("Could not find %s stream in input file '%s'\n",
libavutil.AvGetMediaTypeString(type0), src_filename)
return ret
} else {
stream_index = ret
st = fmt_ctx.GetStream(uint32(stream_index)) /* find decoder for the stream */
dec = libavcodec.AvcodecFindDecoder(st.Codecpar.CodecId)
if dec == nil {
fmt.Printf("Failed to find %s codec\n",
libavutil.AvGetMediaTypeString(type0))
return -libavutil.EINVAL
} /* Allocate a codec context for the decoder */
*dec_ctx = dec.AvcodecAllocContext3()
if *dec_ctx == nil {
fmt.Printf("Failed to allocate the %s codec context\n",
libavutil.AvGetMediaTypeString(type0))
return -libavutil.ENOMEM
} /* Copy codec parameters from input stream to output codec context */
ret = (*dec_ctx).AvcodecParametersToContext(st.Codecpar)
if ret < 0 {
fmt.Printf("Failed to copy %s codec parameters to decoder context\n",
libavutil.AvGetMediaTypeString(type0))
return ret
} /* Init the decoders */
ret = (*dec_ctx).AvcodecOpen2(dec, &opts)
if ret < 0 {
fmt.Printf("Failed to open %s codec\n",
libavutil.AvGetMediaTypeString(type0))
return ret
}
*stream_idx = stream_index
} return 0
} 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 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-04-04:使用 Golang 和 ffmpeg-go 库实现 demuxing_decoding.c,轻松掌握音视频分离解码技巧。的更多相关文章

  1. 音视频编解码流程与如何使用 FFMPEG 命令进行音视频处理

    一.前言 FFMPEG 是特别强大的专门用于处理音视频的开源库.你既可以使用它的 API 对音视频进行处理,也可以使用它提供的工具,如 ffmpeg, ffplay, ffprobe,来编辑你的音视频 ...

  2. FFmpeg音视频编解码实践总结

    PS:由于目前开发RTSP服务器传输模块时用到了h264文件,所以攻了一段时间去实现h264的视频编解码,借用FFmpeg SDK实现了任意文件格式之间的转换,并实现了流媒体实时播放,目前音视频同步需 ...

  3. 【FFMPEG】各种音视频编解码学习详解 h264 ,mpeg4 ,aac 等所有音视频格式

    目录(?)[-] 编解码学习笔记二codec类型 编解码学习笔记三Mpeg系列Mpeg 1和Mpeg 2 编解码学习笔记四Mpeg系列Mpeg 4 编解码学习笔记五Mpeg系列AAC音频 编解码学习笔 ...

  4. 基于ffmpeg的简单音视频编解码的例子

    近日需要做一个视频转码服务器,对我这样一个在该领域的新手来说却是够我折腾一番,在别人的建议下开始研究开源ffmpeg项目,下面是在代码中看到的一 段例子代码,对我的学习非常有帮助.该例子代码包含音频的 ...

  5. Android 音视频深入 十一 FFmpeg和AudioTrack播放声音(附源码下载)

    项目地址,求starhttps://github.com/979451341/AudioVideoStudyCodeTwo/tree/master/FFmpeg%E6%92%AD%E6%94%BE%E ...

  6. FFmpeg命令行工具和批处理脚本进行简单的音视频文件编辑

    FFmpeg_Tutorial FFmpeg工具和sdk库的使用demo 一.使用FFmpeg命令行工具和批处理脚本进行简单的音视频文件编辑 1.基本介绍 对于每一个从事音视频技术开发的工程师,想必没 ...

  7. 视频编解码的理论和实践2:Ffmpeg视频编解码

    近几年,视频编解码技术在理论及应用方面都取得了重大的进展,越来越多的人想要了解编解码技术.因此,网易云信研发工程师为大家进行了归纳梳理,从理论及实践两个方面简单介绍视频编解码技术. 相关阅读推荐 &l ...

  8. vmware虚拟机下ubuntu 13.04使用zeranoe脚本交叉编译ffmpeg

    2013-07-01今天是建党节,习总书记指出,党的建设要以“照镜子.正衣冠.洗洗澡.治治病”为总要求.希望我们的党越来越纯洁,为人民谋福利.言归正传,每次项目中需要编译相应的ffmpeg,都很费时费 ...

  9. Ubuntu16.04下编译OpenCV2.4.13静态库(.a文件)

    Ubuntu16.04下编译OpenCV2.4.13静态库(.a文件) https://blog.csdn.net/woainishifu/article/details/79712110 我们在做项 ...

  10. Leetcode:面试题 04.04. 检查平衡性

    Leetcode:面试题 04.04. 检查平衡性 Leetcode:面试题 04.04. 检查平衡性 Talk is cheap . Show me the code . /** * Definit ...

随机推荐

  1. String的Equals()和==比较

    String的Equals()和==比较 常量池(Constant Pool):指的是在编译期被确定,并被保存在已编译的.class文件中的一些数据.JVM虚拟机为每个被装载的类型维护一个常量池.常量 ...

  2. python练习--1

    ID_CARD = input("Input your ID Card: ") length = len(ID_CARD) if length < 5: NEW_ID_CAR ...

  3. MySql8错误记录.巨坑!File './binlog.index' not found

    mysql8存在大小写敏感,若要设置不敏感,需要在mysql初始化时设置:然后库中已有项目存在,mysql备份文件夹后无法重启,还原数据后存在权限问题,更改文件夹权限后,发现仍然不行,将SELinux ...

  4. ACM需要知道的STL小技巧

    天天用stl,但是有一些小技巧如果不知道,偶尔会导致TLE,这里说几个打比赛需要用到的. 主要是大概了解一下其底层原理:https://www.jianshu.com/p/834cc223bb57 就 ...

  5. Nacos 服务发现

    更多内容,前往 IT-BLOG 一.Nacos 简介 Nacos 是阿里的一个开源产品,它是针对微服务架构中的服务发现.配置管理.服务治理的综合型解决方案.Nacos 使服务更容易注册,并通过 DNS ...

  6. CSAPP-Attack Lab

    目录 Code Injection Attacks Level 1 Level 2 Level_3 Return-Oriented Programming Level_4 Level_5 获取栈顶地址 ...

  7. iOS 深色模式适配

    要求:iOS13.0以上 重点:需要所有界面进行适配,工作量巨大,需要从项目开始就进行适配:H5界面无法进行适配 实现方式:System Colors(常用).Semantic Colors(常用). ...

  8. vue环境安装与配置

    https://www.jb51.net/article/251371.htmhttps://www.yht7.com/news/193355 一.下载和安装Vue: https://nodejs.o ...

  9. 设计模式(三十)----综合应用-自定义Spring框架-自定义Spring IOC-定义bean、注册表相关类

    现要对下面的配置文件进行解析,并自定义Spring框架的IOC对涉及到的对象进行管理. <?xml version="1.0" encoding="UTF-8&qu ...

  10. ChatGPT4实现前一天

    目录 提出需求 代码实现 需求分析 单元测试 等价类划分 决策表 软件测试作业,用ChatGPT4来帮个小忙,小划水,勿喷勿喷,近期有相关作业的同学看到我的文章,建议修改一下,别撞车了,哈哈哈~ 提出 ...