Skip to content

Instantly share code, notes, and snippets.

@hlorand
Last active November 8, 2024 20:34
Show Gist options
  • Save hlorand/e5012fa315dcfe358008cf1b4611c7e0 to your computer and use it in GitHub Desktop.
Save hlorand/e5012fa315dcfe358008cf1b4611c7e0 to your computer and use it in GitHub Desktop.
Video stabilization using VidStab and FFMPEG

Video stabilization using VidStab and FFMPEG

** Step 1 **

Install ffmpeg with the vidstab plugin.

** Step 2 **

Analyze any video and generate a so called transform file, which describes camera movements. The command below generates a transforms.trf file

ffmpeg -i input.mp4 -vf vidstabdetect=shakiness=7 -f null -

Options:

  • shakiness: Set the shakiness of input video or quickness of camera. It accepts an integer in the range 1-10, a value of 1 means little shakiness, a value of 10 means strong shakiness. Default value is 5.

** Step 3 **

Stabilize video

ffmpeg -i input.mp4 -vf vidstabtransform=smoothing=30:zoom=5:input="transforms.trf" stabilized.mp4

Options:

  • smoothing: Default value is 10. For example, a number of 10 means that 21 frames are used (10 in the past and 10 in the future) to smoothen the motion in the video. A larger value leads to a smoother video, but limits the acceleration of the camera (pan/tilt movements).
    • recommended value: videoFPS / 2
  • zoom: Set percentage (%) to zoom into video. A positive value will result in a zoom-in effect, a negative value in a zoom-out effect. Default value is 0 (no zoom).

Read more in the vidstab documentation

Create side-by-side comparsion video:

ffmpeg -i original.mp4 -i stabilized.mp4 -filter_complex "[0:v:0]pad=iw*2:ih[bg]; [bg][1:v:0]overlay=w" sidebyside.mp4
@radpopl
Copy link

radpopl commented Jul 22, 2023

Why I cant use "curves=master='0.0625/0 1/1'" after vidstabtransform in -vf string? ffmpeg always ends when I put curves at the end.

@hlorand
Copy link
Author

hlorand commented Jul 22, 2023

@radpopl This is an anwser from Perplexity AI:

Please provide full command you want to run.

  1. Filter order: The order of filters in the -vf string is important in FFmpeg. Some filters may have dependencies or require specific input formats. If the "curves" filter is not compatible with the output of the "vidstabtransform" filter, it could cause FFmpeg to fail. You may need to adjust the order of the filters or use additional filter options to ensure compatibility.

  2. Syntax error: It's possible that there is a syntax error in the way you are specifying the "curves" filter in your command. Double-check the syntax and make sure you are using the correct options and values for the "curves" filter.

  3. Unsupported combination: Certain combinations of filters may not be supported by FFmpeg. It's possible that the "vidstabtransform" and "curves" filters are not designed to work together in the way you are trying to use them. In this case, you may need to find an alternative approach or use separate commands to achieve the desired result.

@radpopl
Copy link

radpopl commented Jul 22, 2023

for %%a in ("*.mp4") do ffmpeg -i "%%a" -vf "scale=in_range=full:in_color_matrix=bt709:out_color_matrix=rgb:out_range=full,vidstabtransform=crop=black:interpol=bicubic:optzoom=2:input='%%~na.trf',curves=master='0.0625/0 1/1'" -map 0:0 -c:v utvideo -pix_fmt gbrp -sws_flags +accurate_rnd+full_chroma_int "%%~na_stab.avi"

@ghastlyguy21
Copy link

I'm very new to ffmpeg but I can't seem to get the transform.trf code to work I enter ffmpeg -i input.mp4 -vf vidstabdetect=shakiness=7 -f null - and replace my input file and I only get an error that says an output file must be specified any suggestions on why this is happening and how I can fix it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment