How to Convert a Video File to a Different Format using FFmpeg

26 October 2022 | 2 min read
Casper Kloppenburg

Prerequisites

FFmpeg is a free and open-source video editing tool capable of trimming, cropping, concatenating, muxing, and transcoding almost any type of media file you throw at it.

It's also a very robust solution for implementing video automation, as we use it extensively in our own video editing API. For this tutorial we'll use FFmpeg 5.1.2, but any recent version will do.

Converting to H.264 format (MP4)

Here's how to convert any video to H.264, a format that's mostly associated with MP4.

$ ffmpeg -i input.mp4 -c:v libx264 -pix_fmt yuv420p -c:a copy output.mp4
  • The -c:v libx264 argument tells FFmpeg to use H.264 video compression.
  • When the -pix_fmt yuv420p argument is specified, 4:2:0 subsampling is used, a widely used and supported pixel format.
  • When we use -c:a copy, no re-encoding is done on the audio track. It's simply copied to the output file. Alternatively, if you also want to convert the audio track to a modern audio format such as AAC, use -c:a aac.

Converting to VP9 format (WebM)

VP9 is a video format developed by Google that offers better video compression than H.264. Support for VP9 has greatly increased in recent years, making it a solid choice for online video.

Simply use the following command to convert any video to VP9:

$ ffmpeg -i input.mp4 -c:v libvpx-vp9 output.webm

Supported video formats by FFmpeg

For a list of all the video formats that FFmpeg supports, use the following command:

$ ffmpeg -codecs

Simply pass the codec to FFmpeg with the -c:v argument to convert it to one of the formats. For example, here's how to convert a video to MPEG-4:

$ ffmpeg -i input.mp4 -c:v mpeg4 output.mp4

Converting a video to MP3

The following command can be used to extract audio from a video:

$ ffmpeg -i input.mp4 output.mp3

Converting a video to GIF

The following article covers the process of making a GIF from a video using FFmpeg: How to Make a GIF from a Video using FFmpeg.

Start automating today

Start with a full-featured trial with 50 credits, no credit card required.
Get started for free