From time to time I need to encode or re encode videos. I use mencoder a lot, but since two or three years I use nothing except ffmpeg. Before you start with video encoding you have to know a little about common video and audio codecs and the containers. Some times I hear statements like “I decoded that video to mkv, now it is small an has a good quality” … Whatever, I like to post some ffmpeg command lines that work well for me.


USEFULL COMMANDS

Convert a MPEG2 Transport Stream to simple MPEG2 fo further use or DVD compatibility

ffmpeg -i video1.m2t -acodec copy -vcodec copy output.mpg

Cut videos from starting position to end position (use -t instead of -to to use relative time from starting point (duration))

ffmpeg -i input.mpg -ss 00:01:33 -to 00:44:55 -acodec copy -vcodec copy cut.mpg

ENCODING

Encode a video to the popular H264-codec and audio to the free Vorbis-codec

ffmpeg -i input.mpg -deinterlace -c:v libx264 -preset veryslow -crf 26 -codec:a libvorbis -qscale:a 3 output.mkv

CRF: Constant Rate Factor: bigger value means smaller file and smaller value means bigger file but better quality (standard 23)

preset: predefined templates for configuration parameters (slower means better quality), possible values are: ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow, placebo (standard medium)

qscale:a: quality level for Vorbis codec: 3 means an nominal bit rate of 112 KBit/s

deinterlace: used to merge the interlaced TV-signal

More information on H264 encoding

Encode a video with the newer H265/HEVC-codec

ffmpeg -i input.mpg -c:v libx265 -codec:a libvorbis -qscale:a 3  output.mp4

CRF standard = 28

Video encoding to the free Theora-codec

ffmpeg -i input.mpg -codec:v libtheora -qscale:v 7 -codec:a libvorbis -qscale:a 3 output.ogv

qscale:v: video quality range from 0 to 10, where 10 is highest quality.

2PASS encoding with a fixed bit rate of 1MBit/s:

ffmpeg -i input.mpg -deinterlace -b:v 1000K -c:v libtheora  -pass 1 -an -f rawvideo -y /dev/null

ffmpeg -i input.mpg -deinterlace -b:v 1000K -codec:v libtheora -pass 2 -codec:a libvorbis -qscale:a 3 output.ogv

Encoding to the free VP8 and VP9 codecs:

VP8 and VP9 are widely supported by all main browsers (except IE). The VP9 is the standard codec at YouTube.

VP8 at variable bitrate:

ffmpeg -i input.mp4 -c:v libvpx -crf 10 -b:v 1M -c:a libvorbis -qscale:a 3 output.webm

CRF: can be a value from 4 to 63, lower values mean better quality

b:v: This is the target bit rate, the encoder is trying to reach this value

VP9 at VBR variable bitrate (target 700KBit/s) and opus audio codec:

ffmpeg -i input.mp4 -c:v libvpx-vp9 -b:v 700K -c:a libopus -b:a 96000  output.webm  

VP9 in constant quality mode:

ffmpeg -i input.mp4 -c:v libvpx-vp9 -crf 38 -b:v 0 -c:a libopus -b:a 96000  output.webm

CRF: a value from 0 to 63, where lower values mean better quality


NVIDIA graphics card with CUDA:

For a little period of time I got a fast Geforce graphics card with CUDA support and H264 encoding capability. Therefore I had t recompile ffmpeg to activate the Nvidia H264 encoder named nvenc_h264.

The encoded videos with the nvenc_h264 engine are bigger, but your are able to encode like 100 times faster.

variable bitrate:

ffmpeg -i input.avi -c:v nvenc_h264 -preset hq  -codec:a libvorbis -qscale:a 3 output.mkv

Or you are using a constant bit rate, but in comparison with the x264 encoder the quality is worse.

constant bitrate:

ffmpeg -i input.mpg  -b:v 1000K -c:v nvenc_h264 -preset hq   -codec:a libvorbis -qscale:a 3 output.mkv

Next post: yet another vim cheat sheet

Previous post: Fairphone2 - half a year of crashes