zoukankan      html  css  js  c++  java
  • ffmpeg一揽子

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

    [cpp] view plain copy
     在CODE上查看代码片派生到我的代码片
    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():写入文件尾。

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

    以上转自雷神:http://blog.csdn.net/leixiaohua1020/article/details/39802913

  • 相关阅读:
    如何利用 iTunes 把 m4a/wav 文件转成 MP3 格式
    The best way to learn a programming language
    琼瑶哀悼丈夫去世
    与“芯片”相关的专业有哪些?
    君子使物,不为物使
    SRID (空间引用识别号, 坐标系)【转】
    编码
    test
    剪贴板神器:Ditto
    写Markdown博客时遇到的一些问题
  • 原文地址:https://www.cnblogs.com/wainiwann/p/5608038.html
Copyright © 2011-2022 走看看