How to Encode VP9 Video with FLAC Audio in FFmpeg

This article provides a direct guide and the exact FFmpeg commands needed to create a Matroska (MKV) file containing VP9 video and FLAC audio. You will learn how to write the command for transcoding existing media files, understand what each parameter does, and discover how to adjust quality settings for the best results.

The Standard FFmpeg Command

To transcode an input video and audio file into a VP9 video and FLAC audio stream wrapped in an MKV container, use the following command:

ffmpeg -i input.mp4 -c:v libvpx-vp9 -crf 30 -b:v 0 -c:a flac output.mkv

Parameter Breakdown

  • -i input.mp4: Specifies the path to your source input file. This can be almost any video format (MP4, MOV, AVI, etc.).
  • -c:v libvpx-vp9: Sets the video codec to VP9 using the libvpx-vp9 library.
  • -crf 30: Constant Rate Factor. This controls the video quality. The recommended range for VP9 is between 15 (best quality, larger file size) and 35 (lower quality, smaller file size). A value of 30 is a common starting point for 1080p video.
  • -b:v 0: This is a mandatory setting when using CRF mode with the VP9 encoder. It forces the encoder to use pure quality-based variable bitrate.
  • -c:a flac: Sets the audio codec to FLAC, ensuring lossless compression for your audio stream.
  • output.mkv: The name of the resulting file. The .mkv extension tells FFmpeg to package the streams into a Matroska container, which natively supports both VP9 and FLAC.

Advanced Adjustments

1. Controlling Audio Bit Depth

FLAC is lossless, but you can control its sample format (such as 16-bit or 24-bit) if your source file supports it. FFmpeg handles this automatically based on the input, but you can force 16-bit depth by adding the sample format flag:

ffmpeg -i input.mp4 -c:v libvpx-vp9 -crf 30 -b:v 0 -c:a flac -sample_fmt s16 output.mkv

2. Speed vs. Quality CPU Usage

The VP9 encoder is CPU-intensive. You can control the encoding speed using the -deadline or -cpu-used parameters.

ffmpeg -i input.mp4 -c:v libvpx-vp9 -crf 30 -b:v 0 -deadline good -cpu-used 2 -c:a flac output.mkv
  • -deadline good: Sets the quality deadline (choices are best, good, or realtime). "Good" is recommended for most use cases.
  • -cpu-used 2: Accepts values from 0 to 5. Higher numbers encode faster but result in slightly lower quality per bitrate.

3. Copying Streams Without Re-encoding (Remuxing)

If your source file already contains VP9 video and FLAC audio, you can package them into an MKV container instantly without re-encoding by copying the codecs:

ffmpeg -i input.webm -c:v copy -c:a copy output.mkv