Update.
[scripts.git] / capslides.sh
1 #!/bin/bash
2
3 #########################################################################
4 # This program is free software: you can redistribute it and/or modify  #
5 # it under the terms of the version 3 of the GNU General Public License #
6 # as published by the Free Software Foundation.                         #
7 #                                                                       #
8 # This program is distributed in the hope that it will be useful, but   #
9 # WITHOUT ANY WARRANTY; without even the implied warranty of            #
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      #
11 # General Public License for more details.                              #
12 #                                                                       #
13 # You should have received a copy of the GNU General Public License     #
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.  #
15 #                                                                       #
16 # Written by and Copyright (C) Francois Fleuret                         #
17 # Contact <francois@fleuret.org> for comments & bug reports             #
18 #########################################################################
19
20 DESIRABLE_SIZE="1440,1080"
21 AUDIO_DEV="/dev/dsp1"
22 WINDOW_NAME="pdfpc - presentation"
23
24 ######################################################################
25
26 SLIDES_WIN=($(xwininfo -root -all | grep "${WINDOW_NAME}" | sed -e 's/^.*\")[\t ]*\([0-9]*\)x\([0-9]*\)+\([-0-9]*\)+\([-0-9]*\)[ \t]*+\([-0-9]*\)+\([-0-9]*\).*$/\1 \2 \3 \4 \5 \6/'))
27
28 if [[ ! "${SLIDES_WIN}" ]]
29 then
30     echo "Cannot find a \"${WINDOW_NAME}\" window"
31     exit 1
32 fi
33
34 SLIDES_W=${SLIDES_WIN[0]}
35 SLIDES_H=${SLIDES_WIN[1]}
36
37 # wmctrl -r "${WINDOW_NAME}" -b add,above,sticky -e "0,-1,-1,${DESIRABLE_SIZE}"
38
39 if [[ "${SLIDES_W},${SLIDES_H}" != "${DESIRABLE_SIZE}" ]]
40 then
41     echo "The window is not ${DESIRABLE_SIZE}"
42     exit 1
43 fi
44
45 let SLIDES_X=${SLIDES_WIN[2]}+${SLIDES_WIN[4]}
46 let SLIDES_Y=${SLIDES_WIN[3]}+${SLIDES_WIN[5]}
47
48 ######################################################################
49 # Move the window to the front and keep it there
50 wmctrl -r "${WINDOW_NAME}" -b add,above,sticky
51 ######################################################################
52
53 echo "Capturing ${SLIDES_W}x${SLIDES_H}+${SLIDES_X}+${SLIDES_Y}"
54
55 TAG_START="$(date +%d%m%y-%H%M%S)"
56 RECORDING_FILE="wincast-${TAG_START}.mp4"
57
58 # Fancy option to make web browser-friendly videos
59
60 ######################################################################
61 OPT_WEBCAM=(
62     "-vf"
63     "movie=/dev/video0, scale=320:-1, fps=30, setpts=N/(30*TB) [movie]; [in][movie] overlay=main_w-overlay_w-8:main_h-overlay_h-8 [out]"
64 )
65 # "movie=/dev/video0:f=video4linux2, scale=320:-1, fps, setpts=PTS-STARTPTS [movie]; [in][movie] overlay=main_w-overlay_w-8:main_h-overlay_h-8 [out]"
66 ######################################################################
67
68 #OPT_WHITE_FRAME=( '-vf' 'crop=1436:1076:2:2, pad=width=1440:height=1080:x=2:y=2:color=white' )
69
70 ffmpeg \
71     \
72     -thread_queue_size 1024 \
73     \
74     -guess_layout_max 0 -f oss -i "${AUDIO_DEV}" \
75     \
76     -f x11grab \
77     \
78     -vsync 1 -async 1 \
79     -r 30 -video_size ${SLIDES_W}x${SLIDES_H} -probesize 32M -i :0.0+${SLIDES_X},${SLIDES_Y} \
80     -codec:v libx264 -crf 22 -bf 2 -flags +cgop -pix_fmt yuv420p \
81     -codec:a aac -strict -2 -b:a 384k -r:a 48000 \
82     -movflags faststart \
83     \
84     "${OPT_WHITE_FRAME[@]}" \
85     \
86     "${RECORDING_FILE}"
87
88 FINAL_FILE="wincast-${TAG_START}-$(date +%H%M%S).mp4"
89
90 mv "${RECORDING_FILE}" "${FINAL_FILE}"
91
92 ######################################################################
93
94 duration=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "${FINAL_FILE}")
95
96 if [[ $(echo "${duration} < 10" | bc) == 1 ]]
97 then
98     echo "Video is less than 10s, moved to /tmp/"
99     mv "${FINAL_FILE}" /tmp/
100 fi
101
102 ######################################################################