1. 什麼是 H264?什麼是 x264?
- H264 是
一套標準、規範
,H264 標準運行製造廠商自由地開發具備競爭力的創新產品,它沒有定義一個編碼器,而是定義了編碼器應該產生的碼流標準。
- x264 是一款免費的高性能的 H264 開源編碼器。
2. 如何經過一個 mp4 格式的視頻,獲取 yuv420p 格式的視頻呢?
# 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格式播放
複製代碼
# 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
複製代碼
#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;
}
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);
}
}
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;
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);
end:
inFile.close();
outFile.close();
if (frame) {
av_freep(&frame->data[0]);
av_frame_free(&frame);
}
av_packet_free(&pkt);
avcodec_free_context(&ctx);
qDebug() << "線程正常結束";
}
複製代碼