Video

FFmpeg

FFmpeg software project includes programs and libraries for audio/video processing, and for handling multimedia files and streams. The core program ffmpeg is a command-line tool for audio/video format convertions, basic editing (trimming, concatenation), video scaling, video post-production effects, etc. It can also capture and encode in real-time from various hardware and software sources such as TV capture cards.

Among other things, FFmpeg includes libavcodec - an audio/video codec library used by many commercial and free software products.

Developer's site is www.ffmpeg.org. Static builds for Linux can be downloaded from this site. There is also avconv - a video and audio converter, the part of Libav project which is the fork (2011) of FFmpeg project. Ubuntu/Debian supported avconv/Libav since 2011, but in 2015 switched back to FFmpeg.

You can get some info using following cmds:

ffmpeg -formats

ffmpeg -codecs

ffmpeg -filters

ffmpeg -preset help

ffmpeg -y -i clip01.avi -c:v libx264 -preset -tune -f mp4 /dev/null

ffmpeg -i clip002.vob

ffmpeg -i clip003.mp4

One-pass encoding / Constant Rate Factor (CRF)

This method provides a certain output quality for the whole file. Each frame gets the bitrate it needs to keep the requested quality level. The downside is that you cannot specify the output file size. The following example is supposed to provide very good quality, though the process of encoding may be slow:

ffmpeg -i clip01.vob -c:v libx264 -preset veryslow -crf 20 -c:a copy clip01.mp4

Note that the audio stream is just copied to the output "as is". Container format (MP4) is auto detected from the output file extenstion.

ffmpeg -i movie02.vob -c:a libmp3lame -ab 160k -map 0.0 -map 0.1 -map 0.3 -c:v libx264 -preset slow -crf 22 -threads 0 movie02.mp4

In this case audio is converted to MP3, audio bitrate is set to 160 Kbit/s.

Note that by default only map 0.0 (video stream) and map 0.1 (audio track #1) are included in the output. To transfer several audio tracks, you should specify them explicitly with -map.

In general, one-pass enc is better than two-pass, because it's faster (~ 40%) and provides better visual quality when comparing the total file sizes.

For better results choose the slowest preset you can tolerate.

Two-pass encoding

is used if you want a specific output file size while output quality from frame to frame is of less importance.

ffmpeg -y -i clip05.vob -c:v libx264 -preset medium -b:v 800k -pass 1 -an -f mp4 /dev/null && \

ffmpeg -i clip05.vob -c:v libx264 -preset medium -b:v 800k -pass 2 -c:a libfdk_aac -b:a 128k clip05.mp4

(Windows users use NUL instead of /dev/null)

As before, choose the slowest preset you can tolerate.

A two-pass encoding ensures, that the stream has a fixed bitrate, but without the artifacts and drawbacks known from CBR (Constant Bitrate). Fixed bitrate is important when video is supposed to be used on a real stream server, since it simplifies load balancing. For a regular web-server one-pass encoding is O.K.

Options and settings

Most used only.

-an    disable audio processing; usually it is used during the first pass of the two-pass encoding, because only video stats is recorded anyway;

-aq n

use a variable bitrate for audio; n can be 0-255, where 0 gives the best quality, and 100 is a reasonable choice;

-b:a n

set audio bitrate in bits/s;

-b:v n

set video bitrate in bits/s;

-c:a str (-acodec str)

encode audio stream using specified lib; currently ffmpeg supports several AAC (Advanced Audio Coding) standards: 4 AAC-LC encoders (aac, libfaac, libfdk_aac, libvo_aacenc) and 2 AAC-HE (libaacplus and libfdk_aac), however, libaacplus, libfaac, libfdk_aac are not compatible with GPL! You can also use libmp3lame to convert audio to MP3, and libvorbis to convert to OGG Vorbis;

-c:v str (-vcodec str)

encode video using specified library; the library in most cases is libx264 and the video compression format is H.264/MPEG-4 AVC;

-i file

the name of the input file (source file);

-preset str

preset is a set of options that provide a certain speed/compression ratio; a slower preset provides better compression (smaller files, better quality at the same bitrate); use the slowest preset you have patience for; there are: ultrafast, superfast, veryfast, faster, fast, medium (default), slow, slower, veryslow, placebo (?);

-profile:v str (-vprofile str)

optional setting which limits the output to a specific H.264 profile (see H.264/MPEG-4 AVC in Wikipedia); this can be used if target device only supports a certain profile; currently there are: baseline, main, high, high10, high422, high444; if you want your videos to have the best compatibility with target devices (e.g., older iOS and Android versions), use baseline -level 3.0; Note! this is incompatible with lossless encoding;

-threads n

(with libx264) the number of threads to be used during encoding; usually n = 0 which means: choose the optimal number of threads (i.e., use all available processor cores); you can also limit resources avaliable to ffmpeg by specifying an explicit [small] number of cores to be used;

-tune str

allows to adapt settings to the specifics of your input; currently available: film, animation, grain, stillimage, psnr, ssim, fastdecode, zerolatency; if you are not sure what is best for your kind of video, just omit this option;

-vf fade=in:m:n,fade=out:p:q

apply video fade (in/out) filter; you can specify any one of them or both; all params are frames: m - frame number to start fade-in, n - fade-in duration (num of frames), p - frame num to start fade-out, q - fade-out duration; see example.

-vf scale=m:n

apply scale filter to resize video to the desired resolution; scale is specified like 720:480 or -1:480 , where -1 requires to preserve the initial aspect ratio;

Choosing CRF

The range is from 0 (lossles; the best quality) to 51 (bad). The default is 23, the reasonable range is 18-28, since 18 looks nearly lossless. The range is exponential, so changing CRF by -6 roughly doubles the bitrate.

Note:

The CRF quantizer scale discussed here only applies to 8-bit x264 (more common among distributors). There is also 10-bit x264 quantizer scale with the range 0-63.

ABR (Average Bit Rate)

This cmd tries to provide some average bitrate:

ffmpeg -i clip04.vob -c:v libx264 -b:v 1000k clip04.mp4

If it gets a lot of black frames (which cost very little), it encodes them with less than requested bitrate, but then the following [non-black] frames are encodes at very high quality to bring the average back in line. This works better with 2-pass encoding (see below).

CBR (Constant Bit Rate)

is not supported, but you can simulate it like this:

ffmpeg -i clip04.vob -c:v libx264 -b:v 4000k -minrate 4000k -maxrate 4000k -bufsize 1835k clip4.m2v

In this cmd -bufsize is the "rate control buffer", it will enforce the requested average (4000 kbit/s) across each 1835k worth of video. It is assumed that the player will buffer that much data so it's ok to fluctuate within.

Limiting bit rate

You can limit bit rate by specifying crf and maxrate.

ffmpeg -i clip03.avi -c:v libx264 -crf 20 -maxrate 800k -bufsize 1835k clip03.mp4

This cmd tries to provide crf 20, but if the output exceeds 800 kbit/s, it will degrade to lower quality.

Converting MP4 to WEBM

With the libvpx used for WebM, quality depends on quantization levels -qmin and -qmax ranging from 0 to 51. Setting qmin may have no difference. Setting qmax effectively controls the max compression that will be applied to each frame, i.e., a higher qmax means lower quality with higher compr (smaller file size). Try to start with 25. Setting bitrate (-ab for audio, -b for video) makes little or no difference.

ffmpeg -i clip04.mp4 -acodec libvorbis -aq 5 -ac 2 -qmax 20 -threads 2 clip04.webm

Audio codec for WebM is Ogg Vorbis (-acodec libvorbis, default in late versions). The quality can be adjusted with -aq [1-10], higher means better, 4 or 5 is good in most cases. To be exact:

-aq 4 = 128 kb/s
-aq 5 = 160 kb/s
-aq 6 = 192 kb/s
-aq 7 = 224 kb/s
-aq 8 = 256 kb/s

You can also ajust audio volume:

-vol 256 = 100%
-vol 512 = 200%
-vol 768 = 300%
-vol 1024 = 400%
-vol 2048 = 800%

Intermediate values can be used as well.

Use -ac to set the number of channels: 1 (mono), 2 (stereo).

Transcoding flags: -threads sets the num of CPU threads to be used when transcoding, -pass provides 2-pass encoding.

Resize / Rotate

Resizing video

The original file is 640x480, we're going to resize it to 320x240 (remember about aspect ratio):

ffmpeg -i clip01.mp4 -s 320x240 -c:v libx264 -c:a copy clip01sm.mp4

or

ffmpeg -i clip01.mp4 -vf scale=320:240 -c:v libx264 -c:a copy clip01sm.mp4

Rotating video

To rotate video you can use following options:

-vf transpose=1  90° clockwise rotation;
-vf transpose=2 90° counterclockwise rotation;
-vf transpose=3 90° clockwise rotation + vertical flip;
-vf transpose=0 90° counterclockwise rotation + vertical flip;

The following example rotates video 90° clockwise, copies audio as is, and saves it all to a new file:

ffmpeg -i clip01.mp4 -vf transpose=1 -c:a copy clip02.mp4

Cuttung a fragment

The following cmd cuts a fragment from Movie.vob starting at 1 hour 1 min 30 sec from the beginning (-ss), the length of the fragment is 122 sec (-t). It's also important to notice that streams to be copied are explicitly specified: video stream (-map 0.0) and audio track # 2 (129) (-map 0.2):

ffmpeg -ss 01:01:30 -t 00:02:02 -i Movie.vob -vcodec copy -acodec copy TinManSong.vob -map 0.0 -map 0.2

Note that -ss and -t must precede input file, otherwise the result could be strange. Anyway, the starting point and the ending point of the clip may not be exactly what you want, because the clip can only start at the base frame. Since VOB files have more base frames then AVI or MP4 or FLV, they are the most suitable for cutting.

The following sequence of cmds cuts a fragment from MP4 (60s), reduces frame rate from 30 to 25, converts and resizes to FLV:

ffmpeg -ss 00:00:06 -t 00:01:00 -i clip20.mp4 -c:v copy -c:a copy clip21.mp4

ffmpeg -i clip21.mp4 -r 25 -c:v libx264 -c:a copy clip22.mp4

ffmpeg -i clip22.mp4 -s 320x240 -c:v libx264 -c:a copy -q:v 5 clip22.flv

Merging two (or more) video files

Create a simple text file (e.g. files.txt) containing the names of all video files you want to concatenate:

# This is a comment;
file 'clip01.mp4'
file 'clip02.mp4'
file 'clip03.mp4'
File's paths can be absolute or relative. Now join these files with:

ffmpeg -f concat -i files.txt -c copy clip04.mp4

Applying fade-in/out filters

The following cmd applies simultaneously audio/video fade-in and fade-out:

ffmpeg -i clip01.mp4 -vf fade=in:0:90,fade=out:3541:90 -af afade=t=in:ss=0:d=3,afade=t=out:st=118:d=3 clip02.mp4

You may need some info before you start (frame rate, total number of frames, etc). In this example frame rate = 30 fr/s, total number of frames = 3630, total length in seconds = 121s. Video fade (fade) is specified in frames, audio fade (afade) - in seconds. Usually fade-in is simple if you start exactly at the beginning: fade=in:0:90 means "start at frame 0 and reach max at frame 90" (~3s from dark to full picture). Fade-out, however, requires some calculations. For example: total length is 3630 frames, we want to fade out in 3s, which is 90 frames. So, since count starts at 0 and last frame is 3631, fade-out must start at frame 3541.

Audio

Changing audio volume

Reducing audio to 25% of the original value:

ffmpeg -i CAM00015.mp4 -vcodec copy -ab 96k -vol 64 mov0015.mp4

Note that 256 = 100%, so -vol 64 means 25% of the initial level, while -vol 512 means 200% of the initial level.

Extracting audio

To extract the first (-map 0:1) AC-3 encoded audio stream exactly as it was multiplexed (-vn disables video stream processing):

ffmpeg -i video01.vob -map 0:1 -c:a copy -vn video01.ac3

To convert the third (-map 0:3) DTS audio stream to an AAC file with a bitrate of 192 kbit/s and a sampling rate of 96000 Hz:

ffmpeg -i video02.vob -map 0:3 -c:a libvo-aacenc -ab 192k -ar 96000 -vn video02.aac

Converting WAV to ...

MPEG-1 layer 3, Constant Bit Rate (CBR):

ffmpeg -i file01.wav -c:a libmp3lame -ab 192k file01.mp3

MPEG-1 layer 3, Variable Bit Rate (VBR):

ffmpeg -i file02.wav -c:a libmp3lame -aq 100 file02.mp3

AAC, Constant Bit Rate (CBR):

ffmpeg -i file03.wav -f adts -c:a libfaac -ab 192k file03.aac

AAC, Variable Bit Rate (VBR):

ffmpeg -i file04.wav -f adts -c:a libfaac -aq 90 file04.aac

OGG Vorbis, Constant Bit Rate (CBR):

ffmpeg -i file05.wav -f ogg -c:a libvorbis -ab 192k file05.ogg

OGG Vorbis, Variable Bit Rate (VBR):

ffmpeg -i file06.wav -f ogg -c:a libvorbis -aq 105 file06.ogg

FLAC:

ffmpeg -i file07.wav -c:a flac file07.flac

Windows Audio Media v2:

ffmpeg -i file08.wav -c:a wmav2 -ab 256k file08.wma

Creating a poster for video

HTML5 video players have a poster property that requires a picture of the appropriate size. The following cmds give you the simplest way to create such pictures. Convert PNG to JPG to decrease size.

The following cmd takes a single (first) frame from a video file and converts it to a PNG image file.

ffmpeg -i file01.mp4 -vcodec png -vframes 1 file01.png

You can also get at once the series of PNG images based on the sequential frames from video file (20 pictures in this case):

ffmpeg -i file01.mp4 -vcodec png -vframes 20 file01-%03d.png

The same trick, but skip the first 9 seconds of video, and only than create 20 PNG files:

ffmpeg -i file01.mp4 -vcodec png -vframes 20 -ss 10 file01-%03d.png

Creating animated GIF from video

First, we need an appropriate video file, i.e., MP4, AVI, quite small (like 160x120), with low framerate (~15 frames/s), 30..100 sec length. Otherwise the resulting GIF can be very large.

ffmpeg -i file1.mp4 file1.gif

or (if the above does not work)

ffmpeg -i file1.mp4 -pix_fmt rgb24 file1.gif

Now we can use ImageMagick's convert utility to reduce the file size:

convert -layers Optimize file1.gif file2.gif

Let's make this GIF playing in an infinite loop:

convert -loop 0 -layers Optimize file2.gif file3.gif

Sometimes it plays too fast or too slow. You can ajust the animation speed with the following utilities (ImageMagick). First, let's see what is the current delay param:

identify -verbose file3.gif | grep Delay

It shows delay for each frame, i.e., the output can be 1000 lines and more (CTRL+C stops the process). If delay is 4x100, then 2x100 would make animation faster, while 10x100 would make it essentially slower:

convert -delay 10x100 file3.gif file3slow.gif

or

convert -delay 10 file3.gif file3slow.gif

Mplayer

(playing, ripping, encoding video)

There are 2 related apps: mplayer and mencoder (video encoding). Both are cmdline apps with intimidating number of options. You can get src, codecs, skins, etc. from www.mplayerhq.hu.

There are several GUI front-ends for Mplayer, for example, SMPlayer (config file: ~/.config/smplayer/*).

Following keys can be used to control mplayer (cmd line and GUI):

ffullscreen mode toggle;
mmute sound;
ppause;
qquit;
/ | *-/+ audio volume (Num Key Pad);
| 10s back/forward;
| 60s back/forward;
Page Up10 min forward;
Page Down10 min backward;

Playing and ripping from DVD:

mplayer dvd://1

play title 1 (all chapters) from the default DVD device;

mplayer dvd://1 -chapter 2-2

play title 1, chapter 2 from the default DVD device;

mplayer dvd://6 -aid 129 -dvd-device /dev/hda

play title 6 from non-default device using audio track 129;

mplayer dvd://2 -chapter 4-9 -aid 138 -dvd-device /opt/movies/TheCore

play title 2, chapters 4..9, use audio track 138 (the movie was copied from DVD to HDD);

mplayer dvd://4 -aid 128 -dumpstream

save title 4, audio track 128 from DVD to a file (by default stream.dump in the current dir);

mplayer dvd://24 -dumpstream -dumpfile clip024.vob

save title 24 from a DVD to the file clip024.vob;

mplayer dvd://1 -aid 130 -vf cropdetect -dvd-device /dev/sr0

find actual picture size to be used by mencoder;

mplayer -ss 00:01:48 -endpos 10 mov005.mp4

skip the first 1 min 48 sec of mov005.mp4 and play the following 10s;

The next example is DVD to AVI two-pass encoding: audio track 138 is copied as is, video is encoded using xvid codec, bitrate is auto adjusted to produce ~1.4 Gb output file faculty.avi, codec options (vhq, bvhq, etc) are set for "good" video quality, picture size is left unchanged, black areas around the picture are removed (crop option):

mencoder -oac copy -ovc xvid -xvidencopts pass=1:vhq=2: bvhq=1:chroma_opt:quant_type=mpeg -vf crop=704:544:4:16 -o /dev/null dvd://1 -aid 138 -dvd-device /dev/hda

mencoder -oac copy -ovc xvid -xvidencopts pass=2:bitrate=-1400000: vhq=2:bvhq=1:chroma_opt:quant_type=mpeg -vf crop=704:544:4:16 -o faculty.avi dvd://1 -aid 138 -dvd-device /dev/hda

Video DVD creation

It's assumed that files were ripped from the original DVD to some dir on HDD (dir matrix, in the following example). For reference: it should contain AUDIO_TS (optional) and VIDEO_TS. Many movie DVDs can be just copied file by file. Damaged files can be copied with ddrescue utility (not always, of course).

One-step procedure:

growisofs -dvd-video -Z /dev/hda -V Matrix.1 ./matrix

Option -dvd-video specifies the creation of UDF filesystem, and this is important for video DVD.

Two-step procedure (requires additional space for an image file):

mkisofs -o matrix1.iso -dvd-video -V Matrix.1 ./matrix

Second step, burning DVD (+/-R, +/-RW):

growisofs -dvd-compat -Z /dev/hda=matrix1.iso