背景
Ffmpeg 中带有h264的解码,没有编码,需要添加x264。libx264是一个自由的H.264编码库,是x264项目的一部分,使用广泛,ffmpeg的H.264实现就是用的libx264。
FFmpeg是一套可以用来记录、转换数字音频、视频,并能将其转化为流的开源计算机程序。采用LGPL或GPL许可证。它提供了录制、转换以及流化音视频的完整解决方案。
librtmp用来接收、发布RTMP协议格式的数据。FFmpeg支持RTMP协议,将librtmp编译进去后支持协议RTMPE、RMTPTE、RTMPS。这里我直接使用FFmpeg自带的RTMP功能。
host平台 :Ubuntu 18.04
arm平台 : S5P6818
x264 :20171212
x265 :v2.6
ffmpeg :3.4.1
arm-gcc :4.8.1
使用以下脚本一键编译。
##
# Copyright By Schips, All Rights Reserved
# https://gitee.com/schips/
# File Name: make.sh
# Created : Mon 02 Sep 2019 08:05:53 PM HKT
##
#!/bin/sh
BASE=`pwd`
BUILD_HOST=arm-linux
OUTPUT_PATH=${BASE}/install
OTHER_LIB=${OUTPUT_PATH}/__all_without_ffmpeg
make_dirs () {
#为了方便管理,创建有关的目录
cd ${BASE} && mkdir compressed 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 http://download.videolan.org/pub/videolan/x264/snapshots/x264-snapshot-20171212-2245.tar.bz2
tget http://ffmpeg.org/releases/ffmpeg-3.4.1.tar.bz2
tget http://download.videolan.org/videolan/x265/x265_2.6.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*
CC=${BUILD_HOST}-gcc
./configure
--host=${BUILD_HOST}
--enable-shared
--enable-static
--enable-pic
--prefix=${OUTPUT_PATH}/x264
--cross-prefix=${BUILD_HOST}-
--disable-asm
make -j4 && make install
}
make_x265() {
#sudo apt-get install cmake -y
# 其他编译选项可以 通过 在 cmake ../source 以后 ccmake ../source 可以查看 ( ccmake 可以通过 sudo apt-get install cmake-curses-gui 进行安装 )
cd ${BASE}/source/x265*/source
# 获取 工具链所在位置 下面的操作为的是在 CMakeLists.txt 中插入下面内容
#set( CMAKE_SYSTEM_NAME Linux )
#set( CMAKE_SYSTEM_PROCESSOR ARM )
#set( CMAKE_C_COMPILER "/opt/hisi-linux/x86-arm/arm-hisiv500-linux/target/bin/arm-hisiv500-linux-gcc" )
#set( CMAKE_CXX_COMPILER "/opt/hisi-linux/x86-arm/arm-hisiv500-linux/target/bin/arm-hisiv500-linux-g++" )
#set( CMAKE_FIND_ROOT_PATH "/opt/hisi-linux/x86-arm/arm-hisiv500-linux/target/bin/" )
GCC_FULL_PATH=`whereis ${BUILD_HOST}-gcc | awk -F: '{ print $2 }' | awk '{print $1}'` # 防止多个结果
GCC_DIR=`dirname ${GCC_FULL_PATH}/`
sed -i "1iset( CMAKE_SYSTEM_NAME Linux )" CMakeLists.txt
sed -i "2aset( CMAKE_SYSTEM_PROCESSOR ARM )" CMakeLists.txt
sed -i "2aset( CMAKE_C_COMPILER ${GCC_DIR}/${BUILD_HOST}-gcc )" CMakeLists.txt
sed -i "2aset( CMAKE_CXX_COMPILER ${GCC_DIR}/${BUILD_HOST}-g++ )" CMakeLists.txt
sed -i "2aset( CMAKE_FIND_ROOT_PATH ${GCC_DIR} )" CMakeLists.txt
cmake ../source
# 指定安装路径
sed -i "1iset( CMAKE_INSTALL_PREFIX "${BASE}/install/x265" )" cmake_install.cmake
make && make install
}
prepare_other_lib () {
# 这一个是针对 ffmpeg 方便管理外部库使用的
# 核心思想是把 所有的库都放到一起,再让 ffmpeg ld的时候在这里找(而不是添加多行) --extra-cflags="-I${X264_DIR}/include -I${xxx}/include"
cd ${BASE}/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() {
cd ${BASE}/source/ffmpeg*
./configure
--cross-prefix=${BUILD_HOST}-
--enable-cross-compile
--target-os=linux
--cc=${BUILD_HOST}-gcc
--arch=arm
--prefix=${OUTPUT_PATH}/ffmpeg
--enable-shared
--enable-static
--enable-gpl
--enable-nonfree
--enable-ffmpeg
--disable-ffplay
--enable-ffserver
--enable-swscale
--enable-pthreads
--disable-armv5te
--disable-armv6
--disable-armv6t2
--disable-yasm
--disable-stripping
--enable-libx264
--extra-cflags=-I${OTHER_LIB}/include
--extra-ldflags=-L${OTHER_LIB}/lib
make clean && make -j4 && make install
}
echo "Using ${BUILD_HOST}-gcc"
make_dirs
download_package
tar_package
make_x264
make_x265
prepare_other_lib
make_ffmpeg
配置完成以后可能会有这个警告,但是不影响后续的结果
License: nonfree and unredistributable
Creating configuration files ...
WARNING: The --disable-yasm option is only provided for compatibility and will be
removed in the future. Use --enable-x86asm / --disable-x86asm instead.
WARNING: unknown architecture linux
WARNING: using libx264 without pkg-config
开发板准备:
拷贝 __all_without_ffmpeg 中的 lib下的所有文件到板子上的/usr/lib 中,同样地,拷贝ffmpeg中的lib到板子上。
测试:
ffmpeg -f video4linux2 -s 320x240 -i /dev/video0 /mnt/tmp/test.avi
# video4linux2 代表Linux下
# /dev/video0 代表摄像头设备
# /mnt/tmp/test.avi代表输出路径