/
capture.sh
executable file
·79 lines (62 loc) · 1.86 KB
/
capture.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/bin/bash
# Probably broken
if [[ $# -lt 3 ]]; then
echo "Usage: $(basename "$0") <out.mp4> <width:height> <program...>"
echo "Captures OpenGL output."
exit 0
fi
RESULT=$1
if ! rm -f "$RESULT" && touch "$RESULT"; then
echo "Error: Cannot write to $RESULT"
exit 1
fi
WIDTH_HEIGHT=$2
shift 2
TMP_DIR=$(mktemp -d)
cd "$TMP_DIR" || exit 1
# shellcheck disable=SC2064
trap "rm -rf '$TMP_DIR'" EXIT
declare -a PIDS
echo 'Setting up fifo chain...'
mkfifo capture audio_source video_source video.y4m
#glc-capture --start -o >( \
# glc-play <(cat) -v 10 -y 1 -o - | \
# mplayer - &>/dev/null \
# ) --disable-audio "$@" &> /dev/null
#exit 1
# Duplicate the stream.
#tee video_source >audio_source <capture &
#(while :; do cat capture >video_source; done) &>/dev/null &
#PIDS=( ${PIDS[@]} $! )
glc-play ./capture -y 1 -o - | \
mplayer -vo yuv4mpeg:file=./video.y4m -vf scale="$WIDTH_HEIGHT" -nosound -benchmark - &>/dev/null &
PIDS=( "${PIDS[@]}" $! )
#cat audio_source > /dev/null &
#PIDS=( "${PIDS[@]}" $! )
#glc-play ./audio_source -a 1 -o - | \
# mplayer - &
#oggenc --output=/tmp/audio.ogg - &
#PIDS=( "${PIDS[@]}" $! )
/usr/bin/x264 --tune animation --preset medium --crf 28 --threads auto \
--output ./r.mp4 ./video.y4m &>/dev/null &
PIDS=( "${PIDS[@]}" $! )
echo 'Launching program...'
# shellcheck disable=SC2317
clean_up() {
echo "Cleaning up..."
rm -rf "$TMP_DIR"
exec &>/dev/null
for P in "${PIDS[@]}"; do kill "$P" &>/dev/null; done
sleep 0.1
for P in "${PIDS[@]}"; do kill -9 "$P" &>/dev/null; done
}
trap clean_up EXIT
glc-capture --start -o ./capture --disable-audio "$@" &> /dev/null
echo 'Waiting for processes to terminate...'
wait
# shellcheck disable=SC2064
trap "rm -rf '$TMP_DIR'" EXIT
rm -f "$RESULT"
MP4Box -add r.mp4 "$RESULT" &>/dev/null
echo "Done. Result: $RESULT ($(du -sh "$RESULT" | cut -f 1))"
exit 0