-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaptureInit.sh
More file actions
executable file
·154 lines (123 loc) · 4.81 KB
/
captureInit.sh
File metadata and controls
executable file
·154 lines (123 loc) · 4.81 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
152
153
#! /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%/*} # Need to be in dirname for this script to work
ofilen="$_now.mkv"
pe () {
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.
-f Force remove display/record from loopback device
without checking if it is 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"
# Handle command line options.
while getopts "qfd:o:" opt; do
case $opt in
q) # Remove display/record from loopback device and exit if not in use.
if [ -f $PIDFILE ] ; then
PID=$(cat $PIDFILE)
if ps -p $PID > /dev/null 2>&1 ; then
pe "At line $LINENO -- $mode process with PID $PID is still running. Removing $mode from loopback device."
kill -TERM $PID || {
pe "Error killing $mode process with PID $PID. Exiting."
exit 1
}
rm -f $PIDFILE
exit 0
else
pe "At line $LINENO -- $mode process with PID $PID is not running. Removing PID file."
rm -f $PIDFILE
exit 0
fi
else
pe "At line $LINENO -- $mode not running. Nothing to remove."
exit 0
fi
;;
f) # Force remove display from loopback device without checking if it is in use.
if [ -f $PIDFILE ] ; then
PID=$(cat $PIDFILE)
if ps -p $PID > /dev/null 2>&1 ; then
pe "At line $LINENO -- $mode process with PID $PID is still running. Force removing $mode from loopback device."
kill -TERM $PID || {
pe "Error killing $mode process with PID $PID. Exiting."
exit 1
}
rm -f $PIDFILE
exit 0
else
pe "At line $LINENO -- $mode process with PID $PID is not running. Removing PID file."
rm -f $PIDFILE
exit 0
fi
else
pe "At line $LINENO -- $mode not running. Nothing to remove."
exit 0
fi
;;
i) inputDev="$OPTARG" ;;
o) ofilen="$OPTARG" ;;
*) usage ;;
esac
done
shift $((OPTIND -1))
# Check if mode is already running.
[ -f $PIDFILE ] && {
pe "At line $LINENO -- $mode already running. Use -q to remove." ; exit 1 ; }
# cameraInit.sh running?
[ -f /tmp/cameraInit.sh.pid ] || {
pe "cameraInit.sh not running -- exiting." ; exit 1 ; }
# Check if loopback video device exists.
[ -c $inputDev ] || {
pe "At line $LINENO -- loopback video device $inputDev not found." ; exit 1 ; }
main() {
case "$mode" in
display) pe "Starting display from $inputDev..."
ffplay -hide_banner -loglevel error $inputDev &
;;
record) pe "Starting record from $inputDev..."
ffmpeg -hide_banner -loglevel error -nostdin -f v4l2 -i $inputDev -c copy -f matroska $ofilen &
;;
esac
PID=$!
pe "Started ${mode}ing with PID $PID"
pe $PID > $PIDFILE
cleanup() {
trap - HUP TERM INT EXIT
$0 $mode -q
}
trap cleanup HUP TERM INT EXIT
wait $PID
}
main &
mainPid=$!
pe "Started main function in background with PID $mainPid"