wiki:

Encode

/

YouTube


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

add -pix_fmt yuv420p to examples

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 copy the audio. The output should be a similar quality as the input and will hopefully be a more manageable size.

ffmpeg -i input -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 copying it:

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

Examples with drawtext

Create a video with a colored background using the color video source and add text with the drawtext filter showing song title:

ffmpeg -f lavfi -i color=c=orange:s=1280x720:r=2 -i audio.ogg -c:v libx264 \
-preset medium -tune stillimage -crf 18 -vf "drawtext=fontfile=Vera.ttf: \
text='Song Title'" -c:a copy -shortest output.mkv

Note: fontfile requires the full path to the font file, such as /usr/share/fonts/TTF/Vera.ttf.

Same as above, but embiggen the text, color it yellow, and put it in a blue box with 80% opacity, and center it:

ffmpeg -f lavfi -i color=c=orange:s=1280x720:r=2 -i audio.wav -c:v libx264 \
-preset medium -tune stillimage -crf 18 -vf "drawtext=fontfile=Vera.ttf: \
fontsize=48: fontcolor=yellow@0.8: box=1: boxcolor=blue@0.8: text='Song Title': \
x=(w-text_w)/2: y=(h-text_h)/2" -c:a copy -shortest output.mkv

Position the text on the bottom right:

ffmpeg -f lavfi -i color=c=orange:s=1280x720:r=2 -i audio.wav -c:v libx264 \
-preset medium -tune stillimage -crf 18 -vf "drawtext=fontfile=Vera.ttf: \
text='Song Title': x=w-text_w-line_h: y=h-text_h-line_h" -c:a copy -shortest output.mkv

Same as above, and use another drawtext to add author name:

ffmpeg -f lavfi -i color=c=orange:s=1280x720:r=2 -i audio.wav -c:v libx264 \
-preset medium -tune stillimage -crf 18 -vf "drawtext=fontfile=Vera.ttf: \
text='Song Title': x=w-text_w-ascent: y=h-text_h-(line_h*2),drawtext=fontfile=Vera.ttf: \
text='Artist Name': x=w-text_w-ascent: y=h-text_h-line_h" -c:a copy -shortest output.mkv

Use the mandelbrot filter instead of a boring colored background:

ffmpeg -f lavfi -i mandelbrot=s=1280x720:r=ntsc -i audio.mp3 -c:v libx264 \
-preset medium -crf 18 -vf "drawtext=fontfile=Vera.ttf:text='Song Title'" \
-c:a copy -shortest output.mkv

Other interesting video sources to try include cellauto, mptestsrc, and life. See Fancy Filtering Examples.

FAQ

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. Now what?

Try adding -pix_fmt yuv420p as an output option although YouTube will probably re-encode the video properly without this option.

Attachments (2)

Download all attachments as: .zip