FFmpeg实现的从YUV编码到H264的简单视频编码器,FFmpeg version = 4.3.1
encoder.h
#ifndef __ENCODER__
#define __ENCODER__
#include <iostream>
#include <string>
extern "C" {
#include "libavformat/avformat.h"
#include "libavutil/imgutils.h"
#include "libavutil/opt.h"
}
#define FRAME_PRE_SECOND 25
class CEncoder {
public:
CEncoder(std::string& infile, std::string& outfile);
~CEncoder();
void initialize();
void compression(AVFrame* frame);
void encode();
private:
//*********** encode ***********
std::string enInfilePath;
std::string enOutfilePath;
FILE* enInfile;
uint8_t* rBuf;
//*********** ffmpeg ***********
AVFormatContext* pFmtCtx;
AVCodecContext* pCodecCtx;
AVStream* vStream;
AVCodec* pCodec;
AVPacket* pkt;
};
#endif //__ENCODER__
encoder.cpp
#include "encoder.h"
CEncoder::CEncoder(std::string& infile, std::string& outfile) :
//encode
enInfilePath(infile),
enOutfilePath(outfile),
pFmtCtx(nullptr),
vStream(nullptr),
pCodecCtx(nullptr),
pCodec(nullptr),
rBuf(nullptr),
enInfile(nullptr) {
}
CEncoder::~CEncoder() {
//free read file buffer
if (!rBuf) {
av_free(rBuf);
}
//free packet
if (!pkt) {
av_packet_free(&pkt);
}
//free AVCodecContext
if (