1.1. Download FFmpeg
- First, go to https://www.ffmpeg.org/download.html to download the FFmpeg executables.
- Then, unzip it. We can find the bin folder which contains ffmpeg.exe
1.2. Convert PNG into YUV
- To convert a PNG file into YUV, just simply enter below command with pixel format specified as YUV420:
ffmpeg -i 0001.png -pix_fmt yuv420p 0001.yuv
- Then we can convert 0001.png into 0001.yuv.
2. OpenCV C++ API
- As I need some manipulations during the conversion, so I also tried OpenCV C++ API.
2.1. OpenCV C++ API Installation
- (For the installation of OpenCV C++ API, please feel free to read OpenCV v4.2.0 Installation in Windows 10.)
2.2. Convert PNG into YUV
- First, include some headers:
#include <iostream>using namespace cv;
using namespace std;
- Then, in the main function, to read the 0001.png, and also get the width and height of the image:
Mat image = imread("0001.png");
int iHeight = image.size;
int iWidth = image.size;
- And convert image into yuv by specifying COLOR_BGR2YUV_I420:
cvtColor(image, yuv, COLOR_BGR2YUV_I420);
- Finally, estimate the buffer size for YUV420, write out the YUV buffer to file as 0001.yuv:
int bufLen = iWidth * iHeight * 3 / 2;
unsigned char* pYuvBuf = new unsigned char[bufLen];File * pFileYuv;
fopen_s(&pFileYuv, "0001.yuv", "wb");fwrite(yuv.data, sizeof(unsigned char), bufLen, pFileYuv);fclose(pFileYuv);
pFileYuv = NULL;delete  pYuvBuf;
During the days of coronavirus, let me have a challenge of writing 30 stories again for this month ..? Is it good? This is the 17th story in this month. Thanks for visiting my story..