Convert videos with FFmpeg

There is a lot to say on this extremely versatile program. Here I’ll try to cover some basic but super usefull cases as introduction.

FFmpeg free, open-source, cross-platform and super-powerfull

Installation

If you’re on linux, a quick sudo apt install ffmpeg might suffice.

On the following page you can download compiled version of ffmpeg : https://www.ffmpeg.org/download.html

For windows, this github build page have multiple choice, Take the version that start with ffmpeg-nx.x (ffmpeg-N-xxxx are nighlty build), in this version take the gpl (contain all library) that endswith .zip.

This youtube videos cover installation steps

The zip contain 3 binary in ffmpeg/bin subfolder :

  • ffmpeg : transcode tool
  • ffprobe : analyse tool
  • ffplay : video player

I rarely use probe and play even less but they can prove usefull somethimes to test some files.

Once everything is donwloaded, put those 3 in a place of your disc that will not be subject to change. For example, I like to place those command line tools at the root of my C: drive in a utils folder.

Then you can add this foler to you PATH (in my case I add C:\utils) so you just have to hit ffmpeg in terminal to access it.

Use

How the command is organised. Here is the most simple use case.

For this exemplle we assume that the terminal is opened in the same folder as the video and the output file

ffmpeg -i myvideo.mkv newvideo.mp4

As is, this command already transcode to a relatively optimised videofile.

The -i flag (input) followed by a space then myvideo.mkv point the source.
Ouput video new_video.mp4 don’t need any flag, since it’s at the end of the command, ffmpeg know it’s the output destination file. Added arguments will take place between the source and destination.

Transcode

Transcode a video allow to gain weight, if for exemple you want to send it or for archive purposes.

the official wiki show a nice starting guide that explain with more completeness.

We gonna check 3 arguments :

-pix_fmt (pixel format) - Defined how pixels are encoded. This allow to lose some weight but also to define compatibility with devices and players. The common widely playable format is yuv420p.
This define the chroma subsampling.

-crf (constant rate factor) - Easily define the wight/quality ratio. the closer to zero the closer to source quality. usually we use a range between 15 and 25.

  • 0 : super heavy, match source quality (lossless).
  • 10 : heavy, great quality
  • 30 : light, bad quality

-preset - Determine encoding speed. A lower speed allow further optimisation to thin the file a bit more.
Available values are :

  • ultrafast
  • superfast
  • veryfast
  • faster
  • fast
  • medium (default)
  • slow
  • slower
  • veryslow

The command I use regularly to gain some disk space on my video use those 3 parameters:

Transcoding command

ffmpeg -i myvideo.mov -crf 20 -preset slower -pix_fmt yuv420p newvideo.mp4

Rescale

We often need to rescale a video, this affect the weight as lot !

Let’s consider we have a fullHD source (1080p : 1920x1080) that we whant to downscale to HD (1280x720).
The simplest method is to use a scale video filter : -vf scale=[new width]:-2

Rescale command

ffmpeg -i myvideo.mov -vf scale=1280:-2 -pix_fmt yuv420p newvideo.mp4

Here we input only the pixel width. the -2 determine automatically the height concerving the ratio.

Same as -1, -2 keep a ratio, but also enforce a value divisible par 2, that avoid potential errors.

If we aslready know the expected widthxheight it’s faster to type -s 1280x720.

Delete audio

With this exemple we can talk about stream copy.
Usually the file contain one video stream and one audio stream (sometimes more).
If you just need to remove audio you don’t have to re-encode since you can just copy chosen stream with -c.

-c copy : copy “all”, both video and audio stream

-c:v copy : copy video stream

-c:a copy : copy audio stream

-an : disable audio

Remove audio command

ffmpeg -i myvideo.mp4 -c:v copy -an myvideo_sansson.mp4

here we could have used -c copy -an since -an disable audio anyway.

More commands

There are a lot ! For exemple you can also cut a video:

ffmpeg -i myvideo.mp4 -ss [start] -to [end] myvideodecoupe.mp4

For exemple to start at 1min 10s you can either specify in seconds -ss 70 or, more usable, as a timecode -ss 00:01:10

A solution to quickly cut a video from will probably the subject of another post.

Il existe énormément d’autre commande que vous pourrez explorer dans la documentation officielle

For ressources about command, check the official documentation

And users around the world have been packing snippets of usefull commands.
Some links: