Version 16 (modified by darkmoon, 3 years ago) (diff)

To reproduce, download YouTube posted video with a streaming media capture utility.

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 -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

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

Alternative Encoding Options

YouTube provides a guide to Advanced encoding settings which, if certain recommendations are followed, will produce a video that will not be recoded after it is uploaded. (To verify that this works on YouTube, download your posted video with a streaming media capture utility such as Orbit Downloader or the DownloadHelper? add-on for Firefox, and then compare it to your original video. Using the download link in YouTube's control panel produces a recoded video that is not what you uploaded.) More than likely, some of the recommendations have to do with Adobe's Flash player, so these may also work for other video sharing sites.

The crucial options appear to be the container format (MP4) and options, the video and audio codecs, and the right profile for each codec (but with additional restrictions on the video stream). Video frame rates, resolutions, and aspect ratios, audio sample rates, and bit rates for video and audio are not crucial, so long as the video and audio codecs will encode them. YouTube, at least, supports VBR video and audio and, with the latest Flash Player versions, Variable Frame Rate video.

Note: all the following options to FFMPEG control the output file that will be uploaded. They must all follow the list of input sources on the command line.

  • MP4 container format
    • MOOV atom at the front of the file (Fast Start)
      • FFMPEG option: -movflags faststart
  • Video: H.264 AVC (FFMPEG option: -c:v libx264)
    • Progressive scan (not interlaced)
      • Detect interlaced video (including field order)
        • MediaInfo
        • FFMPEG's idet filter (adjust -ss and -frames:v to taste):
          ffmpeg -y -ss 00:03:00 -i INPUT -an -frames:v 200 -vf idet -f rawvideo /dev/null  #or NUL for Windows
          idet will output stats to your console just before FFMPEG exits.
      • FFMPEG de-interlacing options:
      • FFMPEG Filtering Guide
    • Profile: High
      • FFMPEG option: -preset veryfast
        libx264 will automatically set the profile to High. Selecting a preset faster than veryfast is not recommended.
    • Consecutive B-frames: 2
      • Consecutive B-frames must be limited to 2, regardless of other libx264 options.
        • FFMPEG option: -bf 2
    • Closed GOP, approximately half the video frame rate
      • The default, 12, works for 24-25 fps. For NTSC and 30 fps, select 15.
        • FFMPEG option: -g
    • Chroma subsampling: 4:2:0
      • Translation: yuv420p
        • Check input file pixel format:
          ffprobe -select_streams v -show_streams InputFile
        • FFMPEG option: -pix_fmt yuv420p

Some examples:

Inverse telecine a NTSC DVD rip (output video framerate will be "ntsc-film", 24000/1001):

ffmpeg -i input.mpg -strict experimental -c:a aac -b:a 128k -vf "fieldmatch=combmatch=full:mode=pc_n_ub, yadif=deint=interlaced, decimate" -pix_fmt yuv420p -c:v libx264 -crf 18 -preset slow -tune film -bf 2 -movflags faststart -shortest output.mp4

Same thing, but just de-interlace it (output video framerate will be "ntsc", 30000/1001):

ffmpeg -i input.mpg -strict experimental -c:a aac -b:a 128k -vf yadif -pix_fmt yuv420p -c:v libx264 -crf 18 -preset slow -tune film -g 15 -bf 2 -movflags faststart -shortest output.mp4


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.

Attachments (2)

Download all attachments as: .zip