How to: Prepare video for YouTube with FFmpeg

When preparing media for Youtube, it's best to stick with their recommended settings. This command below resizes, adjusts frame rate (optional), and encodes your video to a new MP4 with specific settings for quality and compatibility.

19 days ago   •   2 min read

By Tom O'Brien
If you need help installing FFmpeg on your system, check out this earlier post which will walk you through the process.

Let's take a look at Youtube's recommended settings for uploading media to their platform. From here we can apply these settings with FFmpeg.

ffmpeg \                # Calling the binary
-i input.mp4 \          # Input video file
-r 30000/1001 \         # Set the frame rate - optional
-vf scale="1920:1080" \ # Resize video - optional
-codec:v libx264 \      # X264 Video Codec
-crf 21 \               # Video Quality
-bf 2 \                 # Maximum 2 B-frames as per guideline
-flags +cgop \          # Closed GOP as per guideline
-pix_fmt yuv420p \      # Chroma subsampling 4:2:0 as per guideline
-c:a aac \              # Fraunhofer FDK AAC codec library
-b:a 128k \             # Audio Bitrate
-ac 2 \                 # Audio channels
-r:a 44100 \            # Audio samplerate
-map 0:v:0 \            # First file : video : first track
-map 0:a:0 \            # First file : audio : first track 
-movflags faststart \   # Put MOOV atom at the front of the file

Command Breakdown: Converting video with specific settings

This FFmpeg command takes an MP4 video (input.mp4) and converts it to another MP4 file (output.mp4) with some specific settings:

Input and Output:

  • -i input.mp4: This tells FFmpeg to use the file named "input.mp4" as the source video.
  • output.mp4: This specifies the name of the output file, which will also be in MP4 format.

Video Processing (Optional):

  • -r 30000/1001: This option is used to set the frame rate of the output video to 30 frames per second (fps). This is optional, and if omitted, the original frame rate will be kept.
  • -vf scale="1920:1080": This option resizes the video to a resolution of 1920x1080 pixels. This is also optional, and the video will remain its original size if not specified.

Video Encoding:

  • -codec:v libx264: This specifies the video codec used for encoding. Here, libx264 is used, which is a popular choice for high-quality H.264 video encoding.
  • -crf 21: This sets the Constant Rate Factor (CRF) for video quality. Lower values result in higher quality (larger file size), and higher values result in lower quality (smaller file size). 21 is a commonly used value for good quality videos.
  • -bf 2: This sets the maximum number of B-frames to 2. B-frames are used for better compression, but using too many can affect compatibility with some players. Following encoding guidelines, this is set to 2.
  • -flags +cgop: This enables Closed GOP (Group of Pictures) encoding. Following guidelines, this is enabled for better compatibility with some players.
  • -pix_fmt yuv420p: This sets the pixel format to YUV 4:2:0, which is a common color space used for video compression. Following guidelines, this format is chosen.

Audio Encoding:

  • -c:a aac: This specifies the audio codec used for encoding. Here, aac is used, which is a popular and efficient audio codec.
  • -b:a 128k: This sets the audio bitrate to 128 kilobits per second (kbps). This is a common choice for good quality audio.
  • -ac 2: This sets the number of audio channels to 2 (stereo).
  • -r:a 44100: This sets the audio sample rate to 44.1 kHz, which is a standard audio sample rate.

Mapping Streams (Optional):

  • -map 0:v:0: This tells FFmpeg to copy the first video stream (stream 0) from the input file (indicated by the "0") to the output file.
  • -map 0:a:0: Similar to the previous option, this copies the first audio stream (stream 0) from the input file to the output file.

Output Optimization:

  • -movflags faststart: This places the MOOV atom (which contains essential information about the video) at the beginning of the file. This can improve seeking performance in some players.

Need some help with online video delivery, storage and processing? Get in touch with us using the button below and we'll be happy to help you out.

Spread the word

Keep reading