Version 9 (modified by llogan, 5 years ago) (diff)

remove superflous examples and add better filtering example

How to Encode Videos for YouTube and other Video Sharing Sites

Since YouTube, Vimeo, and other similar sites will re-encode anything you give it the best practice is to provide the highest quality video that is practical for you to upload. Uploading the original content is the first recommendation, but this is not always a choice due to file size or bandwidth limitations, so re-encoding may be required. This guide will show you how to create a high quality video using ffmpeg.

First read the FFmpeg and x264 Encoding Guide; specifically the section on CRF. The examples below use the same method as shown in the encoding guide but optimized for YouTube.

Basic Examples

Re-encode the video and stream copy the audio. The output should be a similar quality as the input and will hopefully be a more manageable size.

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

Same as above, but also re-encode the audio instead of stream copying it:

ffmpeg -i -c:v libx264 -preset slow -crf 18 -c:a libvorbis -q:a 5 -pix_fmt yuv420p output.mkv

Create a video with a still image (input.png) and an audio file (audio.ogg):

ffmpeg -loop 1 -r 2 -i input.png -i audio.ogg -c:v libx264 -preset medium -tune stillimage -crf 18 -c:a copy -shortest -pix_fmt yuv420p output.mkv

Using filters

You can use filters to create effects and to add text. This example will use the avectorscope, showspectrum, and showwaves filters to create effects, the overlay filter to place each effect, and the drawtext filter to add text:

Effects for audio

ffmpeg -i input.mp3 -filter_complex \
"[0:a]avectorscope=s=640x518,pad=1280:720[vs]; \
 [0:a]showspectrum=mode=separate:color=intensity:scale=cbrt:s=640x518[ss]; \
 [0:a]showwaves=s=1280x202:mode=line[sw]; \
 [vs][ss]overlay=w[bg]; \
 [bg][sw]overlay=0:H-h,drawtext=fontfile=/usr/share/fonts/TTF/Vera.ttf:fontcolor=white:x=10:y=10:text='\"Song Title\" by Artist'[out]" \
-map "[out]" -map 0:a -c:v libx264 -preset fast -crf 18 -c:a copy output.mkv


Encoding is too slow. What should I do?

Use a faster -preset value. See the FFmpeg and x264 Encoding Guide for more information.

My video colors are messed up in my computer's media player or the video sharing site I uploaded my video to. Now what?

Add -pix_fmt yuv420p as an output option. Most (or perhaps all) non-FFmpeg based players do not support proper decoding of YUV 4:2:2 or YUV 4:4:4. YouTube works however.

Attachments (2)

Download all attachments as: .zip