-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaptureInit.sh
More file actions
executable file
·153 lines (117 loc) · 4.18 KB
/
Copy pathcaptureInit.sh
File metadata and controls
executable file
·153 lines (117 loc) · 4.18 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#! /bin/bash
# Start an ffmplay process to display or record video from a loopback device.
_now=$(printf "%(%Y%m%d-%H%M%S)T" -1)
progn=${0##*/} # basename
cd "${0%/*}" || exit 1 # Need to be in dirname for this script to work
ofilen="$_now.mkv"
pi () {
echo "[$progn] $*" >&2
}
[ -f /tmp/cameraInit.sh-vars.txt ] && . /tmp/cameraInit.sh-vars.txt
VID1=${VID1:-10} # Base number for display loopback devices.
VID2=${VID2:-11} # Base number for record loopback devices.
DISPLAY_INPUT="/dev/video${VID1}" # Loopback video device to display.
RECORD_INPUT="/dev/video${VID2}" # Loopback video device to record.
usage () {
cat <<_EOL_
Usage: $progn [display|record] <options>
Start an ffmplay process to display video from a loopback device,
or ffmpeg to record video from a loopback device.
Options:
-q Remove display/record from loopback device
and exit if not in use.
-i device Loopback video device to display/record.
Should be created by cameraInit.sh.
Current device settings:
DISPLAY_INPUT=$DISPLAY_INPUT
RECORD_INPUT=$RECORD_INPUT
-o filename Output filename for recording. Default is
YYYYMMDD-HHMM.mkv in the current directory.
_EOL_
exit 1
}
mode="$1" ; shift
case "$mode" in
display) inputDev="$DISPLAY_INPUT" ;;
record) inputDev="$RECORD_INPUT" ;;
*) usage ;;
esac
PIDFILE="/tmp/${progn}-${mode}.pid"
removeFromLoopback () {
# Remove display/record from loopback device and exit if not in use.
pi "$mode: request to quit"
if [ -f $PIDFILE ] ; then
PID=$(cat $PIDFILE)
# Sometimes ffmpeg just doesn't listen...
for sig in TERM HUP INT QUIT KILL ; do
if ps -p $PID > /dev/null 2>&1 ; then
pi "L$LINENO -- $mode process with PID $PID" \
" is still running. Killing with -$sig."
kill -$sig $PID
sleep .1
else
pi "L$LINENO -- $mode process with PID $PID is "\
"not running. Removing PID file."
[ -f $PIDFILE ] && rm $PIDFILE
exit 0
fi
done
# If we get here, something's wrong
pi "!!! Can't stop $PID; bailing !!!" ; exit 2
else
pi "at $LINENO - $mode no pid file; apparently not running."
pi "Nothing to remove."
exit 0
fi
}
# Handle command line options.
while getopts "qi:o:" opt; do
case $opt in
q) removeFromLoopback ;;
i) inputDev="$OPTARG" ;;
o) ofilen="$OPTARG" ;;
*) usage ;;
esac
done
shift $((OPTIND -1))
# Check if mode is already running. We return success.
[ -f "$PIDFILE" ] && {
pi "At line $LINENO -- $mode already running. Use -q to remove." ; exit 0 ; }
# cameraInit.sh running?
[ -f /tmp/cameraInit.sh.pid ] || {
pi "cameraInit.sh not running -- exiting." ; exit 1 ; }
# Check if loopback video device exists.
[ -c "$inputDev" ] || {
pi "At line $LINENO -- loopback video device $inputDev not found." ; exit 1 ; }
main() {
case "$mode" in
display) pi "Starting display from $inputDev..."
ffplay -hide_banner -loglevel error "$inputDev" &
;;
record) pi "Starting record from $inputDev..."
ffmpeg -hide_banner -loglevel error -nostdin -f v4l2 -i "$inputDev" -c copy -f matroska "$ofilen" &
;;
esac
PID=$!
pi "Started ${mode}ing with PID $PID"
echo $PID > $PIDFILE
cleanup() {
trap - TERM
pi "($mode) Trap signal [$1] rcvd, cleanup called."
$0 $mode -q
}
ignore () {
pi "($mode) ignoring kill signal $1"
}
trap 'cleanup TERM' TERM
trap 'ignore HUP' HUP
trap 'ignore EXIT' EXIT
trap 'ignore INT' INT
wait $PID
# We got here because ffplay or ffmpeg exited
pi "$mode - $PID exited."
[ -f $PIDFILE ] && rm $PIDFILE
}
main &
mainPid=$!
pi "Started main() $mode function in background with PID $mainPid"