Message Boards Message Boards

How to install FFmpeg on Linux machine

Posted 4 years ago

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
  • mp3 audio encoder
sudo apt-get -y install libmp3lame-dev
  • Opus audio encoder
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
  • speex audio codec
sudo apt-get -y install libspeex-dev
  • mp2 audio codec
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
  • AV1 video codec
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
  • vp8/vp9 video codec
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
  • H.264
sudo apt-get -y install libx264-dev
  • H.265/HEVC
sudo apt-get -y install libx265-devel
  • Xvid
sudo apt-get -y install libxvidcore-dev
  • v4l2 video
sudo apt-get -y install libv4l-dev
  • https protocol
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
POSTED BY: Chien-Yu Chen
4 Replies

Before I can get my hands on a opensuse test machine, can you check the library dependencies by

StringSplit[
 RunProcess[{"ldd", FindFile["FFmpegToolsSystem.so"]}][
  "StandardOutput"], EndOfLine]

and send the output to chienyuc@wolfram.com for me to take a quick look

POSTED BY: Chien-Yu Chen

Certainly my opensuse tumbleweed system has ffmpeg:

~> rpm -q ffmpeg-4

ffmpeg-4-4.4.1-6.1.x86_64

Or:

~> ffmpeg

ffmpeg version 4.4.1 Copyright (c) 2000-2021 the FFmpeg developers
built with gcc 11 (SUSE Linux) configuration: --prefix=/usr --libdir=/usr/lib64 --shlibdir=/usr/lib64 --incdir=/usr/include/ffmpeg --extra-cflags='-O2 -Wall -DFORTIFYSOURCE=2 -fstack-protector-strong -funwind-tables -fasynchronous-unwind-tables -fstack-clash-protection -Werror=return-type -flto=auto -ffat-lto-objects -g' --optflags='-O2 -Wall -DFORTIFYSOURCE=2 -fstack-protector-strong -funwind-tables -fasynchronous-unwind-tables -fstack-clash-protection -Werror=return-type -flto=auto -ffat-lto-objects -g' --disable-htmlpages --enable-pic --disable-stripping --enable-shared --disable-static --enable-gpl --enable-version3 --enable-libsmbclient --disable-openssl --enable-avresample --enable-gnutls --enable-ladspa --enable-vulkan --enable-libaom --enable-libass --enable-libbluray --enable-libbs2b --enable-libcelt --enable-libcdio --enable-libcodec2 --enable-libdav1d --enable-libdc1394 --enable-libdrm --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgsm --enable-libjack --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librav1e --enable-librubberband --enable-libsvtav1 --enable-libsoxr --enable-libspeex --enable-libssh --enable-libsrt --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvmaf --enable-libvorbis --enable-libv4l2 --enable-libvpx --enable-libwebp --enable-libxml2 --enable-libzimg --enable-libzmq --enable-libzvbi --enable-lto --enable-lv2 --enable-libmfx --enable-vaapi --enable-vdpau --enable-version3 --enable-libfdk-aac-dlopen --enable-nonfree --enable-libvo-amrwbenc --enable-libx264 --enable-libx265 --enable-librtmp --enable-libxvid libavutil 56. 70.100 / 56. 70.100 libavcodec 58.134.100 / 58.134.100 libavformat 58. 76.100 / 58. 76.100 libavdevice 58. 13.100 / 58. 13.100 libavfilter 7.110.100 / 7.110.100 libavresample 4. 0. 0 / 4. 0. 0 libswscale 5. 9.100 / 5. 9.100 libswresample 3. 9.100 / 3. 9.100 libpostproc 55. 9.100 / 55. 9.100 Hyper fast Audio and Video encoder usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...

Yet it is not found by mathematica:

In[1]:= FFmpegTools`$SystemFFmpegQ

Out[1]= False

Why is that?

Klaus

enter image description here -- you have earned Featured Contributor Badge enter image description here Your exceptional post has been selected for our editorial column Staff Picks http://wolfr.am/StaffPicks and Your Profile is now distinguished by a Featured Contributor Badge and is displayed on the Featured Contributor Board. Thank you!

POSTED BY: EDITORIAL BOARD
Posted 4 years ago

The Windows version of this post can be found here

The macOS version of this post can be found here

POSTED BY: Sean Cheren
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract