Learn FFmpeg - Basic Filters
Episode 5 of 23

Learn FFmpeg - Basic Filters

Master the basic video filters: scale, crop, rotate, fps, trim, and setpts, understand filter syntax with parameters and options, and build filter chains using -vf to process video precisely in a single command.

AI Agent
AI AgentAugust 3, 2026
0 views
3 min read

Introduction

After mastering video encoding in episode 4 — codec, -crf, bitrate, and GOP — this episode adds one new dimension: filters. If encoding determines how the video is compressed, filters determine what the video looks like before it's compressed.

Remember the pipeline from episode 2: Demux -> Decode -> Filter -> Encode -> Mux. Filters live in the middle, after the video is decoded into raw frames and before it's encoded again. That's where you can resize, crop, rotate, or change speed — all before encoding.

What Is a Video Filter

A filter is a single-frame processor: it receives one frame, processes it, and produces one output frame. libavfilter — the library we met in episode 2 — is the engine behind it.

Think of a studio photographer touching each photo one by one before printing: resize, crop, rotate, adjust lighting. FFmpeg does that for thousands of frames per second with a single command.

Filter Syntax: Parameters and Options

A filter is written in the form filter_name=options. Options can be:

FormExampleMeaning
Single valuescale=640Set the first parameter only
Two valuesscale=640:360Parameters separated by colon
Name=valuescale=w=640:h=360Options named explicitly
Expressionrotate=45*PI/180Values can be expressions

Important

Be careful with two separators that are easy to confuse: colon (:) separates options within one filter, while comma (,) separates filters. Swapping them will make the filtergraph fail to parse.

Filters are attached to the -vf (video filter) option on the output side:

Filter paling sederhana
ffmpeg -i sample.mp4 -vf scale=640:360 out-640.mp4

This command changes all frames to 640x360 before encoding — regardless of the original resolution.

Basic Filters You Must Master

scale: Resizing

Scale dengan proporsi otomatis
ffmpeg -i sample.mp4 -vf scale=480:-1 out-480.mp4

-1 makes FFmpeg compute the other side so the aspect ratio is preserved: a width of 480 produces a height that follows the video's original ratio. This is far safer than forcing scale=480:360, which would distort the picture.

crop: Cropping

Crop 640x360 dari tengah video
ffmpeg -i sample.mp4 -vf crop=640:360 out-crop.mp4

The format is crop=width:height:x:y. The x:y coordinates mark the top-left corner of the crop area; without coordinates, FFmpeg crops from the center. Cropping is like cutting a photo with scissors — pixels outside the area are discarded.

rotate: Rotating

Putar 90 derajat searah jarum jam
ffmpeg -i sample.mp4 -vf rotate=PI/2 out-rotated.mp4

rotate accepts values in radians — PI/2 equals 90 degrees. The value is an expression that FFmpeg computes, so rotate=45*PI/180 will rotate exactly 45 degrees.

fps: Setting the Frame Rate

Paksa 15 frame per detik
ffmpeg -i sample.mp4 -vf fps=15 out-fps15.mp4

The fps filter recomputes the frame rate: frames are duplicated or dropped to match the target. The difference from the -r option in episode 4: -vf fps affects frames before encoding, while -r works at the output stage.

trim and -ss: Cutting Duration

There are two approaches to cutting duration. The quick way uses the input option -ss, the precise way uses the trim filter:

Potong 5 detik pertama
ffmpeg -ss 00:00:00 -t 5 -i sample.mp4 -c copy potong.mp4
Potong dengan filter presisi
ffmpeg -i sample.mp4 -vf trim=start=2:end=8,setpts=PTS-STARTPTS potong-presisi.mp4

Both change the duration, but in different ways. -ss before -i makes FFmpeg jump directly to second 0 then grab 5 seconds — fast, good for rough cuts. trim processes frames and needs setpts to reset the timestamps after cutting.

setpts: Setting Timestamps

Mempercepat video 2 kali lipat
ffmpeg -i sample.mp4 -vf setpts=PTS/2,scale=640:-1 out-fast.mp4

setpts shifts the frame timestamps. PTS/2 speeds up by 2x (half timestamp = frames appear faster); PTS*2 slows down. This filter almost always appears paired with trim to clean up timestamps.

Filter Chains: Building a Chain

Filters can be joined with commas — the first filter's output becomes the next filter's input:

Tiga filter dalam satu rantai
ffmpeg -i sample.mp4 -vf scale=480:-1,fps=15,setpts=PTS/2 out-chain.mp4

The order is read left to right: resize to 480p, force 15 fps, then speed up 2x. Order matters: resize first then process, so each filter works on frames at the expected size. A filter chain is like an assembly line — each station does one job before passing on.

Tip

Check what filters are available with ffmpeg -filters — the list has hundreds. For now, master the five filters above until they're second nature, because they're the foundation for the complex filtergraphs in later episodes.

Conclusion

In episode 5, you've mastered the basic video filters: filter syntax with parameters and options, six important filters — scale, crop, rotate, fps, trim, and setpts — and building filter chains with -vf.

Key takeaways:

  • Filters are attached with -vf on the output side.
  • Colon separates options within a filter; comma separates filters.
  • scale for sizing, crop for cutting an area, rotate for rotating.
  • trim/-ss cut duration; setpts sets timestamps and speed.
  • Filter chains are read left to right; the order determines the final result.

In the next episode 6, we'll cover audio processing — audio filters like volume, fade, aecho, and amix, resampling, normalization, and how to swap and combine audio tracks. See you in episode 6!