SHARE
TWEET

Untitled

a guest Jul 14th, 2017 24 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/env bash
  2.  
  3.  
  4.  
  5. while getopts ":d:f:S:s:i:n:g:hv" opt; do
  6.   case $opt in
  7.     d)
  8.       declare -r  location="$OPTARG"
  9.       ;;
  10.     f)
  11.       declare -r filename="$OPTARG"
  12.       ;;
  13.     S)
  14.       declare -r sub="$OPTARG"
  15.       ;;
  16.     s)
  17.       declare -r startFrame="$OPTARG"
  18.       ;;
  19.     i)
  20.       declare -r intervalScreenshots="$OPTARG"
  21.       ;;
  22.     n)
  23.       declare -r numberScreenshots="$OPTARG"
  24.       ;;
  25.     g)
  26.       declare -r videoHeight="$OPTARG"
  27.       ;;
  28.     h)
  29.       declare -r help="TRUE"
  30.       ;;
  31.     v)
  32.       declare -r verbose="TRUE"
  33.       ;;
  34.     \?)
  35.       echo "Invalid option: -$OPTARG" >&2
  36.       exit 1
  37.       ;;
  38.     :)
  39.       echo "Option -$OPTARG requires an argument." >&2
  40.       exit 1
  41.       ;;
  42.   esac
  43. done
  44.  
  45. # Quit if we missed -s or -n. Or if -h is used.
  46. if [[ -z "$startFrame" ]] || [[ -z "$numberScreenshots" ]] || [[ -n "$help" ]]  ; then
  47.   printf 'You must at the minimum set -s and -n.
  48.  -d <set a chosen directory>
  49.  -f <the video file>
  50.  -S <sub options: lang|auto|no>
  51.  -s <the start frame>
  52.  -i <set an optional frame interval>
  53.  -n <the number of screenshots you want>
  54.  -g <the desired display resolution>
  55.  -h <help>
  56.  -v <set verbose to true>\n\n'
  57.  
  58.   exit 1
  59. # If user tries to use -d and -f together. This is redundent. Preventing the creation stupid questions later.
  60. elif [[ -d "$location" ]] && [[ -f "$filename" ]]   ; then
  61.   printf '
  62.  Cannot set -d and -f at the same time
  63.  Please use -d for multiple files only
  64.  Please only use full path with -f \n\n'
  65.    
  66.   exit 1
  67.  
  68. fi
  69.  
  70. # Subtitle Selection
  71.  
  72. if [[ -S 'no' ]] ;
  73. then
  74. declare langcom='sid'
  75. declare lang=no
  76.  
  77. elif [[ -z "$sub" ]] ;
  78. then
  79. declare langcom='sid'
  80. declare lang='auto'
  81. else
  82. declare langcom='slang'
  83. declare lang="$sub"
  84. fi
  85.  
  86. # Automatic directory finding if -d isn't set.
  87. if [[ -z "$location" ]] ; then
  88.   declare -r dir="."
  89. else
  90.   declare -r dir="$location"
  91. fi
  92. for f in "$dir"/*.*
  93.  
  94. do
  95. # Loop for file. Sets to either f in $dir or $filename.
  96. if [[ -z "$filename" ]] ;
  97. then
  98. declare file="$f"
  99.  
  100. elif [[ -f "$filename" ]] ; then
  101. declare file="$filename"
  102. else
  103. echo "No Files Found"
  104.  
  105. exit
  106.  
  107. fi
  108. # Creates a directiry for image placement
  109.  declare base="$(basename "$file" | cut -d "." -f1)"
  110. mkdir "$base"
  111. # Start mpv
  112.  
  113. 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 &
  114.  
  115.  
  116. # Information grabbing
  117.  
  118.  sleep 1
  119.  
  120.  declare -r lastFrame="$(echo '{ "command": ["get_property", "estimated-frame-count"]}' | socat - /tmp/mpvsocket | cut -d":" -f2 | cut -d , -f1)"
  121.  
  122.  sleep 1
  123.  
  124.  declare -r fpsVideo="$(echo '{ "command": ["get_property", "estimated-vf-fps"]}' | socat - /tmp/mpvsocket | cut -d":" -f2 | cut -d , -f1)"
  125.  
  126.  
  127.  sleep 1
  128.  
  129. # Declare interval for each screenshot
  130. if [[ -z "$intervalScreenshots" ]] ; then
  131.   declare diffFrame="$(awk "BEGIN {printf $lastFrame - $startFrame}")"
  132.   declare intervalFrame="$(awk "BEGIN {printf $diffFrame / $numberScreenshots}")"
  133.  
  134. else
  135.   declare -r intervalFrame="$intervalScreenshots"
  136.  
  137. fi
  138.  
  139. # Looping to take screenshots
  140. declare currentFrame="$startFrame"
  141. for i in $(seq 1 "$numberScreenshots") ; do
  142.  
  143.    declare currentTime="$(awk "BEGIN {printf $currentFrame / $fpsVideo}")"
  144.  
  145.   if [[ -n "$verbose" ]] ; then
  146.     printf 'File: %s\n' "$base"
  147.     printf 'Current time: %.2f\n' "$currentTime"
  148.     printf 'Last frame: %s\n' "$lastFrame"
  149.     printf 'FPS: %s\n' "$fpsVideo"
  150.     printf 'Interval: %s\n' "$intervalFrame"
  151.     printf 'Screenshot: %02d\n\n\n' "$i"
  152.  
  153.  fi
  154.   # Debug line
  155.  
  156.  
  157.  
  158.    echo '{ "command": ["set_property", "pause", true] }'    | socat - /tmp/mpvsocket
  159.  
  160.    sleep 1
  161.    
  162.    echo '{ "command": ["seek", '$currentTime', "absolute"] }' | socat - /tmp/mpvsocket
  163.    
  164.    sleep 1
  165.    
  166.    echo '{ "command": ["screenshot", "window"] }' | socat - /tmp/mpvsocket
  167.    
  168.    sleep 1
  169.    
  170.    
  171.  
  172. currentFrame="$(awk "BEGIN {printf $currentFrame+$intervalFrame}")"
  173.  
  174.  
  175.  
  176. done
  177.  
  178. echo '{ "command": ["quit"] }'  | socat - /tmp/mpvsocket
  179.  
  180. done
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top