Skip to content

Instantly share code, notes, and snippets.

@denji
Forked from lucaspar/nvenc-install.sh
Last active August 24, 2025 10:53
Show Gist options
  • Save denji/fc963b79498328a2145944fc5029cddc to your computer and use it in GitHub Desktop.
Save denji/fc963b79498328a2145944fc5029cddc to your computer and use it in GitHub Desktop.

Revisions

  1. denji revised this gist Aug 24, 2025. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions nvenc-install.sh
    Original file line number Diff line number Diff line change
    @@ -62,6 +62,7 @@ function install_nvenc_sdk() {
    NVIDIA_DRIVER_MAJOR=$(nvidia-smi --query-gpu=driver_version --format=csv,noheader | head -n1 | cut -d. -f1)

    # Decide which nv-codec-headers branch matches the driver
    #NV_CODEC_HEADERS_BRANCH="master"
    if [ "$NVIDIA_DRIVER_MAJOR" -ge 550 ]; then NV_CODEC_HEADERS_BRANCH="sdk/12.2"
    elif [ "$NVIDIA_DRIVER_MAJOR" -ge 535 ]; then NV_CODEC_HEADERS_BRANCH="sdk/12.1"
    elif [ "$NVIDIA_DRIVER_MAJOR" -ge 525 ]; then NV_CODEC_HEADERS_BRANCH="sdk/12.0"
  2. denji revised this gist Aug 24, 2025. 1 changed file with 27 additions and 1 deletion.
    28 changes: 27 additions & 1 deletion nvenc-install.sh
    Original file line number Diff line number Diff line change
    @@ -54,7 +54,33 @@ function install_nvenc_sdk() {
    cd "${DIR_FFMPEG_SOURCES}" || exit 1
    dir_nvcodec="${DIR_FFMPEG_SOURCES}/nv-codec-headers"
    if [ ! -d "${dir_nvcodec}" ]; then
    git clone https://git.videolan.org/git/ffmpeg/nv-codec-headers.git "${dir_nvcodec}"
    # -------------------------------------------------------------------------
    # Detect NVIDIA driver version and choose matching Video Codec SDK branch
    # -------------------------------------------------------------------------

    # Extract major version from nvidia-smi (e.g. 535 from 535.183.01)
    NVIDIA_DRIVER_MAJOR=$(nvidia-smi --query-gpu=driver_version --format=csv,noheader | head -n1 | cut -d. -f1)

    # Decide which nv-codec-headers branch matches the driver
    if [ "$NVIDIA_DRIVER_MAJOR" -ge 550 ]; then NV_CODEC_HEADERS_BRANCH="sdk/12.2"
    elif [ "$NVIDIA_DRIVER_MAJOR" -ge 535 ]; then NV_CODEC_HEADERS_BRANCH="sdk/12.1"
    elif [ "$NVIDIA_DRIVER_MAJOR" -ge 525 ]; then NV_CODEC_HEADERS_BRANCH="sdk/12.0"
    elif [ "$NVIDIA_DRIVER_MAJOR" -ge 515 ]; then NV_CODEC_HEADERS_BRANCH="sdk/11.1"
    elif [ "$NVIDIA_DRIVER_MAJOR" -ge 510 ]; then NV_CODEC_HEADERS_BRANCH="sdk/11.0"
    elif [ "$NVIDIA_DRIVER_MAJOR" -ge 470 ]; then NV_CODEC_HEADERS_BRANCH="sdk/8.1"
    elif [ "$NVIDIA_DRIVER_MAJOR" -ge 440 ]; then NV_CODEC_HEADERS_BRANCH="old/sdk/10.0"
    elif [ "$NVIDIA_DRIVER_MAJOR" -ge 410 ]; then NV_CODEC_HEADERS_BRANCH="old/sdk/9.1"
    elif [ "$NVIDIA_DRIVER_MAJOR" -ge 400 ]; then NV_CODEC_HEADERS_BRANCH="old/sdk/9.0"
    elif [ "$NVIDIA_DRIVER_MAJOR" -ge 390 ]; then NV_CODEC_HEADERS_BRANCH="old/sdk/8.2"
    elif [ "$NVIDIA_DRIVER_MAJOR" -ge 384 ]; then NV_CODEC_HEADERS_BRANCH="old/sdk/8.0"
    else
    echo "❌ NVIDIA driver ($NVIDIA_DRIVER_MAJOR) is too old or unsupported for nv-codec-headers"
    exit 1
    fi

    echo "✅ Detected NVIDIA driver $NVIDIA_DRIVER_MAJOR → using nv-codec-headers branch $NV_CODEC_HEADERS_BRANCH"

    git clone --branch "$NV_CODEC_HEADERS_BRANCH" https://git.videolan.org/git/ffmpeg/nv-codec-headers.git "${dir_nvcodec}"
    fi
    cd "${dir_nvcodec}" || exit 1
    make
  3. denji revised this gist Aug 24, 2025. 1 changed file with 16 additions and 12 deletions.
    28 changes: 16 additions & 12 deletions nvenc-install.sh
    Original file line number Diff line number Diff line change
    @@ -3,13 +3,15 @@
    # Source: https://gist.github.com/lucaspar/27f5e108b80524b315be10b2a9049817
    # =========================================================================
    # This script will compile and install a static FFmpeg build with
    # support for NVENC in Ubuntu. Developed in Ubuntu 23.10,
    # with NVIDIA Drivers v535.129.03 and CUDA v12.2 with a GPU
    # with CUDA capability 8.6 (RTX 3080) (see ccap below).
    # It assumes NVIDA drivers are installed and that you have a
    # support for NVENC in Ubuntu. Updated for Ubuntu 24.04.2,
    # with NVIDIA Drivers v535.183.01 and CUDA v12.2 with a GPU
    # with CUDA capability 8.6 (RTX 3080). Set ccap below if using
    # a different GPU.
    # It assumes NVIDIA drivers are installed and that you have a
    # CUDA-compatible GPU. You can check installed drivers with:
    # $ apt list *nvidia-driver-* | grep installed
    # $ nvidia-smi
    # The script may be run multiple times if a step fails.
    # =========================================================================
    set -e

    @@ -30,10 +32,10 @@ NASM_VERSION=2.16.01
    # ffmpeg version to install (most recent version at the time of writing)
    # check versions at https://github.com/FFmpeg/FFmpeg/branches/active
    # check changelog at https://github.com/FFmpeg/FFmpeg/blob/master/Changelog
    FFMPEG_VERSION=6.1
    FFMPEG_VERSION=7.1

    # Install required things from apt
    install_libs() {
    function install_libs() {
    echo " 🚀 Installing prerequisites"
    sudo apt-get update
    sudo apt-get -y install autoconf automake build-essential \
    @@ -42,10 +44,12 @@ install_libs() {
    libxcb-xfixes0-dev pkg-config texi2html zlib1g-dev libopus-dev libunistring-dev \
    libavdevice-dev libfdk-aac-dev libmp3lame-dev libx264-dev libavcodec-dev \
    libgnutls28-dev libx265-dev libnuma-dev libaom-dev libaom3
    # install cuda toolkit, which provides nvcc
    sudo apt-get -y install nvidia-cuda-toolkit
    }

    # Install NVENC SDK
    install_nvenc_sdk() {
    function install_nvenc_sdk() {
    echo " 🚀 Installing the NVIDIA NVENC SDK."
    cd "${DIR_FFMPEG_SOURCES}" || exit 1
    dir_nvcodec="${DIR_FFMPEG_SOURCES}/nv-codec-headers"
    @@ -58,7 +62,7 @@ install_nvenc_sdk() {
    }

    # Compile NASM
    compile_nasm() {
    function compile_nasm() {
    echo " 🚀 Compiling nasm"
    dir_nasm="${DIR_FFMPEG_SOURCES}/nasm-${NASM_VERSION}"
    if [ ! -d "${dir_nasm}" ]; then
    @@ -74,7 +78,7 @@ compile_nasm() {
    }

    # Compile libvpx
    compile_libvpx() {
    function compile_libvpx() {
    echo " 🚀 Compiling libvpx"
    dir_vpx="${DIR_FFMPEG_SOURCES}/libvpx"
    if [ ! -d "${dir_vpx}" ]; then
    @@ -94,7 +98,7 @@ compile_libvpx() {
    }

    # Compile ffmpeg and install it
    compile_and_install_ffmpeg() {
    function compile_and_install_ffmpeg() {
    echo " 🚀 Compiling ffmpeg"
    dir_ffmpeg="${DIR_FFMPEG_SOURCES}/ffmpeg-repo"
    if [ ! -d "${dir_ffmpeg}" ]; then
    @@ -149,7 +153,7 @@ compile_and_install_ffmpeg() {
    }

    # check if nvidia-smi succeeds
    check_nvidia_smi() {
    function check_nvidia_smi() {
    if ! nvidia-smi &>/dev/null; then
    echo -e "\tnvidia-smi not found or failed to run."
    echo -e "\tMake sure NVIDIA drivers are installed e.g.:"
    @@ -159,7 +163,7 @@ check_nvidia_smi() {
    }

    # link installed binaries to user bin
    link_installed() {
    function link_installed() {
    echo "Linking installed binaries to ${DIR_USR_BIN}"
    cd "${DIR_USR_BIN}" || exit 1
    declare -a arr=("ffmpeg" "ffprobe" "ffplay" "ffserver" "ffprobe")
  4. denji revised this gist Jan 30, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion nvenc-install.sh
    Original file line number Diff line number Diff line change
    @@ -178,7 +178,7 @@ function main() {

    # set number of cores to use
    num_proc="$(nproc)"
    NUM_CORES=$((num_proc - 2))
    NUM_CORES=$((num_proc <= 2 ? num_proc : num_proc - 1))
    echo "Using up to ${NUM_CORES} cores"

    # create directories
  5. denji revised this gist Jan 30, 2024. No changes.
  6. denji revised this gist Jan 30, 2024. No changes.
  7. denji revised this gist Jan 30, 2024. 1 changed file with 150 additions and 85 deletions.
    235 changes: 150 additions & 85 deletions nvenc-install.sh
    Original file line number Diff line number Diff line change
    @@ -1,106 +1,118 @@
    #!/bin/bash
    # ==================================================================
    # This script will compile and install a static ffmpeg build with
    # support for NVENC in Ubuntu. Developed in Ubuntu 22.04 LTS,
    # with NVIDIA Drivers v510.73 and CUDA v11.6
    # =========================================================================
    # Source: https://gist.github.com/lucaspar/27f5e108b80524b315be10b2a9049817
    # =========================================================================
    # This script will compile and install a static FFmpeg build with
    # support for NVENC in Ubuntu. Developed in Ubuntu 23.10,
    # with NVIDIA Drivers v535.129.03 and CUDA v12.2 with a GPU
    # with CUDA capability 8.6 (RTX 3080) (see ccap below).
    # It assumes NVIDA drivers are installed and that you have a
    # CUDA-compatible GPU. You can check installed drivers with:
    # $ apt list *nvidia-driver-* | grep installed
    # $ nvidia-smi
    # ==================================================================
    # =========================================================================
    set -e

    # Directories you might want to change
    DIR_USR_BIN="$HOME/.local/bin" # user-writable binaries, where to install ffmpeg
    DIR_INSTALL_ROOT="${XDG_STATE_HOME:-$HOME/.local/state}" # location to clone repos and build artifacts
    DIR_FFMPEG_BUILD="$DIR_INSTALL_ROOT/ffmpeg-build" # where to build ffmpeg
    DIR_FFMPEG_SOURCES="$DIR_INSTALL_ROOT/ffmpeg-sources" # ffmpeg source code
    # Variables you might want to change
    DIR_USR_BIN="${HOME}/.local/bin" # user-writable binaries, where to install ffmpeg
    DIR_INSTALL_ROOT="${XDG_STATE_HOME:-${HOME}/.local/state}" # location to clone repos and build artifacts
    DIR_FFMPEG_BUILD="${DIR_INSTALL_ROOT}/ffmpeg-build" # where to build ffmpeg
    DIR_FFMPEG_SOURCES="${DIR_INSTALL_ROOT}/ffmpeg-sources" # ffmpeg source code

    # CUDA compute capability: check yours at https://developer.nvidia.com/cuda-gpus, or run:
    # nvidia-smi --query-gpu=compute_cap --format=csv
    ccap=86

    # NASM (Netwide Assembler) version to install (most recent version at the time of writing)
    # https://trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu#NASM
    NASM_VERSION=2.16.01

    # ffmpeg version to install (most recent version at the time of writing)
    # check versions at https://github.com/FFmpeg/FFmpeg/branches/active
    # check changelog at https://github.com/FFmpeg/FFmpeg/blob/master/Changelog
    FFMPEG_VERSION=6.1

    # Install required things from apt
    install_libs() {
    echo "Installing prerequisites"
    echo " 🚀 Installing prerequisites"
    sudo apt-get update
    sudo apt-get -y --force-yes install autoconf automake build-essential \
    libass-dev libfreetype6-dev libgpac-dev libsdl1.2-dev libtheora-dev \
    sudo apt-get -y install autoconf automake build-essential \
    libass-dev libfreetype6-dev libsdl1.2-dev libtheora-dev \
    libtool libva-dev libvdpau-dev libvorbis-dev libxcb1-dev libxcb-shm0-dev \
    libxcb-xfixes0-dev pkg-config texi2html zlib1g-dev libopus-dev libunistring-dev \
    libavdevice-dev libfdk-aac-dev libmp3lame-dev libx264-dev libavcodec-dev \
    libgnutls28-dev
    libgnutls28-dev libx265-dev libnuma-dev libaom-dev libaom3
    }

    # Install NVENC SDK
    install_nvenc_sdk() {
    echo "Installing the NVIDIA NVENC SDK."
    cd "$DIR_FFMPEG_SOURCES" || exit 1
    dir_nvcodec="$DIR_FFMPEG_SOURCES/nv-codec-headers"
    if [ ! -d "$dir_nvcodec" ]; then
    git clone https://git.videolan.org/git/ffmpeg/nv-codec-headers.git "$dir_nvcodec"
    echo " 🚀 Installing the NVIDIA NVENC SDK."
    cd "${DIR_FFMPEG_SOURCES}" || exit 1
    dir_nvcodec="${DIR_FFMPEG_SOURCES}/nv-codec-headers"
    if [ ! -d "${dir_nvcodec}" ]; then
    git clone https://git.videolan.org/git/ffmpeg/nv-codec-headers.git "${dir_nvcodec}"
    fi
    cd "$dir_nvcodec" || exit 1
    cd "${dir_nvcodec}" || exit 1
    make
    sudo make install
    }

    # Compile nasm
    # Compile NASM
    compile_nasm() {
    echo "Compiling nasm"
    dir_nasm="$DIR_FFMPEG_SOURCES/nasm-2.15"
    if [ ! -d "$dir_nasm" ]; then
    cd "$DIR_FFMPEG_SOURCES" || exit 1
    wget "https://www.nasm.us/pub/nasm/releasebuilds/2.15/nasm-2.15.tar.gz"
    tar xzvf nasm-2.15.tar.gz
    echo " 🚀 Compiling nasm"
    dir_nasm="${DIR_FFMPEG_SOURCES}/nasm-${NASM_VERSION}"
    if [ ! -d "${dir_nasm}" ]; then
    cd "${DIR_FFMPEG_SOURCES}" || exit 1
    wget "https://www.nasm.us/pub/nasm/releasebuilds/${NASM_VERSION}/nasm-${NASM_VERSION}.tar.gz"
    tar xzvf nasm-${NASM_VERSION}.tar.gz && rm -f nasm-${NASM_VERSION}.tar.gz
    fi
    cd "$dir_nasm" || exit 1
    ./configure --prefix="$DIR_FFMPEG_BUILD" --bindir="$DIR_USR_BIN"
    cd "${dir_nasm}" || exit 1
    ./configure --prefix="${DIR_FFMPEG_BUILD}" --bindir="${DIR_USR_BIN}"
    make -j"${NUM_CORES}" || true # too many false positives, ignoring errors
    make -j"${NUM_CORES}" install || true # too many false positives, ignoring errors
    make -j"${NUM_CORES}" distclean
    }

    # Compile libvpx
    compile_libvpx() {
    echo "Compiling libvpx"
    dir_vpx="$DIR_FFMPEG_SOURCES/libvpx"
    if [ ! -d "$dir_vpx" ]; then
    cd "$DIR_FFMPEG_SOURCES" || exit 1
    git clone https://chromium.googlesource.com/webm/libvpx "$dir_vpx"
    echo " 🚀 Compiling libvpx"
    dir_vpx="${DIR_FFMPEG_SOURCES}/libvpx"
    if [ ! -d "${dir_vpx}" ]; then
    cd "${DIR_FFMPEG_SOURCES}" || exit 1
    git clone https://chromium.googlesource.com/webm/libvpx "${dir_vpx}"
    fi
    cd "$dir_vpx" || exit 1
    PATH="$DIR_USR_BIN:$PATH" ./configure --prefix="$DIR_FFMPEG_BUILD" --disable-examples \
    cd "${dir_vpx}" || exit 1
    PATH="${DIR_USR_BIN}:${PATH}" ./configure --prefix="${DIR_FFMPEG_BUILD}" --disable-examples \
    --enable-runtime-cpu-detect --enable-vp9 --enable-vp8 \
    --enable-postproc --enable-vp9-postproc --enable-multi-res-encoding \
    --enable-webm-io --enable-better-hw-compatibility --enable-vp9-highbitdepth \
    --enable-onthefly-bitpacking --enable-realtime-only --enable-pic \
    --cpu=native --as=nasm
    PATH="$DIR_USR_BIN:$PATH" make -j"${NUM_CORES}"
    PATH="${DIR_USR_BIN}:${PATH}" make -j"${NUM_CORES}"
    make -j"${NUM_CORES}" install
    make -j"${NUM_CORES}" clean
    }

    # Compile ffmpeg
    compile_ffmpeg() {
    echo "Compiling ffmpeg"
    dir_ffmpeg="$DIR_FFMPEG_SOURCES/ffmpeg-repo"
    if [ ! -d "$dir_ffmpeg" ]; then
    cd "$DIR_FFMPEG_SOURCES" || exit 1
    git clone https://github.com/FFmpeg/FFmpeg.git -b master "$dir_ffmpeg"
    # Compile ffmpeg and install it
    compile_and_install_ffmpeg() {
    echo " 🚀 Compiling ffmpeg"
    dir_ffmpeg="${DIR_FFMPEG_SOURCES}/ffmpeg-repo"
    if [ ! -d "${dir_ffmpeg}" ]; then
    cd "${DIR_FFMPEG_SOURCES}" || exit 1
    git clone https://github.com/FFmpeg/FFmpeg.git -b master "${dir_ffmpeg}"
    fi
    cd "$dir_ffmpeg" || exit 1
    # switch to version 5.1 (most recent version at the time of writing)
    # check versions at https://github.com/FFmpeg/FFmpeg/branches/active
    # check changelog at https://github.com/FFmpeg/FFmpeg/blob/master/Changelog
    cd "${dir_ffmpeg}" || exit 1
    git fetch --tags
    git switch release/5.1
    ccap=37
    PATH="$DIR_USR_BIN:$PATH" PKG_CONFIG_PATH="$DIR_FFMPEG_BUILD/lib/pkgconfig" ./configure \
    git switch "release/${FFMPEG_VERSION}"
    PATH="${DIR_USR_BIN}:${PATH}" PKG_CONFIG_PATH="${DIR_FFMPEG_BUILD}/lib/pkgconfig" ./configure \
    --pkg-config-flags="--static" \
    --prefix="$DIR_FFMPEG_BUILD" \
    --extra-cflags="-I$DIR_FFMPEG_BUILD/include" \
    --extra-ldflags="-L$DIR_FFMPEG_BUILD/lib" \
    --prefix="${DIR_FFMPEG_BUILD}" \
    --extra-cflags="-I${DIR_FFMPEG_BUILD}/include" \
    --extra-ldflags="-L${DIR_FFMPEG_BUILD}/lib" \
    --extra-cflags="-I/usr/local/cuda/include/" \
    --extra-ldflags=-L/usr/local/cuda/lib64/ \
    --nvccflags="-gencode arch=compute_${ccap},code=sm_${ccap} -O2" \
    --bindir="$DIR_USR_BIN" \
    --bindir="${DIR_USR_BIN}" \
    --enable-static \
    --enable-cuda-nvcc \
    --enable-cuvid \
    @@ -121,15 +133,16 @@ compile_ffmpeg() {
    --enable-libvorbis \
    --enable-libvpx \
    --enable-libx264 \
    --enable-libx265 \
    --enable-libaom \
    --enable-nonfree \
    --enable-nvdec \
    --enable-nvenc \
    --enable-pic \
    --enable-protocol=file \
    --enable-protocol=https \
    --enable-shared \
    --enable-vaapi
    PATH="$DIR_USR_BIN:$PATH" make -j"${NUM_CORES}"
    PATH="${DIR_USR_BIN}:${PATH}" make -j"${NUM_CORES}"
    make -j"${NUM_CORES}" install
    make -j"${NUM_CORES}" distclean
    hash -r
    @@ -140,42 +153,94 @@ check_nvidia_smi() {
    if ! nvidia-smi &>/dev/null; then
    echo -e "\tnvidia-smi not found or failed to run."
    echo -e "\tMake sure NVIDIA drivers are installed e.g.:"
    echo -e "\t\tsudo apt install nvidia-driver-515"
    echo -e "\t\tsudo apt install nvidia-driver-545"
    exit 1
    fi
    }
    check_nvidia_smi

    # set number of cores to use
    num_proc="$(nproc)"
    NUM_CORES=$((num_proc - 2))
    echo "Using up to $NUM_CORES cores"

    # create directories
    cd "$DIR_INSTALL_ROOT" || exit 1
    mkdir -p "$DIR_FFMPEG_SOURCES"
    # link installed binaries to user bin
    link_installed() {
    echo "Linking installed binaries to ${DIR_USR_BIN}"
    cd "${DIR_USR_BIN}" || exit 1
    declare -a arr=("ffmpeg" "ffprobe" "ffplay" "ffserver" "ffprobe")
    for f in "${arr[@]}"; do
    f_path="${DIR_FFMPEG_BUILD}/bin/${f}"
    if [ -f "${f_path}" ]; then
    ldd "${f_path}"
    fi
    done
    }

    # main things
    install_libs
    install_nvenc_sdk
    compile_nasm
    compile_libvpx
    compile_ffmpeg
    function main() {

    check_nvidia_smi

    # set number of cores to use
    num_proc="$(nproc)"
    NUM_CORES=$((num_proc - 2))
    echo "Using up to ${NUM_CORES} cores"

    # create directories
    DIR_PREV="$(pwd)"
    cd "${DIR_INSTALL_ROOT}" || exit 1
    mkdir -p "${DIR_FFMPEG_SOURCES}"

    install_libs
    install_nvenc_sdk
    compile_nasm
    compile_libvpx
    compile_and_install_ffmpeg

    cd "${DIR_PREV}" || exit 1
    link_installed

    wrap_up
    benchmark

    }

    function wrap_up() {

    echo " 🏁 Complete!"

    echo -e " 💡 Some commands to try:\n"
    echo "which ffmpeg ffprobe"
    echo "whereis ffmpeg ffprobe # to run the correct binary in case your system has others"
    echo "ffmpeg 2>/dev/null -version | grep -i 'nv[a-z]*|nvidia|cuda|nvenc'"
    echo "ffprobe 2>/dev/null -decoders | grep -i 'nvidia|cuda|nvenc'"
    echo "ffprobe 2>/dev/null -encoders | grep -i 'nvidia|cuda|nvenc'"
    echo "ffprobe 2>/dev/null -filters | grep -i 'nvidia|cuda|nvenc'"

    echo -e "\n\n\t\t EXAMPLE OF NVENC USAGE"
    echo -e "\n 👉 Get a sample video to work with:\n"
    echo -e "wget https://delamain.s3.amazonaws.com/public/samples/video-sample-720p.mp4 -O input.mp4"
    echo -e "\n 👉 GPU encoding test:\n"
    echo -e "time ffmpeg -y -hwaccel cuda -hwaccel_output_format cuda -extra_hw_frames 4 -i input.mp4 -c:v h264_nvenc output-nvenc.mp4"
    echo -e "\n 👉 CPU encoding test:\n"
    echo -e "time ffmpeg -y -i input.mp4 -c:v h264 output-cpu.mp4"

    echo -e "\n 👉 Wall times for tested run:\t15.695s (CPU, 24 cores) vs. (CUDA, RTX 3080) 4.952s"

    echo " >> Complete!"
    echo -e "\nIf everything works, you can mark the following directories for deletion:\n\n 🧹 ${DIR_FFMPEG_SOURCES}\n 🧹 ${DIR_FFMPEG_BUILD}\n"

    echo -e " >> Some commands to try:\n"
    echo "ffmpeg 2>/dev/null -version | grep -i 'nv[a-z]*|nvidia|cuda|nvenc'"
    echo "ffprobe 2>/dev/null -decoders | grep -i 'nvidia|cuda|nvenc'"
    echo "ffprobe 2>/dev/null -encoders | grep -i 'nvidia|cuda|nvenc'"
    echo "ffprobe 2>/dev/null -filters | grep -i 'nvidia|cuda|nvenc'"
    echo -e "\n\n"
    }

    function benchmark() {

    if ! [ -f input.mp4 ]; then
    echo "Downloading sample video"
    wget https://delamain.s3.amazonaws.com/public/samples/video-sample-720p.mp4 -O input.mp4
    fi

    echo "Benchmarking x264 (MPEG-4/H.264) CPU vs. NVENC"
    time ffmpeg -y -i input.mp4 -c:v h264 output-cpu-h264.mp4
    time ffmpeg -y -hwaccel cuda -hwaccel_output_format cuda -extra_hw_frames 4 -i input.mp4 -c:v h264_nvenc output-nvenc-h264.mp4

    echo -e " >> Example of nvenc usage:\n"
    echo -e "wget https://filesamples.com/samples/video/mp4/sample_1280x720_surfing_with_audio.mp4 -O input.mp4"
    echo -e "time ffmpeg -hwaccel cuda -hwaccel_output_format cuda -extra_hw_frames 4 -i input.mp4 -c:v h264_nvenc output.mp4"
    echo -e "time ffmpeg -y -i input.mp4 -c:v h264 output.mp4"
    echo "Benchmarking x265 (HEVC/H.265) CPU vs. NVENC"
    time ffmpeg -y -i input.mp4 -c:v hevc output-cpu-hevc.mp4
    time ffmpeg -y -hwaccel cuda -hwaccel_output_format cuda -extra_hw_frames 4 -i input.mp4 -c:v hevc_nvenc output-nvenc-hevc.mp4

    echo -e "\n\tWall times for tested run:\t15.695s (CPU, 24 cores) vs. (CUDA, RTX 3080) 4.952s"
    }

    echo -e "\nIf everything works, you can mark the following directories for deletion:\n$DIR_FFMPEG_SOURCES\n$DIR_FFMPEG_BUILD\n"
    main
  8. @lucaspar lucaspar revised this gist Jul 26, 2022. No changes.
  9. @lucaspar lucaspar revised this gist Jul 20, 2022. 1 changed file with 0 additions and 3 deletions.
    3 changes: 0 additions & 3 deletions nvenc-install.sh
    Original file line number Diff line number Diff line change
    @@ -44,12 +44,9 @@ install_nvenc_sdk() {
    # Compile nasm
    compile_nasm() {
    echo "Compiling nasm"
    # dir_nasm="$DIR_FFMPEG_SOURCES/nasm-2.14rc0"
    dir_nasm="$DIR_FFMPEG_SOURCES/nasm-2.15"
    if [ ! -d "$dir_nasm" ]; then
    cd "$DIR_FFMPEG_SOURCES" || exit 1
    # wget "http://www.nasm.us/pub/nasm/releasebuilds/2.14rc0/nasm-2.14rc0.tar.gz"
    # tar xzvf nasm-2.14rc0.tar.gz
    wget "https://www.nasm.us/pub/nasm/releasebuilds/2.15/nasm-2.15.tar.gz"
    tar xzvf nasm-2.15.tar.gz
    fi
  10. @lucaspar lucaspar revised this gist Jul 20, 2022. No changes.
  11. @lucaspar lucaspar revised this gist Jul 20, 2022. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions nvenc-install.sh
    Original file line number Diff line number Diff line change
    @@ -11,10 +11,10 @@
    set -e

    # Directories you might want to change
    DIR_INSTALL_ROOT="${XDG_STATE_HOME:-$HOME/.local/state}" # where to install ffmpeg
    DIR_USR_BIN="$HOME/.local/bin" # user-writable binaries, where to install ffmpeg
    DIR_INSTALL_ROOT="${XDG_STATE_HOME:-$HOME/.local/state}" # location to clone repos and build artifacts
    DIR_FFMPEG_BUILD="$DIR_INSTALL_ROOT/ffmpeg-build" # where to build ffmpeg
    DIR_FFMPEG_SOURCES="$DIR_INSTALL_ROOT/ffmpeg-sources" # ffmpeg source code
    DIR_USR_BIN="$HOME/.local/bin" # user-writable binaries

    # Install required things from apt
    install_libs() {
  12. @lucaspar lucaspar revised this gist Jul 20, 2022. 1 changed file with 5 additions and 7 deletions.
    12 changes: 5 additions & 7 deletions nvenc-install.sh
    Original file line number Diff line number Diff line change
    @@ -11,10 +11,10 @@
    set -e

    # Directories you might want to change
    DIR_INSTALL_ROOT="${XDG_STATE_HOME:-$HOME/.local/state}" # where to install ffmpeg
    DIR_FFMPEG_BUILD="$DIR_INSTALL_ROOT/ffmpeg-build" # where to build ffmpeg
    DIR_FFMPEG_SOURCES="$DIR_INSTALL_ROOT/ffmpeg-sources" # ffmpeg source code
    DIR_USR_BIN="$HOME/.local/bin" # user-writable binaries
    DIR_INSTALL_ROOT="${XDG_STATE_HOME:-$HOME/.local/state}" # where to install ffmpeg
    DIR_FFMPEG_BUILD="$DIR_INSTALL_ROOT/ffmpeg-build" # where to build ffmpeg
    DIR_FFMPEG_SOURCES="$DIR_INSTALL_ROOT/ffmpeg-sources" # ffmpeg source code
    DIR_USR_BIN="$HOME/.local/bin" # user-writable binaries

    # Install required things from apt
    install_libs() {
    @@ -155,7 +155,6 @@ NUM_CORES=$((num_proc - 2))
    echo "Using up to $NUM_CORES cores"

    # create directories
    mkdir -p "$DIR_INSTALL_ROOT"
    cd "$DIR_INSTALL_ROOT" || exit 1
    mkdir -p "$DIR_FFMPEG_SOURCES"

    @@ -182,5 +181,4 @@ echo -e "time ffmpeg -y -i input.mp4 -c:v h264 output.mp4"

    echo -e "\n\tWall times for tested run:\t15.695s (CPU, 24 cores) vs. (CUDA, RTX 3080) 4.952s"

    echo -e "\n\n\t >>> Add this line to your ~/.bashrc or equivalent: <<<"
    echo -e "export LD_LIBRARY_PATH=\"\$LD_LIBRARY_PATH:$DIR_FFMPEG_BUILD/lib\"\n\n"
    echo -e "\nIf everything works, you can mark the following directories for deletion:\n$DIR_FFMPEG_SOURCES\n$DIR_FFMPEG_BUILD\n"
  13. @lucaspar lucaspar revised this gist Jul 20, 2022. No changes.
  14. @lucaspar lucaspar revised this gist Jul 20, 2022. 1 changed file with 5 additions and 4 deletions.
    9 changes: 5 additions & 4 deletions nvenc-install.sh
    Original file line number Diff line number Diff line change
    @@ -11,10 +11,10 @@
    set -e

    # Directories you might want to change
    DIR_INSTALL_ROOT="${XDG_STATE_HOME:-$HOME/.local/state}"
    DIR_FFMPEG_BUILD="$DIR_INSTALL_ROOT/ffmpeg-build"
    DIR_FFMPEG_SOURCES="$DIR_INSTALL_ROOT/ffmpeg-sources"
    DIR_USR_BIN="$HOME/.local/bin"
    DIR_INSTALL_ROOT="${XDG_STATE_HOME:-$HOME/.local/state}" # where to install ffmpeg
    DIR_FFMPEG_BUILD="$DIR_INSTALL_ROOT/ffmpeg-build" # where to build ffmpeg
    DIR_FFMPEG_SOURCES="$DIR_INSTALL_ROOT/ffmpeg-sources" # ffmpeg source code
    DIR_USR_BIN="$HOME/.local/bin" # user-writable binaries

    # Install required things from apt
    install_libs() {
    @@ -155,6 +155,7 @@ NUM_CORES=$((num_proc - 2))
    echo "Using up to $NUM_CORES cores"

    # create directories
    mkdir -p "$DIR_INSTALL_ROOT"
    cd "$DIR_INSTALL_ROOT" || exit 1
    mkdir -p "$DIR_FFMPEG_SOURCES"

  15. @lucaspar lucaspar revised this gist Jul 20, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion nvenc-install.sh
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@
    set -e

    # Directories you might want to change
    DIR_INSTALL_ROOT="$HOME/demos/"
    DIR_INSTALL_ROOT="${XDG_STATE_HOME:-$HOME/.local/state}"
    DIR_FFMPEG_BUILD="$DIR_INSTALL_ROOT/ffmpeg-build"
    DIR_FFMPEG_SOURCES="$DIR_INSTALL_ROOT/ffmpeg-sources"
    DIR_USR_BIN="$HOME/.local/bin"
  16. @lucaspar lucaspar revised this gist Jul 20, 2022. 1 changed file with 96 additions and 129 deletions.
    225 changes: 96 additions & 129 deletions nvenc-install.sh
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,14 @@
    #!/bin/bash

    # This script will compile and install a static ffmpeg build with support for nvenc in Ubuntu.
    # See the prefix path and compile options if edits are needed to suit your needs.
    # ==================================================================
    # This script will compile and install a static ffmpeg build with
    # support for NVENC in Ubuntu. Developed in Ubuntu 22.04 LTS,
    # with NVIDIA Drivers v510.73 and CUDA v11.6
    # It assumes NVIDA drivers are installed and that you have a
    # CUDA-compatible GPU. You can check installed drivers with:
    # $ apt list *nvidia-driver-* | grep installed
    # $ nvidia-smi
    # ==================================================================
    set -e

    # Directories you might want to change
    DIR_INSTALL_ROOT="$HOME/demos/"
    @@ -10,137 +17,83 @@ DIR_FFMPEG_SOURCES="$DIR_INSTALL_ROOT/ffmpeg-sources"
    DIR_USR_BIN="$HOME/.local/bin"

    # Install required things from apt
    installLibs() {
    install_libs() {
    echo "Installing prerequisites"
    sudo apt-get update
    sudo apt-get -y --force-yes install autoconf automake build-essential \
    libass-dev libfreetype6-dev libgpac-dev libsdl1.2-dev libtheora-dev \
    libtool libva-dev libvdpau-dev libvorbis-dev libxcb1-dev libxcb-shm0-dev \
    libxcb-xfixes0-dev pkg-config texi2html zlib1g-dev libopus-dev libunistring-dev \
    libavdevice-dev
    libavdevice-dev libfdk-aac-dev libmp3lame-dev libx264-dev libavcodec-dev \
    libgnutls28-dev
    }

    # # Install CUDA SDK
    # InstallCUDASDK() {
    # echo "Installing CUDA and the latest driver repositories from repositories"
    # cd "$DIR_FFMPEG_SOURCES" || exit 1
    # wget -c -v -nc "https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/cuda-repo-ubuntu1604_9.2.88-1_amd64.deb"
    # sudo dpkg -i cuda-repo-ubuntu1604_9.2.88-1_amd64.deb
    # sudo apt-key adv --fetch-keys "http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/7fa2af80.pub"
    # sudo apt-get -y update
    # sudo apt-get -y install cuda
    # sudo add-apt-repository ppa:graphics-drivers/ppa
    # sudo apt-get update && sudo apt-get -y upgrade
    # }

    # Install NVIDIA SDK
    installSDK() {
    # Install NVENC SDK
    install_nvenc_sdk() {
    echo "Installing the NVIDIA NVENC SDK."
    cd "$DIR_FFMPEG_SOURCES" || exit 1
    git clone https://git.videolan.org/git/ffmpeg/nv-codec-headers.git
    cd nv-codec-headers || exit 1
    dir_nvcodec="$DIR_FFMPEG_SOURCES/nv-codec-headers"
    if [ ! -d "$dir_nvcodec" ]; then
    git clone https://git.videolan.org/git/ffmpeg/nv-codec-headers.git "$dir_nvcodec"
    fi
    cd "$dir_nvcodec" || exit 1
    make
    sudo make install
    }

    # Compile nasm
    compileNasm() {
    compile_nasm() {
    echo "Compiling nasm"
    cd "$DIR_FFMPEG_SOURCES" || exit 1
    wget "http://www.nasm.us/pub/nasm/releasebuilds/2.14rc0/nasm-2.14rc0.tar.gz"
    tar xzvf nasm-2.14rc0.tar.gz
    cd nasm-2.14rc0 || exit 1
    # dir_nasm="$DIR_FFMPEG_SOURCES/nasm-2.14rc0"
    dir_nasm="$DIR_FFMPEG_SOURCES/nasm-2.15"
    if [ ! -d "$dir_nasm" ]; then
    cd "$DIR_FFMPEG_SOURCES" || exit 1
    # wget "http://www.nasm.us/pub/nasm/releasebuilds/2.14rc0/nasm-2.14rc0.tar.gz"
    # tar xzvf nasm-2.14rc0.tar.gz
    wget "https://www.nasm.us/pub/nasm/releasebuilds/2.15/nasm-2.15.tar.gz"
    tar xzvf nasm-2.15.tar.gz
    fi
    cd "$dir_nasm" || exit 1
    ./configure --prefix="$DIR_FFMPEG_BUILD" --bindir="$DIR_USR_BIN"
    # use nproc - 2
    make -j"${NUM_CORES}"
    make -j"${NUM_CORES}" install
    make -j"${NUM_CORES}" distclean
    }

    # Compile libx264
    compileLibX264() {
    echo "Compiling libx264"
    cd "$DIR_FFMPEG_SOURCES" || exit 1
    # --- older code with broken URL:
    # wget http://download.videolan.org/pub/x264/snapshots/last_x264.tar.bz2
    # tar xjvf last_x264.tar.bz2
    # cd x264-snapshot*
    # --- newer block:
    wget "https://download.videolan.org/pub/x264/snapshots/x264-snapshot-20191217-2245.tar.bz2"
    tar xjvf x264-snapshot-20191217-2245.tar.bz2
    cd x264-snapshot-20191217-2245 || exit 1
    # ---
    PATH="$DIR_USR_BIN:$PATH" ./configure --prefix="$DIR_FFMPEG_BUILD" --bindir="$DIR_USR_BIN" --enable-static
    PATH="$DIR_USR_BIN:$PATH" make -j"${NUM_CORES}"
    make -j"${NUM_CORES}" install
    make -j"${NUM_CORES}" distclean
    }

    # Compile libfdk-acc
    compileLibfdkcc() {
    echo "Compiling libfdk-cc"
    sudo apt-get install unzip
    cd "$DIR_FFMPEG_SOURCES" || exit 1
    wget -O fdk-aac.zip https://github.com/mstorsjo/fdk-aac/zipball/master
    unzip fdk-aac.zip
    cd mstorsjo-fdk-aac* || exit 1
    autoreconf -fiv
    ./configure --prefix="$DIR_FFMPEG_BUILD" --disable-shared
    make -j"${NUM_CORES}"
    make -j"${NUM_CORES}" install
    make -j"${NUM_CORES}" distclean
    }

    # Compile libmp3lame
    compileLibMP3Lame() {
    echo "Compiling libmp3lame"
    sudo apt-get install nasm
    cd "$DIR_FFMPEG_SOURCES" || exit 1
    wget http://downloads.sourceforge.net/project/lame/lame/3.99/lame-3.99.5.tar.gz
    tar xzvf lame-3.99.5.tar.gz
    cd lame-3.99.5 || exit 1
    ./configure --prefix="$DIR_FFMPEG_BUILD" --enable-nasm --disable-shared
    make -j"${NUM_CORES}"
    make -j"${NUM_CORES}" install
    make -j"${NUM_CORES}" distclean
    }

    # Compile libopus
    compileLibOpus() {
    echo "Compiling libopus"
    cd "$DIR_FFMPEG_SOURCES" || exit 1
    wget http://downloads.xiph.org/releases/opus/opus-1.2.1.tar.gz
    tar xzvf opus-1.2.1.tar.gz
    cd opus-1.2.1 || exit 1
    ./configure --prefix="$DIR_FFMPEG_BUILD" --disable-shared
    make -j"${NUM_CORES}"
    make -j"${NUM_CORES}" install
    make -j"${NUM_CORES}" || true # too many false positives, ignoring errors
    make -j"${NUM_CORES}" install || true # too many false positives, ignoring errors
    make -j"${NUM_CORES}" distclean
    }

    # Compile libvpx
    compileLibPvx() {
    compile_libvpx() {
    echo "Compiling libvpx"
    cd "$DIR_FFMPEG_SOURCES" || exit 1
    git clone https://chromium.googlesource.com/webm/libvpx
    cd libvpx || exit 1
    dir_vpx="$DIR_FFMPEG_SOURCES/libvpx"
    if [ ! -d "$dir_vpx" ]; then
    cd "$DIR_FFMPEG_SOURCES" || exit 1
    git clone https://chromium.googlesource.com/webm/libvpx "$dir_vpx"
    fi
    cd "$dir_vpx" || exit 1
    PATH="$DIR_USR_BIN:$PATH" ./configure --prefix="$DIR_FFMPEG_BUILD" --disable-examples \
    --enable-runtime-cpu-detect --enable-vp9 --enable-vp8 \
    --enable-postproc --enable-vp9-postproc --enable-multi-res-encoding \
    --enable-webm-io --enable-better-hw-compatibility --enable-vp9-highbitdepth \
    --enable-onthefly-bitpacking --enable-realtime-only \
    --enable-onthefly-bitpacking --enable-realtime-only --enable-pic \
    --cpu=native --as=nasm
    PATH="$DIR_USR_BIN:$PATH" make -j"${NUM_CORES}"
    make -j"${NUM_CORES}" install
    make -j"${NUM_CORES}" clean
    }

    # Compile ffmpeg
    compileFfmpeg() {
    compile_ffmpeg() {
    echo "Compiling ffmpeg"
    cd "$DIR_FFMPEG_SOURCES" || exit 1
    git clone https://github.com/FFmpeg/FFmpeg -b master
    cd FFmpeg || exit 1
    dir_ffmpeg="$DIR_FFMPEG_SOURCES/ffmpeg-repo"
    if [ ! -d "$dir_ffmpeg" ]; then
    cd "$DIR_FFMPEG_SOURCES" || exit 1
    git clone https://github.com/FFmpeg/FFmpeg.git -b master "$dir_ffmpeg"
    fi
    cd "$dir_ffmpeg" || exit 1
    # switch to version 5.1 (most recent version at the time of writing)
    # check versions at https://github.com/FFmpeg/FFmpeg/branches/active
    # check changelog at https://github.com/FFmpeg/FFmpeg/blob/master/Changelog
    git fetch --tags
    git switch release/5.1
    ccap=37
    PATH="$DIR_USR_BIN:$PATH" PKG_CONFIG_PATH="$DIR_FFMPEG_BUILD/lib/pkgconfig" ./configure \
    --pkg-config-flags="--static" \
    @@ -151,68 +104,82 @@ compileFfmpeg() {
    --extra-ldflags=-L/usr/local/cuda/lib64/ \
    --nvccflags="-gencode arch=compute_${ccap},code=sm_${ccap} -O2" \
    --bindir="$DIR_USR_BIN" \
    --disable-static \
    --enable-static \
    --enable-cuda-nvcc \
    --enable-cuvid \
    --enable-libnpp \
    --enable-decoder=aac \
    --enable-decoder=h264 \
    --enable-decoder=h264_cuvid \
    --enable-demuxer=mov \
    --enable-filter=scale \
    --enable-gnutls \
    --enable-gpl \
    --enable-libass \
    --enable-libfdk-aac \
    --enable-vaapi \
    --enable-libfreetype \
    --enable-libmp3lame \
    --enable-libnpp \
    --enable-libopus \
    --enable-libtheora \
    --enable-libvorbis \
    --enable-libvpx \
    --enable-libx264 \
    --enable-nonfree \
    --enable-nvenc \
    --enable-nvdec \
    --enable-decoder=aac \
    --enable-decoder=h264 \
    --enable-decoder=h264_cuvid \
    --enable-demuxer=mov \
    --enable-filter=scale \
    --enable-nvenc \
    --enable-pic \
    --enable-protocol=file \
    --enable-protocol=https \
    --enable-gnutls \
    --enable-shared
    --enable-shared \
    --enable-vaapi
    PATH="$DIR_USR_BIN:$PATH" make -j"${NUM_CORES}"
    make -j"${NUM_CORES}" install
    make -j"${NUM_CORES}" distclean
    hash -r
    }

    # check if nvidia-smi succeeds
    check_nvidia_smi() {
    if ! nvidia-smi &>/dev/null; then
    echo -e "\tnvidia-smi not found or failed to run."
    echo -e "\tMake sure NVIDIA drivers are installed e.g.:"
    echo -e "\t\tsudo apt install nvidia-driver-515"
    exit 1
    fi
    }
    check_nvidia_smi

    # set number of cores to use
    num_proc="$(nproc)"
    NUM_CORES=$((num_proc - 2))
    echo "Using $NUM_CORES cores"
    echo "Using up to $NUM_CORES cores"

    # create directories
    cd "$DIR_INSTALL_ROOT" || exit 1
    mkdir -p "$DIR_FFMPEG_SOURCES"

    # main things
    installLibs
    installSDK
    compileNasm
    compileLibX264
    compileLibfdkcc
    compileLibMP3Lame
    compileLibOpus
    compileLibPvx
    compileFfmpeg
    echo "Complete!"
    install_libs
    install_nvenc_sdk
    compile_nasm
    compile_libvpx
    compile_ffmpeg

    echo " >> Complete!"

    echo -e " >> Some commands to try:\n"
    echo "ffprobe -decoders"
    echo "ffprobe -encoders"
    echo "ffprobe -filters"
    echo "ffprobe -protocols"
    echo "ffprobe -devices"
    echo "ffmpeg -version | grep -C 20 -i \"nv[a-z]*\""
    echo -e "\n"
    echo "ffmpeg 2>/dev/null -version | grep -i 'nv[a-z]*|nvidia|cuda|nvenc'"
    echo "ffprobe 2>/dev/null -decoders | grep -i 'nvidia|cuda|nvenc'"
    echo "ffprobe 2>/dev/null -encoders | grep -i 'nvidia|cuda|nvenc'"
    echo "ffprobe 2>/dev/null -filters | grep -i 'nvidia|cuda|nvenc'"
    echo -e "\n\n"

    echo -e " >> Example of nvenc usage:\n"
    echo -e "wget https://filesamples.com/samples/video/mp4/sample_1280x720_surfing_with_audio.mp4 -O input.mp4"
    echo -e "time ffmpeg -hwaccel cuda -hwaccel_output_format cuda -extra_hw_frames 4 -i input.mp4 -c:v h264_nvenc output.mp4"
    echo -e "time ffmpeg -y -i input.mp4 -c:v h264 output.mp4"

    echo -e "\n\tWall times for tested run:\t15.695s (CPU, 24 cores) vs. (CUDA, RTX 3080) 4.952s"

    echo -e "\n\n\t >>> Add this line to your ~/.bashrc or equivalent: <<<"
    echo -e "export LD_LIBRARY_PATH=\"\$LD_LIBRARY_PATH:$DIR_FFMPEG_BUILD/lib\"\n\n"
  17. @lucaspar lucaspar revised this gist Jul 20, 2022. 1 changed file with 1 addition and 12 deletions.
    13 changes: 1 addition & 12 deletions nvenc-install.sh
    Original file line number Diff line number Diff line change
    @@ -151,6 +151,7 @@ compileFfmpeg() {
    --extra-ldflags=-L/usr/local/cuda/lib64/ \
    --nvccflags="-gencode arch=compute_${ccap},code=sm_${ccap} -O2" \
    --bindir="$DIR_USR_BIN" \
    --disable-static \
    --enable-cuda-nvcc \
    --enable-cuvid \
    --enable-libnpp \
    @@ -168,18 +169,6 @@ compileFfmpeg() {
    --enable-nonfree \
    --enable-nvenc \
    --enable-nvdec \
    --disable-doc \
    --disable-static \
    --disable-bsfs \
    --disable-decoders \
    --disable-encoders \
    --disable-filters \
    --disable-demuxers \
    --disable-devices \
    --disable-muxers \
    --disable-parsers \
    --disable-postproc \
    --disable-protocols \
    --enable-decoder=aac \
    --enable-decoder=h264 \
    --enable-decoder=h264_cuvid \
  18. @lucaspar lucaspar revised this gist Jul 20, 2022. 1 changed file with 25 additions and 12 deletions.
    37 changes: 25 additions & 12 deletions nvenc-install.sh
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@
    # Directories you might want to change
    DIR_INSTALL_ROOT="$HOME/demos/"
    DIR_FFMPEG_BUILD="$DIR_INSTALL_ROOT/ffmpeg-build"
    FFMPEG_SOURCES_DIR="$DIR_INSTALL_ROOT/ffmpeg-sources"
    DIR_FFMPEG_SOURCES="$DIR_INSTALL_ROOT/ffmpeg-sources"
    DIR_USR_BIN="$HOME/.local/bin"

    # Install required things from apt
    @@ -16,13 +16,14 @@ installLibs() {
    sudo apt-get -y --force-yes install autoconf automake build-essential \
    libass-dev libfreetype6-dev libgpac-dev libsdl1.2-dev libtheora-dev \
    libtool libva-dev libvdpau-dev libvorbis-dev libxcb1-dev libxcb-shm0-dev \
    libxcb-xfixes0-dev pkg-config texi2html zlib1g-dev libopus-dev libunistring-dev
    libxcb-xfixes0-dev pkg-config texi2html zlib1g-dev libopus-dev libunistring-dev \
    libavdevice-dev
    }

    # # Install CUDA SDK
    # InstallCUDASDK() {
    # echo "Installing CUDA and the latest driver repositories from repositories"
    # cd "$FFMPEG_SOURCES_DIR" || exit 1
    # cd "$DIR_FFMPEG_SOURCES" || exit 1
    # wget -c -v -nc "https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/cuda-repo-ubuntu1604_9.2.88-1_amd64.deb"
    # sudo dpkg -i cuda-repo-ubuntu1604_9.2.88-1_amd64.deb
    # sudo apt-key adv --fetch-keys "http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/7fa2af80.pub"
    @@ -35,7 +36,7 @@ installLibs() {
    # Install NVIDIA SDK
    installSDK() {
    echo "Installing the NVIDIA NVENC SDK."
    cd "$FFMPEG_SOURCES_DIR" || exit 1
    cd "$DIR_FFMPEG_SOURCES" || exit 1
    git clone https://git.videolan.org/git/ffmpeg/nv-codec-headers.git
    cd nv-codec-headers || exit 1
    make
    @@ -45,7 +46,7 @@ installSDK() {
    # Compile nasm
    compileNasm() {
    echo "Compiling nasm"
    cd "$FFMPEG_SOURCES_DIR" || exit 1
    cd "$DIR_FFMPEG_SOURCES" || exit 1
    wget "http://www.nasm.us/pub/nasm/releasebuilds/2.14rc0/nasm-2.14rc0.tar.gz"
    tar xzvf nasm-2.14rc0.tar.gz
    cd nasm-2.14rc0 || exit 1
    @@ -59,7 +60,7 @@ compileNasm() {
    # Compile libx264
    compileLibX264() {
    echo "Compiling libx264"
    cd "$FFMPEG_SOURCES_DIR" || exit 1
    cd "$DIR_FFMPEG_SOURCES" || exit 1
    # --- older code with broken URL:
    # wget http://download.videolan.org/pub/x264/snapshots/last_x264.tar.bz2
    # tar xjvf last_x264.tar.bz2
    @@ -79,7 +80,7 @@ compileLibX264() {
    compileLibfdkcc() {
    echo "Compiling libfdk-cc"
    sudo apt-get install unzip
    cd "$FFMPEG_SOURCES_DIR" || exit 1
    cd "$DIR_FFMPEG_SOURCES" || exit 1
    wget -O fdk-aac.zip https://github.com/mstorsjo/fdk-aac/zipball/master
    unzip fdk-aac.zip
    cd mstorsjo-fdk-aac* || exit 1
    @@ -94,7 +95,7 @@ compileLibfdkcc() {
    compileLibMP3Lame() {
    echo "Compiling libmp3lame"
    sudo apt-get install nasm
    cd "$FFMPEG_SOURCES_DIR" || exit 1
    cd "$DIR_FFMPEG_SOURCES" || exit 1
    wget http://downloads.sourceforge.net/project/lame/lame/3.99/lame-3.99.5.tar.gz
    tar xzvf lame-3.99.5.tar.gz
    cd lame-3.99.5 || exit 1
    @@ -107,7 +108,7 @@ compileLibMP3Lame() {
    # Compile libopus
    compileLibOpus() {
    echo "Compiling libopus"
    cd "$FFMPEG_SOURCES_DIR" || exit 1
    cd "$DIR_FFMPEG_SOURCES" || exit 1
    wget http://downloads.xiph.org/releases/opus/opus-1.2.1.tar.gz
    tar xzvf opus-1.2.1.tar.gz
    cd opus-1.2.1 || exit 1
    @@ -120,7 +121,7 @@ compileLibOpus() {
    # Compile libvpx
    compileLibPvx() {
    echo "Compiling libvpx"
    cd "$FFMPEG_SOURCES_DIR" || exit 1
    cd "$DIR_FFMPEG_SOURCES" || exit 1
    git clone https://chromium.googlesource.com/webm/libvpx
    cd libvpx || exit 1
    PATH="$DIR_USR_BIN:$PATH" ./configure --prefix="$DIR_FFMPEG_BUILD" --disable-examples \
    @@ -137,7 +138,7 @@ compileLibPvx() {
    # Compile ffmpeg
    compileFfmpeg() {
    echo "Compiling ffmpeg"
    cd "$FFMPEG_SOURCES_DIR" || exit 1
    cd "$DIR_FFMPEG_SOURCES" || exit 1
    git clone https://github.com/FFmpeg/FFmpeg -b master
    cd FFmpeg || exit 1
    ccap=37
    @@ -201,7 +202,7 @@ echo "Using $NUM_CORES cores"

    # create directories
    cd "$DIR_INSTALL_ROOT" || exit 1
    mkdir -p "$FFMPEG_SOURCES_DIR"
    mkdir -p "$DIR_FFMPEG_SOURCES"

    # main things
    installLibs
    @@ -214,3 +215,15 @@ compileLibOpus
    compileLibPvx
    compileFfmpeg
    echo "Complete!"

    echo -e " >> Some commands to try:\n"
    echo "ffprobe -decoders"
    echo "ffprobe -encoders"
    echo "ffprobe -filters"
    echo "ffprobe -protocols"
    echo "ffprobe -devices"
    echo "ffmpeg -version | grep -C 20 -i \"nv[a-z]*\""
    echo -e "\n"

    echo -e "\n\n\t >>> Add this line to your ~/.bashrc or equivalent: <<<"
    echo -e "export LD_LIBRARY_PATH=\"\$LD_LIBRARY_PATH:$DIR_FFMPEG_BUILD/lib\"\n\n"
  19. @lucaspar lucaspar revised this gist Jul 20, 2022. 1 changed file with 29 additions and 5 deletions.
    34 changes: 29 additions & 5 deletions nvenc-install.sh
    Original file line number Diff line number Diff line change
    @@ -16,7 +16,7 @@ installLibs() {
    sudo apt-get -y --force-yes install autoconf automake build-essential \
    libass-dev libfreetype6-dev libgpac-dev libsdl1.2-dev libtheora-dev \
    libtool libva-dev libvdpau-dev libvorbis-dev libxcb1-dev libxcb-shm0-dev \
    libxcb-xfixes0-dev pkg-config texi2html zlib1g-dev libopus-dev
    libxcb-xfixes0-dev pkg-config texi2html zlib1g-dev libopus-dev libunistring-dev
    }

    # # Install CUDA SDK
    @@ -134,23 +134,25 @@ compileLibPvx() {
    make -j"${NUM_CORES}" clean
    }

    #Compile ffmpeg
    # Compile ffmpeg
    compileFfmpeg() {
    echo "Compiling ffmpeg"
    cd "$FFMPEG_SOURCES_DIR" || exit 1
    git clone https://github.com/FFmpeg/FFmpeg -b master
    cd FFmpeg || exit 1
    ccap=37
    PATH="$DIR_USR_BIN:$PATH" PKG_CONFIG_PATH="$DIR_FFMPEG_BUILD/lib/pkgconfig" ./configure \
    --pkg-config-flags="--static" \
    --prefix="$DIR_FFMPEG_BUILD" \
    --extra-cflags="-I$DIR_FFMPEG_BUILD/include" \
    --extra-ldflags="-L$DIR_FFMPEG_BUILD/lib" \
    --extra-cflags="-I/usr/local/cuda/include/" \
    --extra-ldflags=-L/usr/local/cuda/lib64/ \
    --nvccflags="-gencode arch=compute_${ccap},code=sm_${ccap} -O2" \
    --bindir="$DIR_USR_BIN" \
    --enable-cuda-nvcc \
    --enable-cuvid \
    --enable-libnpp \
    --extra-cflags="-I/usr/local/cuda/include/" \
    --extra-ldflags=-L/usr/local/cuda/lib64/ \
    --enable-gpl \
    --enable-libass \
    --enable-libfdk-aac \
    @@ -163,7 +165,29 @@ compileFfmpeg() {
    --enable-libvpx \
    --enable-libx264 \
    --enable-nonfree \
    --enable-nvenc
    --enable-nvenc \
    --enable-nvdec \
    --disable-doc \
    --disable-static \
    --disable-bsfs \
    --disable-decoders \
    --disable-encoders \
    --disable-filters \
    --disable-demuxers \
    --disable-devices \
    --disable-muxers \
    --disable-parsers \
    --disable-postproc \
    --disable-protocols \
    --enable-decoder=aac \
    --enable-decoder=h264 \
    --enable-decoder=h264_cuvid \
    --enable-demuxer=mov \
    --enable-filter=scale \
    --enable-protocol=file \
    --enable-protocol=https \
    --enable-gnutls \
    --enable-shared
    PATH="$DIR_USR_BIN:$PATH" make -j"${NUM_CORES}"
    make -j"${NUM_CORES}" install
    make -j"${NUM_CORES}" distclean
  20. @lucaspar lucaspar revised this gist Jul 20, 2022. 1 changed file with 14 additions and 10 deletions.
    24 changes: 14 additions & 10 deletions nvenc-install.sh
    Original file line number Diff line number Diff line change
    @@ -3,13 +3,20 @@
    # This script will compile and install a static ffmpeg build with support for nvenc in Ubuntu.
    # See the prefix path and compile options if edits are needed to suit your needs.

    # Directories you might want to change
    DIR_INSTALL_ROOT="$HOME/demos/"
    DIR_FFMPEG_BUILD="$DIR_INSTALL_ROOT/ffmpeg-build"
    FFMPEG_SOURCES_DIR="$DIR_INSTALL_ROOT/ffmpeg-sources"
    DIR_USR_BIN="$HOME/.local/bin"

    # Install required things from apt
    installLibs() {
    echo "Installing prerequisites"
    sudo apt-get update
    sudo apt-get -y --force-yes install autoconf automake build-essential libass-dev libfreetype6-dev libgpac-dev \
    libsdl1.2-dev libtheora-dev libtool libva-dev libvdpau-dev libvorbis-dev libxcb1-dev libxcb-shm0-dev \
    libxcb-xfixes0-dev pkg-config texi2html zlib1g-dev
    sudo apt-get -y --force-yes install autoconf automake build-essential \
    libass-dev libfreetype6-dev libgpac-dev libsdl1.2-dev libtheora-dev \
    libtool libva-dev libvdpau-dev libvorbis-dev libxcb1-dev libxcb-shm0-dev \
    libxcb-xfixes0-dev pkg-config texi2html zlib1g-dev libopus-dev
    }

    # # Install CUDA SDK
    @@ -134,11 +141,12 @@ compileFfmpeg() {
    git clone https://github.com/FFmpeg/FFmpeg -b master
    cd FFmpeg || exit 1
    PATH="$DIR_USR_BIN:$PATH" PKG_CONFIG_PATH="$DIR_FFMPEG_BUILD/lib/pkgconfig" ./configure \
    --pkg-config-flags="--static" \
    --prefix="$DIR_FFMPEG_BUILD" \
    --extra-cflags="-I$DIR_FFMPEG_BUILD/include" \
    --extra-ldflags="-L$DIR_FFMPEG_BUILD/lib" \
    --bindir="$DIR_USR_BIN" \
    --enable-cuda-sdk \
    --enable-cuda-nvcc \
    --enable-cuvid \
    --enable-libnpp \
    --extra-cflags="-I/usr/local/cuda/include/" \
    @@ -167,12 +175,8 @@ num_proc="$(nproc)"
    NUM_CORES=$((num_proc - 2))
    echo "Using $NUM_CORES cores"

    # directories
    DIR_INSTALLATION="$HOME/demos/"
    DIR_FFMPEG_BUILD="$DIR_INSTALLATION/ffmpeg-build"
    DIR_USR_BIN="$HOME/.local/bin"
    cd "$DIR_INSTALLATION" || exit 1
    FFMPEG_SOURCES_DIR="$DIR_INSTALLATION/ffmpeg-sources"
    # create directories
    cd "$DIR_INSTALL_ROOT" || exit 1
    mkdir -p "$FFMPEG_SOURCES_DIR"

    # main things
  21. @lucaspar lucaspar renamed this gist Jul 20, 2022. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  22. @lucaspar lucaspar revised this gist Jul 20, 2022. 1 changed file with 188 additions and 1 deletion.
    189 changes: 188 additions & 1 deletion nvenv-install.sh
    Original file line number Diff line number Diff line change
    @@ -1 +1,188 @@
    ‎‎​
    #!/bin/bash

    # This script will compile and install a static ffmpeg build with support for nvenc in Ubuntu.
    # See the prefix path and compile options if edits are needed to suit your needs.

    # Install required things from apt
    installLibs() {
    echo "Installing prerequisites"
    sudo apt-get update
    sudo apt-get -y --force-yes install autoconf automake build-essential libass-dev libfreetype6-dev libgpac-dev \
    libsdl1.2-dev libtheora-dev libtool libva-dev libvdpau-dev libvorbis-dev libxcb1-dev libxcb-shm0-dev \
    libxcb-xfixes0-dev pkg-config texi2html zlib1g-dev
    }

    # # Install CUDA SDK
    # InstallCUDASDK() {
    # echo "Installing CUDA and the latest driver repositories from repositories"
    # cd "$FFMPEG_SOURCES_DIR" || exit 1
    # wget -c -v -nc "https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/cuda-repo-ubuntu1604_9.2.88-1_amd64.deb"
    # sudo dpkg -i cuda-repo-ubuntu1604_9.2.88-1_amd64.deb
    # sudo apt-key adv --fetch-keys "http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/7fa2af80.pub"
    # sudo apt-get -y update
    # sudo apt-get -y install cuda
    # sudo add-apt-repository ppa:graphics-drivers/ppa
    # sudo apt-get update && sudo apt-get -y upgrade
    # }

    # Install NVIDIA SDK
    installSDK() {
    echo "Installing the NVIDIA NVENC SDK."
    cd "$FFMPEG_SOURCES_DIR" || exit 1
    git clone https://git.videolan.org/git/ffmpeg/nv-codec-headers.git
    cd nv-codec-headers || exit 1
    make
    sudo make install
    }

    # Compile nasm
    compileNasm() {
    echo "Compiling nasm"
    cd "$FFMPEG_SOURCES_DIR" || exit 1
    wget "http://www.nasm.us/pub/nasm/releasebuilds/2.14rc0/nasm-2.14rc0.tar.gz"
    tar xzvf nasm-2.14rc0.tar.gz
    cd nasm-2.14rc0 || exit 1
    ./configure --prefix="$DIR_FFMPEG_BUILD" --bindir="$DIR_USR_BIN"
    # use nproc - 2
    make -j"${NUM_CORES}"
    make -j"${NUM_CORES}" install
    make -j"${NUM_CORES}" distclean
    }

    # Compile libx264
    compileLibX264() {
    echo "Compiling libx264"
    cd "$FFMPEG_SOURCES_DIR" || exit 1
    # --- older code with broken URL:
    # wget http://download.videolan.org/pub/x264/snapshots/last_x264.tar.bz2
    # tar xjvf last_x264.tar.bz2
    # cd x264-snapshot*
    # --- newer block:
    wget "https://download.videolan.org/pub/x264/snapshots/x264-snapshot-20191217-2245.tar.bz2"
    tar xjvf x264-snapshot-20191217-2245.tar.bz2
    cd x264-snapshot-20191217-2245 || exit 1
    # ---
    PATH="$DIR_USR_BIN:$PATH" ./configure --prefix="$DIR_FFMPEG_BUILD" --bindir="$DIR_USR_BIN" --enable-static
    PATH="$DIR_USR_BIN:$PATH" make -j"${NUM_CORES}"
    make -j"${NUM_CORES}" install
    make -j"${NUM_CORES}" distclean
    }

    # Compile libfdk-acc
    compileLibfdkcc() {
    echo "Compiling libfdk-cc"
    sudo apt-get install unzip
    cd "$FFMPEG_SOURCES_DIR" || exit 1
    wget -O fdk-aac.zip https://github.com/mstorsjo/fdk-aac/zipball/master
    unzip fdk-aac.zip
    cd mstorsjo-fdk-aac* || exit 1
    autoreconf -fiv
    ./configure --prefix="$DIR_FFMPEG_BUILD" --disable-shared
    make -j"${NUM_CORES}"
    make -j"${NUM_CORES}" install
    make -j"${NUM_CORES}" distclean
    }

    # Compile libmp3lame
    compileLibMP3Lame() {
    echo "Compiling libmp3lame"
    sudo apt-get install nasm
    cd "$FFMPEG_SOURCES_DIR" || exit 1
    wget http://downloads.sourceforge.net/project/lame/lame/3.99/lame-3.99.5.tar.gz
    tar xzvf lame-3.99.5.tar.gz
    cd lame-3.99.5 || exit 1
    ./configure --prefix="$DIR_FFMPEG_BUILD" --enable-nasm --disable-shared
    make -j"${NUM_CORES}"
    make -j"${NUM_CORES}" install
    make -j"${NUM_CORES}" distclean
    }

    # Compile libopus
    compileLibOpus() {
    echo "Compiling libopus"
    cd "$FFMPEG_SOURCES_DIR" || exit 1
    wget http://downloads.xiph.org/releases/opus/opus-1.2.1.tar.gz
    tar xzvf opus-1.2.1.tar.gz
    cd opus-1.2.1 || exit 1
    ./configure --prefix="$DIR_FFMPEG_BUILD" --disable-shared
    make -j"${NUM_CORES}"
    make -j"${NUM_CORES}" install
    make -j"${NUM_CORES}" distclean
    }

    # Compile libvpx
    compileLibPvx() {
    echo "Compiling libvpx"
    cd "$FFMPEG_SOURCES_DIR" || exit 1
    git clone https://chromium.googlesource.com/webm/libvpx
    cd libvpx || exit 1
    PATH="$DIR_USR_BIN:$PATH" ./configure --prefix="$DIR_FFMPEG_BUILD" --disable-examples \
    --enable-runtime-cpu-detect --enable-vp9 --enable-vp8 \
    --enable-postproc --enable-vp9-postproc --enable-multi-res-encoding \
    --enable-webm-io --enable-better-hw-compatibility --enable-vp9-highbitdepth \
    --enable-onthefly-bitpacking --enable-realtime-only \
    --cpu=native --as=nasm
    PATH="$DIR_USR_BIN:$PATH" make -j"${NUM_CORES}"
    make -j"${NUM_CORES}" install
    make -j"${NUM_CORES}" clean
    }

    #Compile ffmpeg
    compileFfmpeg() {
    echo "Compiling ffmpeg"
    cd "$FFMPEG_SOURCES_DIR" || exit 1
    git clone https://github.com/FFmpeg/FFmpeg -b master
    cd FFmpeg || exit 1
    PATH="$DIR_USR_BIN:$PATH" PKG_CONFIG_PATH="$DIR_FFMPEG_BUILD/lib/pkgconfig" ./configure \
    --prefix="$DIR_FFMPEG_BUILD" \
    --extra-cflags="-I$DIR_FFMPEG_BUILD/include" \
    --extra-ldflags="-L$DIR_FFMPEG_BUILD/lib" \
    --bindir="$DIR_USR_BIN" \
    --enable-cuda-sdk \
    --enable-cuvid \
    --enable-libnpp \
    --extra-cflags="-I/usr/local/cuda/include/" \
    --extra-ldflags=-L/usr/local/cuda/lib64/ \
    --enable-gpl \
    --enable-libass \
    --enable-libfdk-aac \
    --enable-vaapi \
    --enable-libfreetype \
    --enable-libmp3lame \
    --enable-libopus \
    --enable-libtheora \
    --enable-libvorbis \
    --enable-libvpx \
    --enable-libx264 \
    --enable-nonfree \
    --enable-nvenc
    PATH="$DIR_USR_BIN:$PATH" make -j"${NUM_CORES}"
    make -j"${NUM_CORES}" install
    make -j"${NUM_CORES}" distclean
    hash -r
    }

    # set number of cores to use
    num_proc="$(nproc)"
    NUM_CORES=$((num_proc - 2))
    echo "Using $NUM_CORES cores"

    # directories
    DIR_INSTALLATION="$HOME/demos/"
    DIR_FFMPEG_BUILD="$DIR_INSTALLATION/ffmpeg-build"
    DIR_USR_BIN="$HOME/.local/bin"
    cd "$DIR_INSTALLATION" || exit 1
    FFMPEG_SOURCES_DIR="$DIR_INSTALLATION/ffmpeg-sources"
    mkdir -p "$FFMPEG_SOURCES_DIR"

    # main things
    installLibs
    installSDK
    compileNasm
    compileLibX264
    compileLibfdkcc
    compileLibMP3Lame
    compileLibOpus
    compileLibPvx
    compileFfmpeg
    echo "Complete!"
  23. @lucaspar lucaspar created this gist Jul 20, 2022.
    1 change: 1 addition & 0 deletions nvenv-install.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    ‎‎​