FFmpeg – Basic Filter Graphs

Introduction

Many people consider FFmpeg to be the Swiss Army knife of streaming and transcoding videos. This robust and adaptable instrument offers a wide array of functionalities for managing multimedia information, rendering it a crucial asset for individuals engaged in the processing of audio and video. FFmpeg provides the versatility and functionality required to carry out a wide range of tasks, from straightforward format conversions to intricate filter applications and streaming configurations, regardless of your level of experience.

The main feature of FFmpeg is its support for filter graphs, which let users combine multiple filters to create complex processing chains. You can perform a wide range of manipulations on video and audio streams using these filter graphs, including effects application, resizing, cropping, text or image overlay, and much more. Comprehending the fundamentals of filter graphs is essential to fully utilizing FFmpeg.

To show you what can be done with FFmpeg, we will begin this introduction with a few very simple examples. These examples will give you a basic understanding of how to accomplish common audio and video processing tasks using FFmpeg’s filter graphs. You will be more prepared to investigate and use the more sophisticated features and methods that this extraordinarily potent tool has to offer as you grow more acquainted with these fundamentals.

 

Format conversion

ffmpeg -y -i input.mkv output.mp4

In this simplest example, FFmpeg produces MP4 output from MKV input. -y denotes that we want to overwrite output.mp4 if it already exists. -i marks the input. In addition to these two, FFmpeg supports many other popular multimedia file formats, including MXF, AVI, WAV, M4A, JPG, PNG etc.

Codec conversion

ffmpeg -y -i input.mkv -vcodec libx264 -acodec flac output.mkv

It is common to use FFmpeg to transcode from one codec to another. Here, we specify the video and audio codecs with -vcodec and -acodec.

Take only video or audio

ffmpeg -y -i input.mkv -map 0:v output.mp4

-map 0:v selects the video streams from the first input (we have only one input file here anyway, marked by 0), while 0:a means the audio streams.

Resize the video

ffmpeg -y -i input.mkv -filter_complex scale=720:480 output.mp4

-filter_complex can be used to combine many filters together. Filters sit between the input and the output and make some change to the media flowing through them. Here we are using the scale filter and specifying the output width and height.

Clipping

ffmpeg -y -i input.mkv -ss 30.4 -to 40.15 output.mp4

Here, -ss and -to are used to specify the start and end times in seconds.

Snapshot at time

ffmpeg -y -i input.mkv -ss 2.598 -vframes 1 output.jpg

We can give a filename with .jpg extension, and -vframes 1 will tell FFmpeg to produce a single image at the specified time instance.

Thumbnails at interval

ffmpeg -y -i input.mp4 -filter_complex "fps=1/5,scale=320:180" thumbnail-%03d.jpg

The fps filter is used here to say that we need 1 frame every 5 seconds. We scale it to required dimensions and give a pattern for the thumbnail filename to be numbered as we like.

Extract audio channels

ffmpeg -y -i input.mxf \
-filter_complex "[0:a]asplit=2[a1][a2];[a1]pan=mono|c0=c0[ch1];[a2]pan=mono|c0=c1[ch2]" \
-map [ch1] channel_1.wav \
-map [ch2] channel_2.wav

Getting slightly complicated now, but the graph image above should make it easier to understand. We first fork the input audio with the asplit filter into two identical branches labeled a1 and a2. Then we connect each of these to pan filters that are each set up to produce one mono channel. The first pan filter will select the first channel (c0) from a1 and the second one will select the second channel (c1)from a2, and we label the outputs from these pan filters ch1 and ch2 respectively. Lastly, we map ch1 and ch2 to produce two separate WAV files.

Complex graphs with shared filters

In this final example, we try to reuse common filters and produce simultaneous outputs in parallel.

ffmpeg -y -i input.mkv \
-filter_complex "[0:v]format=yuv420p,yadif,split=3[in1][in2][in3];[in1]scale=1920:1080[hd];[in2]scale=720:576,hflip[sd];[in3]fps=1/5,scale=320:180[thumbnails];[0:a]aresample=48000,asplit=2[a1][a2]" \
-map [hd] -map [a1] hd.mov \
-map [sd] -map [a2] sd-flipped.mp4 \
-map [thumbnails] thumbnail-%03d.jpg

As shown above, we apply the common operations early in the graph and try to reuse the filters whenever possible. For example, we reuse formatyadifand aresample filters for the HD and SD outputs, but do the scaling for them separately since they require different dimensions. In addition, we do a horizontal flip with hflip for the SD output only, just for fun! On the other hand, the thumbnails share some common filters (deinterlacing with yadif) with the video filters, but then needs fps and scale to adjust the frame rate and size specific to thumbnails only.

Conclusion

With support for an extensive array of filters, FFmpeg is an immensely strong and adaptable tool that makes it possible to implement a wide range of sophisticated needs in audio and video processing. The aforementioned examples give you a basic understanding of FFmpeg’s functionality so you can begin using this powerful tool. FFmpeg’s rich feature set can assist users of all skill levels in accomplishing their media processing objectives.

You can build intricate processing chains to fulfill particular requirements, like transcoding, filtering, streaming, and more, by combining different filters. Because of its versatility, FFmpeg can be used for a wide range of tasks, from straightforward ones like file format conversion to more complex ones like noise reduction, effect addition, and platform-specific media optimization.

I suggest looking into additional learning resources for people who want to expand their knowledge and skill set. A succinct Udemy course on FFmpeg provides a thorough overview of its principles through clear illustrations and practical demonstration videos. This course is a great resource for anyone who is eager to fully utilize FFmpeg because it covers a wide range of real-world use cases and examples.

In conclusion, FFmpeg is a vital resource for anyone working with media processing. Its broad filter library and strong features give it the ability to handle almost any media-related task. You can use FFmpeg to produce professional-caliber media content by learning the fundamentals and keeping an eye out for new features.

Happy transcoding!

Hire Exceptional Developers Quickly

Share this blog on

Hire Your Software Development Team

Let us help you pull out your hassle recruiting potential software engineers and get the ultimate result exceeding your needs.

Contact Us Directly

Address:

Plot # 272, Lane # 3 (Eastern Road) DOHS Baridhara, Dhaka 1206

Talk to Us
Scroll to Top