本文的主要内容:使用H.264编码对YUV视频进行压缩。
如果是命令行的操作,非常简单。
| ffmpeg -s 640x480 -pix_fmt yuv420p -i in.yuv -c:v libx264 out.h264 |
| |
接下来主要讲解如何通过代码的方式使用H.264编码,用到了avcodec、avutil两个库,整体过程跟《AAC编码实战》类似。
类的声明
| extern "C" { |
| #include <libavutil/avutil.h> |
| } |
| |
| typedef struct { |
| const char *filename; |
| int width; |
| int height; |
| AVPixelFormat pixFmt; |
| int fps; |
| } VideoEncodeSpec; |
| |
| class FFmpegs { |
| public: |
| FFmpegs(); |
| |
| static void h264Encode(VideoEncodeSpec &in, |
| const char *outFilename); |
| }; |
类的使用
| VideoEncodeSpec in; |
| in.filename = "F:/res/in.yuv"; |
| in.width = 640; |
| in.height = 480; |
| in.fps = 30; |
| in.pixFmt = AV_PIX_FMT_YUV420P; |
| |
| FFmpegs::h264Encode(in, "F:/res/out.h264"); |
宏定义
| extern "C" { |
| #include <libavcodec/avcodec.h> |
| #include <libavutil/avutil.h> |
| #include <libavutil/imgutils.h> |
| } |
| |
| #define ERROR_BUF(ret) \ |
| char errbuf[1024]; \ |
| av_strerror(ret, errbuf, sizeof (errbuf)); |
变量定义
| |
| QFile inFile(in.filename); |
| QFile outFile(outFilename); |
| |
| |
| int imgSize = av_image_get_buffer_size(in.pixFmt, in.width, in.height, 1); |
| |
| |
| int ret = 0; |
| |
| |
| AVCodec *codec = nullptr; |
| |
| |
| AVCodecContext *ctx = nullptr; |
| |
| |
| AVFrame *frame = nullptr; |
| |
| |
| AVPacket *pkt = nullptr; |
初始化
| |
| codec = avcodec_find_encoder_by_name("libx264"); |
| if (!codec) { |
| qDebug() << "encoder not found"; |
| return; |
| } |
| |
| |
| if (!check_pix_fmt(codec, in.pixFmt)) { |
| qDebug() << "unsupported pixel format" |
| << av_get_pix_fmt_name(in.pixFmt); |
| return; |
| } |
| |
| |
| ctx = avcodec_alloc_context3(codec); |
| if (!ctx) { |
| qDebug() << "avcodec_alloc_context3 error"; |
| return; |
| } |
| |
| |
| ctx->width = in.width; |
| ctx->height = in.height; |
| ctx->pix_fmt = in.pixFmt; |
| |
| ctx->time_base = {1, in.fps}; |
| |
| |
| ret = avcodec_open2(ctx, codec, nullptr); |
| if (ret < 0) { |
| ERROR_BUF(ret); |
| qDebug() << "avcodec_open2 error" << errbuf; |
| goto end; |
| } |
| |
| |
| frame = av_frame_alloc(); |
| if (!frame) { |
| qDebug() << "av_frame_alloc error"; |
| goto end; |
| } |
| frame->width = ctx->width; |
| frame->height = ctx->height; |
| frame->format = ctx->pix_fmt; |
| frame->pts = 0; |
| |
| |
| ret = av_image_alloc(frame->data, frame->linesize, |
| in.width, in.height, in.pixFmt, 1); |
| if (ret < 0) { |
| ERROR_BUF(ret); |
| qDebug() << "av_frame_get_buffer error" << errbuf; |
| goto end; |
| } |
| |
| |
| pkt = av_packet_alloc(); |
| if (!pkt) { |
| qDebug() << "av_packet_alloc error"; |
| goto end; |
| } |
编码
| |
| if (!inFile.open(QFile::ReadOnly)) { |
| qDebug() << "file open error" << in.filename; |
| goto end; |
| } |
| if (!outFile.open(QFile::WriteOnly)) { |
| qDebug() << "file open error" << outFilename; |
| goto end; |
| } |
| |
| |
| while ((ret = inFile.read((char *) frame->data[0], |
| imgSize)) > 0) { |
| |
| if (encode(ctx, frame, pkt, outFile) < 0) { |
| goto end; |
| } |
| |
| |
| frame->pts++; |
| } |
| |
| |
| encode(ctx, nullptr, pkt, outFile); |
encode函数的实现如下所示:
| |
| |
| static int encode(AVCodecContext *ctx, |
| AVFrame *frame, |
| AVPacket *pkt, |
| QFile &outFile) { |
| |
| int ret = avcodec_send_frame(ctx, frame); |
| if (ret < 0) { |
| ERROR_BUF(ret); |
| qDebug() << "avcodec_send_frame error" << errbuf; |
| return ret; |
| } |
| |
| |
| while (true) { |
| ret = avcodec_receive_packet(ctx, pkt); |
| if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) { |
| |
| return 0; |
| } else if (ret < 0) { |
| return ret; |
| } |
| |
| |
| |
| outFile.write((char *) pkt->data, pkt->size); |
| |
| |
| av_packet_unref(pkt); |
| } |
| } |
回收资源
| end: |
| |
| inFile.close(); |
| outFile.close(); |
| |
| if (frame) { |
| av_freep(&frame->data[0]); |
| av_frame_free(&frame); |
| } |
| av_packet_free(&pkt); |
| avcodec_free_context(&ctx); |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2013-05-25 【零基础学习iOS开发】【02-C语言】06-变量与内存