-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
video.sh
executable file
·136 lines (127 loc) · 4.15 KB
/
video.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
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
#!/usr/bin/env bash
function create_pipe() {
if [[ "$UPLOAD_DESTINATION_PREFIX" != "false" ]];
then
echo "Create pipe if not exists for video upload stream"
if [ ! -p ${SE_VIDEO_FOLDER}/uploadpipe ];
then
mkfifo ${SE_VIDEO_FOLDER}/uploadpipe
fi
fi
}
create_pipe
function wait_util_force_exit_consume() {
if [[ "$UPLOAD_DESTINATION_PREFIX" != "false" ]];
then
while [[ -f ${SE_VIDEO_FOLDER}/force_exit ]]
do
echo "Waiting for force exit file to be consumed by uploader"
sleep 1
done
echo "Ready to shutdown the recorder"
fi
}
function add_exit_signal() {
if [[ "$UPLOAD_DESTINATION_PREFIX" != "false" ]];
then
echo "exit" > ${SE_VIDEO_FOLDER}/uploadpipe &
echo "exit" > ${SE_VIDEO_FOLDER}/force_exit
fi
}
function exit_on_max_session_reach() {
if [ $max_recorded_count -gt 0 ] && [ $recorded_count -ge $max_recorded_count ];
then
echo "Node will be drained since max sessions reached count number ($max_recorded_count)"
exit
fi
}
function finish {
add_exit_signal
wait_util_force_exit_consume
kill -INT `cat /var/run/supervisor/supervisord.pid`
}
trap finish EXIT
FRAME_RATE=${FRAME_RATE:-$SE_FRAME_RATE}
CODEC=${CODEC:-$SE_CODEC}
PRESET=${PRESET:-$SE_PRESET}
DISPLAY_CONTAINER_NAME=${DISPLAY_CONTAINER_NAME:-"localhost"}
export DISPLAY=${DISPLAY_CONTAINER_NAME}:${DISPLAY_NUM}.0
max_attempts=600
attempts=0
if [[ "$UPLOAD_DESTINATION_PREFIX" = "" ]]
then
echo Upload destination not known since UPLOAD_DESTINATION_PREFIX is not set. Exiting video recorder.
exit
fi
echo Checking if the display is open
until xset b off || [[ $attempts = $max_attempts ]]
do
echo Waiting before next display check
sleep 0.5
attempts=$((attempts+1))
done
if [[ $attempts = $max_attempts ]]
then
echo Can not open display, exiting.
exit
fi
VIDEO_SIZE=$(xdpyinfo | grep 'dimensions:' | awk '{print $2}')
recording_started="false"
video_file_name=""
video_file=""
prev_session_id=""
attempts=0
max_recorded_count=${SE_DRAIN_AFTER_SESSION_COUNT:-0}
recorded_count=0
echo Checking if node API responds
until curl -sk --request GET ${SE_SERVER_PROTOCOL}://${DISPLAY_CONTAINER_NAME}:${SE_NODE_PORT}/status || [[ $attempts = $max_attempts ]]
do
echo Waiting before next API check
sleep 0.5
attempts=$((attempts+1))
done
if [[ $attempts = $max_attempts ]]
then
echo Can not reach node API, exiting.
exit
fi
while curl -sk --request GET ${SE_SERVER_PROTOCOL}://${DISPLAY_CONTAINER_NAME}:${SE_NODE_PORT}/status > /tmp/status.json
do
session_id=$(jq -r '.[]?.node?.slots | .[0]?.session?.sessionId' /tmp/status.json)
echo $session_id
if [[ "$session_id" != "null" && "$session_id" != "" && "$recording_started" = "false" ]]
then
video_file_name="$session_id.mp4"
video_file="${SE_VIDEO_FOLDER}/$video_file_name"
echo "Starting to record video"
ffmpeg -nostdin -y -f x11grab -video_size ${VIDEO_SIZE} -r ${FRAME_RATE} -i ${DISPLAY} -codec:v ${CODEC} ${PRESET} -pix_fmt yuv420p $video_file &
recording_started="true"
echo "Video recording started"
elif [[ "$session_id" != "$prev_session_id" && "$recording_started" = "true" ]]
then
echo "Stopping to record video"
pkill -INT ffmpeg
recorded_count=$((recorded_count+1))
recording_started="false"
if [[ "$UPLOAD_DESTINATION_PREFIX" != "false" ]]
then
upload_destination=${UPLOAD_DESTINATION_PREFIX}/${video_file_name}
echo "Uploading video to $upload_destination"
echo $video_file $upload_destination >> ${SE_VIDEO_FOLDER}/uploadpipe &
fi
if [ $max_recorded_count -gt 0 ] && [ $recorded_count -ge $max_recorded_count ];
then
echo "Node will be drained since max sessions reached count number ($max_recorded_count)"
exit
fi
elif [[ $recording_started = "true" ]]
then
echo "Video recording in progress"
sleep 1
else
echo "No session in progress"
sleep 1
fi
prev_session_id=$session_id
done
echo "Node API is not responding, exiting."