音视频处理FFMPeg开发实战(3) -- 视频媒体文件打开和输出

本例演示如何打开一个媒体文件,把输出到另一个媒体文件中,并把一些包去掉。

定义全局变量

AVFormatContext *g_inputContext = NULL;
AVFormatContext * g_outputContext;
int64_t g_lastReadPacktTime = 0;

1、打开一个输入媒体文件

打开媒体文件,读取媒体文件的数据包,分析其中的流信息。分析其帧率


int OpenInputMediaFile(string inputUrl)
{
	//创建一个AVFormatContext类型变量并初始化默认参数
	g_inputContext = avformat_alloc_context();

	// 取得时间,单位为microsecond,微秒
	g_lastReadPacktTime = av_gettime();
	
	// 填写检测是否中止阻塞功能的回调函数。
	g_inputContext->interrupt_callback.callback = interrupt_cb;

	// 强制以指定格式打开,为NULL时,自动检测其格式。
	AVInputFormat *fmt = NULL;

	// 强制以指定格式打开,为NULL时,自动检测其格式。
	AVDictionary **options = NULL;

	// 打开一个输入流(本地文件名或者是网络流地址URL),并解析其头部格式,并把信息填写到g_inputContext中。
	int ret = avformat_open_input(&g_inputContext, inputUrl.c_str(), fmt, options);
	if (ret < 0)
	{
		return  ret;
	}

	// 读取媒体文件的数据包,分析其中的流信息。分析其帧率
	ret = avformat_find_stream_info(g_inputContext, nullptr);

	return ret;
}

2、打开输出媒体文件


int OpenOutputMediaFile(string outUrl)
{
	// 创建一个用于输出的格式信息对像(AVFormatContext)
	// 格式指定flv
	int ret = avformat_alloc_output_context2(&g_outputContext, nullptr, "flv", outUrl.c_str());
	if (ret < 0)
	{
		goto Error;
	}

	// 打开文件 并初始AVIOContext(g_outputContext->pb)
	ret = avio_open2(&g_outputContext->pb, outUrl.c_str(), AVIO_FLAG_WRITE, nullptr, nullptr);
	if (ret < 0)
	{
		goto Error;
	}

	// 找出源媒体中流的信息
	for (int i = 0; i < g_inputContext->nb_streams; i++)
	{
		// 用输入流的编码信息来创建一个流对像,
		AVStream * stream = avformat_new_stream(g_outputContext, g_inputContext->streams[i]->codec->codec);

		// 把输入流的编码信息拷贝到个输出流对的编码信息中,
		ret = avcodec_copy_context(stream->codec, g_inputContext->streams[i]->codec);
		if (ret < 0)
		{
			goto Error;
		}
	}

	// 分配流的私有信息,并把流的头部信息写到输出文件
	ret = avformat_write_header(g_outputContext, nullptr);
	if (ret < 0)
	{
		goto Error;
	}

	return ret;
Error:

	// 出错后关闭输出环境变量
	if (g_outputContext)
	{
		for (int i = 0; i < g_outputContext->nb_streams; i++)
		{
			avcodec_close(g_outputContext->streams[i]->codec);
		}
		avformat_close_input(&g_outputContext);
	}
	return ret;
}

3、读入一帧数据


shared_ptr<AVPacket> ReadPacketFromSource()
{
	shared_ptr<AVPacket> packet(static_cast<AVPacket*>(av_malloc(sizeof(AVPacket))), [&](AVPacket *p) 
			{ av_packet_free(&p); 
				av_freep(&p); }
			);

	// 初始化packet
	av_init_packet(packet.get());
	
	// 取时间
	g_lastReadPacktTime = av_gettime();

	// 读入一帧数据到packet
	int ret = av_read_frame(g_inputContext, packet.get());
	if (ret >= 0)
	{
		return packet;
	}
	else
	{
		return nullptr;
	}
}

4、把数据写到输出文件中

int WritePacket(shared_ptr<AVPacket> packet)
{
	return av_interleaved_write_frame(g_outputContext, packet.get());
}

5、初始化库

// 初始化FFMpeg库
void InitFFMpeg()
{
	av_register_all();    // 注册所有的编解码器
	avfilter_register_all();// 注册所有的滤镜
					
	// 初网络组件。如果输入输出文件,不涉及网络,可以不调用。
	avformat_network_init();
}

6、关闭资源


void CloseInput()
{
	if (g_inputContext != nullptr)
	{
		// 关闭输入环境,释放结构中的资源
		avformat_close_input(&g_inputContext); 
	}
}

void CloseOutput()
{
	if (g_outputContext != nullptr)
	{
		for (int i = 0; i < g_outputContext->nb_streams; i++)
		{
			AVCodecContext *codecContext = g_outputContext->streams[i]->codec;
			// 关闭输出编码器,释放相关的数据。
			avcodec_close(codecContext);
		}
		// 关闭输出环境,释放结构中的资源
		avformat_close_input(&g_outputContext);
	}
}

7、主控流程


int convert_to_new_file(int startPacketNum, int discardPacketNum, string sourceFileName, string destFileName)
{
	int  discardtPacketNum = discardPacketNum;
	int packetCount = 0;
	int64_t lastPacketPts = AV_NOPTS_VALUE;
	int64_t lastPts = AV_NOPTS_VALUE;

	InitFFMpeg();
	int ret = OpenInputMediaFile(sourceFileName);
	if (ret >= 0)
	{
		ret = OpenOutputMediaFile(destFileName);
	}

	if (ret <0) 
		goto Error;
	
	int endPackNum = startPacketNum + discardtPacketNum;

	while (true)
	{
		// 从输入流中读取一个数据包
		auto packet = ReadPacketFromSource();
		if (packet)
		{
			packetCount++;
			// 不在指定范围的数据包,输出到文件中。
			if (packetCount <= startPacketNum || packetCount >= endPackNum)
			{
				// 重新计算输出数据包中的时间
				if (packetCount >= endPackNum)
				{
					if (packet->pts - lastPacketPts > 120) // 说明是跨区间【startPacketNum,endPackNum】的帧
					{
						lastPts = lastPacketPts + 30; // 加上一个帧率间隔,假定是30ms.
					}
					else
					{
						auto diff = packet->pts - lastPacketPts;// 计算与上一数据包的时间差
						lastPts += diff;
					}
				}
				lastPacketPts = packet->pts;// 记录输入数据包中的时间信息
				if (lastPts != AV_NOPTS_VALUE)
				{
					packet->pts = packet->dts = lastPts;
				}
				ret = WritePacket(packet);// 输出到文件中。
			}// 不在指定范围的输入数据,不写入。
		}
		else
		{
			break;
		}
	}
Error:
	CloseInput(); // 关闭输入
	CloseOutput();// 关闭输出
	return 0;
}

8、下载完成代码

在Debug–x86编译通过。

下载源文件

发布了43 篇原创文章 · 获赞 9 · 访问量 2653

猜你喜欢

转载自blog.csdn.net/x879014419/article/details/105267968