Linux下编译ffmpeg5.1.3详细图文教程

FFmpeg编译详细教程

FFmpeg编译详细教程

本文原创:猿视野 ( 一家分享技术架构思路,扩展程序员视野的网站,遇到技术问题,可以加联系方式相互交流)
转载请注明出处和相关链接,否则追究其法律责任!
原文地址:https://developer.aliyun.com/article/1326862?source=5176.11533457

环境准备

  1. Window系统安装虚拟机 VMware® Workstation 17 Pro 这里没有选择其它路线,是因为在linux系统上编译FFmpeg更加方便。坑相对少一点。
  2. electerm ssh工具,我这里用的是electerm,一款功能强大的开源ssh连接工具,本文用它来ssh连接我的虚拟机。
  3. ffmpeg-5.1.3.tar.xz
  4. android-ndkc-linux.zip
如果你是window系统,在进行本文之前,请先安装Linux虚拟机。我虚拟机安装的是CentOS6.5 建议大家虚拟机上安装CentOS7系列镜像。CentOS6系列版本在2020年的时候,官方不在支持了。影响你yum的使用。

下载文件

下载ndk25

官网下载:ndk官网
网盘下载:android-ndk-r25c-linux.zip

注意,这是linux环境下的ndk。

下载FFmpeg 5.1.3

官网下载:FFmpeg官网
网盘下载:FFmpeg 5.1.3

下载electerm

网盘下载:electerm

ssh连接工具,用这个远程连接Linux虚拟机

下载VMware 17 pro

网盘下载:VMware 17 pro破解版

下载CentOS6.5镜像或CentOS7镜像

下载地址:

下载glibc-2.17

网盘下载:glibc-2.17.tar.gz

这个文件提前下载好,后文会用到

文件下载好了,把FFmpeg 5.1.3和android-ndk-r25c-linux.zip 文件

先上传到你Linux虚拟机自定义的文件夹目录中,如图所示:

Linux下编译ffmpeg5.1.3详细图文教程_第1张图片

解压文件

解压文件:


#解压ffmpeg-5.1.3.tar.xz 
tar -xf ffmpeg-5.1.3.tar.xz

#解压android-ndk-r25c-linux.zip 
unzip android-ndk-r25c-linux.zip 

编译检查

# 编译FFmpeg之前先检查下环境
[root@CentOS6 ffmpeg-5.1.3]# ./configure --disable-x86asm    
#报错,没有安装gcc编译器
gcc is unable to create an executable file.
If gcc is a cross-compiler, use the --enable-cross-compile option.
Only do this if you know what cross compiling means.
C compiler test failed.

If you think configure made a mistake, make sure you are using the latest
version from Git.  If the latest version fails, report the problem to the
[email protected] mailing list or IRC #ffmpeg on irc.libera.chat.
Include the log file "ffbuild/config.log" produced by configure as this will help
solve the problem.
安装 gcc编译库
[root@CentOS6 yum.repos.d]# yum install -y  gcc

Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
YumRepo Error: All mirror URLs are not using ftp, http[s] or file.
 Eg. Invalid release/repo/arch combination/
removing mirrorlist with no valid mirrors: /var/cache/yum/x86_64/6/base/mirrorlist.txt
Error: Cannot find a valid baseurl for repo: base
yum源失效问题

根据日志得出,问题是由于CentOS 6.5 yum源失效导致的错误,我虚拟机安装的是CentOS6.5版本,官方已经不支持CentOS6系列了,所以需要更换yum源地址。

解决CentOS6 yum源失效的问题:

#先切换文件夹目录,找到目录下的文件CentOS-Base.repo
cd /etc/yum.repos.d

CentOS-Base.repo文件修改为如下:

我这里用的是阿里的镜像


# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client.  You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the 
# remarked out baseurl= line instead.
#
#

[base]
name=CentOS-$releasever - Base
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
#baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/
#baseurl=https://vault.centos.org/6.10/os/x86_64/
#阿里云镜像
baseurl=http://mirrors.aliyun.com/centos-vault/6.5/os/x86_64/
gpgcheck=0
#enabled=0
gpgkey=https://vault.centos.org/6.10/os/x86_64/RPM-GPG-KEY-CentOS-6

#released updates 
[updates]
name=CentOS-$releasever - Updates
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates
#baseurl=http://mirror.centos.org/centos/$releasever/updates/$basearch/
#baseurl=https://vault.centos.org/6.10/updates/$basearch/
#阿里云镜像
baseurl=http://mirrors.aliyun.com/centos-vault/6.5/updates/x86_64/
gpgcheck=0
#enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras
#baseurl=http://mirror.centos.org/centos/$releasever/extras/$basearch/
#baseurl=https://vault.centos.org/6.10/extras/$basearch/
#阿里云镜像
baseurl=http://mirrors.aliyun.com/centos-vault/6.5/extras/x86_64/
gpgcheck=0
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus
#baseurl=http://mirror.centos.org/centos/$releasever/centosplus/$basearch/
#baseurl=https://vault.centos.org/6.10/centosplus/$basearch/
#阿里云镜像
baseurl=http://mirrors.aliyun.com/centos-vault/6.5/centosplus/x86_64/
gpgcheck=0
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6


#contrib - packages by Centos Users
[contrib]
name=CentOS-$releasever - Contrib
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=contrib
#baseurl=http://mirror.centos.org/centos/$releasever/contrib/$basearch/
#baseurl=https://vault.centos.org/6.10/contrib/$basearch/
#阿里云镜像
baseurl=http://mirrors.aliyun.com/centos-vault/6.5/contrib/x86_64/
gpgcheck=0
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6

保存退出后,清除下缓存 yum clean all && yum makecache

其中gpgcheck改为 gpgcheck=0 否则会引发错误:GPG key retrieval failed: [Errno 14] problem making ssl connection

参考:CentOS 6停止维护更新后 yum源失效问题的解决

做完这一步,可以正确安装gcc了

# 安装gcc 编译库
yum install -y  gcc

确认您已经安装了正确版本的gcc和其他编译器工具。您可以使用以下命令来检查:

gcc --version make --version
 #执行命令之后,出现下图,没有报错的话,就可以进行下一步操作了。
 ./configure --disable-x86asm    

Linux下编译ffmpeg5.1.3详细图文教程_第2张图片

编译脚本

安装好gcc编译库之后,现在可以来新建编译脚本了

在FFmpeg目录下新建build_android.sh,想了解详细配置参数请前往这里

# touch 新建文件
[root@CentOS6 ffmpeg-5.1.3]# touch build_android.sh
# vim 编辑build_android.sh
[root@CentOS6 ffmpeg-5.1.3]# vim build_android.sh

写入脚本内容:

#!/bin/bash
#特别注意: 以下路径需要修改成自己Linux系统中的NDK目录!前面上传的ndk解压文件当中
TOOLCHAIN=/usr/local/src/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64
# 最低支持的android sdk版本
API=21
# --extra-cflags中添加'-DVK_ENABLE_BETA_EXTENSIONS=0', 防止引用vulkan_beta.h头文件
function build_android
{
echo "Compiling FFmpeg for $CPU"
./configure \
 --prefix=$PREFIX \
 --disable-neon \
 --disable-hwaccels \
 --disable-gpl \
 --disable-postproc \
 --enable-shared \
 --enable-jni \
 --disable-mediacodec \
 --enable-small \
 --enable-gpl \
 --disable-decoder=h264_mediacodec \
 --disable-static \
 --disable-doc \
 --disable-programs \
 --disable-ffmpeg \
 --disable-ffplay \
 --disable-ffprobe \
 --disable-avdevice \
 --disable-symver \
 --cross-prefix=$CROSS_PREFIX \
 --target-os=android \
 --arch=$ARCH \
 --cpu=$CPU \
 --cc=$CC \
 --cxx=$CXX \
 --enable-cross-compile \
 --sysroot=$SYSROOT \
 --extra-cflags="-DVK_ENABLE_BETA_EXTENSIONS=0 -mno-stackrealign -Os -fpic $OPTIMIZE_CFLAGS" \
 --extra-ldflags="$ADDI_LDFLAGS" \
 $ADDITIONAL_CONFIGURE_FLAG
make clean
make -j4
make install
echo "The Compilation of FFmpeg for $CPU is completed"
}

#armv8-a
ARCH=arm64
CPU=armv8-a
# r21版本的ndk中所有的编译器都在/toolchains/llvm/prebuilt/darwin-x86_64/目录下(clang)
CC=$TOOLCHAIN/bin/aarch64-linux-android$API-clang
CXX=$TOOLCHAIN/bin/aarch64-linux-android$API-clang++
# NDK头文件环境
SYSROOT=$TOOLCHAIN/sysroot
CROSS_PREFIX=$TOOLCHAIN/bin/llvm-
# so输出路径
PREFIX=$(pwd)/android/$CPU
OPTIMIZE_CFLAGS="-march=$CPU"
build_android

# 交叉编译工具目录,对应关系如下
# armv8a -> arm64 -> aarch64-linux-android-
# armv7a -> arm -> arm-linux-androideabi-
# x86 -> x86 -> i686-linux-android-
# x86_64 -> x86_64 -> x86_64-linux-android-

# CPU架构
#armv7-a
ARCH=arm
CPU=armv7-a
CC=$TOOLCHAIN/bin/armv7a-linux-androideabi$API-clang
CXX=$TOOLCHAIN/bin/armv7a-linux-androideabi$API-clang++
SYSROOT=$TOOLCHAIN/sysroot
CROSS_PREFIX=$TOOLCHAIN/bin/llvm-
PREFIX=$(pwd)/android/$CPU
OPTIMIZE_CFLAGS="-mfloat-abi=softfp -mfpu=vfp -marm -march=$CPU "
build_android

# #x86
# ARCH=x86
# CPU=x86
# CC=$TOOLCHAIN/bin/i686-linux-android$API-clang
# CXX=$TOOLCHAIN/bin/i686-linux-android$API-clang++
# SYSROOT=$TOOLCHAIN/sysroot
# CROSS_PREFIX=$TOOLCHAIN/bin/i686-linux-android-
# PREFIX=$(pwd)/android/$CPU
# OPTIMIZE_CFLAGS="-march=i686 -mtune=intel -mssse3 -mfpmath=sse -m32"
# build_android

# #x86_64
# ARCH=x86_64
# CPU=x86-64
# CC=$TOOLCHAIN/bin/x86_64-linux-android$API-clang
# CXX=$TOOLCHAIN/bin/x86_64-linux-android$API-clang++
# SYSROOT=$TOOLCHAIN/sysroot
# CROSS_PREFIX=$TOOLCHAIN/bin/x86_64-linux-android-
# PREFIX=$(pwd)/android/$CPU
# OPTIMIZE_CFLAGS="-march=$CPU -msse4.2 -mpopcnt -m64 -mtune=intel"
# 方法调用
# build_android

赋权限755

在FFmpeg目录下的ffbuild目录中,将图中sh文件权限改为755,否则编译过程中可能报错无权限执行该文件。build_android.sh文件也赋权一下。

Linux下编译ffmpeg5.1.3详细图文教程_第3张图片

chmod 755  build_android.sh

执行脚本

./ build_android.sh

发生报错:

/usr/local/src/ffmpeg5.1.3/android-ndk-r25c/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android21-clang is unable to create an executable file.
C compiler test failed.

编译失败日志(日志信息位于ffbuild目录下的config.log文件):

BEGIN /tmp/ffconf.YxWX1yPS/test.c
    1    int main(void){ return 0; }
END /tmp/ffconf.YxWX1yPS/test.c
/usr/local/src/ffmpeg5.1.3/android-ndk-r25c/toolchains/llvm/prebuilt/linux-x86_64/bin/armv7a-linux-androideabi21-clang --sysroot=/usr/local/src/ffmpeg5.1.3/android-ndk-r25c/toolchains/llvm/prebuilt/linux-x86_64/sysroot -DVK_ENABLE_BETA_EXTENSIONS=0 -mno-stackrealign -Os -fpic -mfloat-abi=softfp -mfpu=vfp -marm -march=armv7-a -march=armv7-a -c -o /tmp/ffconf.YxWX1yPS/test.o /tmp/ffconf.YxWX1yPS/test.c
/usr/local/src/ffmpeg5.1.3/android-ndk-r25c/toolchains/llvm/prebuilt/linux-x86_64/bin/clang: /lib64/libc.so.6: version `GLIBC_2.14' not found (required by /usr/local/src/ffmpeg5.1.3/android-ndk-r25c/toolchains/llvm/prebuilt/linux-x86_64/bin/clang)
/usr/local/src/ffmpeg5.1.3/android-ndk-r25c/toolchains/llvm/prebuilt/linux-x86_64/bin/clang: /lib64/libc.so.6: version `GLIBC_2.15' not found (required by /usr/local/src/ffmpeg5.1.3/android-ndk-r25c/toolchains/llvm/prebuilt/linux-x86_64/bin/clang)
/usr/local/src/ffmpeg5.1.3/android-ndk-r25c/toolchains/llvm/prebuilt/linux-x86_64/bin/clang: /lib64/libc.so.6: version `GLIBC_2.14' not found (required by /usr/local/src/ffmpeg5.1.3/android-ndk-r25c/toolchains/llvm/prebuilt/linux-x86_64/bin/../lib64/libc++.so.1)
/usr/local/src/ffmpeg5.1.3/android-ndk-r25c/toolchains/llvm/prebuilt/linux-x86_64/bin/clang: /lib64/libc.so.6: version `GLIBC_2.17' not found (required by /usr/local/src/ffmpeg5.1.3/android-ndk-r25c/toolchains/llvm/prebuilt/linux-x86_64/bin/../lib64/libc++.so.1)
C compiler test failed.

这个错误的原因是未安装2.17版本的glibc库。

而在CentOS6上,使用yum install glibc命令,只能更新到2.12版本。需要手动下载编译安装。

查看当前版本:

[root@CentOS6 ffmpeg-5.1.3]# rpm -qa | grep glibc
glibc-common-2.12-1.132.el6.x86_64
glibc-devel-2.12-1.132.el6.x86_64
glibc-2.12-1.132.el6.x86_64
glibc-headers-2.12-1.132.el6.x86_64

升级glibc到2.17版本

升级教程参考:Linux中提示:/lib64/libc.so.6: version `GLIBC_2.17' not found 的解决办法

编译成功

再次编译FFmpeg:

./ build_android.sh

编译成功,可以看到已经生成了.so动态库文件。接下来需要做动态库的裁剪,我们不需要这么大的文件。

Linux下编译ffmpeg5.1.3详细图文教程_第4张图片

裁剪编译

裁剪FFmpeg打包动态库:

你可能感兴趣的:(ffmpeg音视频linux)