H264编码profile & level控制 。H265编码初探

背景知识

先科普一下profile&level。(这里讨论最常用的H264) 
H.264有四种画质级别,分别是baseline, extended, main, high: 
  1、Baseline Profile:基本画质。支持I/P 帧,只支持无交错(Progressive)和CAVLC; 
  2、Extended profile:进阶画质。支持I/P/B/SP/SI 帧,只支持无交错(Progressive)和CAVLC;(用的少) 
  3、Main profile:主流画质。提供I/P/B 帧,支持无交错(Progressive)和交错(Interlaced), 
    也支持CAVLC 和CABAC 的支持; 
  4、High profile:高级画质。在main Profile 的基础上增加了8x8内部预测、自定义量化、 无损视频编码和更多的YUV 格式; 
H.264 Baseline profile、Extended profile和Main profile都是针对8位样本数据、4:2:0格式(YUV)的视频序列。在相同配置情况下,High profile(HP)可以比Main profile(MP)降低10%的码率。 
根据应用领域的不同,Baseline profile多应用于实时通信领域,Main profile多应用于流媒体领域,High profile则多应用于广电和存储领域。

下图清楚的给出不同的profile&level的性能区别。 
profile 
这里写图片描述

level 
这里写图片描述

2.1 ffmpeg如何控制profile&level

举3个例子吧 

ffmpeg -i input.mp4 -profile:v baseline -level 3.0 output.mp4

ffmpeg -i input.mp4 -profile:v main -level 4.2 output.mp4

ffmpeg -i input.mp4 -profile:v high -level 5.1 output.mp4

如果ffmpeg编译时加了external的libx264,那就这么写: 

ffmpeg -i input.mp4 -c:v libx264 -x264-params "profile=high:level=3.0" output.mp4

 

从压缩比例来说,baseline< main < high,对于带宽比较局限的在线视频,可能会选择high,但有些时候,做个小视频,希望所有的设备基本都能解码(有些低端设备或早期的设备只能解码 baseline),那就牺牲文件大小吧,用baseline。自己取舍吧!

苹果的设备对不同profile的支持。 
这里写图片描述

2.2. 编码效率和视频质量的取舍(preset, crf)

除了上面提到的,强行配置biterate,或者强行配置profile/level,还有2个参数可以控制编码效率。 
一个是preset,一个是crf。 
preset也挺粗暴,基本原则就是,如果你觉得编码太快或太慢了,想改改,可以用profile。 
preset有如下参数可用:

ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow and placebo. 
编码加快,意味着信息丢失越严重,输出图像质量越差。

CRF(Constant Rate Factor): 范围 0-51: 0是编码毫无丢失信息, 23 is 默认, 51 是最差的情况。相对合理的区间是18-28. 
值越大,压缩效率越高,但也意味着信息丢失越严重,输出图像质量越差。

举个例子吧。 
ffmpeg -i input -c:v libx264 -profile:v main -preset:v fast -level 3.1 -x264opts crf=18 
(参考自:https://trac.ffmpeg.org/wiki/Encode/H.264)

2.3. H265 (HEVC)编码tile&level控制
背景知识

和H264的profile&level一样,为了应对不同应用的需求,HEVC制定了“层级”(tier) 和“等级”(level)。 
tier只有main和high。 
level有13级,如下所示: 
这里写图片描述

不多说,直接给出怎么用。(supposed你用libx265编码) 
ffmpeg -i input.mp4 -c:v libx265 -x265-params "profile=high:level=3.0" output.mp4

  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 FFmpeg 库设置 H.264 编码器的 profilelevel 的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdint.h> #include <inttypes.h> #include <unistd.h> #include <libavcodec/avcodec.h> int main(int argc, char *argv[]) { AVCodec *codec; AVCodecContext *codec_ctx = NULL; AVDictionary *opts = NULL; int ret; avcodec_register_all(); codec = avcodec_find_encoder_by_name("libx264"); if (codec == NULL) { fprintf(stderr, "Codec 'libx264' not found\n"); return 1; } codec_ctx = avcodec_alloc_context3(codec); if (codec_ctx == NULL) { fprintf(stderr, "Failed to allocate codec context\n"); return 1; } codec_ctx->width = 1280; codec_ctx->height = 720; codec_ctx->time_base.num = 1; codec_ctx->time_base.den = 30; codec_ctx->framerate.num = 30; codec_ctx->framerate.den = 1; codec_ctx->gop_size = 30; codec_ctx->pix_fmt = AV_PIX_FMT_YUV420P; codec_ctx->profile = FF_PROFILE_H264_HIGH; // 设置 profile codec_ctx->level = 41; // 设置 level av_dict_set(&opts, "preset", "medium", 0); av_dict_set(&opts, "tune", "zerolatency", 0); if (avcodec_open2(codec_ctx, codec, &opts) < 0) { fprintf(stderr, "Failed to open codec\n"); return 1; } // ... avcodec_close(codec_ctx); avcodec_free_context(&codec_ctx); av_dict_free(&opts); return 0; } ``` 在上面的代码中,`codec_ctx->profile` 和 `codec_ctx->level` 分别用于设置 H.264 编码器的 profilelevel。可以通过设置 `codec_ctx->profile` 的值为 `FF_PROFILE_H264_BASELINE`、`FF_PROFILE_H264_MAIN` 或 `FF_PROFILE_H264_HIGH` 来选择不同的 profile。而 `codec_ctx->level` 的值可以设置为对应的 level 值,比如 `10`、`11`、`31`、`41` 等。 需要注意的是,不同的 profilelevel 有不同的编码复杂度和兼容性,需要根据实际情况选择合适的参数。同时,需要使用 FFmpeg 版本 4.0 或以上才支持设置 profilelevel

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值