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.

https://support.google.com/youtube/answer/58134?hl=en&rd=1

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

ffmpeg -i input.mov -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.

inputFile=input.mov; 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:

#!/bin/bash

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

open "$1"_youtube_crf21.mov

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 youtubehigh.sh

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:

youtubehigh.sh myvideo.mov

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

Further reading:

https://trac.ffmpeg.org/wiki/Encode/YouTube

https://trac.ffmpeg.org/wiki/Encode/H.264