美文网首页
FFMpeg使用x264和x265编码库

FFMpeg使用x264和x265编码库

作者: Nothing_655f | 来源:发表于2022-07-05 10:23 被阅读0次

    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编码

    相关文章

      网友评论

          本文标题:FFMpeg使用x264和x265编码库

          本文链接:https://www.haomeiwen.com/subject/gzqsbrtx.html