wiki:

Encode

/

YouTube

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 H.264 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 input.mov -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 -framerate 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

Utilizing the included album cover/art

Some music files contain the album cover.

fake album example

You can see if your music file contains the album cover with ffmpeg. Look for a video stream:

$ ffmpeg -i input.mp3
...
  Duration: 00:04:09.70, start: 0.025056, bitrate: 243 kb/s
    Stream #0:0: Audio: mp3, 44100 Hz, stereo, s16p, 235 kb/s
    Stream #0:1: Video: mjpeg, yuvj420p(pc), 600x600 [SAR 1:1 DAR 1:1], 90k tbr, 90k tbn, 90k tbc

Or simply play it with ffplay and it will show the album cover and play the music:

ffplay -i input.mp3

Then you can use it as the video for your music that you want to upload to YouTube:

ffmpeg -i input.mp3 -c copy output.mkv

If your music file does not contain album art then see the Create a video with a still image example above.

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

FAQ

Encoding is too slow. What should I do?

Use a faster -preset value. See the H.264 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.

Last modified 20 months ago Last modified on Jun 22, 2015, 9:08:54 PM

Attachments (2)

Download all attachments as: .zip