FFMpeg使用x264和x265编码库

FFMpeg使用x264和x265编码库

最近收到一个小任务是转换1920x1080@120Hz片源出来确认下平台120Hz的支持能力

然后看了下spec 120Hz的支持规格有

H264只有一个规格

Level Max macroblocks Max video bit rate (VCL) Examples for high resolution @ frame rate (max stored frames)
per second per frame BP, XP, MP (kbit/s) HiP (kbit/s) Hi10P (kbit/s) Hi422P, Hi444PP (kbit/s)
5.1 983,040 36,864 240,000 300,000 720,000 960,000 1,920×1,080@120.5 (16) 4,096×2,048@30.0 (5) 4,096×2,304@26.7 (5)

H265 要5.0 level 以上才有

MPEG4也有,不过具体规格没去看

这边用ffmpeg命令生成

ffmpeg -i 132.mp4 -r 120 132.120hz.mp4

ffmpeg是默认编译的,转成120Hz的时候将编码格式默认转为了MPEG4了

General
Complete name                            : D:\Media\120Hz\132.MPEG4.1920x1080@120hz.mp4
Format                                   : MPEG-4
Format profile                           : Base Media
Codec ID                                 : isom (isom/iso2/avc1/mp41)
File size                                : 61.4 MiB
Duration                                 : 1 min 28 s
Overall bit rate                         : 5 837 kb/s
Writing application                      : Lavf59.4.101

Video
ID                                       : 1
Format                                   : AVC
Format/Info                              : Advanced Video Codec
Format profile                           : High@L5.1
Format settings                          : CABAC / 4 Ref Frames
Format settings, CABAC                   : Yes
Format settings, Reference frames        : 4 frames
Codec ID                                 : avc1
Codec ID/Info                            : Advanced Video Coding
Duration                                 : 1 min 28 s
Bit rate                                 : 5 689 kb/s
Width                                    : 1 920 pixels
Height                                   : 1 080 pixels
Display aspect ratio                     : 16:9
Frame rate mode                          : Constant
Frame rate                               : 120.000 FPS
Color space                              : YUV
Chroma subsampling                       : 4:2:0
Bit depth                                : 8 bits
Scan type                                : Progressive
Bits/(Pixel*Frame)                       : 0.023
Stream size                              : 59.8 MiB (97%)
Writing library                          : x264 core 150
Encoding settings                        : cabac=1 / ref=3 / deblock=1:0:0 / analyse=0x3:0x113 / me=hex / subme=7 / psy=1 / psy_rd=1.00:0.00 / mixed_ref=1 / me_range=16 / chroma_me=1 / trellis=1 / 8x8dct=1 / cqm=0 / deadzone=21,11 / fast_pskip=1 / chroma_qp_offset=-2 / threads=34 / lookahead_threads=5 / sliced_threads=0 / nr=0 / decimate=1 / interlaced=0 / bluray_compat=0 / constrained_intra=0 / bframes=3 / b_pyramid=2 / b_adapt=1 / b_bias=0 / direct=1 / weightb=1 / open_gop=0 / weightp=2 / keyint=250 / keyint_min=25 / scenecut=40 / intra_refresh=0 / rc_lookahead=40 / rc=crf / mbtree=1 / crf=23.0 / qcomp=0.60 / qpmin=0 / qpmax=69 / qpstep=4 / ip_ratio=1.40 / aq=1:1.00
Language                                 : English
Color range                              : Limited
Color primaries                          : BT.709
Transfer characteristics                 : BT.709
Matrix coefficients                      : BT.709
Codec configuration box                  : avcC

Audio
ID                                       : 2
Format                                   : AAC LC
Format/Info                              : Advanced Audio Codec Low Complexity
Codec ID                                 : mp4a-40-2
Duration                                 : 1 min 28 s
Source duration                          : 1 min 28 s
Bit rate mode                            : Constant
Bit rate                                 : 132 kb/s
Channel(s)                               : 2 channels
Channel layout                           : L R
Sampling rate                            : 48.0 kHz
Frame rate                               : 46.875 FPS (1024 SPF)
Compression mode                         : Lossy
Stream size                              : 1.37 MiB (2%)
Source stream size                       : 1.37 MiB (2%)
Language                                 : English
Default                                  : Yes
Alternate group                          : 1
mdhd_Duration                            : 88192

因为我这边编译的ffmpeg 默认是不带x264和x265的,可以通过如下命令确认

ffmpeg -encoders

没有看到x264跟x265的支持,随手也添加下这两个的支持

x264和x265编译安装

编译帮助可以看官网介绍

https://trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu

编译x265

下载源码和配置Cmake参数

tar xjvf x265.tar.bz2
cd multicoreware*/build/linux
cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX="$MY_TOOLS" \
 -DENABLE_SHARED=off ../../source
make
make install

编译x264

先下载编译x264支持库源码,配置编译安装prefix路径、是否静态库安装等等

 cd x264/
 make -j4
 ./configure --prefix=$MY_TOOLS --enable-static --enable-pic \
   --extra-cflags="-fPIE -pie" --extra-ldflags="-fPIE -pie" --disable-asm
 make -j4
 make install

重编译ffmpeg

然后在重新编译ffmpeg,主要是使能 --enable-libx264 --enable-libx265 编译配置

 ./configure --prefix=/home/workspace5/xxxxx/tools --disable-yasm \
 --enable-nonfree  --enable-shared  --enable-gpl --enable-libx264 \
 --enable-libx265 \
 --enable-debug=3 --disable-optimizations --disable-asm --disable-stripping \
 --extra-cflags=-I/home/workspace5/xxxxx/tools/include \
 --extra-ldflags=-L/home/workspace5/xxxxx/tools/lib \
 --enable-rpath --libdir=/home/workspace5/xxxxx/tools/lib \
 --extra-libs=-ldl
 make -j4
 make install

x264+ffmpeg编译错误处理

编译ffmpeg的时候出现ERROR: libx264 not found

最近用Linux编译ffmpeg的时候出现了错误ERROR: libx264 not found

查看config.log原因如下

./x264/libx264.a(opencl.o): In function `x264_opencl_close_library': 
opencl.c:(.text+0x572): undefined reference to `dlclose' 
./x264/libx264.a(opencl.o): In function `x264_opencl_load_library': 
opencl.c:(.text+0x5d7): undefined reference to `dlopen' 
opencl.c:(.text+0x5f1): undefined reference to `dlsym' 
opencl.c:(.text+0x60e): undefined reference to `dlsym' 
opencl.c:(.text+0x62b): undefined reference to `dlsym' 
opencl.c:(.text+0x648): undefined reference to `dlsym' 
opencl.c:(.text+0x665): undefined reference to `dlsym' 
./x264/libx264.a(opencl.o):opencl.c:(.text+0x682): more undefined references to `dlsym' follow 
./x264/libx264.a(opencl.o): In function `x264_opencl_load_library': 
opencl.c:(.text+0x8fd): undefined reference to `dlclose' 
./x264/libx264.a(opencl.o): In function `x264_opencl_lookahead_init': 
opencl.c:(.text+0x1841): undefined reference to `dlopen' 
opencl.c:(.text+0x185b): undefined reference to `dlsym' 
opencl.c:(.text+0x186d): undefined reference to `dlsym' 
opencl.c:(.text+0x1881): undefined reference to `dlsym' 
opencl.c:(.text+0x1895): undefined reference to `dlsym' 
opencl.c:(.text+0x18d4): undefined reference to `dlclose' 
collect2: ld returned 1 exit status

查阅资料需要在ffmpeg编译的时候额外指定一下-ldl库

例如进入ffmpeg源码目录

./configure --prefix=/home/workspace5/xxxxx/tools --disable-yasm --enable-nonfree --enable-shared \
               --enable-gpl --enable-libx264 --enable-debug=3 --disable-optimizations --disable-asm --disable-stripping  \
               --extra-cflags=-I/home/workspace5/xxxxx/tools/include --extra-ldflags=-L/home/workspace5/xxxxx/tools/lib \
               --enable-rpath --libdir=/home/workspace5/xxxxx/tools/lib --extra-libs=-ldl

重点在于

--extra-libs=-ldl

然后可以通过 如下命令获取x264的帮助,可以看到版本信息,编译和运行配置和格式支持及命令帮助等

$ffmpeg -h encoder=libx264
ffmpeg version N-103078-gbc5faac Copyright (c) 2000-2021 the FFmpeg developers
  built with gcc 4.8 (Ubuntu 4.8.4-2ubuntu1~14.04.4)
configuration: --prefix=/home/workspace5/xxxxx/tools --disable-yasm --enable-nonfree --enable-shared \
               --enable-gpl --enable-libx264 --enable-debug=3 --disable-optimizations --disable-asm --disable-stripping  \
               --extra-cflags=-I/home/workspace5/xxxxx/tools/include --extra-ldflags=-L/home/workspace5/xxxxx/tools/lib \
               --enable-rpath --libdir=/home/workspace5/xxxxx/tools/lib --extra-libs=-ldl
  libavutil      57\.  2.100 / 57\.  2.100
  libavcodec     59\.  3.102 / 59\.  3.102
  libavformat    59\.  4.101 / 59\.  4.101
  libavdevice    59\.  0.100 / 59\.  0.100
  libavfilter     8\.  1.103 /  8\.  1.103
  libswscale      6\.  0.100 /  6\.  0.100
  libswresample   4\.  0.100 /  4\.  0.100
  libpostproc    56\.  0.100 / 56\.  0.100
Encoder libx264 [libx264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10]:
    General capabilities: dr1 delay threads 
    Threading capabilities: other
    Supported pixel formats: yuv420p yuvj420p yuv422p yuvj422p yuv444p yuvj444p nv12 nv16 nv21
libx264 AVOptions:
  -preset            <string>     E..V....... Set the encoding preset (cf. x264 --fullhelp) (default "medium")
  -tune              <string>     E..V....... Tune the encoding params (cf. x264 --fullhelp)
  -profile           <string>     E..V....... Set profile restrictions (cf. x264 --fullhelp) 
  -fastfirstpass     <boolean>    E..V....... Use fast settings when encoding first pass (default true)
  -level             <string>     E..V....... Specify level (as defined by Annex A)
  -passlogfile       <string>     E..V....... Filename for 2 pass stats
  -wpredp            <string>     E..V....... Weighted prediction for P-frames
  -a53cc             <boolean>    E..V....... Use A53 Closed Captions (if available) (default true)
  -x264opts          <string>     E..V....... x264 options
  -crf               <float>      E..V....... Select the quality for constant quality mode (from -1 to FLT_MAX) (default -1)
  -crf_max           <float>      E..V....... In CRF mode, prevents VBV from lowering quality beyond this point. (from -1 to FLT_MAX) (default -1)
  -qp                <int>        E..V....... Constant quantization parameter rate control method (from -1 to INT_MAX) (default -1)
  -aq-mode           <int>        E..V....... AQ method (from -1 to INT_MAX) (default -1)
     none            0            E..V.......
     variance        1            E..V....... Variance AQ (complexity mask)
     autovariance    2            E..V....... Auto-variance AQ
     autovariance-biased 3            E..V....... Auto-variance AQ with bias to dark scenes
  -aq-strength       <float>      E..V....... AQ strength. Reduces blocking and blurring in flat and textured areas. (from -1 to FLT_MAX) (default -1)
  -psy               <boolean>    E..V....... Use psychovisual optimizations. (default auto)
  -psy-rd            <string>     E..V....... Strength of psychovisual optimization, in <psy-rd>:<psy-trellis> format.
  -rc-lookahead      <int>        E..V....... Number of frames to look ahead for frametype and ratecontrol (from -1 to INT_MAX) (default -1)
  -weightb           <boolean>    E..V....... Weighted prediction for B-frames. (default auto)
  -weightp           <int>        E..V....... Weighted prediction analysis method. (from -1 to INT_MAX) (default -1)
     none            0            E..V.......
     simple          1            E..V.......
     smart           2            E..V.......
  -ssim              <boolean>    E..V....... Calculate and print SSIM stats. (default auto)
  -intra-refresh     <boolean>    E..V....... Use Periodic Intra Refresh instead of IDR frames. (default auto)
  -bluray-compat     <boolean>    E..V....... Bluray compatibility workarounds. (default auto)
  -b-bias            <int>        E..V....... Influences how often B-frames are used (from INT_MIN to INT_MAX) (default INT_MIN)
  -b-pyramid         <int>        E..V....... Keep some B-frames as references. (from -1 to INT_MAX) (default -1)
     none            0            E..V.......
     strict          1            E..V....... Strictly hierarchical pyramid
     normal          2            E..V....... Non-strict (not Blu-ray compatible)
  -mixed-refs        <boolean>    E..V....... One reference per partition, as opposed to one reference per macroblock (default auto)
  -8x8dct            <boolean>    E..V....... High profile 8x8 transform. (default auto)
  -fast-pskip        <boolean>    E..V....... (default auto)
  -aud               <boolean>    E..V....... Use access unit delimiters. (default auto)
  -mbtree            <boolean>    E..V....... Use macroblock tree ratecontrol. (default auto)
  -deblock           <string>     E..V....... Loop filter parameters, in <alpha:beta> form.
  -cplxblur          <float>      E..V....... Reduce fluctuations in QP (before curve compression) (from -1 to FLT_MAX) (default -1)
  -partitions        <string>     E..V....... A comma-separated list of partitions to consider. Possible values: p8x8, p4x4, b8x8, i8x8, i4x4, none, all
  -direct-pred       <int>        E..V....... Direct MV prediction mode (from -1 to INT_MAX) (default -1)
     none            0            E..V.......
     spatial         1            E..V.......
     temporal        2            E..V.......
     auto            3            E..V.......
  -slice-max-size    <int>        E..V....... Limit the size of each slice in bytes (from -1 to INT_MAX) (default -1)
  -stats             <string>     E..V....... Filename for 2 pass stats
  -nal-hrd           <int>        E..V....... Signal HRD information (requires vbv-bufsize; cbr not allowed in .mp4) (from -1 to INT_MAX) (default -1)
     none            0            E..V.......
     vbr             1            E..V.......
     cbr             2            E..V.......
  -avcintra-class    <int>        E..V....... AVC-Intra class 50/100/200/300/480 (from -1 to 480) (default -1)
  -me_method         <int>        E..V....... Set motion estimation method (from -1 to 4) (default -1)
     dia             0            E..V.......
     hex             1            E..V.......
     umh             2            E..V.......
     esa             3            E..V.......
     tesa            4            E..V.......
  -motion-est        <int>        E..V....... Set motion estimation method (from -1 to 4) (default -1)
     dia             0            E..V.......
     hex             1            E..V.......
     umh             2            E..V.......
     esa             3            E..V.......
     tesa            4            E..V.......
  -forced-idr        <boolean>    E..V....... If forcing keyframes, force them as IDR frames. (default false)
  -coder             <int>        E..V....... Coder type (from -1 to 1) (default default)
     default         -1           E..V.......
     cavlc           0            E..V.......
     cabac           1            E..V.......
     vlc             0            E..V.......
     ac              1            E..V.......
  -b_strategy        <int>        E..V....... Strategy to choose between I/P/B-frames (from -1 to 2) (default -1)
  -chromaoffset      <int>        E..V....... QP difference between chroma and luma (from INT_MIN to INT_MAX) (default 0)
  -sc_threshold      <int>        E..V....... Scene change threshold (from INT_MIN to INT_MAX) (default -1)
  -noise_reduction   <int>        E..V....... Noise reduction (from INT_MIN to INT_MAX) (default -1)
  -x264-params       <dictionary> E..V....... Override the x264 configuration using a :-separated list of key=value parameters

添加完x264编码库后就是可以配置其Codec模式编码生成120Hz片源了,主要是配置 -c:v libx264, x265同理

ffmpeg -i 132.mp4 -c:v libx264 -r 120 132.120hz.mp4

关于 ffmpeg x264更多使用方法可以看

https://www.likecs.com/show-279356.html

H.264 Video Encoding Guide

代码的使用方法可以参考

FFmpeg H264编码

使用FFmpeg实现H.264编码

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,869评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,716评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 166,223评论 0 357
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,047评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,089评论 6 395
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,839评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,516评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,410评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,920评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,052评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,179评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,868评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,522评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,070评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,186评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,487评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,162评论 2 356

推荐阅读更多精彩内容