How to Use the FFmpeg mestimate Filter
The FFmpeg mestimate (motion estimation) filter is a
powerful tool used to estimate and reconstruct motion vectors between
video frames. This article provides a quick overview and a
straightforward guide on how to use the mestimate filter,
explaining its key parameters, basic syntax, and how to visualize the
estimated motion vectors in your video processing workflows.
Understanding the mestimate Filter
The mestimate filter calculates motion vectors by
comparing macroblocks between consecutive frames. Rather than just using
the motion vectors already encoded in a compressed video stream,
mestimate allows you to force a recalculation of motion
using specific search algorithms. This is highly useful for video
analysis, custom encoding preparations, and research.
Basic Syntax
The basic syntax for the mestimate filter is:
ffmpeg -i input.mp4 -vf "mestimate=method=esa:mb_size=16:search_param=7" output.mp4Key Parameters of mestimate
You can customize the behavior of the motion estimation process using the following parameters:
method: Specifies the motion estimation search method. Available options include:esa: Exhaustive search algorithm (slowest, most accurate).tss: Three-step search algorithm.tdls: Two-dimensional logarithmic search algorithm.ntss: New three-step search algorithm.fss: Four-step search algorithm.ds: Diamond search algorithm (fast).hex: Hexagon-based search algorithm.epzs: Enhanced predictive zonal search algorithm (default).umh: Uneven multi-hexagon search algorithm.
mb_size: Sets the macroblock size. The default value is16(16x16 pixels).search_param: Sets the search parameter radius. The default value is7.
Practical Examples
1. Basic Motion Estimation
To apply motion estimation using the default EPZS method on an input video, run:
ffmpeg -i input.mp4 -vf "mestimate" output.mp42. Using Exhaustive Search for High Accuracy
If you require high-accuracy motion estimation and do not mind longer
processing times, use the Exhaustive Search Algorithm
(esa):
ffmpeg -i input.mp4 -vf "mestimate=method=esa:mb_size=16:search_param=15" output.mp43. Visualizing Motion Vectors
The mestimate filter calculates motion vectors and
attaches them as side data to the video frames. To actually see these
vectors drawn on top of your video, you must pair mestimate
with the codecview filter.
Run the following command to calculate and visualize forward predicted motion vectors:
ffmpeg -i input.mp4 -vf "mestimate,codecview=mv=pf" output.mp4mv=pf: Visualizes forward predicted P-frame motion vectors.mv=bf: Visualizes forward predicted B-frame motion vectors.mv=bb: Visualizes backward predicted B-frame motion vectors.
You can combine them to visualize all vectors at once:
ffmpeg -i input.mp4 -vf "mestimate,codecview=mv=pf+bf+bb" output.mp4