解封装(六):通过遍历获取AVStream音视频流信息并打印参数

一般获取音视频流的方式有如下两种方法:如下代码

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

	//第一种获取音视频流的方法
	//获取音频流信息,(遍历,函数获取)
	for (int i = 0; i < ic->nb_streams; i++)
	{
		AVStream *as = ic->streams[i];
		//判断哪个是音频,哪个是视频
		//音频
		if (as->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
		{
			audioStream = i;
			cout << i<<"音频信息" << endl;
			cout << "sample rete =" << as->codecpar->sample_rate << endl;
			//AVSampleFormat:
			cout << "format =" << as->codecpar->format << endl;
			cout << "channels =" << as->codecpar->channels	 << endl;
			cout << "codec_id =" << as->codecpar->codec_id << endl;   //包含了pcm格式,没有压缩的
			cout << "audio fps=" << r2d(as->avg_frame_rate) <&
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为了使用FFmpe修复H265视频,需要进行以下步骤: 1. 包含FFmpeg头文件和链接到库。可以使用以下命令链接FFmpeg库: ```shell g++ -o output input.cpp -lavformat -lavcodec -lavutil ``` 2. 打开输入文件并获取信息: ```c++ AVFormatContext *inputFormatContext = NULL; if (avformat_open_input(&inputFormatContext, inputFileName, NULL, NULL) < 0) { // 打开输入文件失败 return; } if (avformat_find_stream_info(inputFormatContext, NULL) < 0) { // 获取信息失败 return; } ``` 3. 查找视频并打开码器: ```c++ int videoStreamIndex = -1; for (int i = 0; i < inputFormatContext->nb_streams; i++) { if (inputFormatContext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { videoStreamIndex = i; break; } } if (videoStreamIndex == -1) { // 没有找到视频 return; } AVCodecParameters *videoCodecParameters = inputFormatContext->streams[videoStreamIndex]->codecpar; AVCodec *videoCodec = avcodec_find_decoder(videoCodecParameters->codec_id); if (videoCodec == NULL) { // 找不到码器 return; } AVCodecContext *videoCodecContext = avcodec_alloc_context3(videoCodec); if (avcodec_parameters_to_context(videoCodecContext, videoCodecParameters) < 0) { // 复制码器参数失败 return; } if (avcodec_open2(videoCodecContext, videoCodec, NULL) < 0) { // 打开码器失败 return; } ``` 4. 创建输出文件并写入头信息: ```c++ AVFormatContext *outputFormatContext = NULL; if (avformat_alloc_output_context2(&outputFormatContext, NULL, NULL, outputFileName) < 0) { // 创建输出文件失败 return; } if (avio_open(&outputFormatContext->pb, outputFileName, AVIO_FLAG_WRITE) < 0) { // 打开输出文件失败 return; } AVStream *outputVideoStream = avformat_new_stream(outputFormatContext, NULL); if (outputVideoStream == NULL) { // 创建输出视频失败 return; } AVCodecParameters *outputVideoCodecParameters = avcodec_parameters_alloc(); if (avcodec_parameters_copy(outputVideoCodecParameters, videoCodecParameters) < 0) { // 复制码器参数失败 return; } outputVideoCodecParameters->codec_tag = 0; if (avformat_write_header(outputFormatContext, NULL) < 0) { // 写入头信息失败 return; } ``` 5. 读取视频帧并进行修复: ```c++ AVPacket packet; while (av_read_frame(inputFormatContext, &packet) >= 0) { if (packet.stream_index == videoStreamIndex) { AVFrame *frame = av_frame_alloc(); if (frame == NULL) { // 分配帧内存失败 break; } if (avcodec_send_packet(videoCodecContext, &packet) < 0) { // 发送数据包到码器失败 av_frame_free(&frame); break; } while (avcodec_receive_frame(videoCodecContext, frame) >= 0) { // 修复视频帧 // ... // 将修复后的帧写入输出文件 if (avcodec_send_frame(outputVideoCodecContext, frame) < 0) { // 发送帧到编码器失败 av_frame_free(&frame); break; } while (avcodec_receive_packet(outputVideoCodecContext, &packet) >= 0) { av_packet_rescale_ts(&packet, videoCodecContext->time_base, outputVideoStream->time_base); packet.stream_index = outputVideoStream->index; if (av_interleaved_write_frame(outputFormatContext, &packet) < 0) { // 写入数据包到输出文件失败 av_packet_unref(&packet); av_frame_free(&frame); break; } av_packet_unref(&packet); } } av_frame_free(&frame); } av_packet_unref(&packet); } ``` 6. 写入输出文件的尾信息并释放资源: ```c++ av_write_trailer(outputFormatContext); avcodec_free_context(&videoCodecContext); avformat_close_input(&inputFormatContext); avformat_free_context(inputFormatContext); avcodec_parameters_free(&outputVideoCodecParameters); avformat_free_context(outputFormatContext); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值