Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
P

python_video_stab

> 开发工具
Open source

A Python package to stabilize videos using OpenCV

773 stars0 likes2 views
WebsiteGitHub

About

A Python package to stabilize videos using OpenCV

Python Video Stabilization

Python video stabilization using OpenCV. Full searchable documentation here.

This module contains a single class (VidStab) used for video stabilization. This class is based on the work presented by Nghia Ho in SIMPLE VIDEO STABILIZATION USING OPENCV. The foundation code was found in a comment on Nghia Ho's post by the commenter with username koala.

Input Output
|  

Video used with permission from HappyLiving

Contents:

  1. Installation
    • Install vidstab without installing OpenCV
    • Install vidstab & OpenCV
  2. Basic Usage
    • Using from command line
    • Using VidStab class
  3. Advanced Usage
    • Plotting frame to frame transformations
    • Using borders
    • Using Frame Layering
    • Stabilizing a frame at a time
    • Working with live video
    • Transform File Writing & Reading

Installation

diff
+ Please report issues if you install/try to install and run into problems!

Install vidstab without installing OpenCV

If you've already built OpenCV with python bindings on your machine it is recommended to install vidstab without installing the pypi versions of OpenCV. The opencv-python python module can cause issues if you've already built OpenCV from source in your environment.

The below commands will install vidstab without OpenCV included.

From PyPi

bash
pip install vidstab

From GitHub

bash
pip install git+https://github.com/AdamSpannbauer/python_video_stab.git

Install vidstab & OpenCV

If you don't have OpenCV installed already there are a couple options.

  1. You can build OpenCV using one of the great online tutorials from PyImageSearch, LearnOpenCV, or OpenCV themselves. When building from source you have more options (e.g. platform optimization), but more responsibility. Once installed you can use the pip install command shown above.
  2. You can install a pre-built distribution of OpenCV from pypi as a dependency for vidstab (see command below)

The below commands will install vidstab with opencv-contrib-python as dependencies.

From PyPi

bash
pip install vidstab[cv2]

From Github

bash
 pip install -e git+https://github.com/AdamSpannbauer/python_video_stab.git#egg=vidstab[cv2]

Basic usage

The VidStab class can be used as a command line script or in your own custom python code.

Using from command line

bash
# Using defaults
python3 -m vidstab --input input_video.mov --output stable_video.avi
bash
# Using a specific keypoint detector
python3 -m vidstab -i input_video.mov -o stable_video.avi -k GFTT

Using VidStab class

python
from vidstab import VidStab

# Using defaults
stabilizer = VidStab()
stabilizer.stabilize(input_path='input_video.mov', output_path='stable_video.avi')

# Using a specific keypoint detector
stabilizer = VidStab(kp_method='ORB')
stabilizer.stabilize(input_path='input_video.mp4', output_path='stable_video.avi')

# Using a specific keypoint detector and customizing keypoint parameters
stabilizer = VidStab(kp_method='FAST', threshold=42, nonmaxSuppression=False)
stabilizer.stabilize(input_path='input_video.mov', output_path='stable_video.avi')

Advanced usage

Plotting frame to frame transformations

python
from vidstab import VidStab
import matplotlib.pyplot as plt

stabilizer = VidStab()
stabilizer.stabilize(input_path='input_video.mov', output_path='stable_video.avi')

stabilizer.plot_trajectory()
plt.show()

stabilizer.plot_transforms()
plt.show()
Trajectories Transforms

Using borders

…

border_size=0

border_size=100

border_type='reflect' border_type='replicate'

Video used with permission from HappyLiving

Using Frame Layering

…
layer_func=vidstab.layer_overlay layer_func=vidstab.layer_blend

Video used with permission from HappyLiving

Automatic border sizing

python
from vidstab import VidStab, layer_overlay

stabilizer = VidStab()

stabilizer.stabilize(input_path=INPUT_VIDEO_PATH,
                     output_path='auto_border_stable_video.avi', 
                     border_size='auto',
                     # frame layering to show performance of auto sizing
                     layer_func=layer_overlay)

Stabilizing a frame at a time

The method VidStab.stabilize_frame() can accept numpy arrays to allow stabilization processing a frame at a time. This can allow pre/post processing for each frame to be stabilized; see examples below.

Simplest form

…

Example with object tracking

…

Working with live video

The VidStab class can also process live video streams. The underlying video reader is cv2.VideoCapture(documentation). The relevant snippet from the documentation for stabilizing live video is:

Its argument can be either the device index or the name of a video file. Device index is just the number to specify which camera. Normally one camera will be connected (as in my case). So I simply pass 0 (or -1). You can select the second camera by passing 1 and so on.

The input_path argument of the VidStab.stabilize method can accept integers that will be passed directly to cv2.VideoCapture as a device index. You can also pass a device index to the --input argument for command line usage.

One notable difference between live feeds and video files is that webcam footage does not have a definite end point. The options for ending a live video stabilization are to set the max length using the max_frames argument or to manually stop the process by pressing the Esc key or the Q key. If max_frames is not provided then no progress bar can be displayed for live video stabilization processes.

Example

python
from vidstab import VidStab

stabilizer = VidStab()
stabilizer.stabilize(input_path=0,
                     output_path='stable_webcam.avi',
                     max_frames=1000,
                     playback=True)

Transform file writing & reading

Generating and saving transforms to file

python
import numpy as np
from vidstab import VidStab, download_ostrich_video

# Download video if needed
download_ostrich_video(INPUT_VIDEO_PATH)

# Generate transforms and save to TRANSFORMATIONS_PATH as csv (no headers)
stabilizer = VidStab()
stabilizer.gen_transforms(INPUT_VIDEO_PATH)
np.savetxt(TRANSFORMATIONS_PATH, stabilizer.transforms, delimiter=',')

File at TRANSFORMATIONS_PATH is of the form shown below. The 3 columns represent delta x, delta y, and delta angle respectively.

-9.249733913760086068e+01,2.953221378387767970e+01,-2.875918912994855636e-02
-8.801434576214279559e+01,2.741942225927152776e+01,-2.715232319470826938e-02

Reading and using transforms from file

Below example reads a file of transforms and applies to an arbitrary video. The transform file is of the form shown in above section.

python
import numpy as np
from vidstab import VidStab

# Read in csv transform data, of form (delta x, delta y, delta angle):
transforms = np.loadtxt(TRANSFORMATIONS_PATH, delimiter=',')

# Create stabilizer and supply numpy array of transforms
stabilizer = VidStab()
stabilizer.transforms = transforms

# Apply stabilizing transforms to INPUT_VIDEO_PATH and save to OUTPUT_VIDEO_PATH
stabilizer.apply_transforms(INPUT_VIDEO_PATH, OUTPUT_VIDEO_PATH)

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Pythoncomputer-visionopencvpythonvideo

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category开发工具
PricingOpen source

> Related tools

V
VS Code
流行的开源代码编辑器
G
Git
分布式版本控制系统
V
Vite
下一代前端构建工具