首页 > 其他分享 >ffmpeg一揽子

ffmpeg一揽子

时间:2022-12-26 09:46:53浏览次数:56  
标签:输出 一揽子 ffmpeg format avformat context av liveVFrame

avformat_alloc_output_context2()。在基于FFmpeg的视音频编码器程序中,该函数通常是第一个调用的函数(除了组件注册函数av_register_all())。avformat_alloc_output_context2()函数可以初始化一个用于输出的AVFormatContext结构体。它的声明位于libavformat\avformat.h,如下所示。

  1. /** 
  2.  * Allocate an AVFormatContext for an output format. 
  3.  * avformat_free_context() can be used to free the context and 
  4.  * everything allocated by the framework within it. 
  5.  * 
  6.  * @param *ctx is set to the created format context, or to NULL in 
  7.  * case of failure 
  8.  * @param oformat format to use for allocating the context, if NULL 
  9.  * format_name and filename are used instead 
  10.  * @param format_name the name of output format to use for allocating the 
  11.  * context, if NULL filename is used instead 
  12.  * @param filename the name of the filename to use for allocating the 
  13.  * context, may be NULL 
  14.  * @return >= 0 in case of success, a negative AVERROR code in case of 
  15.  * failure 
  16.  */  
  17. int avformat_alloc_output_context2(AVFormatContext **ctx, AVOutputFormat *oformat,  
  18.                                    const char *format_name, const char *filename);  


代码中的英文注释写的已经比较详细了,在这里拿中文简单叙述一下。

ctx:函数调用成功之后创建的AVFormatContext结构体。
oformat:指定AVFormatContext中的AVOutputFormat,用于确定输出格式。如果指定为NULL,可以设定后两个参数(format_name或者filename)由FFmpeg猜测输出格式。
PS:使用该参数需要自己手动获取AVOutputFormat,相对于使用后两个参数来说要麻烦一些。
format_name:指定输出格式的名称。根据格式名称,FFmpeg会推测输出格式。输出格式可以是“flv”,“mkv”等等。
filename:指定输出文件的名称。根据文件名称,FFmpeg会推测输出格式。文件名称可以是“xx.flv”,“yy.mkv”等等。

函数执行成功的话,其返回值大于等于0。

该函数最典型的例子可以参考:最简单的基于FFMPEG的视频编码器(YUV编码为H.264)

------------------------------------------------------

为编码帧开辟存储空间
设置帧的一些参数

复制代码
liveVFrame = av_frame_alloc();
        if (!liveVFrame)
        {
            return -1;
        }
        liveVFrame->format = liveVideoCodecCtx->pix_fmt;
        liveVFrame->width  = liveVideoCodecCtx->width;
        liveVFrame->height = liveVideoCodecCtx->height;

        ret = av_image_alloc(liveVFrame->data, liveVFrame->linesize, liveVFrame->width, liveVFrame->height,
                             liveVideoCodecCtx->pix_fmt, 32);
复制代码

 在编码前:ret = avcodec_encode_video2(liveVideoCodecCtx, &pkt, liveVFrame, &got_output);

需要填充 liveVFrame->data[0],liveVFrame->data[1],liveVFrame->data[2]; 然后进行编码。

------------------------------------------------------------------------------------------------------------------------------------

FFMPEG打开媒体的的过程开始于avformat_open_input 输入输出结构体AVIOContext的初始化;

输入数据的协议(例如RTMP,或者file)的识别

(通过一套评分机制):1判断文件名的后缀 2读取文件头的数据进行比对;

使用获得最高分的文件协议对应的URLProtocol,通过函数指针的方式,与FFMPEG连接(非专业用词);

剩下的就是调用该URLProtocol的函数进行open,read等操作了

--------------

一共初始化了3个AVFormatContext,其中2个用于输入,1个用于输出。

3个AVFormatContext初始化之后,

通过avcodec_copy_context()函数可以将输入视频/音频的参数拷贝至输出视频/音频的AVCodecContext结构体。

然后分别调用视频输入流和音频输入流的av_read_frame(),

从视频输入流中取出视频的AVPacket,

音频输入流中取出音频的AVPacket,

分别将取出的AVPacket写入到输出文件中即可。

其间用到了一个不太常见的函数av_compare_ts(),是比较时间戳用的。

通过该函数可以决定该写入视频还是音频。

 

简单介绍一下流程中各个重要函数的意义:

avformat_open_input():打开输入文件。

avcodec_copy_context():赋值AVCodecContext的参数。

avformat_alloc_output_context2():初始化输出文件。

avio_open():打开输出文件。

avformat_write_header():写入文件头。

av_compare_ts():比较时间戳,决定写入视频还是写入音频。

这个函数相对要少见一些。

av_read_frame():从输入文件读取一个AVPacket。

av_interleaved_write_frame():写入一个AVPacket到输出文件。

av_write_trailer():写入文件尾。

标签:输出,一揽子,ffmpeg,format,avformat,context,av,liveVFrame
From: https://www.cnblogs.com/kn-zheng/p/17005008.html

相关文章

  • 编译FFmpeg成一个SO库
    编译环境MacOSXCapitan10.11.3NDK-r10e(64-bit)FFmpeg3.0简介在看完了第一篇Android最简单的基于FFmpeg的例子(一)—编译FFmpeg类库的基础上再看这一篇,在......
  • 解决 ffmpeg 在avformat_find_stream_info执行时间太长
    用ffmpeg做demux,网上很多参考文章。对于网络流,avformt_find_stream_info()函数默认需要花费较长的时间进行流格式探测,那么,如何减少探测时间内? 可以通过设置AVFotmatContext......
  • ffmpeg+libmp3lame库安装(linux)
    1.安装lame(libmp3fame的安装包)下载链接:https://sourceforge.net/projects/lame/files/lame/这里下载的文件版本为lame-3.100.tar.gz编译并安装tar-zxflame-3.100......
  • ffmpeg问题汇总及解决方案 <设置avformat_open_input 超时>
    1:如果数据是rtp/rtsp传输的话,ffmpeg会每隔30s(哪里设置该值?)发送一个keepalive包,如果ipc支持GET_PARAMETER命令,就发该命令等ipc回复以确认ipc还活着。某些ipc(IPCamera)不支......
  • ffmpeg打开视频解码器失败:Could not find codec parameters for stream 0 (Video: h26
    在使用ffmpeg进行拉流分离音视频数据再解码播放操作的时候;有时候经常会报错:Couldnotfindcodecparametersforstream0(Video:h264):unspecifiedsizeffmpeg默认......
  • ffmpeg默认输出中文为 UTF-8
    在使用ffmpeg进行对音视频文件解码输出信息的时候会出现乱码。从网上找到了说ffmpeg默认格式为utf-8 如果vs工程使用的的Unicode则需要将utf-8转Unicode才能正......
  • Window 下 FFmpeg 和 LibX264 的编译和配置
    https://glumes.com​​周末在家折腾Windows平台下FFmepg和LibX264库的编译,长期以来都是在Mac平台下做开发,切换到Windows平台下还是踩了不少坑。参考了网上很多编......
  • 播放器 FFmpeg 依赖库的配置
    目前已经完成了项目的创建,是怎样一个项目呢?首先是播放器SDK,也是项目最核心的模块,然后是对SDK进行单元测试的模块,最后是使用SDK做播放器的可视化项目模块。项目工程的......
  • 使用linux的ffmpeg进行B站直播推流
    很久之前买了个友善的开发板R2S,一直在家吃灰。最近看到网上有用ffmpeg进行直播推流的案例,想把吃灰的的开发板利用起来,于是有了这篇教程。第一步:安装ffmpegsudoaptupdat......
  • ffmpeg使用avformat_close_input()函数释放结构体时崩溃的问题
    先看一下我调试时,发现程序崩溃的代码位置//这是我的程序释放流上下文时的操作if(m_pAvFormatContext){if(m_iVideoStreamIndex>=0)avcodec_free_context(&m......