wiki:

Encode

/

YouTube


Version 6 (modified by lithosza, 5 years ago) (diff)

--

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

Create a video from lossless audio file ('audio.flac') and a still image ('720.png'): (Please note that the image needs to be 1280x720 or 1920x1080). The advantage of this method is that transcoding happens only once to the audio.

ffmpeg -loop 1 -i 720p.png -i audio.flac -shortest -c:v libx264 -preset medium -tune stillimage -c:a pcm_s16le -r 24000/1001 -pix_fmt yuv420p -crf 18 output.avi

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, or YouTube is only showing gray video. Now what?

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

Attachments (2)

Download all attachments as: .zip