FFNPEG编译脚本

下面是一个ffmpeg编译脚本:

#!/bin/bash
set -eu -o pipefail
set +eu +o pipefail

FFMPEG_TAG=n4.5-dev
build_path=$1
git_repo="https://github.com/FFmpeg/FFmpeg.git"
cache_tool=""
sysroot=""
c_compiler="gcc"
cxx_compiler="g++"
ffmpeg_dir=""
build_only=false
parallel="-j$(nproc)"

usage="Usage: $0 [FFMPEG_TAG [build_path [git_repo [cache_tool]]]] [Options]

Options:
    FFMPEG_TAG          FFMPEG git tag. (default $FFMPEG_TAG)
    build_path          Target folder to build. (default \$(pwd))
    git_repo            Link of FFmpeg.git. (default $git_repo)
    [-c ]cache_tool     ccache or sccache
    -S sysroot          sysroot
    -C c-compiler       c compiler
    -X cxx-compier      cxx compiler
    -F ECFLAGS          add ECFLAGS to CFLAGS []
    -s ffmpeg_dir       ffmpeg_dir cloned.
    -j parallel         make -j parallel default is \$(nproc)
    -b                  build only
exp:
    1. $0 $FFMPEG_TAG \$(pwd) $git_repo ccache
    2. $0 $FFMPEG_TAG -c ccache
"

for _ff in FFMPEG_TAG build_path git_repo cache_tool; do
    if [ "${1::1}" = "-" ]; then
        break
    else
        eval $_ff="$1"
        shift
    fi
done

while getopts ':hc:C:X:S:bs:j:' opt; do
    case "$opt" in
    b)
        build_only=true
        ;;
    c)
        cache_tool="$OPTARG "
        ;;
    C)
        c_compiler="$OPTARG"
        ;;
    X)
        cxx_compiler="$OPTARG"
        ;;
    S)
        sysroot="--sysroot=$OPTARG"
        ;;
    s)
        ffmpeg_dir=$(realpath $OPTARG)
        ;;
    j)
        if [ "$OPTARG" -eq "$OPTARG" ]; then
            parallel="-j $OPTARG"
        else
            echo "$OPTARG should be a number, use '$parallel' by default"
        fi
        ;;
    ? | h)
        echo "$usage"
        exit 1
        ;;
    esac
done

src_path=$(dirname $(readlink -f $0))
echo "src path: ${src_path}"
echo "build path: ${build_path}"
echo "ffmpeg path: ${ffmpeg_dir}"

if [ -z $ffmpeg_dir ]; then
    echo "delete old FFmpeg files"
    rm -rf ${build_path}/FFmpeg-${FFMPEG_TAG}
    rm -rf ${build_path}/ffmpeg_gcu

    echo "download FFmpeg-${FFMPEG_TAG}"
    git clone -b ${FFMPEG_TAG} $git_repo ${build_path}/FFmpeg-${FFMPEG_TAG}
    ffmpeg_dir=${build_path}/FFmpeg-${FFMPEG_TAG}
elif ! [ -d $ffmpeg_dir ]; then
    echo "FFmpeg need to be cloned to $ffmpeg_dir"
    exit 1
fi

echo "copy FFmpeg GCU Plugin files info FFmpeg source tree"
cd $ffmpeg_dir
cp ${src_path}/src/configure ${ffmpeg_dir}/
cp ${src_path}/src/libavformat/* ${ffmpeg_dir}/libavformat/
cp ${src_path}/src/libavcodec/* ${ffmpeg_dir}/libavcodec/
cp ${src_path}/src/libavutil/* ${ffmpeg_dir}/libavutil/
cp ${src_path}/src/examples/* ${ffmpeg_dir}/doc/examples/

echo "configure FFmpeg"
./configure \
    --prefix=${build_path}/ffmpeg_gcu \
    --extra-cflags="-g" \
    --extra-ldflags="-fuse-ld=gold" \
    --extra-ldflags="-m64 -lefrt -lefdrv -ltopsrt -ltopscodec -ldl -lpthread" \
    --disable-stripping \
    --disable-x86asm \
    --enable-swscale \
    --disable-decoders \
    --disable-optimizations \
    --enable-topscodec \
    --enable-decoder=vc1 \
    --enable-decoder=av1 \
    --enable-decoder=h264 \
    --enable-decoder=hevc \
    --enable-decoder=vp8 \
    --enable-decoder=vp9 \
    --enable-decoder=mpeg4 \
    --enable-decoder=mpeg2video \
    --enable-decoder=mjpeg \
    --enable-decoder=h264_topscodec \
    --enable-decoder=hevc_topscodec \
    --enable-decoder=mpeg2_topscodec \
    --enable-decoder=mpeg4_topscodec \
    --enable-decoder=h263_topscodec \
    --enable-decoder=vc1_topscodec \
    --enable-decoder=vp8_topscodec \
    --enable-decoder=vp9_topscodec \
    --enable-decoder=avs_topscodec \
    --enable-decoder=avs2_topscodec \
    --enable-decoder=mjpeg_topscodec \
    --enable-decoder=av1_topscodec \
    --cc="${cache_tool}$c_compiler" \
    --cxx="${cache_tool}$cxx_compiler" \
    $sysroot \
    --enable-cross-compile \
    --disable-static \
    --enable-shared

if [ $? -ne 0 ]; then
    echo "configure failed"
    exit 1
fi

make clean

echo "make"
make $parallel
if [ $? -ne 0 ]; then
    echo "make failed"
    exit 1
fi

echo "make examples"
make examples -j
if [ $? -ne 0 ]; then
    echo "make examples failed"
    exit 1
fi

if $build_only; then
    exit 0
fi

echo "make install"
make install
if [ $? -ne 0 ]; then
    echo "make install failed"
    exit 1
fi

cp ${ffmpeg_dir}/doc/examples/hw_decode_tops  ${build_path}/ffmpeg_gcu/bin
cp ${ffmpeg_dir}/doc/examples/decode_tops     ${build_path}/ffmpeg_gcu/bin

你可能感兴趣的:(FFmpeg,ffmpeg)