centos 安装 ffmpeg 6.1.1 并添加 libx264库

ffmpeg

写在前面,我们为什么要安装 x264

因为对于两个不同的.mp4视频来说,虽然它们的后缀名是一样的,但是两个MP4视频采用的编码格式可能是不一样的,它们可以采用H.264或H.265的编码格式进行编码,也可以采用MPEG-4的编码方式。而对于MP4视频文件的播放,Chrome只支持标准的H.264方式编码。因此如果MP4视频的编码格式不是H.264,那么这个视频文件就无法正常播放~

那么,为什么Chrome只支持H.264这种编码方式而不支持所有的视频编码方式呢?Google查了一下,网上给出的原因主要是说因为绝大部分的视频编码格式都要付专利费的,Google已经购买了H.264编码格式,而其它的就没有购买了,因此如果一个MP4视频不是H.264格式的,那么Chrome也是不支持播放的~

软件版本:

ffmpeg version 6.1.1 Copyright © 2000-2023 the FFmpeg developers

NASM version 2.14 compiled on Feb 1 2024

x264 0.164.3173 4815cca

centos 安装 ffmpeg 6.1.1 并添加 libx264库

ffmpeg执行时如添加参数-vcodec libx264,会出现错误:Unknown encoder 'libx264’的错误,缺少libx264库,需要安装该库,安装步骤如下
$ 代表普通用户 # 代表root权限

因为用源码安装,版本会比软件安装高一点,我们这里推荐采用源码安装

// 方法一:
// nasm安装
// 官网:https://www.nasm.us/
su -c 'curl https://www.nasm.us/nasm.repo | tee /etc/yum.repos.d/nasm.repo'
yum install nasm
// 安装成功
[root@VM-16-10-centos local]# nasm -v
NASM version 2.14 compiled on Feb  1 2024

1. 源码安装nasm

//方法二:
wget https://www.nasm.us/pub/nasm/releasebuilds/2.14/nasm-2.14.tar.gz

tar -zxvf nasm-2.14.tar.gz
cd nasm-2.14
./configure
make
sudo make install

//添加到环境变量中
export PATH=/usr/local/bin:$PATH

// 安装成功
[root@VM-16-10-centos local]# nasm -v
NASM version 2.14 compiled on Feb  1 2024

2. x264 安装

  1. 下载x264 并安装
$ git clone https://code.videolan.org/videolan/x264.git
$ cd x264
// 如果不将include和lib安装在/usr/local目录,之后运行ffmpeg命令时可能仍然报错误
$ ./configure --prefix=/usr/x264/ --includedir=/usr/local/include --libdir=/usr/local/lib --enable-shared
$ make 
# make install
  1. 配置环境变量
vim ~/.bashrc

在最后PATH添加环境变量:

export PATH="/usr/local/nasm/bin:$PATH"
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH

保存退出

查看是否生效

source ~/.bashrc  设置生效
  1. 验证是否安装成功,当然这里 x264 换成 libx264也是可以的
[root@VM-16-10-centos local]# pkg-config --libs x264
-L/usr/local/lib -lx264
[root@VM-16-10-centos local]# x264 --version
x264 0.164.3173 4815cca
(libswscale 5.5.100)
built on Feb  1 2024, gcc: 4.8.5 20150623 (Red Hat 4.8.5-44)
x264 configuration: --chroma-format=all
libx264 configuration: --chroma-format=all
x264 license: GPL version 2 or later
libswscale license: nonfree and unredistributable
WARNING: This binary is unredistributable!

地址是自己之前设置的,也可以查询出来的

[root@VM-16-10-centos mfile]# whereis libx264
libx264: /usr/local/lib/libx264.so
[root@VM-16-10-centos mfile]# cd /usr/local/
[root@VM-16-10-centos local]# cd include/
[root@VM-16-10-centos include]# ls
libavcodec   libavfilter  libavutil  libpostproc    libswscale  png.h         x264_config.h  zconf.h
libavdevice  libavformat  libpng16   libswresample  pngconf.h   pnglibconf.h  x264.h         zlib.h

3.安装 ffmpeg

  1. 下载
wget https://ffmpeg.org//releases/ffmpeg-6.1.1.tar.gz
tar -zxvf ffmpeg-6.1.1.tar.gz
  1. 进入解压后目录,输入如下命令/usr/local/ffmpeg为自己指定的安装目录

cd ffmpeg-6.1.1
./configure --enable-gpl --enable-libx264 --enable-shared --extra-ldflags=-L/usr/local/lib --extra-cflags=-I/usr/local/include
make
sudo make install
  1. 配置环境变量
1.  vi /etc/profile
  #在最后位置处添加环境变量,点击i进入编辑模式,esc键可退出编辑模式
2.  export PATH=$PATH:/usr/local/ffmpeg/bin
3.  #退出编辑模式后,:wq 保存退出
  #刷新资源,使其生效
4. source /etc/profile
  1. 验证是否安装成功
ffmpeg -version

// 安装成功
ffmpeg version 6.1.1 Copyright (c) 2000-2023 the FFmpeg developers
built with gcc 8.3.0 (GCC)
configuration: --enable-gpl --enable-libx264 --enable-shared --extra-ldflags=-L/usr/local/lib --extra-cflags=-I/usr/local/include
libavutil      58. 29.100 / 58. 29.100
libavcodec     60. 31.102 / 60. 31.102
libavformat    60. 16.100 / 60. 16.100
libavdevice    60.  3.100 / 60.  3.100
libavfilter     9. 12.100 /  9. 12.100
libswscale      7.  5.100 /  7.  5.100
libswresample   4. 12.100 /  4. 12.100
libpostproc    57.  3.100 / 57.  3.100

  1. 卸载
1使用root用户登录
停止ffmpeg进程
sudo pkill ffmpeg
卸载ffmpeg
sudo yum remove ffmpeg
删除ffmpeg的配置文件和数据
sudo rm -rf /usr/local/etc/ffmpeg /usr/local/share/ffmpeg /usr/local/share/doc/ffmpeg /usr/local/share/man/man1/ffmpeg*

遇到过的异常,共勉

1. ERROR: x264 not found using pkg-config

如图:

在这里插入图片描述

说明后缀指定的地址不对,这里x264有三个包,根据大家安装的地址确定 后缀的地址。

如上文安装的 包
./configure --prefix=/usr/x264/ --includedir=/usr/local/include --libdir=/usr/local/lib --enable-shared

那么 咱们的包就是 /usr/local/include,/usr/local/lib

这里有了后,还需要一个pkg-config 包,这样,就能解决这个报错了。用来存放x264.pc

我们的pkg-config 包 可以看下地址 /usr/local/lib/pkgconfig

[root@VM-16-10-centos lib]# pwd
/usr/local/lib
[root@VM-16-10-centos lib]# ls
libavcodec.a             libavdevice.so.60.3.100  libavformat.so.60         libpng16.la          libpostproc.a            libswresample.so.4.12.100  libz.a
libavcodec.so            libavfilter.a            libavformat.so.60.16.100  libpng16.so          libpostproc.so           libswscale.a               libz.so
libavcodec.so.60         libavfilter.so           libavutil.a               libpng16.so.16       libpostproc.so.57        libswscale.so              libz.so.1
libavcodec.so.60.31.102  libavfilter.so.9         libavutil.so              libpng16.so.16.40.0  libpostproc.so.57.3.100  libswscale.so.7            libz.so.1.2.11
libavdevice.a            libavfilter.so.9.12.100  libavutil.so.58           libpng.a             libswresample.a          libswscale.so.7.5.100      libz.so.1.3.1
libavdevice.so           libavformat.a            libavutil.so.58.29.100    libpng.la            libswresample.so         libx264.so                 pkgconfig
libavdevice.so.60        libavformat.so           libpng16.a                libpng.so            libswresample.so.4       libx264.so.164

解决1:

  1. vim /etc/profile
  2. 末尾加入内容export PKG_CONFIG_PATH=/usr/local/x264/lib/pkgconfig,具体看各位自己x264的安装路径
  3. source /etc/profile
  4. 然后再./configure ...就没问题了

解决2:

这个错误信息表示在运行 ./configure 配置脚本时,检测到 libx264 库没有被找到。这可能是由于缺少 libx264 库或者没有正确安装 libx264 的开发包引起的。

sudo yum install pkgconfig
export PKG_CONFIG_PATH="/path/to/libx264/lib/pkgconfig"

2.libavcodec/libavcodec.so: undefined reference to `x264_param_cleanup’

这个错误表明在链接 libavcodec 库时,编译器无法找到或解析 x264_param_cleanup 函数。这可能是由于 libx264 版本不匹配或编译选项不正确引起的。

以下是一些建议:

  1. 检查 libx264 版本:确保你的 libx264 版本与 ffmpeg 兼容。有时,特定版本的 ffmpeg 可能需要特定版本的 libx264。尽量使用较新的版本。

  2. 重新编译 libx264:如果你手动编译了 libx264,请尝试重新编译并安装,确保编译选项正确。可以尝试使用以下步骤:

    bashCopy code
    cd path/to/x264/source
    make distclean
    ./configure --enable-shared
    make
    sudo make install
    
    

    然后,重新编译 ffmpeg

  3. 检查 libx264 的链接选项:确保在链接 libavcodec 时,正确引用了 libx264 库。你可以在编译 ffmpeg 时,通过 lx264llibx264 选项来指定链接 libx264 库。

    例如,你可以在 ffmpegconfigure 阶段添加 --extra-libs=-lx264

    
    ./configure --enable-libx264 --extra-libs=-lx264
    
    

    然后,继续编译 ffmpeg

3.No package ‘x264’ found

pkg-config --modversion x264
Package x264 was not found in the pkg-config search path.
Perhaps you should add the directory containing `x264.pc’
to the PKG_CONFIG_PATH environment variable
No package ‘x264’ found

  1. 设置 PKG_CONFIG_PATH

    如果找到了 libx264.pc 文件,请将其路径添加到 PKG_CONFIG_PATH 环境变量中。例如:

    bashCopy code
    export PKG_CONFIG_PATH=/path/to/directory/containing/libx264.pc:$PKG_CONFIG_PATH
    
    

    请将 /path/to/directory/containing/ 替换为实际的目录路径。

    如果 libx264.pc 文件在 /usr/lib/pkgconfig/ 中,可以运行:

    bashCopy code
    export PKG_CONFIG_PATH=/usr/lib/pkgconfig:$PKG_CONFIG_PATH
    
    
  2. 重新运行 configure

    在重新运行 ./configure 之前,请确保清理之前的编译残留:

    bashCopy code
    make distclean
    

    然后重新运行 configure

    bashCopy code
    ./configure --enable-gpl --enable-shared --enable-libx264
    
    

4 x264 安装好了,可以看到版本信息,但是pkg-config --libs x264 查询报错

x264 --version
x264 0.164.3173 4815cca
(libswscale 5.5.100)
built on Feb  1 2024, gcc: 4.8.5 20150623 (Red Hat 4.8.5-44)
x264 configuration: --chroma-format=all
libx264 configuration: --chroma-format=all
x264 license: GPL version 2 or later
libswscale license: nonfree and unredistributable
WARNING: This binary is unredistributable!

[root@VM-16-10-centos x264]# pkg-config --libs libx264
Package libx264 was not found in the pkg-config search path.
Perhaps you should add the directory containing `libx264.pc'
to the PKG_CONFIG_PATH environment variable
No package 'libx264' found
[root@VM-16-10-centos x264]# pkg-config --libs x264
Package x264 was not found in the pkg-config search path.
Perhaps you should add the directory containing `x264.pc'
to the PKG_CONFIG_PATH environment variable
No package 'x264' found

在这种情况下,问题可能是 x264.pc 文件不在默认的 pkg-config 搜索路径中。通常来说,x264.pc 文件应该在 liblib/pkgconfig 目录下。

如果找到 x264.pc 文件,请将其路径添加到 PKG_CONFIG_PATH 环境变量中:

export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH

根据你的输出,pkg-config 仍然无法找到 libx264 的信息。在这种情况下,有几个可能的原因:

  1. libx264.pc 文件不存在或未正确安装: .pc 文件是由 libx264 的开发包提供的,用于告诉 pkg-config 如何链接到该库。确保你已经安装了 libx264 的开发包,或者手动指定 libx264 的路径。

  2. PKG_CONFIG_PATH 未正确设置: 确保 PKG_CONFIG_PATH 包含 libx264.pc 文件所在的目录。你可以通过运行以下命令来设置:

    bashCopy code
    export PKG_CONFIG_PATH=/path/to/libx264/pkgconfig:$PKG_CONFIG_PATH
    
    

    其中 /path/to/libx264/pkgconfig 是包含 libx264.pc 文件的目录路径。

  3. libx264 的库文件不在默认的库加载路径中: 尝试将 libx264 的库路径添加到 LD_LIBRARY_PATH 中:

    bashCopy code
    export LD_LIBRARY_PATH=/path/to/libx264/lib:$LD_LIBRARY_PATH
    
    

    其中 /path/to/libx264/lib 是包含 libx264.so 文件的目录路径。

请注意,确保替换上述命令中的 /path/to/libx264 为你系统上 libx264 的实际路径。完成这些步骤后,再次运行 pkg-config 或者 ./configure,看看问题是否解决。

5. ffmpeg: error while loading shared libraries: libavdevice.so.60: cannot open shared object file: No such file or directory

// 首先查找 libavdevice.so.60
sudo find / -name libavdevice.so.60
//查找发现 path
/usr/local/lib

// 配置文件中是否存在,换行追加对应路径
vim /etc/ld.so.conf

// include ld.so.conf.d/*.conf
// /usr/local/lib
// wq!保存退出

// 更新库配置
sudo ldconfig

参考文章:

https://blog.csdn.net/ylam4046/article/details/105494957

https://blog.csdn.net/yuxielea/article/details/103146362

https://www.cnblogs.com/Yellow-ice/p/13743400.html

创作不易,如果本文对你有帮助,欢迎点赞、收藏加关注,你的支持和鼓励,是我创作的最大动力。

在这里插入图片描述

  • 23
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

smile.shu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值