URI: 
       record-game.sh - randomcrap - random crap programs of varying quality
  HTML git clone git://git.codemadness.org/randomcrap
   DIR Log
   DIR Files
   DIR Refs
   DIR README
   DIR LICENSE
       ---
       record-game.sh (2121B)
       ---
            1 #!/bin/sh
            2 # record X11 desktop to .webm file/stdout.
            3 # can record a specific window.
            4 #
            5 # Dependencies: ffmpeg for recording.
            6 #               xrandr for desktop dimensions.
            7 #               xdotool for specific window.
            8 #               xwininfo for selecting a window and getting the $WINDOWID.
            9 #
           10 # Docs:
           11 # - https://trac.ffmpeg.org/wiki/Capture/Desktop
           12 # - https://ffmpeg.org/ffmpeg-devices.html#x11grab
           13 
           14 spec="${DISPLAY:-:0.0}"
           15 
           16 # argument 1 is the $WINDOWID.
           17 win=""
           18 if test -n "$1"; then
           19         win="$1"
           20 fi
           21 
           22 # special parameter "sel": click on a window to get the window id and record it.
           23 if test x"$win" = x"sel"; then
           24         win=$(xwininfo | sed -nE 's/.*Window id: (0x[0-9a-f]+).*/\1/p')
           25         if test -z "$win"; then
           26                 echo "could not get window id" >&2
           27                 exit 1
           28         fi
           29 fi
           30 
           31 # get window geometry information if window id specified.
           32 if test -n "$win"; then
           33         # NOTE: a limitation is that the window should not be moved.
           34         eval $(xdotool getwindowgeometry --shell "$win")
           35         if test -z "$X" || test -z "$Y"; then
           36                 echo "unknown window size" >&2
           37                 exit 1
           38         fi
           39         BORDER="3" # workaround: don't record border: for me it's 3 pixels.
           40         X=$((X + BORDER))
           41         Y=$((Y + BORDER))
           42 #        WIDTH=$((WIDTH - BORDER))
           43 #        HEIGHT=$((HEIGHT - BORDER))
           44 
           45         spec="${spec}+$X,$Y"
           46         size="${WIDTH}x${HEIGHT}"
           47 else
           48         # record whole screen.
           49         size=$(xrandr --current | sed -nE 's/.*current ([0-9]+) x ([0-9]+).*/\1x\2/p')
           50         if test -z "$size"; then
           51                 echo "unknown monitor/desktop size" >&2
           52                 exit 1
           53         fi
           54 fi
           55 
           56 # with audio (alsa), stereo (2 channels):
           57 #withaudio=""
           58 
           59 
           60 # alsa, device: hw:CARD[,DEV[,SUBDEV]]
           61 # list: arecord -l, cat /proc/asound/cards, cat proc/asound/devices
           62 withaudio="-f alsa -ac 2 -i hw:1"
           63 
           64 #withaudio="-f pulse -ac 2 -i default"
           65 
           66 tm=$(date +'%Y%m%dT%H%M%S')
           67 
           68 filename="$HOME/tmp/recording_${tm}.mkv"
           69 
           70 # lossless recording, fast but huge filesize.
           71 ffmpeg $withaudio \
           72         -video_size "$size" -framerate 60 -f x11grab -i :0.0 -c:v libx264rgb -crf 0 -preset ultrafast -color_range 2 "$filename"
           73 
           74 printf 'Recorded to file: %s\n' "$filename"
           75 
           76 # NOTE: to re-encode recording to smaller file:
           77 # ffmpeg -i output.mkv -c:v libx264rgb -crf 0 -preset veryslow output-smaller.mkv