H264转YUV,循环获取每一帧数据

使用ffmpeg指令得到yuv视频文件

	#安装ffmpeg
	sudo apt update
	sudo apt install ffmpeg
	ffmpeg -version
	
	#转换音频和视频文件时ffmpeg从文件扩展名中猜测输出格式
	ffmpeg -i input.mp4 output.webm   
	ffmpeg -i input.mp3 output.ogg​    


	#转换指令
	ffmpeg -i test.h264 -s 1920*1080 -pix_fmt nv12 test264.yuv
	

读取yuv视频文件

	//yuv视频文件路径
	std::string yuvSavePath = "/home/path/to/test264.yuv";
	//循环播放前1000帧。
	static int n = 0;
	if (n++ > 1000) n = 0;
	//读取文件数据
	FILE *fp = fopen(yuvSavePath.c_str(), "rb");
	if (fp == nullptr){
		LogError << "Failed to open file: " << yuvSavePath;
		return ;}
		
	//得到整个视频的size和单帧的size
	fseek(fp, 0, SEEK_END);
	uint32_t fileSizeAll = ftell(fp);
	uint32_t fileSize = 3110400;   // 1920*1080*3/2
	fseek(fp, 0, SEEK_SET);
	if (fileSizeAll == 0){
		LogError << "File size is 0!";
		fclose(fp);
		return;}
		
	std::shared_ptr<void> hostBuf = GetHostBuffer(fileSizeAll);
	if (hostBuf == nullptr){
		LogError << "Failed to get host buffer!";
		fclose(fp);
		return;}
	//定位到当前帧
	fseek(fp, fileSize*n, SEEK_SET);
	size_t retRd = fread(hostBuf.get(), 1, fileSize, fp);
	if (retRd == 0){
		LogError << "Failed to read image data to host buffer.";
		fclose(fp);
		return;}
	fclose(fp);
	

你可能感兴趣的:(cv,c++,音视频)