Skip to content

Instantly share code, notes, and snippets.

@karrirasinmaki
Last active August 10, 2020 11:15
Show Gist options
  • Save karrirasinmaki/856506fc2b915b34cc59611ad6adbf10 to your computer and use it in GitHub Desktop.
Save karrirasinmaki/856506fc2b915b34cc59611ad6adbf10 to your computer and use it in GitHub Desktop.
A little script to stabilize given video file
#!/bin/bash
##
# Deshake script.
#
# This script uses vidstab library with ffmpeg. You need a version of ffmpeg with vidstab.
# Read more about vid.stab and its params: https://github.com/georgmartius/vid.stab
#
# author: @karrirasinmaki
####
# script dir (source https://stackoverflow.com/a/246128)
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
input_file=""
output_file=""
FFMPEG="$DIR/ffmpeg"
params=""
width=-1
sharpen=1
frame=0
preset="general"
step="all"
function show_help {
echo "Usage:"
echo "-h help -- show this help document"
echo "-i input -- input file"
echo "-o output -- output file name/location, default=INPUT_stabilized.EXT"
echo "-1, -2 -- which step to run (only one arg possible), 1=detect, 2=transform, default none = all the steps"
echo "-w width -- scale video to width, default=-1 (no scaling)"
echo "-s sharpen -- sharpen 1=default, 0=disabled, default=1"
echo "-f frame -- reference frame to be used with tripod preset, default=0 (disabled)"
echo "-t type/preset -- preset to use (general/gentle/tripod), default=general"
echo "-p ffmpeg params"
}
OPTIND=1
while getopts "12i:p:w:s:t:o:f:h": opt; do
case "$opt" in
h|\?)
show_help
exit 0
;;
1)
step="detect"
;;
2)
step="transform"
;;
i)
input_file="$OPTARG"
;;
o)
output_file="$OPTARG"
;;
p)
params="$OPTARG"
;;
w)
width=$OPTARG
;;
s)
sharpen=$OPTARG
;;
f)
frame=$OPTARG
;;
t)
preset="$OPTARG"
;;
esac
done
if [ $OPTIND -eq 1 ]; then
echo "No options were passed."
show_help
exit 0
fi
file_basename=$(basename -- "$input_file")
file_ext="${file_basename##*.}"
file_name="${file_basename%.*}"
echo "Processing file:"
echo "$input_file"
echo "$file_basename"
echo "$file_name"
echo "$file_ext"
echo "---"
echo "$params"
if [[ $output_file == "" ]]; then
output_file="${file_name}_stabilized.$file_ext"
fi
##
# One step
# "$FFMPEG" -i "$input_file" $params -y -vf vidstabtransform,unsharp=5:5:0.8:3:3:0.4 "${file_name}_stabilizedOne.$file_ext"
##
# Two steps
detect_params=stepsize=6:shakiness=10:accuracy=10
trans_params=zoom=0:smoothing=1
echo "preset: $preset"
if [[ $preset == "general" ]]; then
# pass
echo ""
elif [[ $preset == "gentle" ]]; then
trans_params=$trans_params:maxangle=0.01
elif [[ $preset == "tripod" ]]; then
if [ $frame -eq 0 ]; then
echo "Please set reference frame to use with tripod with option -f."
exit 0
fi
detect_params=$detect_params:tripod=$frame
trans_params=$trans_params:tripod=1:maxshift=0.1:smoothing=10
fi
if [ $sharpen -eq 1 ]; then
# unsharp
# TODO: Possibility to choose sharpening strenght
trans_params=$trans_params,unsharp=5:5:0.8:3:3:0.4
fi
# extra params
# params="$params -tune film"
if [[ $step == "all" || $step == "detect" ]]; then
# Generate info about shakes
"$FFMPEG" -i "$input_file" -vf vidstabdetect=result="$output_file.trf":$detect_params $params -f null -
fi
if [[ $step == "all" || $step == "transform" ]]; then
# Stabilize the video using data from the first pass
"$FFMPEG" -i "$input_file" -vf vidstabtransform=input="$output_file.trf":$trans_params,scale=$width:-1 \
-acodec copy -vcodec libx264 $params "$output_file"
fi
# -vf colormatrix=bt601:bt709 \
# -sws_flags spline+accurate_rnd+full_chroma_int -vf "colorspace=bt709:iall=bt601:fast=1" \
# -color_range 1 -colorspace 1 -color_primaries 1 -color_trc 1 \
# -color_range pc -colorspace smpte170m -color_primaries bt709 -color_trc bt709 \
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment