音視頻 day22 視頻編碼 H264

1. 什麼是 H264?什麼是 x264?

  • H264 是一套標準、規範,H264 標準運行製造廠商自由地開發具備競爭力的創新產品,它沒有定義一個編碼器,而是定義了編碼器應該產生的碼流標準。
  • x264 是一款免費的高性能的 H264 開源編碼器。

2. 如何經過一個 mp4 格式的視頻,獲取 yuv420p 格式的視頻呢?

  • mp4轉yuv
# ffmpeg -i guomei.mp4 -s 864x486 -pix_fmt yuv420p guomei.yuv
注意:
-s:設置yuv數據的分辨率
-pix_fmt:設置yuv數據的具體格式
複製代碼
  • 播放
# ffplay -f rawvideo -video_size 864x486 -i guomei.yuv
注意:必須指定轉碼以前分辨率分辨率:864x486
Or
# ffplay -f rawvideo -video_size 864x486 -pix_fmt yuv420p guomei.yuv
注意:也能夠指定yuv格式播放
複製代碼
  • 按時間截取前5秒幀視頻
# ffmpeg -ss 00:00:00 -i guomei.mp4 -to 00:00:10 -c copy out05.mp4
複製代碼

3. 如何藉助命令行,使用H264編碼對 YUV 視頻進行壓縮?

  • 壓縮後的大小,能夠用來側面印證咱們編碼產生的壓縮文件是否正確
ffmpeg -s 640x480 -pix_fmt yuv420p -framerate 30 -i in.yuv -c:v libx264 out.h264
# -c:v libx264是指定使用libx264做爲編碼器
複製代碼

4. 如何直接播放.h264的視頻文件呢?

  • 使用下面命令行
ffplay out.h264
複製代碼
  • 使用播放器,h264 的普及率高達 91%,因此大部分播放都能支持播放 .h264 文件

5. 經過代碼,將 YUV420p 的視頻進行 H264 編碼壓縮的主要流程是什麼?

  • 讀取的 yuv240p 文件 --> 獲取編碼上下文AVCodecContext 和編碼器AVCodec -- 獲取輸入緩衝區 AVFrame,用於輸入編碼前數據 -- 獲取輸出緩衝區 AVPacket,用於讀取編碼後的數據 -- 寫入 out.h264 文件中
  • .h 文件以下
#ifndef FFMPEGS_H
#define FFMPEGS_H

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);
};

#endif // FFMPEGS_H

複製代碼
  • .m 文件以下
#include "ffmpegs.h"
#include <QDebug>
#include <QFile>

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));

FFmpegs::FFmpegs() {

}

// 檢查像素格式
static int check_pix_fmt(const AVCodec *codec, enum AVPixelFormat pixFmt) {
 const enum AVPixelFormat *p = codec->pix_fmts;
 while (*p != AV_PIX_FMT_NONE) {
     if (*p == pixFmt) return 1;
     p++;
 }
 return 0;
}

// 返回負數:中途出現了錯誤
// 返回0:編碼操做正常完成
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) {
         // 繼續讀取數據到frame,而後送到編碼器
         return 0;
     } else if (ret < 0) { // 其餘錯誤
         return ret;
     }

     // 成功從編碼器拿到編碼後的數據
     // 將編碼後的數據寫入文件
     outFile.write((char *) pkt->data, pkt->size);

     // 釋放pkt內部的資源
     av_packet_unref(pkt);
 }
}

void FFmpegs::h264Encode(VideoEncodeSpec &in, const char *outFilename) {
 // 文件
 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;

 // 存放編碼前的數據(yuv)
 AVFrame *frame = nullptr;

 // 存放編碼後的數據(h264)
 AVPacket *pkt = nullptr;

// uint8_t *buf = 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;
 }

 // 設置yuv參數
 ctx->width = in.width;
 ctx->height = in.height;
 ctx->pix_fmt = in.pixFmt;
 // 設置幀率(1秒鐘顯示的幀數是in.fps)
 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;
 }

 // 建立AVFrame
 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;

 // 利用width、height、format建立緩衝區
 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;
 }

 // 建立輸入緩衝區(方法2)
// buf = (uint8_t *) av_malloc(imgSize);
// ret = av_image_fill_arrays(frame->data, frame->linesize,
// buf,
// in.pixFmt, in.width, in.height, 1);
// if (ret < 0) {
// ERROR_BUF(ret);
// qDebug() << "av_image_fill_arrays error" << errbuf;
// goto end;
// }
// qDebug() << buf << frame->data[0];

 // 建立輸入緩衝區(方法3)
// ret = av_frame_get_buffer(frame, 0);
// if (ret < 0) {
// ERROR_BUF(ret);
// qDebug() << "av_frame_get_buffer error" << errbuf;
// goto end;
// }

 // 建立AVPacket
 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;
 }

 // 讀取數據到frame中
 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);

end:
 // 關閉文件
 inFile.close();
 outFile.close();

// av_freep(&buf);

 // 釋放資源
 if (frame) {
     av_freep(&frame->data[0]);
// av_free(frame->data[0]);
// frame->data[0] = nullptr;
     av_frame_free(&frame);
 }
 av_packet_free(&pkt);
 avcodec_free_context(&ctx);

 qDebug() << "線程正常結束";
}

複製代碼
相關文章
相關標籤/搜索