record 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #!/bin/sh --
  2. set -ue
  3. # better version of Luke Smith script :^)
  4. # https://github.com/LukeSmithxyz/voidrice/raw/master/.local/bin/dmenurecord
  5. PID="${TMP}/.recordingpid"
  6. alias dpy='xdpyinfo | sed -n "s/^\s\+dimensions:\s*\([0-9]\+x[0-9]\+\).*/\1/p"'
  7. killrecording() {
  8. read -r recpid <"${PID}"
  9. # kill with SIGTERM, allowing finishing touches.
  10. kill -15 "${recpid}"
  11. rm -f -- "${PID}"
  12. # even after SIGTERM, ffmpeg may still run, so SIGKILL it.
  13. sleep 3
  14. kill -9 "${recpid}" || :
  15. }
  16. screencast() {
  17. ffmpeg -y \
  18. -f x11grab \
  19. -framerate 60 \
  20. -s "$(dpy)" \
  21. -i "${DISPLAY}" \
  22. -f alsa -i default \
  23. -r 30 \
  24. -c:v h264 -crf 0 -preset ultrafast -c:a aac \
  25. "${HOME}/screencast-$(date '+%y%m%d-%H%M-%S').mp4" &
  26. echo "${!}" >"${PID}"
  27. }
  28. video() {
  29. ffmpeg \
  30. -f x11grab \
  31. -s "$(dpy)" \
  32. -i "${DISPLAY}" \
  33. -c:v libx264 -qp 0 -r 30 \
  34. "${HOME}/video-$(date '+%y%m%d-%H%M-%S').mkv" &
  35. echo "${!}" >"${PID}"
  36. }
  37. videoselected() {
  38. read -r X Y W H <<-EOF
  39. $(slop -f "%x %y %w %h")
  40. EOF
  41. ffmpeg \
  42. -f x11grab \
  43. -framerate 60 \
  44. -video_size "${W}x${H}" \
  45. -i "${DISPLAY}+${X},${Y}" \
  46. -c:v libx264 -qp 0 -r 30 \
  47. "${HOME}/box-$(date '+%y%m%d-%H%M-%S').mkv" &
  48. echo "${!}" >"${PID}"
  49. }
  50. audio() {
  51. ffmpeg \
  52. -f alsa -i default \
  53. -c:a flac \
  54. "${HOME}/audio-$(date '+%y%m%d-%H%M-%S').flac" &
  55. echo "${!}" >"${PID}"
  56. }
  57. choices() {
  58. printf '%s\n' \
  59. 'video selected' \
  60. video \
  61. screencast \
  62. audio
  63. }
  64. asktoend() {
  65. response="$(
  66. printf '%s\n' No Yes \
  67. | dmenu -i -p "Recording still active. End recording?"
  68. )"
  69. case "${response}" in
  70. Y* | y*) killrecording ;;
  71. esac
  72. }
  73. main() {
  74. [ ! -f "${PID}" ] || {
  75. asktoend && exit
  76. }
  77. case "${1:-$(choices | dmenu -i -p 'Record:')}" in
  78. *selected) videoselected ;;
  79. video) video ;;
  80. screencast) screencast ;;
  81. audio) audio ;;
  82. kill) killrecording ;;
  83. *)
  84. echo 1>&2 "invalid action"
  85. exit 1
  86. ;;
  87. esac
  88. }
  89. main "${@}"