-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner.sh
executable file
·157 lines (128 loc) · 3.98 KB
/
runner.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/bin/bash
# Set variables
RTMP_URL="$1" # URL for RTMP
RTMP_KEY="$2" # KEY for RTMP
SOURCE="$3" # UDP Source (see SAP ads)
# Function to handle sleeping
# Sleep until next quarter day (6 hours or less if started as odd time)
# Monitor Service and shutdown if silent error occurs
sleep_during_stream() {
declare -i quarterday=21600
timenow=$(date +'%s')
time_since_midnight=$(( ($timenow - 18000) % 86400 ))
difference=$(($timenow - $time_since_midnight))
SLEEPTIME=$0
echo "Time now: $(date +'%F %r')"
echo "Calculated timenow: $((timenow))"
echo "Quarter day: $((quarterday))"
echo "Calculated Time Since Midnight: $((time_since_midnight))"
echo "Calculated Difference: $((difference))"
if [ $time_since_midnight -lt $((0)) ]; then
echo "ERROR: difference is less than zero!"
exit 1
elif [ $time_since_midnight -lt $quarterday ]; then
echo "before 6am"
SLEEPTIME=$((quarterday - time_since_midnight))
elif [ $time_since_midnight -lt $(($quarterday*2)) ]; then
echo "between 6am and 12pm"
SLEEPTIME=$((quarterday*2 - time_since_midnight))
elif [ $time_since_midnight -lt $(($quarterday*3)) ]; then
echo "between 12pm and 6pm"
SLEEPTIME=$((quarterday*3 - time_since_midnight))
elif [ $time_since_midnight -lt $(($quarterday*4)) ]; then
echo "between 6pm and midnight"
SLEEPTIME=$((quarterday*4 - time_since_midnight))
else
echo "ERROR: time_since_midnight is greater than a day!"
exit 1
fi
echo stream for "$((SLEEPTIME))" seconds
NUMBER_OF_LOOPS=$((SLEEPTIME/60))
for(( index=0; index<NUMBER_OF_LOOPS; index++ ))
do
if ps -p $((pid)) > /dev/null
then
# Time to sleep
thisSleepCycleTime=60
# Check if ffmpeg is stuck sleeping (will check twice)
keepRunning="no"
for(( indexTwo=0; indexTwo<2; indexTwo++ ))
do
COMPARE_TIME=2
frameA=$(tail ${LOG_FILE} -n 1 | sed -nr 's/.*frame=(.*)fps.*/\1/p')
sleep $COMPARE_TIME
frameB=$(tail ${LOG_FILE} -n 1 | sed -nr 's/.*frame=(.*)fps.*/\1/p')
echo "$(date +'%F %r') - Comparing Frames: $frameA -> $frameB"
if [ "$frameA" = "$frameB" ]
then
echo "$(date +'%F %r') - Stream is hanging. Killing ffmpeg"
keepRunning="no"
else
echo "$(date +'%F %r') - Stream looks ok."
keepRunning="yes"
thisSleepCycleTime=$((thisSleepCycleTime-COMPARE_TIME))
break
fi
done
# If can keepRunning then sleep
if [ "$keepRunning" = "yes" ]
then
# Sleep, check again later
echo "[debug] sleeping "$thisSleepCycleTime" seconds"
sleep $((thisSleepCycleTime))
continue
fi
fi
echo "[debug] stream has stopped. beginning shutdown."
break # Abandon the loop.
done
}
# Ensure the STREAM URL is set
if [ -z "$1" ]; then
echo RTMP_URL was not passed in as an argument
exit 1
fi
# Ensure the STREAM KEY is set
if [ -z "$2" ]; then
echo RTMP_KEY was not passed in as an argument
exit 1
fi
# Ensure the VIDEO CAMERA is set
if [ -z "$3" ]; then
echo Camera SOURCE was not passed in as an argument
exit 1
fi
# RUN FFMPEG
while true
do
echo =====
echo Starting Live Stream
echo Time: "$(date +'%F %r')"
echo =====
# Set up log file
LOG_FILE=./stream-logs/ffmpeg-$(date +'%F').log
mkdir ./stream-logs/
# Pause to ensure new stream is started
# Youtube use 60 seconds, Twitch can be less
echo Pausing for 20 seconds to ensure a new stream is created by service provider
sleep 20
# Run ffmpeg
bash ./stream.sh $RTMP_URL $RTMP_KEY $SOURCE $LOG_FILE & pid=$!
echo =====
echo PID: "$((pid))"
echo =====
# wait from stream to start before running logical sleep
sleep 15
sleep_during_stream
# End Stream
echo =====
echo Ending Live Stream
echo Time: "$(date +'%F %r')"
echo -----
echo killing process..
pkill ffmpeg
sleep 8
echo ..done!
echo =====
done
exit 0