ffmpeg音视频解封装代码参考


#include <iostream>
using namespace std;

extern "C" {
#include "libavformat/avformat.h"
#include "libavutil/rational.h"
}

static double r2d(AVRational r) {
	return r.den == 0 ? 0 : (double)r.num / (double)r.den;
}

int main()
{
	cout << "Test Demux FFmpeg.club" << endl;

	const char* path = "test.mp4";

	//初始化封装库
	av_register_all();

	//初始化网络库(可以打开rtsp rtmp http 协议的流媒体视频)
	avformat_network_init();

	
	AVDictionary* opts = nullptr;	//参数设置-字典对象
	AVFormatContext* ic = nullptr;	//解封装上下文

	//设置rtsp流以tcp协议打开
	av_dict_set(&opts, "rtsp_transport", "tcp", 0);
	//网络延时时间
	av_dict_set(&opts, "max_delay", "500", 0);

	//打开视频文件
	int re = avformat_open_input(&ic, path
		, 0 ///表示自动选择解封装器
		, &opts ///参数设置,比如rtsp的延时时间
	);

	if (re != 0) {
		char buf[1024];
		av_strerror(re, buf, sizeof(buf) - 1);
		cout << "open " << path << " failed: " << buf << endl << endl;
		return -1;
	}

	cout << "open " << path << " success" << endl << endl;

	//获取流信息
	re = avformat_find_stream_info(ic, nullptr);

	//总时长(s)
	int total = ic->duration / AV_TIME_BASE;
	cout << "total = " << total << " s" << endl;

	//打印视频流的详细信息
	cout << "=========================================" << endl;
	cout << "视频流详细信息" << endl;
	av_dump_format(ic, 0, path, 0);

	//音视频索引,读取时区分音视频
	int videoStream = 0;
	int audioStream = 1;

	//获取音视频流信息(遍历,函数获取)
	//[0]遍历获取
	for (int i = 0; i < ic->nb_streams; i++) {

		AVStream* as = ic->streams[i];

		if (as->codecpar->codec_type == AVMediaType::AVMEDIA_TYPE_AUDIO) {
			audioStream = i;

			cout << "=========================================" << endl;
			cout << "音频信息" << endl;
			cout << "样本格式 = " << as->codecpar->format << endl;
			cout << "通道 = " << as->codecpar->channels << endl;
			cout << "codec_id = " << as->codecpar->codec_id << endl;
			//单通道样本数
			cout << "frame_size = " << as->codecpar->frame_size << endl;
			//1024*2*2=4096 fps = sample_rate/frame_size
		}
		else if (as->codecpar->codec_type == AVMediaType::AVMEDIA_TYPE_VIDEO) {
			videoStream = i;

			cout << "=========================================" << endl;
			cout << "视频信息" << endl;
			//这个时候不一定能够正常打印出来,解码之后获取更可靠
			cout << "width = " << as->codecpar->width << endl;
			cout << "height = " << as->codecpar->height << endl;
			
			//帧率 fps
			cout << "video fps = " << r2d(as->avg_frame_rate) << endl;
			
		}
	}
	//[1]函数获取
	videoStream = av_find_best_stream(ic, AVMediaType::AVMEDIA_TYPE_VIDEO, -1, -1, nullptr, 0);
	//ic->streams[videoStream]
	audioStream = av_find_best_stream(ic, AVMediaType::AVMEDIA_TYPE_AUDIO, -1, -1, nullptr, 0);
	//ic->streams[audioStream]


	//======================================================================
	if (ic) {
		//释放封装上下文,并且把ic置为0
		avformat_close_input(&ic);
	}
	
	

	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值