准备
ffmpeg 4.4
p准备一段yuv420p的格式的视频原始数据
这里我们使用命令直接提取
ffmpeg -i .\beautlWorld.mp4 -pixel_format yuv420p -s 1280x720 yuv420p_1280x720.yuv
编码流程
大致可以分为以下几步:
1.初始化编码器并设置参数
2.初始化AVPacket和AVFrame,设置参数
3.读取视频文件,进行编码
4.释放内存,结束
编码
以下是完整代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>extern "C"
{
#include <libavcodec/avcodec.h>
#include <libavutil/time.h>
#include <libavutil/opt.h>
#include <libavutil/imgutils.h>
}using namespace std;const char* inFileName = "D:/测试工程/sound/yuv420p_1280x720.yuv";
const char* outFileName = "D:/测试工程/sound/3333encode_yuv420p_1280x720.h264";int encode(AVCodecContext* codecContent, AVPacket * packet, AVFrame* frame, FILE* outFile)
{//解码int ret = avcodec_send_frame(codecContent, frame);if (ret < 0){fprintf(stderr, "Error sending a frame for encoding\n");return -1;}while (ret == 0){ret = avcodec_receive_packet(codecContent, packet);if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {return 0;}else if (ret < 0) {fprintf(stderr, "Error encoding video frame\n");return -1;}if (ret == 0){fwrite(packet->data, 1, packet->size, outFile);}}
}int main(int argc, char* argv[])
{int ret = 0;AVCodec* codec = nullptr;AVCodecContext* codecContent = nullptr;AVPacket* packet = nullptr;AVFrame* frame = nullptr;FILE* inFile = nullptr;FILE* outFile = nullptr;//编码器初始化codec = avcodec_find_encoder(AV_CODEC_ID_H264);if (codec == nullptr){printf("could not find h264 encoder!");return -1;}codecContent = avcodec_alloc_context3(codec);if (codecContent == nullptr){printf("could not alloc h264 content!");return -1;}//必设参数codecContent->width = 1280;codecContent->height = 720;codecContent->time_base = AVRational{ 1, 25 };codecContent->pix_fmt = AV_PIX_FMT_YUV420P;codecContent->gop_size = 25; //关键帧间隔,默认250codecContent->framerate = AVRational{ 25, 1 };ret = avcodec_open2(codecContent, codec, NULL);if (ret < 0) {fprintf(stderr, "Could not open codec: %d\n", ret);exit(1);}packet = av_packet_alloc();if (packet == nullptr){printf("alloc packet error");return -1;}frame = av_frame_alloc();if (packet == nullptr){printf("alloc frame error");return -1;}frame->width = codecContent->width;frame->height = codecContent->height;frame->format = codecContent->pix_fmt;ret = av_frame_get_buffer(frame, 0);if (ret){printf("alloc frame buffer error!");return -1;}inFile = fopen(inFileName, "rb");if (inFile == nullptr){printf("error to open file: %s\n", inFileName);return -1;}outFile = fopen(outFileName, "wb");if (inFile == nullptr){printf("error to open file: %s\n", outFileName);return -1;}int framecount = 0;frame->pts = 0;while (!feof(inFile)){ret = av_frame_is_writable(frame);if (ret < 0) {ret = av_frame_make_writable(frame);}fread(frame->data[0], 1, frame->width * frame->height, inFile); //yfread(frame->data[1], 1, frame->width * frame->height / 4, inFile); //ufread(frame->data[2], 1, frame->width * frame->height / 4, inFile); //vprintf("encode frame num: %d\n", ++framecount);frame->pts += 1000 / (codecContent->time_base.den / codecContent->time_base.num);encode(codecContent, packet, frame, outFile);}encode(codecContent, packet, nullptr, outFile);av_packet_free(&packet);av_frame_free(&frame);avcodec_free_context(&codecContent);fclose(inFile);fclose(outFile);return 0;
}
小结
AVCodecContext
对于视频编码器而言必须设置以下参数:
AVRational time_base;
int width, height;
以下由用户设置,可选
gop_size: 默认250,也就是每250帧一个关键帧,可选设置
max_b_frames: 默认3, b帧最大连续个数
bit_rate: 平均比特率
avcodec_alloc_context3
AVCodecContext *avcodec_alloc_context3(const AVCodec *codec);
为AVCodecContext结构申请空间,并使用默认值初始化!最后使用avcodec_free_context进行释放。
avcodec_open2
int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options);
调用该函数前:time_base(也就是每帧显示的时间),width(帧宽), height(帧高)
av_frame_get_buffer
int av_frame_get_buffer(AVFrame *frame, int align);
为音视频分配数据存储空间。调用该函数前需要设置AVFRAME几个初始值。
音频:1.sample format 2.nb_samples 3.channel_layout
视频:1.pixel format 2.width 3.height
align:直接对其,默认给0,根据当前CPU自行设置