Using FFMPEG to Convert Cudio and Video Formats

FFMPEG is a powerful open-source tool you can use to handle various multimedia files. In this tutorial we will outline the basic functionality of FFMPEG, however, it is important to consider that this is only a small portion of the FFMPEG project. FFMPEG can be downloaded from the official FFMPEG website and it can also be downloaded using a package manager such as APT.

Multimedia files which FFMPEG is able to process typically consist of a container with audio and video streams. Certain types of containers have different capabilities, such as storing subtitles or more than one audio stream inside a container, as well as a range of other types of metadata such as chapters. This is particularly useful when a video has multiple audio tracks in different languages as this can enable switching between them. An example of a container with such capabilities is Matroska or MKV.

Video and audio files are encoded using a codec such as H.264 for video or AAC for audio. Each codec presents a range of advantages and disadvantages, for instance in terms of audio, a useful codec is FLAC, which stands for “Free Lossless Audio Codec”, and it is particularly good for storing lossless audio of very high quality. A significant advantage of the H.264 codec is that it was designed to provide the transmission of high quality video with less bandwidth over the internet. Due to it’s good compression capabilities, it is a good choice in terms of the overall output size of the converted video file when compared to other codecs such as MPEG-4.

Conversion

The command line interface of FFMPEG is quite intuitive and it is relatively simple to convert an audio or video file, though depending on the size of the file there may be a difference in the actual time taken when converting from one format to another. Let’s say that we would like to convert a WAV file, we can easily do that by issuing the following command:

ffmpeg -i audio.wav audio.mp3

From the perspective of FFMPEG, this involves converting a Waveform Audio File which is an audio standard developed by Microsoft, into an MP3 stream. This particular conversion can reduce the original file size. Regarding the container selection in this particular example, FFMPEG selects the codecs for you.

As you may have already guessed, regarding the conversion of a video file it is a similar procedure.

If you would like to convert a video file using Microsoft’s format Audio Video Interleave or AVI into an MP4 file, use the following command:

ffmpeg -i video.avi video.mp4

It is important to note that FFMPEG may not always be able to guess the container format correctly, especially when using a less commonly used format such as MKV, which is designed to contain various types of streams, therefore when proceeding with such a conversion, you may have the same input file stream types in the output file, unless you specify the exact parameters. You can find these in the official FFMPEG documentation. Link

Codecs
When converting video files and using containers such as Matroska, which are able to contain various types of streams, it is good to know how to set specific types of containers.

A good command to know is the following:

ffmpeg -codecs

The output of this command will print every codec which FFMPEG is able to use.

Let’s assume you would like to convert a video file, issue the following command:

ffmpeg -i video.ts -vcodec libx264 -codec:a copy video.mp4

From the perspective of FFMPEG, it is converting a video file using the MPEG Transport Stream container into an MPEG-4 container. The -vcodec flag ensures that the libx264 or H264 codec is being used and the flag -codec:a copy is copying the original source audio stream into the newly converted file.

Video Resolution and Quality

FFMPEG is able to adjust the bitrate of an audio and video file, this plays an important role in the overall quality of the media file in question. Higher bitrate may also present higher bandwidth usage, especially when streaming a video or a high file size when storing a video file. The resolution may also be important, especially when deciding between standard definition and high definition video.

The flag to use for this is -b it is possible to set this both for video and audio bitrate.

To set the bitrate of each stream, you use the -b flag, which works in a similar fashion to the -c flag, except instead of codec options you set a bitrate.

For example, to change the bitrate of the video you would use it like this:

ffmpeg -i video.ts -vcodec libx264 -b:v 1000k -codec:a aac -b:a 128k -s 1280x720 video.mp4

This particular command sets the video codec as libx264 or H264, it sets the video bitrate to 1000 Kbit/s per second, furthermore it sets aac as the audio codec with a bitrate of 128 Kbit/s. The flag -s scales the video to a resolution of 1280×720 which is HD.

Debugging

Sometimes certain video files may contain errors which can be caused by a range of factors, such as an unreliable source of the video or an error when previously converting. A useful tool to see such errors is FFProbe.

Here is an example of usage:

ffprobe -i video.mp4

The original author: Jonáš, Junior Linux Admin, cloudinfrastack