Converting Video and Audio Formats Using FFmpeg
Contents
Media files come in various formats and codecs. Video files can be in formats like MKV, MP4, MOV, and WebM, while audio files come in formats like MP3, WAV, AAC, etc. A media file's format can influence its size, quality, functionality, and compatibility across different platforms. Therefore, there is always a need to convert media files from one format to another.
FFmpeg is a popular and powerful command-line tool for manipulating media files programmatically, including converting file formats. If you’re going to use it for building a video or audio editing tool, you should consider adding the functionality of converting file formats too as it is supported by FFmpeg. In this article, we’ll explore how to use FFmpeg to convert media file formats, with examples like converting MKV to MP4, MOV to MP4, MP4 to MP3, and more.
What is FFmpeg
FFmpeg is a complete, cross-platform solution to record, convert, and stream audio and video. It can decode, encode, transcode, mux, demux, stream, filter, and play media files in any format. It is also highly portable—it compiles and runs in a wide variety of build environments, machine architectures, and configurations like Linux, Mac OS X, Microsoft Windows, etc.
FFmpeg contains multiple tools for end-users to convert, play, and analyze media files and libraries for developers to use in different applications, such as libavcodec, libavutil, libavformat, libavfilter, libavdevice, libswscale, and libswresample. When you download the FFmpeg packages/executable files to your machine, these libraries will be downloaded automatically.
Installing FFmpeg
Before we begin, make sure to download FFmpeg from the official website. You can follow this step-by-step guide to install FFmpeg on your Mac, Windows, or Ubuntu/Linux.
Note: This article is based on FFmpeg v6.0.
Basic Command
To convert a media file from one format to another, you can use a simple command with the structure below:
ffmpeg -i [input_file_name] [output_file_name]
The argument -i
specifies the input file. Following the input file name is the output file name and it can be any file extension type (e.g. .mkv
, .mp4
, .mp3
). FFmpeg will automatically detect the input format and convert it to the output format specified by the file extension.
Let’s look at some examples…
Converting MKV to MP4
MKV, also known as Matroska Video, is a multimedia container format that can hold an unlimited number of video, audio, picture, or subtitle tracks in one file. As MKV files have large storage, they’re often used to store high-definition content with lossless or high-bitrate codecs. However, MKV is not widely supported—it can’t be played on web players and is only supported by some local players, like VLC Player, KMPlayer, and MPC-HC.
Because of the limited support, converting MKV files to a more widely supported format like MP4 makes content distribution easier. To convert an MKV file to MP4 using FFmpeg, use the following command:
ffmpeg -i input.mkv output.mp4
You can add the -c copy
option in the command to tell FFmpeg to copy the video and audio streams from the input file to the output file without re-encoding them.
ffmpeg -i input.mkv -c copy output.mp4
This makes the conversion faster and ensures a lossless conversion.
Converting MOV to MP4
MOV is the filename extension for the QuickTime multimedia file format. Although MOV was initially developed for Apple’s Macs, it is now compatible with Windows 10 or above—it can be played natively using Microsoft Photos. That said, if you want to ensure compatibility on other platforms and devices, MP4, the most common video file format, is still a safer choice.
Besides that, MP4 is also known for its efficient compression, which can result in smaller file sizes. To convert a MOV file to MP4 using FFmpeg, use the following command:
ffmpeg -i input.mov output.mp4
Similarly, you can add the -c copy
option in your command to convert the MOV file to MP4 without re-encoding them.
Converting WebM to MP4
WebM is another popular video format. Its file structure is based on the Matroska container and consists of video streams compressed with the VP8 or VP9 video codecs and audio streams compressed with the Vorbis or Opus audio codecs. Although WebM is a subset of Matroska, it is designed for web use and streaming. Therefore, it is commonly used for HTML5 videos.
However, WebM videos can’t be played on some Apple devices natively. You need to install a third-party media player to play WebM videos, which makes it less convenient compared to MP4. To convert a WebM file to MP4 using FFmpeg, use the following command:
ffmpeg -i input.webm output.mp4
This will convert the WebM video to MP4 and increase its compatibility across various devices and platforms.
Converting MP4 to MP3
Besides converting video files from one format to another, you can also convert them to other media formats like MP3. This can be useful for repurposing content as only the audio will be extracted and saved to the output file. For example, the audio of a video file can be extracted and used as the content for a podcast.
To convert an MP4 file to MP3 or extract audio from a video file using FFmpeg, use the following command:
ffmpeg -i input.mp4 output.mp3
This will encode the audio with the default libmp3lame audio codec and save it as an MP3 file.
🐻 Bear Tips: Do you know visuals improve engagement by 180%? Consider adding a cover art for every podcast episode and automate it with Bannerbear.
Advanced Command
The commands above convert input files to specified file formats using the default options. You can add other options to the command to customize the output file further, like specifying the codec, frame rate, resolution, and more to adjust the output quality.
Changing Video/Audio Codec
A media file’s codec can affect its quality, size, and compatibility. FFmpeg supports a wide range of video and audio codecs and you can specify them in the command using the -c
option.
To specify the video codec, you can use the -vcodec
or -codec:v
/-c:v
option. For example, the command below uses the libx264
codec to encode the output video file:
ffmpeg -i input.mov -vcodec libx264 output.mp4
Similarly, you can specify the audio codec for an audio file using the -acodec
or -codec:a
/-c:a
option:
ffmpeg -i input.mp4 -acodec libshine output.mp3
You can also specify both video and audio codecs in the same command for video files that contain an audio stream:
ffmpeg -i input.mov -vcodec libx264 -acodec libshine ouput.mp4
Adjusting Video/Audio Quality
Many factors control the quality of a video/audio file and bitrate is one of them. Bitrate refers to the amount of data that can be transferred or processed within a certain amount of time. When the a media file has a high bitrate, it typically has a better quality (also results in a larger file size).
You can control a video file’s bitrate using the -b:v
option. The command below sets the video bitrate to 64 kbit/s:
ffmpeg -i input.mov -b:v 64k output.mp4
For audio files, use the -b:a
option:
ffmpeg -i input.mp4 -b:a 224k output.mp3
Conclusion
Choosing the right media file format is important to ensure that the content can be played on the target platforms and devices. It's important to keep in mind that each format has its advantages and disadvantages. Converting the media files to other formats not mentioned in this articles works similarly. If you would like to add the support of other formats, follow the same command structure and modify the input/output name. Happy building!