一. 安装FFmpeg
如果已经安装,建议先卸载.
brew uninstall ffmpeg
然后用以下命令安装.
brew install ffmpeg --with-fdk-aac --with-tools --with-ffplay --with-freetype --with-libass --with-libvorbis --with-opus --with-libvpx --with-x265
此时我们用 ffplay 一个h265地址是无法播放的。因为flv里面不支持
二. 下载源码
git clone 以下地址,
https://github.com/FFmpeg/FFmpeg
切换到 release/4.0版本。
三. 修改源码
应用我们的patch
patch -p1 < /Users/tomxiang/study/ffmpeg学习/h265支持/flv_hevc.patch
四. 重新编译
依然在ffmpeg目录下
./configure --prefix=/Users/tomxiang/develop/ffmpeg
如果需要编译VMAF打分模型需要加入:
./configure --prefix=/Users/only/Documents/ffmpegN/install --enable-shared --disable-static --enable-version3 --enable-libvmaf
make
make install
如果出现错误:nasm/yasm not found or too old. Use –disable-x86asm for a crippled build.
请先执行:
brew install yasm
此时develop/ffmpeg里面出现了.
五.执行
进入bin .
./ffplay http://flv265-3954.liveplay.myqcloud.com/live/3954_290383906.flv\?bizid\=3954\&txSecret\=8586ab1960216ba84ba19c006c8d1f25\&txTime\=5baabbc2
现在即可支持H265播放
在编译ffmpeg时出现错误,请参考下面文档:
https://lvv.me/posts/2020/04/14_build_ffmpeg/
因为不能加附件,这里附上patch如下:
diff --git a/libavformat/flv.h b/libavformat/flv.h
index df5ce3d..97324cc 100755
--- a/libavformat/flv.h
+++ b/libavformat/flv.h
@@ -109,6 +109,7 @@ enum {
FLV_CODECID_H264 = 7,
FLV_CODECID_REALH263= 8,
FLV_CODECID_MPEG4 = 9,
+ FLV_CODECID_HEVC = 12,
};
enum {
diff --git a/libavformat/flvdec.c b/libavformat/flvdec.c
index 34c3e08..060e7d2 100755
--- a/libavformat/flvdec.c
+++ b/libavformat/flvdec.c
@@ -36,6 +36,7 @@
#include "internal.h"
#include "avio_internal.h"
#include "flv.h"
+#include "hevc.h"
#define VALIDATE_INDEX_TS_THRESH 2500
@@ -291,6 +292,8 @@ static int flv_same_video_codec(AVCodecParameters *vpar, int flags)
return vpar->codec_id == AV_CODEC_ID_VP6A;
case FLV_CODECID_H264:
return vpar->codec_id == AV_CODEC_ID_H264;
+ case FLV_CODECID_HEVC:
+ return vpar->codec_id == AV_CODEC_ID_HEVC;
default:
return vpar->codec_tag == flv_codecid;
}
@@ -336,6 +339,10 @@ static int flv_set_video_codec(AVFormatContext *s, AVStream *vstream,
vstream->need_parsing = AVSTREAM_PARSE_HEADERS;
ret = 3; // not 4, reading packet type will consume one byte
break;
+ case FLV_CODECID_HEVC:
+ par->codec_id = AV_CODEC_ID_HEVC;
+ vstream->need_parsing = AVSTREAM_PARSE_HEADERS;
+ return 3; // not 4, reading packet type will consume one byte
case FLV_CODECID_MPEG4:
par->codec_id = AV_CODEC_ID_MPEG4;
ret = 3;
@@ -1151,7 +1158,8 @@ retry_duration:
if (st->codecpar->codec_id == AV_CODEC_ID_AAC ||
st->codecpar->codec_id == AV_CODEC_ID_H264 ||
- st->codecpar->codec_id == AV_CODEC_ID_MPEG4) {
+ st->codecpar->codec_id == AV_CODEC_ID_MPEG4 ||
+ st->codecpar->codec_id == AV_CODEC_ID_HEVC) {
int type = avio_r8(s->pb);
size--;
@@ -1160,7 +1168,8 @@ retry_duration:
goto leave;
}
- if (st->codecpar->codec_id == AV_CODEC_ID_H264 || st->codecpar->codec_id == AV_CODEC_ID_MPEG4) {
+ if (st->codecpar->codec_id == AV_CODEC_ID_H264 || st->codecpar->codec_id == AV_CODEC_ID_MPEG4
+ || st->codecpar->codec_id == AV_CODEC_ID_HEVC) {
// sign extension
int32_t cts = (avio_rb24(s->pb) + 0xff800000) ^ 0xff800000;
pts = dts + cts;
@@ -1176,7 +1185,7 @@ retry_duration:
}
}
if (type == 0 && (!st->codecpar->extradata || st->codecpar->codec_id == AV_CODEC_ID_AAC ||
- st->codecpar->codec_id == AV_CODEC_ID_H264)) {
+ st->codecpar->codec_id == AV_CODEC_ID_H264 || st->codecpar->codec_id == AV_CODEC_ID_HEVC)) {
AVDictionaryEntry *t;
if (st->codecpar->extradata) {
diff --git a/libavformat/flvenc.c b/libavformat/flvenc.c
index e8af48c..e3ff84f 100755
--- a/libavformat/flvenc.c
+++ b/libavformat/flvenc.c
@@ -34,7 +34,7 @@
#include "libavutil/opt.h"
#include "libavcodec/put_bits.h"
#include "libavcodec/aacenctab.h"
-
+#include "libavformat/hevc.h"
static const AVCodecTag flv_video_codec_ids[] = {
{ AV_CODEC_ID_FLV1, FLV_CODECID_H263 },
@@ -46,6 +46,7 @@ static const AVCodecTag flv_video_codec_ids[] = {
{ AV_CODEC_ID_VP6, FLV_CODECID_VP6 },
{ AV_CODEC_ID_VP6A, FLV_CODECID_VP6A },
{ AV_CODEC_ID_H264, FLV_CODECID_H264 },
+ { AV_CODEC_ID_HEVC, FLV_CODECID_HEVC },
{ AV_CODEC_ID_NONE, 0 }
};
@@ -485,7 +486,7 @@ static void flv_write_codec_header(AVFormatContext* s, AVCodecParameters* par) {
AVIOContext *pb = s->pb;
FLVContext *flv = s->priv_data;
- if (par->codec_id == AV_CODEC_ID_AAC || par->codec_id == AV_CODEC_ID_H264
+ if (par->codec_id == AV_CODEC_ID_AAC || par->codec_id == AV_CODEC_ID_H264 || par->codec_id == AV_CODEC_ID_HEVC
|| par->codec_id == AV_CODEC_ID_MPEG4) {
int64_t pos;
avio_w8(pb,
@@ -533,7 +534,12 @@ static void flv_write_codec_header(AVFormatContext* s, AVCodecParameters* par) {
avio_w8(pb, par->codec_tag | FLV_FRAME_KEY); // flags
avio_w8(pb, 0); // AVC sequence header
avio_wb24(pb, 0); // composition time
- ff_isom_write_avcc(pb, par->extradata, par->extradata_size);
+ if (par->codec_id == AV_CODEC_ID_HEVC)
+ ff_isom_write_hvcc(pb, par->extradata, par->extradata_size, 0);
+ else
+ ff_isom_write_avcc(pb, par->extradata, par->extradata_size);
+// liangliang
+ // ff_isom_write_avcc(pb, par->extradata, par->extradata_size);
}
data_size = avio_tell(pb) - pos;
avio_seek(pb, -data_size - 10, SEEK_CUR);
@@ -836,7 +842,7 @@ end:
AVCodecParameters *par = s->streams[i]->codecpar;
FLVStreamContext *sc = s->streams[i]->priv_data;
if (par->codec_type == AVMEDIA_TYPE_VIDEO &&
- (par->codec_id == AV_CODEC_ID_H264 || par->codec_id == AV_CODEC_ID_MPEG4))
+ (par->codec_id == AV_CODEC_ID_H264 || par->codec_id == AV_CODEC_ID_MPEG4 || par->codec_id == AV_CODEC_ID_HEVC))
put_avc_eos_tag(pb, sc->last_ts);
}
}
@@ -882,12 +888,13 @@ static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
if (par->codec_id == AV_CODEC_ID_VP6F || par->codec_id == AV_CODEC_ID_VP6A ||
par->codec_id == AV_CODEC_ID_VP6 || par->codec_id == AV_CODEC_ID_AAC)
flags_size = 2;
- else if (par->codec_id == AV_CODEC_ID_H264 || par->codec_id == AV_CODEC_ID_MPEG4)
+ else if (par->codec_id == AV_CODEC_ID_H264 || par->codec_id == AV_CODEC_ID_MPEG4 ||
+ par->codec_id == AV_CODEC_ID_HEVC)
flags_size = 5;
else
flags_size = 1;
- if (par->codec_id == AV_CODEC_ID_AAC || par->codec_id == AV_CODEC_ID_H264
+ if (par->codec_id == AV_CODEC_ID_AAC || par->codec_id == AV_CODEC_ID_H264 || par->codec_id == AV_CODEC_ID_HEVC
|| par->codec_id == AV_CODEC_ID_MPEG4) {
int side_size = 0;
uint8_t *side = av_packet_get_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA, &side_size);
@@ -951,7 +958,11 @@ static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
if (par->extradata_size > 0 && *(uint8_t*)par->extradata != 1)
if ((ret = ff_avc_parse_nal_units_buf(pkt->data, &data, &size)) < 0)
return ret;
- } else if (par->codec_id == AV_CODEC_ID_AAC && pkt->size > 2 &&
+ } else if (par->codec_id == AV_CODEC_ID_HEVC) {
+ if (par->extradata_size > 0 && *(uint8_t*)par->extradata != 1)
+ if ((ret = ff_hevc_annexb2mp4_buf(pkt->data, &data, &size, 0, NULL)) < 0)
+ return ret;
+ } else if (par->codec_id == AV_CODEC_ID_AAC && pkt->size > 2 &&
(AV_RB16(pkt->data) & 0xfff0) == 0xfff0) {
if (!s->streams[pkt->stream_index]->nb_frames) {
av_log(s, AV_LOG_ERROR, "Malformed AAC bitstream detected: "
@@ -1023,7 +1034,8 @@ static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
(FFALIGN(par->height, 16) - par->height));
} else if (par->codec_id == AV_CODEC_ID_AAC)
avio_w8(pb, 1); // AAC raw
- else if (par->codec_id == AV_CODEC_ID_H264 || par->codec_id == AV_CODEC_ID_MPEG4) {
+ else if (par->codec_id == AV_CODEC_ID_H264 || par->codec_id == AV_CODEC_ID_MPEG4 ||
+ par->codec_id == AV_CODEC_ID_HEVC) {
avio_w8(pb, 1); // AVC NALU
avio_wb24(pb, pkt->pts - pkt->dts);
}