forked from prinsss/live-stream-recorder
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrecord_twitcast.sh
129 lines (113 loc) · 2.97 KB
/
record_twitcast.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
#!/bin/sh
# TwitCasting Live Stream Recorder
if [ -z "${1}" ]; then
echo "usage: $0 twitcasting_id [loop|once] [interval]"
exit 1
fi
ID="${1}"
shift
if [ "${1}" = "loop" ] || [ "${1}" = "once" ]; then
LOOP="${1}"
shift
fi
case $1 in
'' | *[!0-9]*) INTERVAL=10 ;; # Not a number, use default
*)
INTERVAL="$1"
shift
;; # It's a number, proceed as normal
esac
echo "ID: ${ID}"
echo "LOOP: ${LOOP}"
echo "INTERVAL: ${INTERVAL}"
echo "ARGS: $*"
# Discord message with mention role
if [ -n "${DISCORD_WEBHOOK}" ]; then
_body="{
\"content\": \"${DISCORD_MENTION} Twitcasting monitor start! \nhttps://twitcasting.tv/${ID}/\",
\"embeds\": [],
\"components\": [
{
\"type\": 1,
\"components\": [
{
\"type\": 2,
\"style\": 5,
\"label\": \"Twitcasting GO\",
\"url\": \"https://twitcasting.tv/${ID}/\"
}
]
}
]
}"
curl -s -X POST -H 'Content-type: application/json' -d "$_body" "$DISCORD_WEBHOOK"
fi
while true; do
# Monitor live streams of specific user
while true; do
LOG_PREFIX=$(date +"[%m/%d/%y %H:%M:%S] [twitcasting@${ID}] ")
STREAM_API="https://twitcasting.tv/streamserver.php?target=${ID}&mode=client"
(curl -s "$STREAM_API" | grep -q '"live":true') && break
echo "$LOG_PREFIX [VRB] The stream is not available now. Retry after $INTERVAL seconds..."
sleep "$INTERVAL"
done
# Record using MPEG-2 TS format to avoid broken file caused by interruption
echo "$LOG_PREFIX [INFO] Start recording..."
echo "ID: ${ID}"
echo "ARGS: $*"
# Discord message with mention role
if [ -n "${DISCORD_WEBHOOK}" ]; then
_body="{
\"content\": \"${DISCORD_MENTION} Twitcasting Live Begins! \nhttps://twitcasting.tv/${ID}/\",
\"embeds\": [],
\"components\": [
{
\"type\": 1,
\"components\": [
{
\"type\": 2,
\"style\": 5,
\"label\": \"Twitcasting GO\",
\"url\": \"https://twitcasting.tv/${ID}/\"
}
]
}
]
}"
curl -s -X POST -H 'Content-type: application/json' \
-d "$_body" "$DISCORD_WEBHOOK"
fi
# Start recording
SCRIPT_DIR=$(dirname "$0")
if [ -z "$*" ]; then
"$SCRIPT_DIR/main.bin" "${ID}"
else
"$SCRIPT_DIR/main.bin" "$@" "${ID}"
fi
LOG_PREFIX=$(date +"[%m/%d/%y %H:%M:%S] [twitcasting@${ID}] ")
echo "$LOG_PREFIX [INFO] Stop recording ${ID}"
# Discord message with mention role
if [ -n "${DISCORD_WEBHOOK}" ]; then
_body="{
\"content\": \"Twitcasting Live is over! \nhttps://twitcasting.tv/${ID}/\",
\"embeds\": [],
\"components\": [
{
\"type\": 1,
\"components\": [
{
\"type\": 2,
\"style\": 5,
\"label\": \"Check live history\",
\"url\": \"https://twitcasting.tv/${ID}/show/\"
}
]
}
]
}"
curl -s -X POST -H 'Content-type: application/json' \
-d "$_body" "$DISCORD_WEBHOOK"
fi
# Exit if we just need to record current stream
[ "$LOOP" = "once" ] && break
done