Stabilize Shaky Video with FFmpeg Vidstab on Linux
This guide provides a straightforward, step-by-step walkthrough for
stabilizing shaky video footage on Linux using the powerful
vidstab filter extension in FFmpeg. You will learn how to
check for library support, generate stabilization vectors in a first
pass, and apply those vectors to transform your shaky camera work into
smooth, professional-looking video in a second pass.
Checking for Vidstab Support
Before diving into the stabilization process, you need to ensure your
installation of FFmpeg was compiled with the vidstab
libraries enabled. Open your Linux terminal and run the following
command:
ffmpeg -filters | grep vidstabIf your installation supports it, you will see output listing
vidstabdetect and vidstabtransform. If no
results appear, you may need to install a version of FFmpeg that
includes these libraries from your distribution’s repository or a
trusted third-party package manager.
Step 1: The Analysis Pass (Two-Pass Stabilization)
High-quality video stabilization requires a two-pass process. In the
first pass, the vidstabdetect filter analyzes the video
frame by frame to identify camera shakiness and writes this tracking
data to a temporary file (usually named
transforms.trf).
Run the following command in your terminal:
ffmpeg -i input.mp4 -vf vidstabdetect=shakiness=5:accuracy=15:result=transforms.trf -f null -Key Parameters Explained
shakiness=5: Sets how shaky the original video is on a scale from 1 (mild) to 10 (severe). The default is 5.accuracy=15: Determines the accuracy of the camera motion detection from 1 (low) to 15 (high). Higher values provide better results but take longer to process.result=transforms.trf: Specifies the name and path of the output file where the motion data will be saved.
Step 2: The Transformation Pass
Once the analysis file is generated, you apply the
vidstabtransform filter to create the stabilized video
file. This pass reads the transforms.trf file and
physically shifts the video frames to counteract the detected
shakiness.
Run the following command to generate your smooth video:
ffmpeg -i input.mp4 -vf vidstabtransform=smoothing=30:input=transforms.trf -c:a copy output_stabilized.mp4Key Parameters Explained
smoothing=30: Controls the number of frames used to calculate the smooth camera path. A higher number yields a smoother video, but setting it too high can cause the frame to drift significantly or distort. The default is 15.input=transforms.trf: Directs FFmpeg to read the motion data generated during your first pass.-c:a copy: Copies the audio track directly from the original file without re-encoding it, saving processing time and preserving audio quality.
Advanced Optimization: Zoom and Crop
Stabilizing a video inevitably shifts the edges of the frame inward,
which can leave black borders around your video. By default,
vidstabtransform subtly zooms in to crop out these
artifacts. If you notice remaining black edges, you can force a cleaner
crop and zoom by adding the optzoom parameter:
ffmpeg -i input.mp4 -vf vidstabtransform=smoothing=30:optzoom=1:input=transforms.trf -c:a copy output_stabilized.mp4Setting optzoom=1 tells the filter to automatically
calculate the optimal zoom factor required to completely hide any empty
borders throughout the duration of the clip.