vertClip/vertClip.sh
Barbora Viski cdd2ca43b7
Add vertClip.sh
This script processing videos into vertical format.
2026-02-22 22:40:20 +02:00

169 lines
5 KiB
Bash

#!/bin/bash
# Prereq - imagemagick and ffmpeg
#------------------ [ Variables ] ------------------
ccb=30 # Camera border (in case of blurry camera frame)
cch=360 # Height of video
ccw=640 # Width of video
ccd=$(((ccw-cch)/2)) # Difference between W and H divided by two
ccy=$((600+ccb)) # Y value of top left corner
ccx=$((1920+ccd+ccb)) # X value of top left corner
ccr=$((cch-ccb-ccb)) # Diameter of camera
ccn=2560 # Normalization value (what are values above expecting)
cam_w=500 # Width of camera in output
cam_h=500 # Height of camera in output
cam_feather=0 # Thickness of feather edge border (default is 0, no feather)
main_height=1250 # Height of primary video
kill_feed="false"
cam_crop=$ccr":"$ccr":"$ccx":"$ccy #coordinaces: width:height:left:top
#------------------ [ Banner ] ------------------
cat <<EOF
#################################################################
#################################################################
###############[ ~ vertClip ~ ]###############
###############[ Script by: @bashynx ]###############
#################################################################
#################################################################
#[ DEPENDENCIES : imagemagick ffmpeg ]#
#################################################################
#[ DESCRIPTION : Utility for making good looking vertical ]#
#[ video clips. Configure camera coordinates ]#
#[ and other important values in the script. ]#
#[ The output is a 1080p vertical mp4 file. ]#
#################################################################
#################################################################
EOF
#------------------ [ Functions ] ------------------
usage() {
cat <<EOF
#################################################################
Usage: $(basename "$0") [OPTIONS]
Options:
-v FILE Video file
-l FILE Logo file
-c NUM Blur camera video border thickness
-k Show Kill Feed (eg. Counter-Strike, EFT:Arena)
-h Show this help
Examples:
$(basename "$0") -c 10 -l logo.png -v input.mp4
#################################################################
EOF
}
prereq() {
fail="false"
if ! command -v ffmpeg >/dev/null; then
echo "ERROR: Missing dependency: ffmpeg"
fail="true"
fi
if ! command -v identify >/dev/null; then
echo "ERROR: Missing dependency: imagemagick (identify)"
fail="true"
fi
if [ fail == "true" ]; then
echo "INFO: Make sure you install dependencies"
exit 1
fi
}
#------------------ [ MAIN ] ------------------
prereq
while getopts "c:khl:v:" opt; do
case "$opt" in
c)
if [[ "$OPTARG" =~ ^[0-9]+$ ]]; then
cam_feather="$OPTARG"
fi
;;
k)
kill_feed="500:150:2080:95" # No need to normalize, 2k CS matches 1080p Tarkov Arena killfeed
;;
h)
usage
exit 0
;;
l)
if [ -f $OPTARG ]; then
logo="$OPTARG"
lh=`identify -format "%h" $logo`
lw=`identify -format "%w" $logo`
if [ $lw -gt 1030 ]; then
lh=$(((lh*1030)/lw))
lw=1030
echo "INFO: Logo is more than 1030 wide, resized to 1030 - lh:"$lh" lw:"$lw
fi
if [ $lh -gt 200 ]; then
lw=$(((lw*200)/lh))
lh=200
echo "INFO: Logo is more than 200 high, resized to 200 - lh:"$lh" lw:"$lw
fi
echo "INFO: Logo is lh:"$lh" lw:"$lw
else
logo="false"
fi
;;
v)
if [ -f $OPTARG ]; then
vid="$OPTARG"
echo "INFO: Video appears valid, normalizing camera coordinaces"
vh=`ffprobe -v error -select_streams v:0 -show_entries stream=height -of csv=p=0:s=x $vid`
vw=`ffprobe -v error -select_streams v:0 -show_entries stream=width -of csv=p=0:s=x $vid`
if [ $vw != $ccn ]; then
ccw=$((ccw*vw/ccn))
cch=$((cch*vw/ccn))
ccx=$((ccx*vw/ccn))
ccy=$((ccy*vw/ccn))
ccr=$((ccr*vw/ccn))
fi
cam_crop=$ccr":"$ccr":"$ccx":"$ccy
echo "INFO: Camera coordinaces "$cam_crop - $vw
else
echo "ERROR: You need to provide valid video path"
usage
exit 1
fi
;;
\?)
usage
exit 1
;;
:)
usage
exit 1
;;
esac
done
inputs="-i "$vid
if [ $logo != "false" ]; then
logo_place=$((((1920-main_height-cam_h-lh)/2)+main_height+cam_h)) # Calculate Y of logo
inputs=$inputs" -i "$logo
fi
filter="[0:v]format=rgba,crop=$cam_crop,scale=$cam_w:$cam_h,geq=r='r(X,Y)':g='g(X,Y)':b='b(X,Y)':a='255*clip(1 - (sqrt(pow(X-$((cam_w/2)),2)+pow(Y-$((cam_h/2)),2)) - $(((cam_h/2)-cam_feather)))/$cam_feather, 0, 1)'[camera];
[0:v]scale=-1:1920,crop=1080:1920,gblur=sigma=50[background];
[0:v]scale=-1:$main_height[game];
[background][game]overlay=(W-w)/2:$cam_h:format=auto[tmp1];
[tmp1][camera]overlay=(W-w)/2:25:format=auto[tmp2];
[tmp2][1:v]overlay=(W-w)/2:$logo_place:format=auto"
if [ $kill_feed != "false" ]; then
filter="[0:v]crop=$kill_feed[killfeed];"$filter"[tmp3];
[tmp3][killfeed]overlay=(W-w):575:format=auto"
fi
echo "#################################################################"
ffmpeg -hide_banner -loglevel error -stats $inputs -filter_complex "$filter" -c:a copy "output_$(date +%s)_$vid"