This repository has been archived by the owner on Jan 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path_saveCurrentStream.sh
executable file
·68 lines (54 loc) · 2.16 KB
/
_saveCurrentStream.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
#!/bin/bash
# by Max-Joseph Krempl (krempl@in.tum.de)
URL_REGEX="https://live\.rbg\.tum\.de/cgi-bin/streams/MW.001.*/COMB" # MW0001/MW2001 - Präsentation & Kamera
TIMEOUT="03:00:00" # Maximum recording length
DIR="$(dirname "$0")"
getStreamURL="$DIR/_getStreamURL.sh"
error() {
echo -e "$1"
exit 1
}
[ ! -f "$getStreamURL" ] && error "Couldn't find \"$getStreamURL\""
if [ $# -ge 1 ]
then
# extract stream url
URL_LIST=$(curl -s "https://live.rbg.tum.de/cgi-bin/streams" | grep "$URL_REGEX" | sed "s|.*\(http.*\)\".*|\1|g") && \
URL_TIMESTAMP=$(echo "$URL_LIST" | rev | cut -d'/' -f2 | cut -c-12 | rev | sort -r | head -n1) # Get most recent timestamp from the last 12 chars of the second to last path component
URL=$(echo "$URL_LIST" | grep "$URL_TIMESTAMP")
STREAM_URL=$($getStreamURL "$URL")
[[ "$STREAM_URL" = "" ]] && error "No stream url found. ($URL_REGEX)\nStream currently not live?"
[[ ! "$STREAM_URL" =~ https.*\.m3u8 ]] && error "Invalid stream url \"$STREAM_URL\""
# determine output file (avoid overwrite)
if [ $# -ge 2 ]
then NAME="$2"
else NAME="$(date +%Y-%m-%d)"
fi
FILE="$1/$NAME.mp4"
i=2
while [ -f "$FILE" ]
do
FILE="$1/${NAME}_$i.mp4"
((i+=1))
done
# save stream to output file
FFMPEG_INPUT="$DIR/.ffmpeg_input" ; echo "" > "$FFMPEG_INPUT"
<$FFMPEG_INPUT ffmpeg -hide_banner -loglevel info -stats -i "$STREAM_URL" -t $TIMEOUT -c copy -n "$FILE" & #-progress "$FFMPEG_OUTPUT"
FFMPEG_PID=$!
echo "-" > "$FFMPEG_INPUT" # decrease ffmpeg's loglevel verbosity to "warning" (after info was printed)
sleep 2
echo -e "\n\033[0;31m⬤\033[1m Recording\033[0m '$URL' stream to '$FILE'... ($(date +%H:%M:%S))\n(CRL-C to stop recording)\n"
interrupt_handler() {
[[ $INTERRUPTED -eq 1 ]] && return 0
INTERRUPTED=1
echo -e "\n\n\033[1m◼︎ Stopping recording...\033[0m ($(date +%H:%M:%S))"
#kill -INT $FFMPEG_PID
echo "q" > "$FFMPEG_INPUT" # tell ffmpeg to quit
wait $FFMPEG_PID
rm "$FFMPEG_INPUT"
}
INTERRUPTED=0
trap interrupt_handler SIGINT && wait $FFMPEG_PID
interrupt_handler # if ffmpeg quits naturally, also use interrupt_handler to perform cleanup
else
error "Usage: $(basename "$0") {output folder} [file name]"
fi