rec 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #!/usr/bin/env bash
  2. show_help() {
  3. cat<<USE
  4. USAGE:
  5. rec [screencastname]
  6. OPTIONS:
  7. --select|-s: use selection rectangle
  8. --public|-p: upload it to http://tyler.zone
  9. --time|-t: duration of recording (default: 10secs)
  10. --delay|-d: delay before recording starts (default: 5secs)
  11. --help|-h: show help and exit
  12. EXAMPLE:
  13. rec --select --public whatchamajig
  14. USE
  15. }
  16. notify() {
  17. local msg="$1"
  18. local level="${2:-normal}"
  19. if [[ "$level" == "normal" ]]; then
  20. printf "$(tput setaf 6)%s$(tput sgr0)\n" "$msg"
  21. fi
  22. if [[ "$level" == "error" ]]; then
  23. >&2 printf "$(tput setaf 1)%s$(tput sgr0)\n" "$msg"
  24. fi
  25. }
  26. record () {
  27. local i
  28. i=${delay:-5}
  29. notify "Recording starting in ${i} seconds"
  30. if [[ -n "$duration" ]]; then
  31. options="--duration=${duration}"
  32. fi
  33. options="${options} --delay=0 --x=${x_offset} --y=${y_offset} --width=${width} --height=${height}"
  34. # Countdown timer
  35. while (( i > 0 )); do
  36. notify "$i"
  37. sleep 1
  38. (( i-- ))
  39. done
  40. notify "GO!!!!"
  41. byzanz-record $options "$full_path"
  42. }
  43. get_full_width() {
  44. eval "$(xrandr -q | awk -F'[,x]' 'NR==1 {gsub("( |current)", ""); print "width="$3"\nheight="$4}')"
  45. }
  46. get_selection() {
  47. notify "Waiting for your selection..."
  48. eval "$(xrectsel | awk -F '[x+]' '{print "width=" $1 "\nheight=" $2 "\nx_offset=" $3 "\ny_offset=" $4}')"
  49. }
  50. take_recording() {
  51. if [[ -z "$select" ]]; then
  52. select=0
  53. x_offset=0
  54. y_offset=0
  55. get_full_width
  56. else
  57. get_selection
  58. fi
  59. if [[ -z "$path" ]]; then
  60. path="$(date -Is)"
  61. fi
  62. path="${path}.gif"
  63. full_path="${_SCREENCAST_PATH}/${path}"
  64. record
  65. notify "Recording available at $path"
  66. }
  67. make_public() {
  68. if ! command -v s3cmd > /dev/null 2>&1; then
  69. notify "[ERR] s3cmd command not found in $PATH" "critical"
  70. fi
  71. s3cmd put "${full_path}" "s3://tyler.zone/${path}"
  72. s3cmd setacl --acl-public "s3://tyler.zone/${path}"
  73. notify "[URL] http://tyler.zone/${path}"
  74. printf 'http://tyler.zone/%s' "${path}" | xsel -p
  75. }
  76. main() {
  77. if ! command -v xrectsel > /dev/null 2>&1; then
  78. notify "xrectsel command not found in \$PATH" "error"
  79. fi
  80. if ! command -v byzanz-record > /dev/null 2>&1; then
  81. notify "byzanz-record command not found in \$PATH" "error"
  82. fi
  83. mkdir -p "${_SCREENCAST_PATH:=$HOME/screencasts}"
  84. public=0
  85. while [ -n "$1" ]; do
  86. case "$1" in
  87. --select|-s)
  88. select=1
  89. ;;
  90. --time|-t)
  91. shift
  92. duration="$1"
  93. ;;
  94. --delay|-d)
  95. shift
  96. delay="$1"
  97. ;;
  98. --public|-p)
  99. public=1
  100. ;;
  101. --help|-h)
  102. show_help
  103. exit 0
  104. ;;
  105. *)
  106. path="$1"
  107. ;;
  108. esac
  109. shift
  110. done
  111. take_recording
  112. if (( public == 1 )); then
  113. make_public
  114. fi
  115. }
  116. main "$@"