Youtube recommended encoding settings for FFMpeg

Apply Youtube recommended encoding with FFMpeg

Youtube has an official encoding guide that explains how you should encode your videos for best performance on Youtube.

We can use FFMpeg to comply with these recommendations. Here is an explanation of which FFMpeg settings we need.

-c:v libx264 Sets the encoder to x264 (x264 is an open source h.264 encoder)

-preset slow A slower preset will provide better compression. The general guideline is to use the slowest preset that you have patience for.Current presets in descending order of speed are: ultrafast,superfast, veryfast, faster, fast, medium, slow, slower, veryslow, placebo.

-crf 18 Constant Rate Factor: The range of the quantizer scale is 0-51: where 0 is lossless, 23 is default, and 51 is worst possible. A lower value is a higher quality and a subjectively sane range is 18-28. Consider 18 to be visually lossless or nearly so..

-c:a aac Converts audio to AAC-LC

-ar 48000 Sets the sampling frequency/sample rate to 48kHz

-ac 2 Creates a stereo audio track even if the audio is mono or multi channel

-profile:v high -level 4.0 High Profile

-bf 2 2 consecutive B frames. FFMPEG’s default is 16.

-coder 1 CABAC. By default CABAC is the entropy encoder used by x264.

-pix_fmt yuv420p Sets the aspect ratio to 16:9

-b:v 10M Sets the bitrate to 10Mbps. This is good if the fps is 24,25 or 30. Set this to 15M if the video is 48, 50 or 60 fps. More here.

-threads 4 Enables ffmpeg to use 4 threads for multi core CPUs

-cpu-used 0 Most CPU intensive, but best quality. Uses all cores.

-r 30 Limits the frame rate to 30 fps.

-b:a 320k Sets the audio bitrate to 320kbps.

Result:

ffmpeg -i input.ext -c:v libx264 -preset slow -crf 18 -c:a aac -ar 48000 -ac 2 -profile:v high -level 4.0 -bf 2 -coder 1 -pix_fmt yuv420p -b:v 10M -threads 4 -cpu-used 0 -r 30 -b:a 320k output.ext

Making a button in Directory Opus

We can create a button for this in directory opus by displaying dialogs for certain variables.

The result is something like this:

ffmpeg -i {filepath$} -c:v libx264 -preset slow -crf 18 -c:a aac -ar 48000 -ac 2 -profile:v high -level 4.0 -bf 2 -coder 1 -pix_fmt yuv420p -b:v 10M -threads 4 -cpu-used 0 -r 30 -b:a 320k {destpath$}new-{file$|ext=mp4}

Doing the same in a batch script:

for %%a in ("*.mp4") do ffmpeg -i "%%a" -c:v libx264  -vf scale=1920:1080 -preset slow -crf 18 -profile:v high -level 4.0 -bf 2 -coder 1 -pix_fmt yuv420p -b:v 10M -threads 4 -cpu-used 0 -r 30 -an "new-%%~na.mp4"

Sources:

https://superuser.com/questions/866798/what-ffmpeg-command-line-matches-the-one-youtube-uses

https://support.google.com/youtube/answer/1722171?hl=en

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

https://ffmpeg.org/ffmpeg.html

https://superuser.com/questions/319542/how-to-specify-audio-and-video-bitrate