Add gst-screencast.sh
[experiments/gstreamer.git] / shell / gst-screencast.sh
1 #!/bin/sh
2
3 # gst-screencast - screencasting of a window using GStreamer
4 #
5 # Copyright (C) 2015  Antonio Ospite <ao2@ao2.it>
6 #
7 # This program is free software. It comes without any warranty, to
8 # the extent permitted by applicable law. You can redistribute it
9 # and/or modify it under the terms of the Do What The Fuck You Want
10 # To Public License, Version 2, as published by Sam Hocevar. See
11 # http://sam.zoy.org/wtfpl/COPYING for more details.
12
13 set -e
14 set -x
15
16 [ "x" = "x$1" ] && { echo "usage: $(basename $0) <output_file> [<ximagesrc_options>]" 1>&2; exit 1; }
17
18 [ -f "$1" ] && { echo "ERROR: file already exists!" 1>&2; exit 2; }
19
20 FILENAME="$1"
21
22 if [ "x" = "x$2" ];
23 then
24   # uncomment to capure the whole frame with window manager border and decorations
25   XWININFO_OPTIONS="-frame"
26
27   WIN_INFO="$(xwininfo $XWININFO_OPTIONS)"
28
29   #XID=$(echo "$WIN_INFO" | grep "Window id" | cut -d ' ' -f 4)
30   #XIMAGESRC_ARGS="xid=$XID"
31
32   X=$(echo "$WIN_INFO" | sed -n -e "/^[[:space:]]*Absolute upper-left X:[[:space:]]*/s///p")
33   Y=$(echo "$WIN_INFO" | sed -n -e "/^[[:space:]]*Absolute upper-left Y:[[:space:]]*/s///p")
34   WIDTH=$(echo "$WIN_INFO" | sed -n -e "/^[[:space:]]*Width:[[:space:]]*/s///p")
35   HEIGHT=$(echo "$WIN_INFO" | sed -n -e "/^[[:space:]]*Height:[[:space:]]*/s///p")
36
37   BORDER_SIZE=27
38   XIMAGESRC_ARGS="startx=$(($X - $BORDER_SIZE)) starty=$(($Y - $BORDER_SIZE)) endx=$(($X + $WIDTH + $BORDER_SIZE)) endy=$(($Y + $HEIGHT + $BORDER_SIZE))"
39
40   XIMAGESRC_ARGS="startx=$(($X - $BORDER_SIZE - 1)) starty=$(($Y - $BORDER_SIZE - 7)) endx=$(($X + $WIDTH + $BORDER_SIZE)) endy=$(($Y + $HEIGHT + $BORDER_SIZE + 7))"
41 else
42   XIMAGESRC_ARGS="$2"
43 fi
44
45 VIDEO_CODEC="video/x-raw,format=I420 ! jpegenc quality=90"
46 #VIDEO_CODEC="openjpegenc"
47
48 gst-launch-1.0 -v \
49   ximagesrc use-damage=0 show-pointer=0 $XIMAGESRC_ARGS ! video/x-raw,framerate=25/1 ! \
50   videoconvert ! videorate ! $VIDEO_CODEC ! queue ! mux. \
51   matroskamux name=mux ! filesink location="$FILENAME"
52   #pulsesrc ! audioconvert ! 'audio/x-raw,rate=44100,channels=2' ! queue ! mux. \