How to Encode Videos for YouTube, Facebook, Vimeo, twitch, and other Video Sharing Sites
Contents
Because 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 the file size or format, 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 using AAC instead of stream copying it:
ffmpeg -i input.mov -c:v libx264 -preset slow -crf 18 -c:a aac -b:a 192k -pix_fmt yuv420p output.mkv
Create a video with a still image (input.png) and an audio file (audio.m4a):
ffmpeg -loop 1 -framerate 2 -i input.png -i audio.m4a -c:v libx264 -preset medium -tune stillimage -crf 18 -c:a copy -shortest -pix_fmt yuv420p output.mkv
Live Streaming
See Encoding for Streaming Sites.
faststart for MP4/M4V/MOV files
If uploading MP4/M4V/MOV files YouTube recommends that the moov atom should be at the beginning of the file. You can do this with the following option:
-movflags +faststart
If you already have a MP4/M4V/MOV file and want to "faststart" it then you can do so without needing to re-encode by using stream copy mode:
ffmpeg -i input.mp4 -c copy -movflags +faststart output.mp4
Note: For other formats, such as Matroska (MKV), -movflags +faststart is not necessary and will be ignored.
Utilizing the included album cover/art
Some music files contain the album cover.
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:
ffmpeg -i input.mp3 -filter_complex \ "[0:a]avectorscope=s=640x518[left]; \ [0:a]showspectrum=mode=separate:color=intensity:scale=cbrt:s=640x518[right]; \ [0:a]showwaves=s=1280x202:mode=line[bottom]; \ [left][right]hstack[top]; \ [top][bottom]vstack,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, but note that this will increase the file size when using CRF mode. 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)
-
musicfilter.jpg
(26.1 KB) -
added by llogan 7 years ago.
Effects for audio
-
album.jpg
(18.1 KB) -
added by llogan 7 years ago.
fake album example
Download all attachments as: .zip