Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/env bash
- while getopts ":d:f:S:s:i:n:g:hv" opt; do
- case $opt in
- d)
- declare -r location="$OPTARG"
- ;;
- f)
- declare -r filename="$OPTARG"
- ;;
- S)
- declare -r sub="$OPTARG"
- ;;
- s)
- declare -r startFrame="$OPTARG"
- ;;
- i)
- declare -r intervalScreenshots="$OPTARG"
- ;;
- n)
- declare -r numberScreenshots="$OPTARG"
- ;;
- g)
- declare -r videoHeight="$OPTARG"
- ;;
- h)
- declare -r help="TRUE"
- ;;
- v)
- declare -r verbose="TRUE"
- ;;
- \?)
- echo "Invalid option: -$OPTARG" >&2
- exit 1
- ;;
- :)
- echo "Option -$OPTARG requires an argument." >&2
- exit 1
- ;;
- esac
- done
- # Quit if we missed -s or -n. Or if -h is used.
- if [[ -z "$startFrame" ]] || [[ -z "$numberScreenshots" ]] || [[ -n "$help" ]] ; then
- printf 'You must at the minimum set -s and -n.
- -d <set a chosen directory>
- -f <the video file>
- -S <sub options: lang|auto|no>
- -s <the start frame>
- -i <set an optional frame interval>
- -n <the number of screenshots you want>
- -g <the desired display resolution>
- -h <help>
- -v <set verbose to true>\n\n'
- exit 1
- # If user tries to use -d and -f together. This is redundent. Preventing the creation stupid questions later.
- elif [[ -d "$location" ]] && [[ -f "$filename" ]] ; then
- printf '
- Cannot set -d and -f at the same time
- Please use -d for multiple files only
- Please only use full path with -f \n\n'
- exit 1
- fi
- # Subtitle Selection
- if [[ -S 'no' ]] ;
- then
- declare langcom='sid'
- declare lang=no
- elif [[ -z "$sub" ]] ;
- then
- declare langcom='sid'
- declare lang='auto'
- else
- declare langcom='slang'
- declare lang="$sub"
- fi
- # Automatic directory finding if -d isn't set.
- if [[ -z "$location" ]] ; then
- declare -r dir="."
- else
- declare -r dir="$location"
- fi
- for f in "$dir"/*.*
- do
- # Loop for file. Sets to either f in $dir or $filename.
- if [[ -z "$filename" ]] ;
- then
- declare file="$f"
- elif [[ -f "$filename" ]] ; then
- declare file="$filename"
- else
- echo "No Files Found"
- exit
- fi
- # Creates a directiry for image placement
- declare base="$(basename "$file" | cut -d "." -f1)"
- mkdir "$base"
- # Start mpv
- mpv --pause --quiet --no-osc --no-audio --osd-level=0 --no-border --vo=opengl --framedrop=no --"$langcom"="$lang" --scaler-lut-size=8 --scale=spline36 --cscale=spline36 --opengl-fbo-format=rgb16 --linear-scaling --geometry="$videoHeight" --screenshot-template=%F-%ws --screenshot-format=png --screenshot-png-compression=5 --screenshot-directory="$base" "$file" --input-ipc-server=/tmp/mpvsocket > /dev/null 2>&1 &
- # Information grabbing
- sleep 1
- declare -r lastFrame="$(echo '{ "command": ["get_property", "estimated-frame-count"]}' | socat - /tmp/mpvsocket | cut -d":" -f2 | cut -d , -f1)"
- sleep 1
- declare -r fpsVideo="$(echo '{ "command": ["get_property", "estimated-vf-fps"]}' | socat - /tmp/mpvsocket | cut -d":" -f2 | cut -d , -f1)"
- sleep 1
- # Declare interval for each screenshot
- if [[ -z "$intervalScreenshots" ]] ; then
- declare diffFrame="$(awk "BEGIN {printf $lastFrame - $startFrame}")"
- declare intervalFrame="$(awk "BEGIN {printf $diffFrame / $numberScreenshots}")"
- else
- declare -r intervalFrame="$intervalScreenshots"
- fi
- # Looping to take screenshots
- declare currentFrame="$startFrame"
- for i in $(seq 1 "$numberScreenshots") ; do
- declare currentTime="$(awk "BEGIN {printf $currentFrame / $fpsVideo}")"
- if [[ -n "$verbose" ]] ; then
- printf 'File: %s\n' "$base"
- printf 'Current time: %.2f\n' "$currentTime"
- printf 'Last frame: %s\n' "$lastFrame"
- printf 'FPS: %s\n' "$fpsVideo"
- printf 'Interval: %s\n' "$intervalFrame"
- printf 'Screenshot: %02d\n\n\n' "$i"
- fi
- # Debug line
- echo '{ "command": ["set_property", "pause", true] }' | socat - /tmp/mpvsocket
- sleep 1
- echo '{ "command": ["seek", '$currentTime', "absolute"] }' | socat - /tmp/mpvsocket
- sleep 1
- echo '{ "command": ["screenshot", "window"] }' | socat - /tmp/mpvsocket
- sleep 1
- currentFrame="$(awk "BEGIN {printf $currentFrame+$intervalFrame}")"
- done
- echo '{ "command": ["quit"] }' | socat - /tmp/mpvsocket
- done
Add Comment
Please, Sign In to add comment