How to compress video for youtube using the free and fast ffmpeg.

There is little doubt that ffmpeg is one of the most powerful and efficient video conversion tools on the market. Rumor has it that google uses a version ffmpeg to compress its youtube videos on its servers. It is however a command line application, this can be a scary word, but do not worry this is a step by step how to.

Why convert?, Of course if you can get the format right on upload but you stand to lose the least amount of quality with a given file size if it is done right. The added plus youtube will not complain that it is in the wrong format.

todo: test if youtube prefers 30fps or 27.97fps.

Aparrently 30fps (and 24, 25, 50 and 60) is preferred.

These are the ffmpeg settings I use for upload to youtube:

ffmpeg -i -c:v libx264 -preset slow -crf 18 -c:a copy -pix_fmt yuv420p -movflags faststart output.mkv

(put time in front of this to benchmark stuff)

A little more advanced.; ffmpeg -i $inputFile -c:v libx264 -preset slow -crf 18 -c:a copy -pix_fmt yuv420p -movflags faststart $inputFile.mkv; open $inputFile.mkv

Apparently you will not notice any loss in quality at this crf setting of 18.

I use a crf of 21 for larger videos where absolute quality is not required.

This is one of the shell scripts that I use before I upload my videos to youtube:


time ffmpeg -i "$1" -c:v libx264 -preset slow -crf 21 -c:a copy -pix_fmt yuv420p -movflags faststart "$1"

open "$1"

If you want to install this as an easy to use script here is what you need to do:

Download the script files at the bottom of the page.

Open up terminal.

cd ~
mkdir scripts
open scripts

This will open the new folder in finder, drag the files from your download directory to this folder.

cd scripts
chmod +x

add the scripts folder to your profile:

cd ~
open .profile

Edit in text edit and add ~/scripts to your path so it looks something like this:

# MacPorts Installer addition on 2012-11-20_at_14:09:39: adding an appropriate PATH variable for use with MacPorts.
export PATH=/opt/local/bin:/opt/local/sbin:~/scripts:$PATH
# Finished adapting your PATH environment variable for use with MacPorts.

If for some reason you do not have a .profile file (perhaps you do not have macports) just type touch .profile and this single line to it:

 export PATH=~/scripts:$PATH

If all has gone well you should be able to navigate to the folder that has your video file and type:

Once finished the script will print out how long the task took to complete and open the file for you to enjoy.

Further reading: