学习FFmpeg简单Samples之解封装

实例代码

int media_demux()
{

	
	const char* inUrl = "C:/Users/admin/Desktop/测试视频文件不同格式/Classin互动教学视频介绍.mp4";


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

	int re = avformat_open_input(&ictx, inUrl, 0, NULL);
	if (re != 0)
	{
		return XError(re);
	}
	std::cout << "open file " << inUrl << " Success." << endl;

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


	int video_stream = -1;
	int audio_stream = -1;
	//
	video_stream = av_find_best_stream(ictx, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
	audio_stream = av_find_best_stream(ictx, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0);

	std::cout << "video stream index = " << video_stream << endl;
	std::cout << "audio stream index = " << audio_stream << endl;

	if (video_stream < 0)
	{
		avformat_close_input(&ictx);
		return -1;
	}

	AVPacket* packet = av_packet_alloc();

	AVFrame* frame = av_frame_alloc();

	int ret = 0;
	int video_frame_index = 0;
	int audio_frame_index = 0;

	FILE* h264_file = fopen("media_1280x720_25_yuv420p_h264.h264", "wb");
	FILE* aac_file = fopen("media_48k_2_FLTP_aac.aac", "wb");

	AVBitStreamFilterContext* h264bsfc = av_bitstream_filter_init("h264_mp4toannexb");

	char outPut[1024 * 10] = {0};

	long long startTimr = GetCurTime();

	while (ret = av_read_frame(ictx, packet)>=0)
	{
	
		if (ret < 0) {
			break;
		}

		if (packet->stream_index == video_stream)
		{
			ret = av_bitstream_filter_filter(h264bsfc, ictx->streams[video_stream]->codec, NULL, &packet->data, &packet->size, packet->data, packet->size, 0);

			std::cout << "read " << ++video_frame_index << " video frame success " << ret << endl;

			std::fwrite(packet->data, 1, packet->size, h264_file);

			av_free(packet->data);
		}
		else if (packet->stream_index == audio_stream)
		{

			int outSize = AddAdtsHead(ictx->streams[audio_stream]->codec->extradata, ictx->streams[audio_stream]->codec->extradata_size,packet->data,packet->size, outPut);
			
			std::cout << "read " << ++audio_frame_index << " audio frame success size = "<< outSize << endl;

			std::fwrite(outPut, 1, outSize, aac_file);
		}

		av_packet_unref(packet);
	}

	//read 12415 frame use mstime = 7956  2分40秒的视频 解封装 需要 8秒左右
	std::cout << "read "<< video_frame_index+ audio_frame_index <<" frame use mstime = " << (GetCurTime() - startTimr)/1000 << endl; 
	std::cout << "cpu  count  = " << av_cpu_count() << endl;
	std::cout << "framerate = " << ictx->streams[video_stream]->codec->framerate.num << endl;
	std::cout << "avstream timebase = " << ictx->streams[video_stream]->time_base.num << " / " <<ictx->streams[video_stream]->time_base.den <<endl;
	std::cout << "codectx timebase = " << ictx->streams[video_stream]->codec->time_base.num << " / " << ictx->streams[video_stream]->codec->time_base.den <<endl;

	std::fclose(h264_file);
	std::fclose(aac_file);

	av_bitstream_filter_close(h264bsfc);
	h264bsfc = NULL;
	
	avformat_close_input(&ictx);

	av_packet_free(&packet);

	av_frame_free(&frame);

	return 0;
}

你可能感兴趣的:(ffmpeg,c++)