FFMPEG直播推流

所用到的变量

//输入流
AVForamtContext  *ictx  =  NULL:  //封装格式上下文
char  *inUrl  =  " test.mp4 ";  //输入的视频文件

//输出流
AVForamtContext  *octx  =  NULL:  //创建输出流上下文
char  *outUrl  =  " test.flv "; 

av_register_all();

初始化所有的封装和解封装,flv,mp4,mov,,,,

avforamt_network_init();

初始化网络库。

avformat_open_input();

打开文件,解封文件头。一般h264是没有头的,他的文件信息一般都放在后面。
参数:
AVFormatContext **ps,封装格式上下文
const char *url, 文件名或者文件的绝对路径
AVInputFormat *fmt, 指定封装或者解封格式
AVDictionary **option,
用法:

int ret  =  avformat_open_input (&ictx, url, NULL, NULL)
	if (re != 0)
	{
		char  buf[1024 ] =  {0};
		av_strerror(re, buf, sizeof(buf));
		cout<< buf<< endl;
	}

avformat_find_stream_info();

获取音视频流信息,h264,flv
使用:

int ret = avformat_find_stream_info(ictx,0);
if (re != 0)
	{
		char  buf[1024 ] =  {0};
		av_strerror(re, buf, sizeof(buf));
		cout<< buf<< endl;
	}

av_dump_format();

打印关于输入或输出格式的详细信息,例如
*持续时间,比特率,流,容器,程序,元数据,侧数据,
*编解码器和时基。

av_dump_format(ictx, 0, inUrl, 0);

输出流:

avformat_alloc_output_context2();

为输出格式分配一个AVFormatContext。
avformat_free_context()可以用来释放上下文和所有由框架分配的内容。
int avformat_alloc_output_context2(AVFormatContext **ctx, ff_const59 AVOutputFormat *oformat, const char *format_name, const char *filename);
3:封装格式名称
4:输出文件名称

int  ret = avformat_alloc_output_context2(&octx, 0, "flv", "outUrl");
if (ret != 0)
	{
		char  buf[1024 ] =  {0};
		av_strerror(re, buf, sizeof(buf));
		cout<< buf<< endl;
	}

配置输出流

遍历输入的AVstream

for (int i = 0; i < octx->nb_streams; i++)
{
	//创建输出流
	//向媒体文件中添加一个新的流
	AVStrema &out = avformat_new_stream(octx, ictx->stream[i] -> codec->codec);
	//将源AVCodecContext的设置复制到目标中AVCodecContext。
	//适用于mp4
	//accodec_copy_context(out->codec; ictx->stream[i] ->codec);
	avcodec_parameters_copy(out->codecpar; ictx->stream[i] ->codecpar)
	out->codec->codec_tag = 0;
}

rtmp推流

打开IO
avio_open(&octx->pb, outUrl, AVIO_FLAG_WRITE);
if(!octx->pb)
{
	return -1;
}
写入头信息
int re = avformat_write_hander(octx, 0,);
if(re<0)
{
	return -1;
}
推流每一帧数据
AVPacket pkt;
//获取当前时间戳
long long strarttime = av_gettime();
for{;;}
{
	int re = av_read_frame(ictx, &pkt);
	if(re != 0)
		break;
	cout<< pkt.pts <<" "<<flush;
		
	//计算转换pts, dts
	AVRational itime = ictx->stream[pkt.stream_index]->time_base;
	AVRational otime = octx->stream[pkt.stream_index]->time_base;
	pkt.pts = av_rescale_q_rnd(pkt.pts, itime, otime,(AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
	pkt.dts = av_rescale_q_rnd(pkt.dts, itime, otime,(AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
	pkt.duration = av_rescale_q_rnd(pkt.duration, itime, otime,(AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
	pkt.pos = -1;

	//判断是视频帧
	//控制视频帧的推送速度
	if(ictx->streams[pkt.stream_index]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
	{
		AVRational tb = ictx->streams[pkt.steeams_index]->time_base;
		//已经过去的时间
		long long now = av_gettime() - startTime;
		long long dts = 0;
		dts = pkt.dts * (1000 * 1000 * tb.num / tb.den);
		if(dts > now)
		{
			av_usleep(dts - now);
		}
		
		re = av_interleaves_write_frame(octx,&pkt);
	}
	if(re<0)
	{
		return -1;
	}
	
	//空间释放
	av_packet_unrcf(&pkt);
}

main.c

extern "C"
{
#include "libavformat/avformat.h"
#include "libavutil/time.h"
}
#include <iostream>
using namespace std;
#pragma comment(lib,"avformat.lib")
#pragma comment(lib,"avutil.lib")
#pragma comment(lib,"avcodec.lib")

int XError(int errNum)
{
	char buf[1024] = { 0 };
	av_strerror(errNum, buf, sizeof(buf));
	cout << buf << endl;
	getchar();
	return -1;
}
static double r2d(AVRational r)
{
	return r.num == 0 || r.den == 0 ? 0. : (double)r.num / (double)r.den;
}

int main(int argc, char *argv[])
{

	char *inUrl = "test.flv";
	char *outUrl = "rtmp://192.168.1.44/live";

	//初始化所有封装和解封装 flv mp4 mov mp3
	av_register_all();

	//初始化网络库
	avformat_network_init();

	//
	//输入流 1 打开文件,解封装
	//输入封装上下文
	AVFormatContext *ictx = NULL;

	//打开文件,解封文件头
	int re = avformat_open_input(&ictx, inUrl, 0, 0);
	if (re != 0)
	{
		return XError(re);
	}
	cout << "open file " << inUrl << " Success." << endl;

	//获取音频视频流信息 ,h264 flv
	re = avformat_find_stream_info(ictx, 0);
	if (re != 0)
	{
		return XError(re);
	}
	av_dump_format(ictx, 0, inUrl, 0);
	//


	//
	//输出流 

	//创建输出流上下文
	AVFormatContext *octx = NULL;
	re = avformat_alloc_output_context2(&octx, 0, "flv", outUrl);
	if (!octx)
	{
		return XError(re);
	}
	cout << "octx create success!" << 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 XError(0);
		}
		//复制配置信息,同于MP4
		//re = avcodec_copy_context(out->codec, ictx->streams[i]->codec);
		re = avcodec_parameters_copy(out->codecpar, ictx->streams[i]->codecpar);
		out->codec->codec_tag = 0;
	}
	av_dump_format(octx, 0, outUrl, 1);
	//


	//rtmp推流

	//打开io
	re = avio_open(&octx->pb, outUrl, AVIO_FLAG_WRITE);
	if (!octx->pb)
	{
		return XError(re);
	}

	//写入头信息
	re = avformat_write_header(octx, 0);
	if (re < 0)
	{
		return XError(re);
	}
	cout << "avformat_write_header " << re << endl;
	AVPacket pkt;
	long long startTime = av_gettime();
	for (;;)
	{
		re = av_read_frame(ictx, &pkt);
		if (re != 0)
		{
			break;
		}
		cout << pkt.pts << " " << flush;
		//计算转换pts dts
		AVRational itime = ictx->streams[pkt.stream_index]->time_base;
		AVRational otime = octx->streams[pkt.stream_index]->time_base;
		pkt.pts = av_rescale_q_rnd(pkt.pts, itime, otime, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_NEAR_INF));
		pkt.dts = av_rescale_q_rnd(pkt.pts, itime, otime, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_NEAR_INF));
		pkt.duration = av_rescale_q_rnd(pkt.duration, itime, otime, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_NEAR_INF));
		pkt.pos = -1;

		//视频帧推送速度
		if (ictx->streams[pkt.stream_index]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
		{
			AVRational tb = ictx->streams[pkt.stream_index]->time_base;
			//已经过去的时间
			long long now = av_gettime() - startTime;
			long long dts = 0;
			dts = pkt.dts * (1000 * 1000 * r2d(tb));
			if (dts > now)
				av_usleep(dts - now);
		}

		re = av_interleaved_write_frame(octx, &pkt);
		if (re<0)
		{
			return XError(re);
		}
	}

	cout << "file to rtmp test" << endl;
	getchar();
	return 0;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值