zoukankan      html  css  js  c++  java
  • avi视频格式转yuv格式与播放yuv视频

    因为要用到yuv格式视频。而眼下仅仅有avi格式的视频,所以须要转换,而且opencv不支持yuv编码的视频播放。所以须要转换为rgb编码。而后播放。写了两个程序。以供參考:

    1,avi格式视频转yuv格式视频

    2,播放并保存yuv视频

    #include <stdlib.h>
    #include <stdio.h>
    #include <math.h>
    #include <cv.h>
    #include <highgui.h>
    void avi2WriteYuv(const char *in,const char *out)
    {
    	cv::VideoCapture vc;
    	bool flag = vc.open(in);
    	if (!flag)
    	{
    		printf("avi file open error 
    ");
    		system("pause");
    		exit(-1);
    	}
    
    	int frmCount = vc.get(CV_CAP_PROP_FRAME_COUNT);
    	frmCount -= 5;
    	printf("frmCount: %d 
    ", frmCount);
    
    	int w = vc.get(CV_CAP_PROP_FRAME_WIDTH);
    	int h = vc.get(CV_CAP_PROP_FRAME_HEIGHT);
    	printf("wwwwww%d
    ", w);
    	printf("hhhhhh%d
    ", h);
    	int bufLen = w*h * 3 / 2;
    	unsigned char* pYuvBuf = new unsigned char[bufLen];
    	FILE* pFileOut = fopen(out, "w+");
    	if (!pFileOut)
    	{
    		printf("pFileOut open error 
    ");
    		system("pause");
    		exit(-1);
    	}
    	printf("pFileOut open ok 
    ");
    
    	for (int i = 0; i<frmCount; i++)
    	{
    		printf("%d/%d 
    ", i + 1, frmCount);
    
    		cv::Mat srcImg;
    		vc >> srcImg;
    
    		cv::imshow("img", srcImg);
    		cv::waitKey(1);
    
    		cv::Mat yuvImg;
    		cv::cvtColor(srcImg, yuvImg, CV_BGR2YUV_I420);
    		memcpy(pYuvBuf, yuvImg.data, bufLen*sizeof(unsigned char));
    
    		fwrite(pYuvBuf, bufLen*sizeof(unsigned char), 1, pFileOut);
    	}
    
    	fclose(pFileOut);
    	delete[] pYuvBuf;
    }
    void DisplayYUV2RGB(const char *dir,const char *in,int _w,int _h)
    {
    	int w = _w;
    	int h = _h;
    	printf("yuv file w: %d, h: %d 
    ", w, h);
    
    	FILE* pFileIn = fopen(in, "rb+");
    	int bufLen = w*h * 3 / 2;
    	unsigned char* pYuvBuf = new unsigned char[bufLen];
    	int iCount = 0;
    
    
    	for (int i = 0; i<95; i++)
    	{
    		fread(pYuvBuf, bufLen*sizeof(unsigned char), 1, pFileIn);
    
    		cv::Mat yuvImg;
    		yuvImg.create(h * 3 / 2, w, CV_8UC1);
    		memcpy(yuvImg.data, pYuvBuf, bufLen*sizeof(unsigned char));
    		cv::Mat rgbImg;
    		cv::cvtColor(yuvImg, rgbImg, CV_YUV2BGR_I420);
    
    		cv::imshow("img", rgbImg);
    		char s[100];
    		sprintf(s,"%spic%d%s",dir,i,".jpg");
    		cv::imwrite(s, rgbImg);
    		cv::waitKey(1);
    
    		printf("%d 
    ", iCount++);
    	}
    
    	delete[] pYuvBuf;
    
    
    	fclose(pFileIn);
    }
    
    int main(int argc, char *argv[])
    {
    	const char *in = "C:/Users/jiang/Desktop/output/outfile.avi";
    	const char *out = "C:/Users/jiang/Desktop/output/outfile.yuv";
    	const char *dir = "C:/Users/jiang/Desktop/output/tupian1/";
    	avi2WriteYuv(in,out);
    	DisplayYUVtoRGB(dir,out, 1024, 768);
    	return 0;
    }


  • 相关阅读:
    linuxc查看进程命令
    Springboot+post请求接口
    Springboot+get请求接口
    xml 加载多个properties文件
    TCP的三次握手(建立连接)和四次挥手(关闭连接)
    记一次 synchronized 锁字符串引发的坑兼再谈 Java 字符串
    java单点登录原理与简单实现
    关于 Java 面试,你应该准备这些知识点
    Java 内存模型
    java利用SuffixFileFilter统计目录下特定后缀名文件的数目
  • 原文地址:https://www.cnblogs.com/cynchanpin/p/6714286.html
Copyright © 2011-2022 走看看