参考博客:
1. linux(centos)安装 ffmpeg,并添加 libx264库:https://blog.csdn.net/u013015301/article/details/140778199
ffmpeg 执行时如添加参数 -vcodec libx264,会出现错误:Unknown encoder 'libx264’的错误,缺少libx264库,需要安装该库,安装步骤如下:
$ 代表普通用户 # 代表root权限
确认 linux版本为 fedora:
cat /etc/*-release
Amazon Linux release 2023.5.20240819 (Amazon Linux)
NAME="Amazon Linux"
VERSION="2023"
ID="amzn"
ID_LIKE="fedora" // !!!!!!
VERSION_ID="2023"
PLATFORM_ID="platform:al2023"
PRETTY_NAME="Amazon Linux 2023.5.20240819"
ANSI_COLOR="0;33"
CPE_NAME="cpe:2.3:o:amazon:amazon_linux:2023"
HOME_URL="https://aws.amazon.com/linux/amazon-linux-2023/"
DOCUMENTATION_URL="https://docs.aws.amazon.com/linux/"
SUPPORT_URL="https://aws.amazon.com/premiumsupport/"
BUG_REPORT_URL="https://github.com/amazonlinux/amazon-linux-2023"
VENDOR_NAME="AWS"
VENDOR_URL="https://aws.amazon.com/"
SUPPORT_END="2028-03-15"
Amazon Linux release 2023.5.20240819 (Amazon Linux)
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 ~/.bashrc
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
解决方案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