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() << "线程正常结束";
}
复制代码