环境

  1. Ubuntu16.04 x86_64
  2. 交叉编译工具arm-linux-gnueabihf
  3. ARM平台 cortexa9

准备编译

以下均采用脚本进行编译,可自行根据 download_package () 中的下载地址下载包并存放在 compressed 目录中

我这里已经注释掉了 download_package

编译x86版本

在一个空目录下新建一个脚本 ,如 x86-make.sh ,写入以下内容

#!/bin/sh

BASE=`pwd`
OUTPUT_PATH=${BASE}/x86-install

make_dirs () {
    #为了方便管理,创建有关的目录
    cd ${BASE} && mkdir compressed x86-install source -p
}

tget () { #try wget
    filename=`basename $1`
    echo "Downloading [${filename}]..."
    if [ ! -f ${filename} ];then
        wget $1
    fi

    echo "[OK] Downloaded [${filename}] "
}

#下载地址,可自行下载存放在 compressed 目录中
download_package () {
    cd ${BASE}/compressed
    tget https://code.videolan.org/videolan/x264/-/archive/master/x264-master.tar.bz2
    tget https://ffmpeg.org/releases/ffmpeg-5.0.tar.gz
    tget http://download.videolan.org/videolan/x265/x265_3.2.tar.gz
}

tar_package () {
    cd ${BASE}/compressed
    ls * > /tmp/list.txt
    for TAR in `cat /tmp/list.txt`
    do
        tar -xf $TAR -C  ../source
    done
    rm -rf /tmp/list.txt
}

make_yasm(){
	cd ${BASE}/source/yasm*
	./configure 

	make -j8 && sudo make install
}

make_nasm(){
	cd ${BASE}/source/nasm*
	./configure 

	make -j8 && sudo make install
}

make_x264() {
    cd ${BASE}/source/x264*

	./configure --prefix=${OUTPUT_PATH}/ --enable-static --enable-pic

    make -j8 && make install
}

make_x265() {
    cd ${BASE}/source/x265*/build/linux
	# 编译安装
	cmake -G "Unix Makefiles" \
	-DCMAKE_INSTALL_PREFIX=${OUTPUT_PATH} \
	-DCMAKE_ASM_NASM_FLAGS=-w-macro-params-legacy \
	-DENABLE_SHARED:bool=off ../../source

	make -j8 && make install

	cd ${OUTPUT_PATH}/lib/pkgconfig
	# 修改x265.pc
    sed -i '9,9s/-lx265/-lx265 -lm -lstdc++ -ldl -lpthread/g' x265.pc
	sed -i '10,10s/-lstdc++/-lstdc++ -lm -lgcc_s -lgcc -lrt -ldl/g' x265.pc

}

make_sdl() {
	cd ${BASE}/source/SDL*
	# ./autogen.sh

	./configure
	
	make -j8 && sudo make install
}

make_ffmpeg() {
    export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:${OUTPUT_PATH}/lib/pkgconfig/

    cd ${BASE}/source/ffmpeg*

	./configure \
	--prefix=${OUTPUT_PATH}/ffmpeg5.0 \
	--enable-gpl \
	--enable-pthreads \
	--enable-libx264 \
	--enable-libx265 \
	--enable-pic \
	--enable-shared \
	--enable-nonfree \
	--enable-postproc \
	--enable-ffplay

	make -j8 && make install
}

echo "Using ${BUILD_HOST}-gcc"
make_dirs
# download_package
tar_package

sudo apt install cmake -y
sudo apt install pkg-config
sudo apt-get install libx11-dev
sudo apt-get install xorg-dev

make_yasm
make_nasm
make_sdl
make_x264
make_x265
make_ffmpeg

以下为废弃脚本(换了一台新机子之后,该版本不能用了,奇怪的很,也懒得排查错误了,直接写个新的)

#!/bin/sh

BASE=`pwd`
OUTPUT_PATH=${BASE}/x86-install

make_dirs () {
    #为了方便管理,创建有关的目录
    cd ${BASE} && mkdir compressed x86-install source -p
}

tget () { #try wget
    filename=`basename $1`
    echo "Downloading [${filename}]..."
    if [ ! -f ${filename} ];then
        wget $1
    fi

    echo "[OK] Downloaded [${filename}] "
}

#下载地址,可自行下载存放在 compressed 目录中
download_package () {
    cd ${BASE}/compressed
    tget https://code.videolan.org/videolan/x264/-/archive/master/x264-master.tar.bz2
    tget https://ffmpeg.org/releases/ffmpeg-5.0.tar.gz
    tget http://download.videolan.org/videolan/x265/x265_3.2.tar.gz
}

tar_package () {
    cd ${BASE}/compressed
    ls * > /tmp/list.txt
    for TAR in `cat /tmp/list.txt`
    do
        tar -xf $TAR -C  ../source
    done
    rm -rf /tmp/list.txt
}

make_yasm(){
	cd ${BASE}/source/yasm*
	./configure \
	--prefix=${OUTPUT_PATH}/

	make -j8 && make install
}

make_nasm(){
	cd ${BASE}/source/nasm*
	./configure \
	--prefix=${OUTPUT_PATH}/

	make -j8 && make install
}

make_x264() {
    cd ${BASE}/source/x264*

    ./configure \
    --prefix=${OUTPUT_PATH}/ \
    --enable-static \
    --enable-pic \
    --disable-asm

    make -j8 && make install
}

make_x265() {
    cd ${BASE}/source/x265*/build/linux
	# 编译安装
	cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX=${OUTPUT_PATH} -DENABLE_SHARED:bool=off ../../source
	make -j8 && make install
}

make_ffmpeg() {
    MYPKGCONFIG=${OUTPUT_PATH}/lib/pkgconfig/

    cd ${BASE}/source/ffmpeg*

	PKG_CONFIG_PATH=${MYPKGCONFIG}
	CFLAGS="-O3 -fPIC"
	./configure \
	--prefix=${OUTPUT_PATH}/ffmpeg \
	--pkg-config-flags="--static" \
	--extra-cflags=-I${OUTPUT_PATH}/include \
	--extra-ldflags=-L${OUTPUT_PATH}/lib \
	--extra-libs="-lpthread -lm" \
	--enable-gpl \
	--enable-libx264 \
	--enable-libx265 \
	--enable-pic \
	--enable-shared   \
	--enable-nonfree 

	make -j8 && make install
}

echo "Using x86-gcc"
make_dirs
# download_package
tar_package
make_x264
make_x265
make_ffmpeg

编译arm版本

在一个空目录下新建一个脚本 ,如 arm-make.sh ,写入以下内容

#!/bin/sh

BASE=`pwd`
BUILD_HOST=arm-linux-gnueabihf
OUTPUT_PATH=${BASE}/arm-install

OTHER_LIB=${OUTPUT_PATH}/all_without_ffmpeg

set_env(){
	export CROSS_COMPILE=/opt/pkg/petalinux/2018.3/tools/linux-i386/gcc-arm-linux-gnueabi/bin/arm-linux-gnueabihf-
	AS=${CROSS_COMPILE}as
	AR=${CROSS_COMPILE}ar
	NM=${CROSS_COMPILE}nm
	CC=${CROSS_COMPILE}gcc
	GG=${CROSS_COMPILE}g++
	CXX=${CROSS_COMPILE}c++
	LD=${CROSS_COMPILE}ld
	RANLIB=${CROSS_COMPILE}ranlib
	STRIP=${CROSS_COMPILE}strip
	export AS AR NM CC GG LD RANLIB STRIP
	export PATH=$PATH:/opt/pkg/petalinux/2018.3/tools/linux-i386/gcc-arm-linux-gnueabi/bin
}

make_dirs () {
    #为了方便管理,创建有关的目录
    cd ${BASE} && mkdir compressed arm-install source -p
}

tget () { #try wget
    filename=`basename $1`
    echo "Downloading [${filename}]..."
    if [ ! -f ${filename} ];then
        wget $1
    fi

    echo "[OK] Downloaded [${filename}] "
}

download_package () {
    cd ${BASE}/compressed
    tget https://code.videolan.org/videolan/x264/-/archive/master/x264-master.tar.bz2
    tget https://ffmpeg.org/releases/ffmpeg-5.0.tar.gz
    tget http://download.videolan.org/videolan/x265/x265_3.2.tar.gz
}

tar_package () {
    cd ${BASE}/compressed
    ls * > /tmp/list.txt
    for TAR in `cat /tmp/list.txt`
    do
        tar -xf $TAR -C  ../source
    done
    rm -rf /tmp/list.txt
}

make_x264() {
    cd ${BASE}/source/x264*

    ./configure \
    --prefix=${OUTPUT_PATH}/x264 \
    --host=${BUILD_HOST} \
    --enable-shared \
    --enable-pic \
    --disable-asm

    make -j8 && make install
}

make_x265() {
    cd ${BASE}/source/x265*/build
	mkdir arm-x265
	cd arm-x265
    # 获取 工具链所在位置
	GCC_FULL_PATH=`whereis ${BUILD_HOST}-gcc | awk -F: '{ print $2 }' | awk '{print $1}'` # 防止多个结果
    CROSS_PATH=`dirname ${GCC_FULL_PATH}`
	touch crosscompile.cmake
	#echo "set(CROSS_COMPILE_ARM 1)" > crosscompile.cmake
	echo "set(CMAKE_SYSTEM_NAME Linux)" > crosscompile.cmake
	echo "set(CMAKE_SYSTEM_PROCESSOR arm)" >> crosscompile.cmake
	echo "" >> crosscompile.cmake
	echo "# specify the cross compiler" >> crosscompile.cmake
	echo "set(CMAKE_C_COMPILER ${CROSS_PATH}/${BUILD_HOST}-gcc)" >> crosscompile.cmake
	echo "set(CMAKE_CXX_COMPILER ${CROSS_PATH}/${BUILD_HOST}-g++)" >> crosscompile.cmake
	echo "set(CMAKE_SHARED_LINKER_FLAGS \"-ldl \${CMAKE_SHARED_LINKER_FLAGS}\")" >> crosscompile.cmake
	echo "" >> crosscompile.cmake
	echo "# specify the target environment" >> crosscompile.cmake
	echo "SET(CMAKE_FIND_ROOT_PATH  ${CROSS_PATH})" >> crosscompile.cmake
	# 编译安装
	cmake -DCMAKE_TOOLCHAIN_FILE=crosscompile.cmake -G "Unix Makefiles" \
	-DCMAKE_C_FLAGS="-fPIC ${CMAKE_C_FLAGS}" -DCMAKE_CXX_FLAGS="-fPIC ${CMAKE_CXX_FLAGS}" \
	-DCMAKE_SHARED_LINKER_FLAGS="-ldl ${CMAKE_SHARED_LINKER_FLAGS}"  \
	-DCMAKE_INSTALL_PREFIX=${OUTPUT_PATH}/x265 \
	../../source && make -j8
	make install
}

prepare_other_lib () {
    # 这一个是针对 ffmpeg 方便管理外部库使用的
    # 核心思想是把 所有的库都放到一起,再让 ffmpeg ld的时候在这里找(而不是添加多行) --extra-cflags="-I${X264_DIR}/include -I${xxx}/include" \
    cd ${BASE}/arm-install/
    rm ${OTHER_LIB} -rf
    ls > /tmp/list.txt
    mkdir ${OTHER_LIB} -p
    for sub_dir in `cat /tmp/list.txt`
    do
        cp ${sub_dir}/* ${OTHER_LIB} -r -v
    done
    rm -rf /tmp/list.txt
}

make_ffmpeg() {
    MYPKGCONFIG=${BASE}/arm-install/x265/lib/pkgconfig/
    export PKG_CONFIG_PATH=${MYPKGCONFIG}:$PKG_CONFIG_PATH
    cd ${BASE}/source/ffmpeg*
	
	./configure \
	--prefix=${OUTPUT_PATH}/ffmpeg \
	--enable-cross-compile \
	--cross-prefix=${BUILD_HOST}- \
	--arch=arm \
	--host-os=linux \
	--target-os=linux \
	--cc=${BUILD_HOST}-gcc \
	--enable-shared \
	--enable-pic \
	--enable-gpl \
	--enable-nonfree \
	--enable-pthreads \
	--enable-ffmpeg \
	--enable-swscale  \
	--enable-libx264 \
	--enable-libx265 \
	--enable-decoder=hevc \
    --enable-ffplay \
	--disable-stripping \
	--disable-doc  --disable-debug --disable-iconv --disable-armv5te --disable-armv6 --disable-armv6t2 \
	--pkg-config="pkg-config --static" \
	--extra-cflags=-I${OTHER_LIB}/include \
	--extra-ldflags=-L${OTHER_LIB}/lib

	make -j8 && make install
}

echo "Using ${BUILD_HOST}-gcc"
make_dirs
# download_package
tar_package
set_env
make_x264
make_x265
prepare_other_lib
make_ffmpeg

arm编译结果

目录如下

在这里插入图片描述

编译结果如下,all_without_ffmpeg 目录中包含 x264 x265
ffmpeg 目录中发现没有 ffplay ,可以用但是个人觉得还不够

在这里插入图片描述

添加ffplay

给脚本加亿点细节,核心就是交叉编译 SDL 库加上去即可
修改后的脚本如下

#!/bin/sh

BASE=`pwd`
BUILD_HOST=arm-linux-gnueabihf
OUTPUT_PATH=${BASE}/arm-install

OTHER_LIB=${OUTPUT_PATH}/all_without_ffmpeg

set_env(){
	export CROSS_COMPILE=/opt/pkg/petalinux/2018.3/tools/linux-i386/gcc-arm-linux-gnueabi/bin/arm-linux-gnueabihf-
	AS=${CROSS_COMPILE}as
	AR=${CROSS_COMPILE}ar
	NM=${CROSS_COMPILE}nm
	CC=${CROSS_COMPILE}gcc
	GG=${CROSS_COMPILE}g++
	CXX=${CROSS_COMPILE}c++
	LD=${CROSS_COMPILE}ld
	RANLIB=${CROSS_COMPILE}ranlib
	STRIP=${CROSS_COMPILE}strip
	export AS AR NM CC GG LD RANLIB STRIP
	export PATH=$PATH:/opt/pkg/petalinux/2018.3/tools/linux-i386/gcc-arm-linux-gnueabi/bin
}

make_dirs () {
    #为了方便管理,创建有关的目录
    cd ${BASE} && mkdir compressed arm-install source -p
}

tget () { #try wget
    filename=`basename $1`
    echo "Downloading [${filename}]..."
    if [ ! -f ${filename} ];then
        wget $1
    fi

    echo "[OK] Downloaded [${filename}] "
}

download_package () {
    cd ${BASE}/compressed
    tget https://code.videolan.org/videolan/x264/-/archive/master/x264-master.tar.bz2
    tget https://ffmpeg.org/releases/ffmpeg-5.0.tar.gz
    tget http://download.videolan.org/videolan/x265/x265_3.2.tar.gz
	tget http://www.libsdl.org/release/SDL2-2.0.22.tar.gz
}

tar_package () {
    cd ${BASE}/compressed
    ls * > /tmp/list.txt
    for TAR in `cat /tmp/list.txt`
    do
        tar -xf $TAR -C  ../source
    done
    rm -rf /tmp/list.txt
}

make_sdl() {
	cd ${BASE}/source/SDL*
	./autogen.sh

	./configure \
	--prefix=${OUTPUT_PATH}/sdl \
	--host=${BUILD_HOST} \
	--disable-pulseaudio


	make -j8 && make install
}

make_x264() {
    cd ${BASE}/source/x264*

    ./configure \
    --prefix=${OUTPUT_PATH}/x264 \
    --host=${BUILD_HOST} \
    --enable-shared \
    --enable-pic \
    --disable-asm

    make -j8 && make install
}

make_x265() {
    cd ${BASE}/source/x265*/build
	mkdir arm-x265
	cd arm-x265
    # 获取 工具链所在位置
	GCC_FULL_PATH=`whereis ${BUILD_HOST}-gcc | awk -F: '{ print $2 }' | awk '{print $1}'` # 防止多个结果
    CROSS_PATH=`dirname ${GCC_FULL_PATH}`
	touch crosscompile.cmake
	#echo "set(CROSS_COMPILE_ARM 1)" > crosscompile.cmake
	echo "set(CMAKE_SYSTEM_NAME Linux)" > crosscompile.cmake
	echo "set(CMAKE_SYSTEM_PROCESSOR arm)" >> crosscompile.cmake
	echo "" >> crosscompile.cmake
	echo "# specify the cross compiler" >> crosscompile.cmake
	echo "set(CMAKE_C_COMPILER ${CROSS_PATH}/${BUILD_HOST}-gcc)" >> crosscompile.cmake
	echo "set(CMAKE_CXX_COMPILER ${CROSS_PATH}/${BUILD_HOST}-g++)" >> crosscompile.cmake
	echo "set(CMAKE_SHARED_LINKER_FLAGS \"-ldl \${CMAKE_SHARED_LINKER_FLAGS}\")" >> crosscompile.cmake
	echo "" >> crosscompile.cmake
	echo "# specify the target environment" >> crosscompile.cmake
	echo "SET(CMAKE_FIND_ROOT_PATH  ${CROSS_PATH})" >> crosscompile.cmake
	# 编译安装
	cmake -DCMAKE_TOOLCHAIN_FILE=crosscompile.cmake -G "Unix Makefiles" \
	-DCMAKE_C_FLAGS="-fPIC ${CMAKE_C_FLAGS}" -DCMAKE_CXX_FLAGS="-fPIC ${CMAKE_CXX_FLAGS}" \
	-DCMAKE_SHARED_LINKER_FLAGS="-ldl ${CMAKE_SHARED_LINKER_FLAGS}"  \
	-DCMAKE_INSTALL_PREFIX=${OUTPUT_PATH}/x265 \
	../../source && make -j8
	make install
}

prepare_other_lib () {
    # 这一个是针对 ffmpeg 方便管理外部库使用的
    # 核心思想是把 所有的库都放到一起,再让 ffmpeg ld的时候在这里找(而不是添加多行) --extra-cflags="-I${X264_DIR}/include -I${xxx}/include" \
    cd ${BASE}/arm-install/
    rm ${OTHER_LIB} -rf
    ls > /tmp/list.txt
    mkdir ${OTHER_LIB} -p
    for sub_dir in `cat /tmp/list.txt`
    do
        cp ${sub_dir}/* ${OTHER_LIB} -r -v
    done
    rm -rf /tmp/list.txt

    # 把SDL2库放到和其他库同一目录下
	cd ${OTHER_LIB}/include
	cp SDL2/. ./
	rm -rf SDL2
}

make_ffmpeg() {
    MYPKGCONFIG=${OTHER_LIB}/lib/pkgconfig/
    export PKG_CONFIG_PATH=${MYPKGCONFIG}:$PKG_CONFIG_PATH

    cd ${BASE}/source/ffmpeg*
	
	./configure \
	--prefix=${OUTPUT_PATH}/ffmpeg5.0 \
	--enable-cross-compile \
	--cross-prefix=${BUILD_HOST}- \
	--arch=arm \
	--host-os=linux \
	--target-os=linux \
	--cc=${BUILD_HOST}-gcc \
	--enable-shared \
	--enable-pic \
	--enable-gpl \
	--enable-nonfree \
	--enable-pthreads \
	--enable-ffmpeg \
	--enable-swscale  \
	--enable-libx264 \
	--enable-libx265 \
	--enable-decoder=hevc \
	--enable-sdl \
    --enable-ffplay \
	--disable-stripping \
	--disable-doc  --disable-debug --disable-iconv --disable-armv5te --disable-armv6 --disable-armv6t2 \
	--pkg-config="pkg-config --static" \
	--extra-cflags=-I${OTHER_LIB}/include \
	--extra-ldflags=-L${OTHER_LIB}/lib 

	#修改config.mak,使能FFPLAY
	cd ${BASE}/source/ffmpeg*/ffbuild
	sed -i 's/!CONFIG_FFPLAY=yes/CONFIG_FFPLAY=yes/g' config.mak

	cd ${BASE}/source/ffmpeg*
	make -j8 && make install
}

echo "Using ${BUILD_HOST}-gcc"
make_dirs
# download_package
tar_package
set_env
make_sdl
make_x264
make_x265
prepare_other_lib
make_ffmpeg

添加ffplay后的结果

在这里插入图片描述

如图,有了ffplay
file ffplay 一下,结果说明可以运行在arm板子上

在这里插入图片描述

上板测试

需要把 all_without_ffmpegffmpeg5.0 目录中的 lib 中所有文件拷贝到开发板的 /usr/lib
ffmpeg5.0 目录中的 bin 中所有文件拷贝到开发板的 /usr/bin

插入SD卡到ubuntu,拷贝可使用命令

cd ffmpeg5.0
sudo cp -a bin/* /media/hammer/rootfs/usr/bin/
sudo cp -a lib/* /media/hammer/rootfs/usr/lib/

cd all_without_ffmpeg
sudo cp -a lib/* /media/hammer/rootfs/usr/lib/

开发板终端任意目录下输入以下命令

 ffmpeg -f video4linux2 -s 640x480 -i /dev/video0 ./test.avi 
 # video4linux2 代表Linux下  
 # /dev/video0 代表摄像头设备 
 # ./test.avi代表输出路径

出现 test.avi 说明ffmpeg移植成功;同样,ffplay也可自行测试

参考文档

arm linux 移植ffmpeg+x264+265

从零开始学习音视频编程技术(35) Linux搭建Qt5.13和ffmpeg4.2

FFMPEG交叉编译问题(FFmpeg安装没有ffplay)

Linux下配置Qt+ffmpeg开发环境 Ubuntu18.04

编译x86遇到的问题

1. Error:libx264 not found.
ffbuild/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编译选项添加 --extra-libs="-lpthread -lm -ldl"

2. Error:x265 not found using pkg-config
ffbuild/config.log 报错提示

usr/local/include/x264.h:40:4: warning: #warning You must include stdint.h or inttypes.h before x264.h [-Wcpp]
# warning You must include stdint.h or inttypes.h before x264.h
^~~~~~~
/tmp/ffconf.hRcv9aEw/test.c:3:2: error: #error "unsatisfied condition: X264_MPEG2"
#error "unsatisfied condition: X264_MPEG2"
^~~~~
require_pkg_config libx265 x265 x265.h x265_api_get
check_pkg_config libx265 x265 x265.h x265_api_get
test_pkg_config libx265 x265 x265.h x265_api_get
pkg-config --exists --print-errors x265
check_func_headers x265.h x265_api_get -I/usr/local/include -L/usr/local/lib -lx265
test_ld cc -I/usr/local/include -L/usr/local/lib -lx265
test_cc -I/usr/local/include -L/usr/local/lib
BEGIN /tmp/ffconf.hRcv9aEw/test.c
....
ERROR: x265 not found using pkg-config

解决方案:
ffmpeg编译选项改 --pkg-config-flags="--static"--pkg-config="pkg-config --static"

3. x264 using cpu capabilities: none!
使用 libx264 进行视频编码时,发现速度很慢,同时提示using cpu capabilities: none!
原因在于编译x264时加了编译选项 --disable-asm

解决方案:
下载nasm nasm下载地址

cd 目录
./configure
make -j8
sudo make install

# 查看安装是否成功
nasm --version

删去x264的编译选项 --disable-asm

Logo

更多推荐