MODERATOR NOTE:
for installing FFmpeg on MacOS see this post,
https://community.wolfram.com/groups/-/m/t/2189605
for installing on Windows see this post
https://community.wolfram.com/groups/-/m/t/2177967
In an earlier community post, Piotr explained how installing FFmpeg can improve codec support in the Wolfram Language and how to do it on Windows. Here I explain some details on how this can be done on Linux. Let's begin with a recap of why this is important.
Video object and video processing functions were introduced in Wolfram Language 12.1. To read from and write to video files, video functionality uses media libraries provided by operating systems as well as a limited version of FFmpeg that is shipped with the language and can be immediately used. Details and examples showing how to import and export video files can be found in the Importing & Exporting Video tutorial. The Codec Support section in that tutorial describes the limitations of the FFmpeg shipped with the language and shows how installing the full version of FFmpeg gets you better support for audio and video codecs.
Without FFmpeg installed, the first time a video function is called a message is displayed, suggesting to install FFmpeg for a more complete codec support. Here is the list of supported video decoders available before having FFmpeg installed. At the end of this post we will compare the list with the supported decoders after FFmpeg is installed:
In[1]:= Length /@ $VideoDecoders
During evaluation of In[1]:= General::sysffmpeg: Using a limited version of FFmpeg. Install FFmpeg to get more complete codec support.
Out[1]= <|"AVI" -> 37, "Matroska" -> 54, "MP4" -> 10, "Ogg" -> 1, "QuickTime" -> 23, "VideoFormat" -> 59|>
Some distributions will have a recent enough FFmpeg for Wolfram Language to use. For example, sudo apt-get install ffmpeg
would have sufficed on Ubuntu 20.04. If using a distribution that does not come with FFmpeg 4.0.0 or higher, compiling from the source is required. This article will guide you through the process of compiling and installing FFmpeg libraries on a standard Ubuntu 18.04 machine, under the assumption that sudo access is available and extra libraries will be installed in /usr/local
. Other distributions might differ on the package manager and name of the required packages.
Modern Linux distributions place dynamic loader configuration under the directory /etc/ld.so.conf.d
. Since we will be installing the libraries in /usr/local
, we have to make sure that this directory is listed in one of the files under /etc/ld.so.conf.d
. The following show that Ubuntu already has this setup:
wolfram@wolfram-HP-EliteBook-8570w:~$ grep "/usr/local/" /etc/ld.so.conf.d/*
...
/etc/ld.so.conf.d/libc.conf:/usr/local/lib
...
If the distribution does not have /usr/local
set up, something similar to the following will create an extra entry to read the libraries:
sudo bash -c 'echo -e "/usr/local/lib\n/usr/local/lib64" > /etc/ld.so.conf.d/local.conf'
Many programs use the configure script to set up the compiling environment. Next we set up an environment variable for configure to search configurations setting:
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:/usr/lib64/pkgconfig
FFmpeg requires a recent version of the nasm assembler which can be obtained with:
sudo apt-get install nasm
If the distribution does not come with a recent version of nasm, one can be compiled with the following:
cd /tmp
wget http://www.nasm.us/pub/nasm/releasebuilds/2.13.01/nasm-2.13.01.tar.bz2
tar xvf nasm-2.13.01.tar.bz2
cd nasm-2.13.01
./autogen.sh
./configure --prefix=/usr/local
make
sudo make install
GPU support can speed up video encoding/decoding. If your machine has a NVIDIA graphics card and wish to utilize the GPU, first follow the NVIDIA instructions to install the cuda driver. Once the cuda driver is installed, run the following to install the headers for FFmpeg to use:
cd /tmp
git clone https://git.videolan.org/git/ffmpeg/nv-codec-headers.git
cd nv-codec-headers
make
sudo make install
You can choose which codecs to install or skip by removing the flag from the FFmpeg configure step. If a certain codec is to be used, the corresponding library must be installed before compiling FFmpeg.
- ass/ssa subtitle renderer
sudo apt-get -y install libass-dev
sudo apt-get -y install libmp3lame-dev
cd /tmp
git clone --depth 1 https://github.com/xiph/opus.git
cd opus
./autogen.sh
./configure --prefix=/usr/local --disable-static --enable-shared
make
sudo make install
sudo apt-get -y install libspeex-dev
sudo apt-get -y install libtwolame-dev
- vorbis audio codec for ogg
sudo apt-get -y install libvorbis-dev
- theora video codec for ogg
sudo apt-get -y install libtheora-dev
cd /tmp
git clone https://aomedia.googlesource.com/aom
cd aom
mkdir build-dir
cd build-dir
cmake -DCMAKE_INSTALL_PREFIX=/usr/local ..
make -j 8
sudo make install
cd /tmp
git clone --depth 1 https://chromium.googlesource.com/webm/libvpx.git
cd libvpx
./configure --prefix=/usr/local --disable-examples --disable-unit-tests --enable-vp9-highbitdepth --as=nasm --disable-static --enable-shared
make -j 8
sudo make install
sudo apt-get -y install libx264-dev
sudo apt-get -y install libx265-devel
sudo apt-get -y install libxvidcore-dev
sudo apt-get -y install libv4l-dev
sudo apt-get -y install gnutls-dev
sudo apt-get -y install libunistring-dev
Now compile and install the FFmpeg libraries. The most important flags are --disable-static, --enable-shared and --enable-gpl. Also, it is important to run the sudo ldconfig command at the end to make the libraries available to the Wolfram Language.
cd /tmp
wget https://ffmpeg.org/releases/ffmpeg-4.3.1.tar.xz
tar xvf ffmpeg-4.3.1.tar.xz
cd ffmpeg-4.3.1
./configure --prefix=/usr/local --disable-static --enable-shared --enable-decoder=mjpeg --enable-decoder=mpegvideo --enable-decoder=vp9 --enable-gnutls --enable-nvenc --enable-libx265 --enable-gpl --enable-libmp3lame --enable-libx264 --enable-libvorbis --enable-libvpx --disable-debug --disable-doc --enable-cuda --enable-cuvid --enable-libdrm --enable-ffplay --enable-libass --enable-libfontconfig --enable-libopus --enable-libpulse --enable-sdl2 --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libv4l2 --enable-libxcb --enable-libxvid --enable-nonfree --enable-runtime-cpudetect --enable-xlib
make -j 8
sudo make install
sudo ldconfig
Once FFmpeg is installed and kernel restarted, check $VideoDecoders again to see the increased number of available encoders:
In[1]:= Length /@ $VideoDecoders
Out[1]= <|"AVI" -> 159, "Matroska" -> 258, "MP4" -> 29, "Ogg" -> 1, "QuickTime" -> 71, "VideoFormat" -> 284|>
Another way to verify that the full version of FFmpeg is used by Wolfram Language:
In[2]:= FFmpegTools`$SystemFFmpegQ
Out[2]= True