content: # ffmpeg - http://ffmpeg.org/download.html
  #
  # From https://trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu
  #
  # https://hub.docker.com/r/jrottenberg/ffmpeg/
  #
  #
  ARG        PKG_CONFIG_PATH=/opt/ffmpeg/lib/pkgconfig
  
  
  FROM        swift:5.2.4-bionic AS base
  
  WORKDIR     /tmp/workdir
  
  RUN     apt-get -yqq update && \
          apt-get install -yq --no-install-recommends ca-certificates expat libgomp1 && \
          apt-get autoremove -y && \
          apt-get clean -y
  
  FROM base as build
  
  ARG        PKG_CONFIG_PATH
  ARG        LD_LIBRARY_PATH=/opt/ffmpeg/lib
  ARG        PREFIX=/opt/ffmpeg
  ARG        MAKEFLAGS="-j2"
  
  ENV         FFMPEG_VERSION=snapshot \
              FDKAAC_VERSION=0.1.5 \
              OGG_VERSION=1.3.2 \
              OPENCOREAMR_VERSION=0.1.5 \
              OPUS_VERSION=1.2 \
              OPENJPEG_VERSION=2.1.2 \
              THEORA_VERSION=1.1.1 \
              VORBIS_VERSION=1.3.5 \
              VPX_VERSION=1.8.0 \
              WEBP_VERSION=1.0.2 \
              X264_VERSION=20170226-2245-stable \
              X265_VERSION=3.1.1 \
              SRC=/usr/local
  
  ARG         FREETYPE_SHA256SUM="5d03dd76c2171a7601e9ce10551d52d4471cf92cd205948e60289251daddffa8 freetype-2.5.5.tar.gz"
  ARG         FRIBIDI_SHA256SUM="3fc96fa9473bd31dcb5500bdf1aa78b337ba13eb8c301e7c28923fea982453a8 0.19.7.tar.gz"
  ARG         LIBASS_SHA256SUM="8fadf294bf701300d4605e6f1d92929304187fca4b8d8a47889315526adbafd7 0.13.7.tar.gz"
  ARG         LIBVIDSTAB_SHA256SUM="14d2a053e56edad4f397be0cb3ef8eb1ec3150404ce99a426c4eb641861dc0bb v1.1.0.tar.gz"
  ARG         OGG_SHA256SUM="e19ee34711d7af328cb26287f4137e70630e7261b17cbe3cd41011d73a654692 libogg-1.3.2.tar.gz"
  ARG         OPUS_SHA256SUM="77db45a87b51578fbc49555ef1b10926179861d854eb2613207dc79d9ec0a9a9 opus-1.2.tar.gz"
  ARG         THEORA_SHA256SUM="40952956c47811928d1e7922cda3bc1f427eb75680c3c37249c91e949054916b libtheora-1.1.1.tar.gz"
  ARG         VORBIS_SHA256SUM="6efbcecdd3e5dfbf090341b485da9d176eb250d893e3eb378c428a2db38301ce libvorbis-1.3.5.tar.gz"
  
  
  
  RUN      buildDeps="autoconf \
                      automake \
                      cmake \
                      curl \
                      bzip2 \
                      libexpat1-dev \
                      g++ \
                      gcc \
                      git \
                      gperf \
                      libtool \
                      make \
                      nasm \
                      perl \
                      pkg-config \
                      python \
                      libssl-dev \
                      yasm \
                      zlib1g-dev" && \
          apt-get -yqq update && \
          apt-get install -yq --no-install-recommends ${buildDeps}
  ## x264 http://www.videolan.org/developers/x264.html
  RUN \
          DIR=/tmp/x264 && \
          mkdir -p ${DIR} && \
          cd ${DIR} && \
          curl -sL https://download.videolan.org/pub/videolan/x264/snapshots/x264-snapshot-${X264_VERSION}.tar.bz2 | \
          tar -jx --strip-components=1 && \
          ./configure --prefix="${PREFIX}" --enable-shared --enable-pic --disable-cli && \
          make && \
          make install && \
          rm -rf ${DIR}
  ### x265 http://x265.org/
  RUN \
          DIR=/tmp/x265 && \
          mkdir -p ${DIR} && \
          cd ${DIR} && \
          curl -sL https://download.videolan.org/pub/videolan/x265/x265_${X265_VERSION}.tar.gz  | \
          tar -zx && \
          cd x265_${X265_VERSION}/build/linux && \
          sed -i "/-DEXTRA_LIB/ s/$/ -DCMAKE_INSTALL_PREFIX=\${PREFIX}/" multilib.sh && \
          sed -i "/^cmake/ s/$/ -DENABLE_CLI=OFF/" multilib.sh && \
          ./multilib.sh && \
          make -C 8bit install && \
          rm -rf ${DIR}
  ### libogg https://www.xiph.org/ogg/
  RUN \
          DIR=/tmp/ogg && \
          mkdir -p ${DIR} && \
          cd ${DIR} && \
          curl -sLO http://downloads.xiph.org/releases/ogg/libogg-${OGG_VERSION}.tar.gz && \
          echo ${OGG_SHA256SUM} | sha256sum --check && \
          tar -zx --strip-components=1 -f libogg-${OGG_VERSION}.tar.gz && \
          ./configure --prefix="${PREFIX}" --enable-shared  && \
          make && \
          make install && \
          rm -rf ${DIR}
  ### libopus https://www.opus-codec.org/
  RUN \
          DIR=/tmp/opus && \
          mkdir -p ${DIR} && \
          cd ${DIR} && \
          curl -sLO https://archive.mozilla.org/pub/opus/opus-${OPUS_VERSION}.tar.gz && \
          echo ${OPUS_SHA256SUM} | sha256sum --check && \
          tar -zx --strip-components=1 -f opus-${OPUS_VERSION}.tar.gz && \
          autoreconf -fiv && \
          ./configure --prefix="${PREFIX}" --enable-shared && \
          make && \
          make install && \
          rm -rf ${DIR}
  ### libvorbis https://xiph.org/vorbis/
  RUN \
          DIR=/tmp/vorbis && \
          mkdir -p ${DIR} && \
          cd ${DIR} && \
          curl -sLO http://downloads.xiph.org/releases/vorbis/libvorbis-${VORBIS_VERSION}.tar.gz && \
          echo ${VORBIS_SHA256SUM} | sha256sum --check && \
          tar -zx --strip-components=1 -f libvorbis-${VORBIS_VERSION}.tar.gz && \
          ./configure --prefix="${PREFIX}" --with-ogg="${PREFIX}" --enable-shared && \
          make && \
          make install && \
          rm -rf ${DIR}
  ### libtheora http://www.theora.org/
  RUN \
          DIR=/tmp/theora && \
          mkdir -p ${DIR} && \
          cd ${DIR} && \
          curl -sLO http://downloads.xiph.org/releases/theora/libtheora-${THEORA_VERSION}.tar.gz && \
          echo ${THEORA_SHA256SUM} | sha256sum --check && \
          tar -zx --strip-components=1 -f libtheora-${THEORA_VERSION}.tar.gz && \
          ./configure --prefix="${PREFIX}" --with-ogg="${PREFIX}" --enable-shared && \
          make && \
          make install && \
          rm -rf ${DIR}
  ### libvpx https://www.webmproject.org/code/
  RUN \
          DIR=/tmp/vpx && \
          mkdir -p ${DIR} && \
          cd ${DIR} && \
          curl -sL https://codeload.github.com/webmproject/libvpx/tar.gz/v${VPX_VERSION} | \
          tar -zx --strip-components=1 && \
          ./configure --prefix="${PREFIX}" --enable-vp8 --enable-vp9 --enable-vp9-highbitdepth --enable-pic --enable-shared \
          --disable-debug --disable-examples --disable-docs --disable-install-bins  && \
          make && \
          make install && \
          rm -rf ${DIR}
  ### libwebp https://developers.google.com/speed/webp/
  RUN \
          DIR=/tmp/vebp && \
          mkdir -p ${DIR} && \
          cd ${DIR} && \
          curl -sL https://storage.googleapis.com/downloads.webmproject.org/releases/webp/libwebp-${WEBP_VERSION}.tar.gz | \
          tar -zx --strip-components=1 && \
          ./configure --prefix="${PREFIX}" --enable-shared  && \
          make && \
          make install && \
          rm -rf ${DIR}
  
  ### fdk-aac https://github.com/mstorsjo/fdk-aac
  RUN \
          DIR=/tmp/fdk-aac && \
          mkdir -p ${DIR} && \
          cd ${DIR} && \
          curl -sL https://github.com/mstorsjo/fdk-aac/archive/v${FDKAAC_VERSION}.tar.gz | \
          tar -zx --strip-components=1 && \
          autoreconf -fiv && \
          ./configure --prefix="${PREFIX}" --enable-shared --datadir="${DIR}" && \
          make && \
          make install && \
          rm -rf ${DIR}
  ## openjpeg https://github.com/uclouvain/openjpeg
  RUN \
          DIR=/tmp/openjpeg && \
          mkdir -p ${DIR} && \
          cd ${DIR} && \
          curl -sL https://github.com/uclouvain/openjpeg/archive/v${OPENJPEG_VERSION}.tar.gz | \
          tar -zx --strip-components=1 && \
          cmake -DBUILD_THIRDPARTY:BOOL=ON -DCMAKE_INSTALL_PREFIX="${PREFIX}" . && \
          make && \
          make install && \
          rm -rf ${DIR}
  
  ## libsoxr https://sourceforge.net/projects/soxr
  RUN \
          DIR=/tmp/soxr && \
          mkdir -p ${DIR} && \
          git clone https://git.code.sf.net/p/soxr/code ${DIR} && \
          cd ${DIR} && \
          mkdir -p build && \
          cd build && \
          cmake -Wno-dev -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="${PREFIX}" .. && \
          make && \
          make install && \
          rm -rf ${DIR}
  
  ## ffmpeg https://ffmpeg.org/
  RUN  \
          DIR=/tmp/ffmpeg && mkdir -p ${DIR} && cd ${DIR} && \
          curl -sLO https://ffmpeg.org/releases/ffmpeg-${FFMPEG_VERSION}.tar.bz2 && \
          tar -jx --strip-components=1 -f ffmpeg-${FFMPEG_VERSION}.tar.bz2
  
  
  
  RUN \
          DIR=/tmp/ffmpeg && mkdir -p ${DIR} && cd ${DIR} && \
          ./configure \
          --disable-debug \
          --disable-doc \
          --disable-ffplay \
          --enable-shared \
          --enable-avresample \
          --enable-libsoxr \
          --enable-gpl \
          --enable-libopenjpeg \
          --enable-libopus \
          --enable-libtheora \
          --enable-libvorbis \
          --enable-libvpx \
          --enable-libwebp \
          --enable-libx265 \
          --enable-libx264 \
          --enable-nonfree \
          --enable-openssl \
          --enable-libfdk_aac \
          --enable-small \
          --enable-version3 \
          --extra-cflags="-I${PREFIX}/include" \
          --extra-ldflags="-L${PREFIX}/lib" \
          --extra-libs=-ldl \
          --prefix="${PREFIX}" && \
          make && \
          make install && \
          make distclean
  
  ## cleanup
  # RUN \
  #         ldd ${PREFIX}/bin/ffmpeg | grep opt/ffmpeg | cut -d ' ' -f 3 | xargs -i cp {} /usr/local/lib/ && \
  #         cp ${PREFIX}/bin/* /usr/local/bin/ && \
  #         cp -r ${PREFIX}/share/ffmpeg /usr/local/share/ && \
  #         LD_LIBRARY_PATH=/usr/local/lib ffmpeg -buildconf
  
  ENV PKG_CONFIG_PATH=${PKG_CONFIG_PATH}
  
  FROM base
  
  RUN apt update && apt-get install -y software-properties-common build-essential wget gnupg2 git
  
  RUN apt-get install -y ocl-icd-opencl-dev libfreetype6-dev libbsd-dev pkg-config
  
  RUN wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/cuda-ubuntu1804.pin && \
      mv cuda-ubuntu1804.pin /etc/apt/preferences.d/cuda-repository-pin-600 && \
      apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub && \
      add-apt-repository "deb http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/ /" && \
      apt-get update && \
      apt-get install -y --no-install-recommends cuda-driver-dev-10-1 cuda-libraries-dev-10-1 cuda-nvrtc-dev-10-1 libcublas-dev
  
  RUN rm -rf /var/lib/apt/lists/*
  
  COPY --from=build /usr/local /usr/local/
  COPY --from=build /opt/ffmpeg /opt/ffmpeg/
  
  ARG PKG_CONFIG_PATH
  
  ENV LD_LIBRARY_PATH=/opt/ffmpeg/lib:/usr/local/cuda-10.1/lib64/stubs
  ENV PKG_CONFIG_PATH=${PKG_CONFIG_PATH}
  