Shaky footage can be a huge problem. It can detract from the overall quality of your video, making it look unprofessional. Thankfully, FFmpeg provides a powerful solution to stabilize shaky video, making your content look smoother and more polished. In this guide, we will explore how FFmpeg can help fix shaky footage, covering various methods available, and providing insights into the advantages and drawbacks of each.
How FFmpeg Can Stabilize Shaky Video
FFmpeg provides several ways to stabilize shaky videos. Though it may not be as specialized as dedicated video stabilizing software, FFmpeg offers effective techniques to reduce or eliminate the unwanted shakes and jitters in your footage. The process typically involves analyzing the video for motion patterns and then compensating for the shake by shifting the frame position. This makes your video smoother and more watchable.
FFmpeg uses advanced motion estimation techniques to calculate how much the video is shaking, then adjusts the frames to compensate. This process can be done with the deshake filter or vidstab filter, the two most common stabilization methods in FFmpeg. While both methods can significantly improve shaky footage, the results depend on the complexity of the shake and the type of video you are working with.
Methods FFmpeg Uses to Stabilize Video
FFmpeg supports several methods for stabilizing shaky footage. Here, we will discuss the two main methods: deshake and vidstab.
Deshake Filter
Deshake filter is the simplest and quickest method to stabilize shaky video in FFmpeg. It works by analyzing motion between adjacent frames and compensating for the movement. It is easy to use and requires minimal setup, making it a correct choice for beginners or when you need a quick fix for minor shakes.
Pros:
- Simple to Use: No complex configuration or pre-analysis.
- Fast Processing: This method is quicker than more advanced options.
- Good for Minor Shakes: Works well for videos with slight shaking or minimal instability.
Cons:
- Limited for Severe Shakes: If the video has significant shaking or erratic movement, deshake filter might not yield satisfactory results.
- Possible Distortions: The filter can cause some artifacts like warping or softening in areas with more intense shakes.
Vidstab Filter
Vidstab filter is a more advanced method for stabilizing shaky video in FFmpeg. Unlike the deshake filter, the vidstab method requires two steps: first, a motion analysis phase to detect the shake, and then a stabilization phase where the frames are adjusted based on this analysis. This method is more precise and works better for complex shakes and dramatic camera movements.
Pros:
- More Accurate Stabilization: The vidstab filter offers more precise stabilization, especially for severe shakes and erratic movement.
- Better Results for Complex Shakes: It performs much better on shaky footage from handheld cameras or video with larger frame movements.
Cons:
- Slower Processing: Since vidstab requires an initial motion analysis, it takes longer process time than the deshake filter.
- Possible Cropping: To avoid distortions, the filter may crop the edges of the frame as it compensates for the shake, resulting in a slightly smaller video output.
FFmpeg code for Deshake Filter Stabilization
- -i input_video.mp4: specifies the input video file to stabilize. Replace input_video.mp4 with the actual file name of your video.
- -vf: This flag specifies the video filter option.
- rx=16: This sets the horizontal shift limit to 16 pixels. You can increase or decrease this number to allow more or less horizontal shift.
- ry=16: This sets the vertical shift limit to 16 pixels. You can adjust this for the vertical shift.
- edge=none: This prevents the filter from altering the edge pixels, which helps to avoid noticeable artifacts in the video frame.
- -c:a copy: This option copies the audio from the input file without modification. It ensures that the audio remains intact and synchronized with the stabilized video.
- output_video.mp4: This specifies the output file name where the stabilized video will be saved. Replace output_video.mp4 with the desired file name.
FFmpeg Code For Vidstab Video Stabilizer
vidstabdetect: Detecting Shake and Creating Transforms
The vidstabdetect analyzes a video and detects the movement and shake between frames. It essentially examines how the video is shifting from one frame to the next and records this motion. This step doesn't modify the video but gathers the necessary information and stores it in a file named transform.trf.
Vidstabdetect parameters:
stepsize: defines the number of pixels the detection will move between each step. A higher value reduces the number of steps, making it faster, but may decrease accuracy. A smaller value increases accuracy but requires more computation.
Minimum: 1, Maximum: 64, Default: 6
shakiness: determines the amount of shakiness that should be tolerated by the algorithm. It affects how aggressively the algorithm stabilizes the footage. Higher values result in stronger stabilization but may lead to over-stabilization or unnatural movement. Lower values result in less stabilization.
Minimum: 0, Maximum: 20, Default: 10
accuracy: controls the detection accuracy. Lower accuracy values speed up the process but may miss smaller motions, while higher values result in better quality but may take longer.
Minimum: 1, Maximum: 15, Default: 15
border: defines how many pixels of the border of the video should be left untouched. This can be useful to preserve content near the edges of the frame. Setting a value of -1 will ensure no border is left untouched and the entire frame is stabilized, while 0 leaves the edges untouched.
Minimum: 0, Maximum: -1 (no restriction), Default: 0
smoothing: controls how smooth the stabilization will be. Higher values create smoother transitions between frames, reducing visible jitter. Smoothing the motion helps to avoid jerky or erratic behavior in the video.
Minimum: 0, Maximum: 100, Default: 0
mincontrast: sets the minimum contrast value required for the algorithm to detect a frame as useful for stabilization. Lowering this value allows detection of frames with less contrast, but may result in less accurate motion tracking.
Minimum: 0.1, Maximum: 1.0, Default: 0.3
contrast: The threshold for the frame contrast detection to calculate motion. This helps the algorithm detect motion based on frame contrast.
Minimum: 0.0, Maximum: 1.0, Default: 0.5
compute: The type of motion for the stabilization computation. This can include "standard" or "sparse" which is less computationally expensive.
Values: standard or sparse, Default: standard
mode: Defines the method of stabilization. "Wobble" stabilization mode adjusts frame alignment more strictly. "Fast" stabilizer uses less processing power but is less accurate.
Values: wobble or fast, Default: wobble
Vidstabtransform: Stabilizing Video by Applying Motion Data
Once the motion data is captured by vidstabdetect and saved in the transform.trf, the vidstabtransform takes that data and applies it to the video. This process adjusts the position of each frame based on the motion information, resulting in a smoother, more stable video.
vidstabtransform parameters:
Input: specifies the input motion data file (the .trf file created by vidstabdetect).
output: specifies the output filename for the stabilized video.
smoothing: defines the level of smoothing applied to the motion. The higher the value, the smoother the stabilization.
Minimum: 0 (no smoothing), Default: 10
zoom: controls how much zoom is applied to compensate for camera movement. This can help to maintain the frame and avoid black edges due to stabilization.
Minimum: 1.0 (no zoom), default: 1.0.
overlap: controls the amount of overlap between frames when applying transformations. Higher values give a smoother result but may increase processing time.
Minimum: 0.0 (no overlap), Maximum: 0.5, Default: 0.3.
mode: defines the type of stabilization process. You can choose between various stabilization modes based on how the algorithm should perform the transformations.
Available options:
- motion: The basic mode, which compensates for general camera shake (default).
- trans: Translates the image frame (typically used to stabilize more severe shakes).
- rotation: Adjusts for rotational camera shakes (more specific to videos with spinning or tilting cameras).
- crop: Crops the video during stabilization to avoid visible edges or black areas.
- warp: Uses image warping techniques for advanced stabilization (may result in some distortion but compensates for significant shakes).
Want to See the Result?
Are you ready to see the difference for yourself? Watch a sample video before and after applying FFmpeg’s stabilization techniques. The results will speak for themselves, showing how much smoother your footage can be with just a few lines of code.
Stabilizing your shaky video with FFmpeg can help make your content look more professional and polished, ensuring that your viewers focus on the story, not the shaky camera.