Advertisement
Guest User

Untitled

a guest
Feb 12th, 2021
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Defaults
  4. keyboard_binding="Mod+v" # Only used by notifications.
  5. screenshots_folder="/home/anon/Screenshots"
  6. screencasts_folder="/home/anon/Screencasts"
  7. gifcasts_folder="/home/anon/Gifcasts"
  8.  
  9. # Look for prompt script in same folder
  10. DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
  11.  
  12. # Save PID files to
  13. screencast_pid=/tmp/.screencast.pid
  14. gifcast_pid=/tmp/.gifcast.pid
  15. gifcast_mp4_filename=/tmp/.gifcast_filename
  16. gifcast_palette=/tmp/.gifcast_palette.png
  17.  
  18. screenshot() {
  19. mkdir -p $screenshots_folder
  20. grim -g "$(slurp)" $screenshots_folder/`date +%Y-%m-%d.%H:%M:%S`.png
  21. notify-send 'New Screenshot' "Saved to $screenshots_folder folder." -u low
  22. }
  23.  
  24. full_screenshot() {
  25. mkdir -p $screenshots_folder
  26. grim $screenshots_folder/`date +%Y-%m-%d.%H:%M:%S`.png
  27. notify-send 'New Screenshot' "Saved to $screenshots_folder folder." -u low
  28. }
  29.  
  30. convert_mp4_to_gif() {
  31. filename=$gifcasts_folder/`date +%Y-%m-%d.%H:%M:%S`.gif
  32. echo "Converting $1 to $filename"
  33. palette="/tmp/palette.png"
  34. filters="fps=15,scale=$2:-1:flags=lanczos"
  35. ffmpeg -v warning -i "file:$1" -vf "$filters,palettegen" -y $gifcast_palette
  36. ffmpeg -v warning -i "file:$1" -i $gifcast_palette -lavfi "$filters [x]; [x][1:v] paletteuse" -y "file:$filename"
  37. }
  38.  
  39. record_screen() {
  40. geometry=$(slurp) || exit 1
  41. notify-send -t 3000 "Starting 3 seconds..." "Stop the cast by pressing $keyboard_binding"
  42. sleep 3
  43. wf-recorder -g "$geometry" -f $1 & echo $! > $2
  44. }
  45.  
  46. start_screencast() {
  47. mkdir -p $screencasts_folder
  48. record_screen $screencasts_folder/`date +%Y-%m-%d.%H:%M:%S`.mp4 "$screencast_pid"
  49. }
  50.  
  51. stop_screencast() {
  52. kill -INT `cat $screencast_pid`
  53. rm $screencast_pid
  54. notify-send "Screencast Stopped" "The video was saved into ~/Screencasts folder."
  55. }
  56.  
  57. start_gifcast() {
  58. mkdir -p $gifcasts_folder
  59. filename=$screencasts_folder/`date +%Y-%m-%d.%H:%M:%S`.mp4
  60. echo $filename > $gifcast_mp4_filename
  61. record_screen $filename $gifcast_pid
  62. }
  63.  
  64. stop_gifcast() {
  65. kill -INT `cat $gifcast_pid`
  66. rm $gifcast_pid
  67. notify-send "Gifcast Stopped" "Processing the gif might take some time..."
  68.  
  69. mp4_source=`cat $gifcast_mp4_filename`
  70.  
  71. max_width=$(ffprobe -v error -select_streams v:0 -show_entries stream=width -of csv=s=x:p=0 $mp4_source)
  72. scale=$max_width
  73.  
  74. if (( $max_width > 1080 )); then
  75. scale=$($DIR/prompt -o "$max_width,1080,720,480,360,240" -q "Scale GIF resolution (width) to:")
  76. elif (( $max_width > 720 )); then
  77. scale=$($DIR/prompt -o "$max_width,720,480,360,240" -q "Scale GIF resolution (width) to:")
  78. elif (( $max_width > 480 )); then
  79. scale=$($DIR/prompt -o "$max_width,480,360,240" -q "Scale GIF resolution (width) to:")
  80. elif (( $max_width > 360 )); then
  81. scale=$($DIR/prompt -o "$max_width,360,240" -q "Scale GIF resolution (width) to:")
  82. fi
  83.  
  84. notify-send "Processing" "Processing the gif might take some time..."
  85. convert_mp4_to_gif $mp4_source $scale
  86.  
  87. notify-send "Gifcast Ready" "Gifcast was processed and saved to $gifcasts_folder"
  88. }
  89.  
  90. if [ -f "$screencast_pid" ]; then
  91. stop_screencast
  92. exit 0
  93. fi
  94.  
  95. if [ -f "$gifcast_pid" ]; then
  96. stop_gifcast
  97. exit 0
  98. fi
  99.  
  100. case $($DIR/prompt -o "Screenshot,Screencast,Gifcast,Full Screenshot" -q "Capture Screen:") in
  101. 'Screenshot')
  102. screenshot -s
  103. ;;
  104. 'Screencast')
  105. start_screencast
  106. ;;
  107. 'Gifcast')
  108. start_gifcast
  109. ;;
  110. 'Full Screenshot')
  111. full_screenshot
  112. ;;
  113. esac
  114.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement