常用方法
<h2>void av_register_all()</h2>
<h5>简要描述</h5>
<ul>
<li>初始化所有组件,只有调用了该函数,才能使用复用器和编解码器</li>
</ul>
<h2>void avfilter_register_all()</h2>
<h5>简要描述</h5>
<ul>
<li>注册滤波器</li>
</ul>
<h2>void avformat_network_init()</h2>
<h5>简要描述</h5>
<ul>
<li>加载socket库以及网络加密协议相关的库,为后续使用网络相关提供支持</li>
</ul>
<h2>AVFormatContext *avformat_alloc_context()</h2>
<h5>简要描述</h5>
<ul>
<li>AVFormatContext *avformat_alloc_context(void)函数用来申请AVFormatContext类型变量并初始化默认参数。申请的空间</li>
</ul>
<h5>返回示例</h5>
<pre><code> AVFormatContext* pCtx;
pCtx = avformat_alloc_context();</code></pre>
<h2>int avformat_open_input(AVFormatContext <strong>ps, const char <em>url, ff_const59 AVInputFormat </em>fmt, AVDictionary </strong>options)</h2>
<h5>简要描述</h5>
<ul>
<li>打开一个输入流并读取头文件。编解码器没有打开。流必须用avformat_close_input()关闭。</li>
</ul>
<table>
<thead>
<tr>
<th>参数</th>
<th>类型</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>ps</td>
<td>AVFormatContext**</td>
<td>格式化的上下文。要注意,如果传入的是一个AVFormatContext*的指针,则该空间须自己手动清理,若传入的指针为空,则FFmpeg会内部自己创建</td>
</tr>
<tr>
<td>url</td>
<td>const char *</td>
<td>传入的地址。支持http,RTSP,以及普通的本地文件。地址最终会存入到AVFormatContext结构体当中</td>
</tr>
<tr>
<td>fmt</td>
<td>ff_const59 AVInputFormat *</td>
<td>指定输入的封装格式。一般传NULL,由FFmpeg自行探测</td>
</tr>
<tr>
<td>options</td>
<td>AVDictionary **</td>
<td>其它参数设置。它是一个字典,用于参数传递,不传则写NULL</td>
</tr>
</tbody>
</table>
<h5>返回示例</h5>
<pre><code> AVFormatContext* pCtx;
pCtx = avformat_alloc_context();
ret = avformat_open_input(&pCtx, url.c_str(), 0, 0);//执行成功返回0,否则<0</code></pre>
<h1>void avformat_close_input(AVFormatContext **s)</h1>
<h5>简要描述</h5>
<ul>
<li>关闭一个打开的输入AVFormatContext。释放它和它的所有内容,并将*s设置为NULL</li>
</ul>
<table>
<thead>
<tr>
<th>参数</th>
<th>类型</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>s</td>
<td>AVFormatContext**</td>
<td>要关闭的一个格式化的上下文</td>
</tr>
</tbody>
</table>
<h5>使用方法</h5>
<pre><code> AVFormatContext* pCtx;
pCtx = avformat_alloc_context();
ret = avformat_open_input(&pCtx, url.c_str(), 0, 0);//执行成功返回0,否则<0
...
avformat_close_input(&pCtx);</code></pre>
<h2>int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)</h2>
<h5>简要描述</h5>
<ul>
<li>读取媒体文件的数据包以获取流信息</li>
</ul>
<table>
<thead>
<tr>
<th>参数</th>
<th>类型</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>ic</td>
<td>AVFormatContext*</td>
<td>媒体文件句柄</td>
</tr>
<tr>
<td>options</td>
<td>AVDictionary**</td>
<td>其它参数设置。它是一个字典,用于参数传递,不传则写NULL</td>
</tr>
</tbody>
</table>
<h5>使用方法</h5>
<pre><code> int ret;
AVFormatContext* pCtx;
pCtx = avformat_alloc_context();
ret = avformat_open_input(&pCtx, url.c_str(), 0, 0);
if (ret < 0)
{
std::cout << "could not open this url!" << std::endl;
goto end;
}
ret = avformat_find_stream_info(pCtx, 0);
if (ret < 0)
{
std::cout << "failed to get this stream information!" << std::endl;
goto end;
}</code></pre>
<h2>void av_dump_format(AVFormatContext <em>ic, int index, const char </em>url, int is_output)</h2>
<h5>简要描述</h5>
<ul>
<li>打印关于输入或输出格式的详细信息,例如持续时间,比特率,流,容器,程序,元数据,边数据,编解码器和时基</li>
</ul>
<table>
<thead>
<tr>
<th>参数</th>
<th>类型</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>ic</td>
<td>AVFormatContext*</td>
<td>媒体文件句柄</td>
</tr>
<tr>
<td>index</td>
<td>int</td>
<td>要转储信息的流的索引</td>
</tr>
<tr>
<td>url</td>
<td>char*</td>
<td>要打印的URL,例如源文件或目标文件</td>
</tr>
<tr>
<td>is_output</td>
<td>最后一个参数填0,打印输入流;最后一个参数填1,打印输出流</td>
</tr>
</tbody>
</table>
<h5>使用方法</h5>
<pre><code> av_dump_format(pCtx, 0, url.c_str(), 0);</code></pre>
<h2>ff_const59 AVOutputFormat <em>av_guess_format(const char </em>short_name, const char <em>filename, const char </em>mime_type)</h2>
<h5>简要描述</h5>
<ul>
<li>返回与所提供参数最匹配的已注册输出格式列表中的输出格式,如果不匹配则返回NULL。</li>
</ul>
<table>
<thead>
<tr>
<th>参数</th>
<th>类型</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>short_name</td>
<td>const char*</td>
<td>if non-NULL检查short_name是否与注册格式的名称匹配</td>
</tr>
<tr>
<td>filename</td>
<td>const char*</td>
<td>if non-NULL检查filename是否与注册格式的名称匹配</td>
</tr>
<tr>
<td>mime_type</td>
<td>const char *</td>
<td>if non-NULL检查mime_type是否与注册格式的MIME类型匹配</td>
</tr>
</tbody>
</table>
<h5>使用方法</h5>
<pre><code> AVFormatContext* fctx = NULL;
fctx = avformat_alloc_context();
AVOutputFormat* pOutfmt = av_guess_format(NULL, filename.c_str(), NULL);
fctx->oformat = pOutfmt;</code></pre>
<h2>int avio_open2(AVIOContext <strong>s, const char <em>url, int flags, const AVIOInterruptCB </em>int_cb, AVDictionary </strong>options)</h2>
<h5>简要描述</h5>
<ul>
<li>创建并初始化一个AVIOContext来访问由url指示的资源。当url指定的资源以读写模式打开时,AVIOContext只能用于写。</li>
</ul>
<table>
<thead>
<tr>
<th>参数</th>
<th>类型</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>s</td>
<td>AVIOContext**</td>
<td>用于返回指向创建的AVIOContext的指针。在失败的情况下,指向的值被设置为NULL</td>
</tr>
<tr>
<td>url</td>
<td>const char*</td>
<td>地址</td>
</tr>
<tr>
<td>flags</td>
<td>int</td>
<td>控制如何打开由url指示的资源的标志</td>
</tr>
<tr>
<td>int_cb</td>
<td>const AVIOInterruptCB*</td>
<td>在协议级使用的中断回调</td>
</tr>
<tr>
<td>options</td>
<td>AVDictionary**</td>
<td>其它参数设置。它是一个字典,用于参数传递,不传则写NULL</td>
</tr>
</tbody>
</table>
<h5>使用方法</h5>
<pre><code> int ret;
AVStream* stream = NULL;
AVFormatContext* fctx = NULL;
fctx = avformat_alloc_context();
AVOutputFormat* pOutfmt = av_guess_format(NULL, filename.c_str(), NULL);
fctx->oformat = pOutfmt;
ret = avio_open2(&fctx->pb, filename.c_str(), AVIO_FLAG_READ_WRITE, NULL, NULL);</code></pre>
<h2>AVStream <em>avformat_new_stream(AVFormatContext </em>s, const AVCodec *c)</h2>
<h5>简要描述</h5>
<ul>
<li>在 AVFormatContext 中创建 Stream 通道。</li>
</ul>
<table>
<thead>
<tr>
<th>参数</th>
<th>类型</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>s</td>
<td>AVFormatContext*</td>
<td>媒体文件句柄</td>
</tr>
<tr>
<td>url</td>
<td>const AVCodec*</td>
<td>如果非null,与新流对应的AVCodecContext将被初始化以使用这个编解码器。例如,设置特定于编解码器的默认值时需要这样做,所以如果已知的话,应该提供编解码器</td>
</tr>
</tbody>
</table>
<h5>使用方法</h5>
<pre><code> AVStream* stream = avformat_new_stream(fctx, NULL);</code></pre>
<h2>AVCodec *avcodec_find_decoder(enum AVCodecID id)</h2>
<h5>简要描述</h5>
<ul>
<li>查找具有指定名称的已注册解码器。</li>
</ul>
<table>
<thead>
<tr>
<th>参数</th>
<th>类型</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>id</td>
<td>enum AVCodecID</td>
<td>请求的解码器的AVCodecID</td>
</tr>
</tbody>
</table>
<h5>使用方法</h5>
<pre><code> AVCodecContext *pCodecCtx = pFormatCtx->streams[streamIdx]->codec;
AVCodec* pCodec = avcodec_find_decoder(pCodecCtx->codec_id);</code></pre>
<h2>AVCodec *avcodec_find_encoder(enum AVCodecID id)</h2>
<h5>简要描述</h5>
<ul>
<li>查找具有指定名称的已注册编码器。</li>
</ul>
<table>
<thead>
<tr>
<th>参数</th>
<th>类型</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>id</td>
<td>enum AVCodecID</td>
<td>请求的编码器的AVCodecID</td>
</tr>
</tbody>
</table>
<h5>使用方法</h5>
<pre><code> AVCodecContext* pCodecCtx = NULL;
pCodecCtx = pAudioStream->codec;
pCodecCtx->codec = pFormatCtx->audio_codec;
pCodecCtx->codec_id = pFormatCtx->oformat->audio_codec;
pCodecCtx->codec_type = type;
pCodecCtx->sample_fmt = AV_SAMPLE_FMT_S16;
pCodecCtx->sample_rate = pInfctx->streams[0]->codec->sample_rate;
pCodecCtx->channel_layout = AV_CH_LAYOUT_MONO;
pCodecCtx->channels = av_get_channel_layout_nb_channels(pCodecCtx->channel_layout);
pCodecCtx->bit_rate = pCodecCtx->channels * pCodecCtx->sample_rate * 16 / 8; //channels* sample_per_sec* bits_per_sample / 8;//64000
AVCodec* pCodec = avcodec_find_encoder(pCodecCtx->codec_id);</code></pre>
<h2>int avcodec_open2(AVCodecContext <em>avctx, const AVCodec </em>codec, AVDictionary **options)</h2>
<h5>简要描述</h5>
<ul>
<li>打开已注册的解码器。</li>
</ul>
<table>
<thead>
<tr>
<th>参数</th>
<th>类型</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>avctx</td>
<td>AVCodecContext*</td>
<td>解码器上下文</td>
</tr>
<tr>
<td>codec</td>
<td>const AVCodec*</td>
<td>解码器</td>
</tr>
<tr>
<td>options</td>
<td>AVDictionary**</td>
<td>其它参数设置。它是一个字典,用于参数传递,不传则写NULL</td>
</tr>
</tbody>
</table>
<h5>使用方法</h5>
<pre><code> if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0)//成功执行返回0,否则<0
{
printf("avcode open failed!\n");
exit(-2);
}</code></pre>