linux(centos)安装 ffmpeg,并添加 libx264库

1. 前言

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

2. 安装步骤

此步骤包含安装nasm、x264和ffmpeg。

2.1 安装nasm

2.1.1 下载nasm

下载地址:wget https://www.nasm.us/pub/nasm/releasebuilds/2.14/nasm-2.14.tar.gz

2.1.2 解压

tar -zxvf nasm-2.14.tar.gz

2.1.3 进入nasm目录

cd nasm-2.14

2.1.4 执行nasm配置

执行:

./configure

2.1.5 编译nasm

执行:

make

2.1.6 安装nasm

sudo make install

2.1.7 查看是否安装成功

nasm -v

在这里插入图片描述

2.2 x264 安装

2.2.1 下载x264

下载地址:git clone https://code.videolan.org/videolan/x264.git

2.2.2 进入x264目录

cd x264

2.2.3 执行x264配置

注:如果不将include和lib安装在/usr/local目录,之后运行ffmpeg命令时可能仍然报错误

./configure --prefix=/usr/x264/ --includedir=/usr/local/include --libdir=/usr/local/lib --enable-shared

2.2.4 编译x264

执行:

make

2.1.6 安装x264

make install

2.1.7 x264配置环境变量

编辑bashrc文件,执行:

vim ~/.bashrc

在最后PATH添加环境变量:

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

保存退出。
设置生效,执行:source ~/.bashr

2.1.8 验证是否安装成功

pkg-config --libs x264

在这里插入图片描述

2.3 安装 ffmpeg

2.3.1 下载ffmpeg

下载地址:wget https://ffmpeg.org//releases/ffmpeg-6.1.tar.gz

2.3.2 解压

tar -zxvf ffmpeg-6.1.tar.gz

2.3.3 进入ffmpeg目录

cd ffmpeg-6.1

2.3.4 执行ffmpeg配置

./configure --enable-gpl --enable-libx264 --enable-shared --extra-ldflags=-L/usr/local/lib --extra-cflags=-I/usr/local/include

2.3.5 编译ffmpeg

执行:

make

2.3.6 安装ffmpeg

sudo make install

2.3.7 配置环境变量

编辑/etc/profile文件,执行:

vi /etc/profile

在文件的最后位置添加环境变量:

export PATH=$PATH:/usr/local/ffmpeg/bin

刷新资源,使其生效,执行:source /etc/profile

2.3.8 验证是否安装成功

ffmpeg -version

2.3.9 ffmpeg卸载

使用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*

3 遇到过的异常

3.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:

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

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

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

3.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 时,通过 lx264 或 llibx264 选项来指定链接 libx264 库。
例如,你可以在 ffmpeg 的 configure 阶段添加 --extra-libs=-lx264:

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

然后,继续编译 ffmpeg。

3.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

3.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 文件应该在 lib 或 lib/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,看看问题是否解决。

3.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/ayushu/article/details/136687620

  • 28
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

雄哥007

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

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

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

打赏作者

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

抵扣说明:

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

余额充值