博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
FFmpeg浅尝辄止(二)——YUV视频序列编码为视频 ~~ 新版修改版-调通
阅读量:4298 次
发布时间:2019-05-27

本文共 4382 字,大约阅读时间需要 14 分钟。

文章来源: 

针对新版FFmpeg进行修改后,VC2010调通

AVFormatContext* oc;  	AVOutputFormat* fmt;  	AVStream* video_st;  	double video_pts;  	uint8_t* video_outbuf;  	uint8_t* picture_buf;  	AVFrame* picture;  	//  AVFrame* pictureRGB;  	int size;  	int ret;  	int video_outbuf_size;  		FILE *fin = fopen("akiyo_qcif.yuv", "rb"); //视频源文件   		const char* filename = "test.mpeg";	fmt = av_guess_format(NULL, filename, NULL);  		int retValue = avformat_alloc_output_context2(&oc,fmt,NULL,filename); 		oc->oformat = fmt; 	_snprintf(oc->filename, sizeof(oc->filename), "%s", filename);  		video_st = NULL;  	if (fmt->video_codec != CODEC_ID_NONE)  	{  		AVCodecContext* c;  		video_st = avformat_new_stream(oc, 0);  		c = video_st->codec;  		c->codec_id = fmt->video_codec;  		c->codec_type = AVMEDIA_TYPE_VIDEO;  		c->bit_rate = 400000;  		c->width = 176;  		c->height = 144;  		c->time_base.num = 1;  		c->time_base.den = 25;   		c->gop_size = 12;  		c->pix_fmt = PIX_FMT_YUV420P;  		if (c->codec_id == CODEC_ID_MPEG2VIDEO)  		{  			c->max_b_frames = 2;  		}  		if (c->codec_id == CODEC_ID_MPEG1VIDEO)  		{  			c->mb_decision = 2;  		}  		if (!strcmp(oc->oformat->name, "mp4") ||			!strcmp(oc->oformat->name, "mov") || 			!strcmp(oc->oformat->name, "3gp"))  		{  			c->flags |= CODEC_FLAG_GLOBAL_HEADER;  		}  			/*if (av_set_parameters(oc, NULL)<0)  	{  	return;  	}  */		av_opt_set(c->priv_data,"preset","superfast",0);		av_opt_set(c->priv_data,"tune","zerolatency",0);	}  		av_dump_format(oc, 0, filename, 1);  		if (video_st)  	{  		AVCodecContext* c;  		AVCodec* codec;  		c = video_st->codec;  		codec = avcodec_find_encoder(c->codec_id);  		if (!codec)  		{  			return 0;  		}  		if (avcodec_open2(c, codec,NULL) < 0)  		{  			return 0;  		}  		if (!(oc->oformat->flags & AVFMT_RAWPICTURE))  		{  			video_outbuf_size = 200000;  			video_outbuf = (uint8_t*)av_malloc(video_outbuf_size);  		}  		picture = avcodec_alloc_frame();  		size = avpicture_get_size(c->pix_fmt, c->width, c->height);  		picture_buf = (uint8_t*)av_malloc(size);  		if (!picture_buf)  		{  			av_free(picture);  		}  		avpicture_fill((AVPicture*)picture, picture_buf, c->pix_fmt, c->width, c->height);  	}  		if (!(fmt->flags & AVFMT_NOFILE))  	{  		if (avio_open(&oc->pb, filename, AVIO_FLAG_WRITE) < 0)  		{  			return 0;  		}  	}  	avformat_write_header(oc,NULL);  			for (int i=0; i<300; i++)  	{  		if (video_st)  		{  			video_pts = (double)(video_st->pts.val * video_st->time_base.num / video_st->time_base.den);  		}  		else  		{  			video_pts = 0.0;  		}  		if (!video_st/* || video_pts >= 5.0*/)  		{  			break;  		}  		AVCodecContext* c;  		c = video_st->codec;  		size = c->width * c->height;  			if (fread(picture_buf, 1, size*3/2, fin) < 0)  		{  			break;  		}  			picture->data[0] = picture_buf;  // 亮度  		picture->data[1] = picture_buf+ size;  // 色度   		picture->data[2] = picture_buf+ size*5/4; // 色度   			// 如果是rgb序列,可能需要如下代码  		//      SwsContext* img_convert_ctx;  		//      img_convert_ctx = sws_getContext(c->width, c->height, PIX_FMT_RGB24, c->width, c->height, c->pix_fmt, SWS_BICUBIC, NULL, NULL, NULL);  		//      sws_scale(img_convert_ctx, pictureRGB->data, pictureRGB->linesize, 0, c->height, picture->data, picture->linesize);  			if (oc->oformat->flags & AVFMT_RAWPICTURE)  		{  			AVPacket pkt;  			av_init_packet(&pkt);  			pkt.flags |= AV_PKT_FLAG_KEY;  			pkt.stream_index = video_st->index;  			pkt.data = (uint8_t*)picture;  			pkt.size = sizeof(AVPicture);  			ret = av_write_frame(oc, &pkt);  		}  		else  		{  			int out_size = avcodec_encode_video(c, video_outbuf, video_outbuf_size, picture);  			if (out_size > 0)  			{  				AVPacket pkt;  				av_init_packet(&pkt);  				pkt.pts = av_rescale_q(c->coded_frame->pts, c->time_base, video_st->time_base);  				if (c->coded_frame->key_frame)  				{  					pkt.flags |= AV_PKT_FLAG_KEY;  				}  				pkt.stream_index = video_st->index;  				pkt.data = video_outbuf;  				pkt.size = out_size;  				ret = av_write_frame(oc, &pkt);  			}  		}  	}  		if (video_st)  	{  		avcodec_close(video_st->codec);  		//      av_free(picture->data[0]);  		av_free(picture);  		av_free(video_outbuf);  		//      av_free(picture_buf);  	}  	av_write_trailer(oc);  	for (int i=0; i
nb_streams; i++) { av_freep(&oc->streams[i]->codec); av_freep(&oc->streams[i]); } if (!(fmt->flags & AVFMT_NOFILE)) { avio_close(oc->pb); } av_free(oc);

转载地址:http://nwiws.baihongyu.com/

你可能感兴趣的文章
Hive创建table报错:Permission denied: user=lenovo, access=WRITE, inode="":suh:supergroup:rwxr-xr-x
查看>>
Hive执行job时return code 2排查
查看>>
hive常用函数及数据结构介绍
查看>>
Hive面试题干货(亲自跟着做了好几遍,会了的话对面试大有好处)
查看>>
力扣题解-589. N叉树的前序遍历(递归和迭代)
查看>>
力扣题解-700. 二叉搜索树中的搜索(分治法思想,递归的方式求解)
查看>>
力扣题解-230. 二叉搜索树中第K小的元素(递归方法,中序遍历解决)
查看>>
力扣题解-746. 使用最小花费爬楼梯(动态规划)
查看>>
力扣题解-103. 二叉树的锯齿形层序遍历(广度优先搜索)
查看>>
力扣题解-387. 字符串中的第一个唯一字符
查看>>
利用selenium爬虫模拟浏览器访问CSDN博客
查看>>
力扣题解-1046. 最后一块石头的重量
查看>>
力扣题解-86. 分隔链表
查看>>
力扣题解-123. 买卖股票的最佳时机 III(动态规划)
查看>>
力扣题解-122. 买卖股票的最佳时机 II(动态规划/贪心)
查看>>
1202. 交换字符串中的元素(并查集)
查看>>
力扣题解-684. 冗余连接(并查集)
查看>>
力扣题解-1319. 连通网络的操作次数(并查集)
查看>>
力扣题解-1579. 保证图可完全遍历(并查集)
查看>>
(2)mysql支持的数据类型
查看>>