FFmpeg 版本
version 4.0.2
搭建Rtmp服务
具体可以参考一下其它文章,本文主要讲的是如何使用FFmpeg api接口实现推流功能
推流flv文件
av_register_all();
AVFormatContext *ictx = NULL;
//打开文件,解封文件头
int re = avformat_open_input(&ictx, inUrl, 0, 0);
if (re != 0)
{
return ff_Error(re);
}
cout << "open file " << inUrl << " success..." << endl;
//获取音频视频流信息
re = avformat_find_stream_info(ictx, 0);
if (re != 0)
{
return ff_Error(re);
}
cout << "打印输入文件信息" << endl;
av_dump_format(ictx, 0, inUrl, 0);
//创建输出流上下文
AVFormatContext *octx = NULL;
re = avformat_alloc_output_context2(&octx, 0, "flv", outUrl);
if (!octx)
{
return ff_Error(re);
}
cout << "输出上下文创建成功" << endl;
//配置输出流
//遍历输入的AVStream
for (int i = 0; i < ictx->nb_streams; i++)
{
//创建输出流
AVStream *out = avformat_new_stream(octx, ictx->streams[i]->codec->codec);
if (!out)
{
return ff_Error(0);
}
//复制配置信息
AVCodecParameters *codecpar = ictx->streams[i]->codecpar;
re = avcodec_parameters_copy(out->codecpar, codecpar);
out->codec->codec_tag = 0;
}
cout << "打印输出流信息" << endl;
av_dump_format(octx, 0, outUrl, 1);
//rtmp推流