1. Get ffmpeg
It is always a good idea to use a recent build when encoding with ffmpeg
. Development is very active and using a recent build will allow you to avoid fixed bugs while giving you access to the latest features.
There are two main methods to get ffmpeg
and neither will interfere with packages from the repository including the so-called "ffmpeg" package:
Using a static build
This is simply a binary that someone else compiled. All you do is download, extract, and run it (note the ./
before ffmpeg
):
wget http://ffmpeg.gusari.org/static/32bit/ffmpeg.static.32bit.$(date +"%F").tar.gz
tar xzvf ffmpeg.static.32bit.$(date +"%F").tar.gz
./ffmpeg -i input.mp4 <your options> output.mkv
Compiling ffmpeg
When compiling you control exactly how ffmpeg
is configured and also allows you to use certain encoders, such as libfdk_aac
, that are not available in the static builds.
Just follow a step-by-step guide: How to Compile FFmpeg on Ubuntu. If you can copy and paste you can compile ffmpeg
.
2. Encode
ffmpeg -i in.avi -vf scale=-1:720 -c:v libx264 -crf 18 -preset fast -c:a copy out.mkv
In this example:
The scale
filter to resize the video. With scale
you can just declare one dimension, height in this case, and use -1
in the other. ffmpeg
will then automatically calculate the correct value while maintaining the aspect ratio.
The encoder libx264
will produce H.264 video. Quality is controlled with -crf
. The range is a log scale of 0 to 51. 0 is lossless (files will likely be huge), 18 is often considered to be "visually lossless", 23 is default, and 51 is worst quality. Generally you use the highest value that still gives you an acceptable quality.
Video encoding speed/compression efficiency for this encoder is controlled with the -preset
. These are: ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow. Default is "medium". Generally you use the slowest preset that you have patience for.
For a set of videos use the same -crf
and -preset
for all of them.
The audio will be stream copied from the input to the output. Think of it like a copy and paste.
The Matroska output container will be used. It supports more formats not it is not as widely supported by players and devices as MP4 for example.
Encoding all videos
You can use a bash "for loop" to encode all videos in a directory:
mkdir encoded
for f in *.avi; do ./ffmpeg -i "$f" -vf scale=-1:720 -c:v libx264 -crf 18 -preset fast -c:a copy encoded/"${f%.avi}.mkv"; done
Also see
ffmpeg
andavconv
are not the same thing. See Who can tell me the difference and relation between ffmpeg, libav, and avconv?.