Skip to content

Instantly share code, notes, and snippets.

@rochmad
Last active August 8, 2019 06:30
Show Gist options
  • Save rochmad/3270c6744b5488aa2769e383cfd4b4e1 to your computer and use it in GitHub Desktop.
Save rochmad/3270c6744b5488aa2769e383cfd4b4e1 to your computer and use it in GitHub Desktop.
INSTALL FFMPEG 4 WITH NVEC
https://gist.github.com/AlexanderC/3986104
https://developer.nvidia.com/ffmpeg
https://ntown.at/de/knowledgebase/cuda-gpu-accelerated-h264-h265-hevc-video-encoding-with-ffmpeg/
https://arstech.net/ffmpeg-gpu-transcoding-examples/
------------
# INSTALL FFMPEG WITH NVEC
https://developer.nvidia.com/ffmpeg
#### Download and install ffnvcodec:
git clone https://git.videolan.org/git/ffmpeg/nv-codec-headers.git
cd nv-codec-headers && sudo make install && cd –
#### Download the latest FFmpeg or libav source code, by cloning the corresponding GIT repositories
wget https://ffmpeg.org/releases/ffmpeg-4.2.tar.gz
OR
FFmpeg: https://git.ffmpeg.org/ffmpeg.git
Libav: https://github.com/libav/libav
#### Download and install the compatible driver from NVIDIA web site
apt install nvidia-384
#### Download and install the CUDA Toolkit CUDA toolkit
https://developer.nvidia.com/cuda-downloads?target_os=Linux&target_arch=x86_64&target_distro=Ubuntu&target_version=1810&target_type=debnetwork
http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1810/x86_64/cuda-repo-ubuntu1810_10.1.168-1_amd64.deb
sudo dpkg -i cuda-repo-ubuntu1810_10.1.168-1_amd64.deb
sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1810/x86_64/7fa2af80.pub
sudo apt-get update
sudo apt-get install cuda
/etc/profile.d/cuda.sh
export PATH=$PATH:/usr/local/cuda/bin
export CUDADIR=/usr/local/cuda
source /etc/profile.d/cuda.sh
#### FFMPEG 4.2
git clone https://github.com/FFmpeg/FFmpeg
#or
wget https://ffmpeg.org/releases/ffmpeg-4.2.tar.gz
#### Use the following configure command (Use correct CUDA library path in config command below)
```bash
apt install git yasm build-essential pkg-config libmp3lame-dev libvorbis-dev libvpx-dev libfaac-dev libx11-dev libx264-dev librtmp-dev libass-dev libavutil-dev openssl libssl-dev make libpcre3-dev libpcre3 openssl autoconf automake cmake libfreetype6-dev libsdl2-dev libtool libva-dev libvdpau-dev libxcb1-dev libxcb-shm0-dev libxcb-xfixes0-dev texinfo wget zlib1g-dev nasm libx265-dev libnuma-dev libfdk-aac-dev libopus-dev libtheora-dev mercurial checkinstall libgpac-dev libopencore-amrnb-dev libopencore-amrwb-dev texi2html libopencv-dev
./configure --prefix=/usr --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu \
--extra-cflags=-I/usr/local/cuda/include --extra-ldflags=-L/usr/local/cuda/lib64 \
--enable-gpl --enable-nonfree --enable-postproc --disable-stripping \
--enable-cuda-nvcc --enable-cuvid --enable-nvenc --enable-libnpp \
--enable-avresample --enable-avisynth --enable-gnutls --enable-libass \
--enable-libfontconfig --enable-libfreetype --enable-libfribidi \
--enable-libmp3lame --enable-libopus --enable-libpulse --enable-libvorbis \
--enable-libvpx --enable-libx265 --enable-opengl --enable-sdl2 \
--enable-libx264 --enable-shared --enable-pthreads \
--enable-swscale --enable-filter=movie --enable-librtmp
make -j 10
make install
```
#### Use FFmpeg/libav binary as required. To start with FFmpeg, try the below sample command line for 1:2 transcoding
wget https://sample-videos.com/video123/mp4/720/big_buck_bunny_720p_30mb.mp4
ffmpeg -y -hwaccel cuvid -c:v h264_cuvid -vsync 0 -i big_buck_bunny_720p_30mb.mp4 -vf scale_npp=1920:1072 -vcodec h264_nvenc big_buck_bunny_720p_30mb_h264_nvenc.mp4 -vf scale_npp=1280:720 -vcodec h264_nvenc big_buck_bunny_720p_30mb_h264_nvenc.mp4
#### OPENCV (OPTIONAL)
apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev qtbase5-dev
wget https://github.com/Itseez/opencv/archive/3.4.0.zip
unzip 3.4.0.zip
cd opencv-3.4.0
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D WITH_V4L=ON -D WITH_QT=ON -D WITH_OPENGL=ON -DBUILD_opencv_cudacodec=OFF ..
make -j $(nproc)
sudo make install
## TEST VIDEO
wget https://sample-videos.com/video123/mp4/720/big_buck_bunny_720p_30mb.mp4
mkdir -p ffmresu
* note cpu-used in convert to mp4 deprecated
decode vp9 slower than vp8 /100 vs 30 fps
------------
### Example
#### WEBM VP8 480
```bash
ffmpeg -y -i big_buck_bunny_720p_30mb.mp4 -threads 8 -vf scale=-1:1080 -map 0 -vcodec libvpx-vp9 -strict -2 ffmresu/video_bunny_converted_vp8.webm
```
#### WEBM VP9 480
ffmpeg -y -i big_buck_bunny_720p_30mb.mp4 -threads 8 -vf scale=-1:1080 -map 0 -vcodec libvpx-vp9 -strict -2 ffmresu/video_bunny_converted_vp9.webm
### MP4 H264 (CPU)
ffmpeg -y -i big_buck_bunny_720p_30mb.mp4 -threads 8 -vf scale=-1:1080 -vcodec h264 ffmresu/video_bunny_converted_h264.mp4
### MP4 H264 (GPU)
ffmpeg -y -i big_buck_bunny_720p_30mb.mp4 -threads 8 -vf scale=-1:1080 -vcodec h264_nvenc ffmresu/video_bunny_converted_h264_nvenc.mp4
### MP4 H264 (CPUGPU)
ffmpeg -y -hwaccel cuvid -c:v h264_cuvid -i big_buck_bunny_720p_30mb.mp4 -vf scale_npp=-1:1080 -c:v h264_nvenc ffmresu/video_bunny_converted_cuvid_h264_nvenc.mp4
### MP4 H265 (CPU)
ffmpeg -y -i big_buck_bunny_720p_30mb.mp4 -threads 8 -vf scale=-1:1080 -vcodec libx265 ffmresu/video_bunny_converted_h265.mp4
### MP4 H265 (CPUGPU)
ffmpeg -y -i big_buck_bunny_720p_30mb.mp4 -threads 8 -vf scale=-1:1080 -vcodec hevc_nvenc ffmresu/video_bunny_converted_h264_nvenc.mp4
### MP4 H265 (GPU)
ffmpeg -y -hwaccel cuvid -c:v h264_cuvid -i big_buck_bunny_720p_30mb.mp4 -vf scale_npp=-1:1080 -c:v hevc_nvenc ffmresu/video_bunny_converted_cuvid_hevc_nvenc.mp4
## Bench mode
echo -e "\nWEBM VP8 (CPU)" && \
{ time ffmpeg -y -i big_buck_bunny_720p_30mb.mp4 -threads 8 -vf scale=-1:1080 -map 0 -vcodec libvpx-vp9 -strict -2 ffmresu/video_bunny_converted_vp8.webm >/dev/null; } |& grep real && \
echo -e "\nWEBM VP9 (CPU)" && \
{ time ffmpeg -y -i big_buck_bunny_720p_30mb.mp4 -threads 8 -vf scale=-1:1080 -map 0 -vcodec libvpx-vp9 -strict -2 ffmresu/video_bunny_converted_vp9.webm >/dev/null; } |& grep real && \
echo -e "\nMP4 H264 (CPU)" && \
{ time ffmpeg -y -i big_buck_bunny_720p_30mb.mp4 -threads 8 -vf scale=-1:1080 -vcodec h264 ffmresu/video_bunny_converted_h264.mp4 >/dev/null; } |& grep real && \
echo -e "\nMP4 H264 (CPUGPU)" && \
{ time ffmpeg -y -i big_buck_bunny_720p_30mb.mp4 -threads 8 -vf scale=-1:1080 -vcodec h264_nvenc ffmresu/video_bunny_converted_h264_nvenc.mp4 >/dev/null; } |& grep real && \
echo -e "\nMP4 H264 (GPU)" && \
{ time ffmpeg -y -hwaccel cuvid -c:v h264_cuvid -i big_buck_bunny_720p_30mb.mp4 -vf scale_npp=-1:1080 -c:v h264_nvenc ffmresu/video_bunny_converted_cuvid_h264_nvenc.mp4 >/dev/null; } |& grep real && \
echo -e "\nMP4 H265 (CPU)" && \
{ time ffmpeg -y -i big_buck_bunny_720p_30mb.mp4 -threads 8 -vf scale=-1:1080 -vcodec libx265 ffmresu/video_bunny_converted_h265.mp4 >/dev/null; } |& grep real && \
echo -e "\nMP4 H265 (CPUGPU)" && \
{ time ffmpeg -y -i big_buck_bunny_720p_30mb.mp4 -threads 8 -vf scale=-1:1080 -vcodec hevc_nvenc ffmresu/video_bunny_converted_h264_nvenc.mp4 >/dev/null; } |& grep real && \
echo -e "\nMP4 H265 (GPU)" && \
{ time ffmpeg -y -hwaccel cuvid -c:v h264_cuvid -i big_buck_bunny_720p_30mb.mp4 -vf scale_npp=-1:1080 -c:v hevc_nvenc ffmresu/video_bunny_converted_cuvid_hevc_nvenc.mp4 >/dev/null; } |& grep real && \
echo -e "DONE "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment