Simple Video Processing
We will use the open-source software package FFmpeg to work with video, which supports most types of formats for video and audio. In addition, it contains a wide range of filters https://ffmpeg.org/ffmpeg-filters.html for video processing. The FFmpeg software package is available for most Linux distributions, for macOS and for Windows.
We will run the FFmpeg software package on the cluster via the command line. To make it easier to set up the switches, we can use a web interface such as FFmpeg Commander, or the program - on Linux and Windows, for example WinFF.
Format Conversion
First, we load the appropriate module that will allow us to use ffmpeg
on the cluster.
$ module load FFmpeg
module load FFmpeg
Let's check that the module is working properly:
$ ffmpeg -version
ffmpeg version 4.3.2 Copyright (c) 2000-2021 the FFmpeg developers
built with gcc 11.2.0 (GCC)
configuration: --prefix=/ceph/grid/software/modules/software/FFmpeg/4.3.2-GCCcore-11.2.0 --enable-pic --enable-shared --enable-gpl --enable-version3 --enable-nonfree --cc=gcc --cxx=g++ --enable-libx264 --enable-libx265 --enable-libmp3lame --enable-libfreetype --enable-fontconfig --enable-libfribidi
libavutil 56. 51.100 / 56. 51.100
libavcodec 58. 91.100 / 58. 91.100
libavformat 58. 45.100 / 58. 45.100
...
ffmpeg -version
Let's now try to use ffmpeg
to process the video. First, we download the video llama.mp4
from link1 and save it on the cluster.
The simplest command ffmpeg
converts the clip from one format to another (without any additional switches, ffmpeg
will itself choose the appropriate encoding settings according to the given file extensions). We need to make sure that the conversion is done on one of the compute nodes, so we use srun
.
$ srun --ntasks=1 ffmpeg -y -i llama.mp4 llama.avi
srun --ntasks=1 ffmpeg -y -i llama.mp4 llama.avi
Use the -y
switch to request that ffmpeg
overwrites existing files without asking. Use the -i
switch to specify the input file llama.mp4
, and the last argument is the name of the output file llama.avi
to be produced by ffmpeg
.
Lowering Resolution
The plethora of switches can make the switch combinations for ffmpeg
quite complex. There are many examples on the web that can be used as a starting point for writing your own commands.
For example, if we want to reduce the resolution of a clip, we can use the scale filter. To reduce the resolution from the original 960 × 540 to 640 × 360 pixels, we write:
$ srun --ntasks=1 ffmpeg \
-y -i llama.mp4 -codec:a copy -vf scale=640:360 llama-small.mp4
srun --ntasks=1 ffmpeg -y -i llama.mp4 -codec:a copy -filter:v scale=640:360 llama-small.mp4
This command creates a new video llama-small.mp4
from the input video llama.mp4
, where the width and height of the video are both 640 × 360 pixels. The \
symbol at the end of the first line indicates that the command continues on the next line. The new arguments in the command are:
-codec:a copy
: the audio samples are copied exactly as they are to the output files, and-filter:v scale=640:360
: applies the (video) filter scale, where the width of the output video should be 640 and the height should be 360.
To use another filter, we only need to change argument scale=640:360
the corrensponding switches, some examples are :
hflip
andvflip
: mirror the image horizontally or vertically,edgedetect
: detects edges in an image,crop=480:270:240:135
: crops image with region size 480 × 270 and starting point (240, 135),drawtext=text=%{pts}:x=w/2-tw/2:y=h-2*lh:fontcolor=green
: write timestamp to image (pts
).
filters can be combined simply by separating them with a comma, for example,
scale=640:360,edgedetect
.
Exercise
Here you will find exercises to strengthen your knowledge on the employment of ffmpeg
for video processing.