forked from prinsss/live-stream-recorder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecord_twitch.sh
39 lines (31 loc) · 1.24 KB
/
record_twitch.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
#!/bin/bash
# Twitch Live Stream Recorder
if [[ ! -n "$1" ]]; then
echo "usage: $0 twitch_id [format] [loop|once]"
exit 1
fi
# Record the best format available but not better that 720p by default
FORMAT="${2:-720p,480p,best}"
while true; do
# Monitor live streams of specific channel
while true; do
LOG_PREFIX=$(date +"[%Y-%m-%d %H:%M:%S]")
echo "$LOG_PREFIX Try to get current live stream of twitch.tv/$1"
# Get the m3u8 address with streamlink
M3U8_URL=$(streamlink --stream-url "twitch.tv/$1" "$FORMAT")
(echo "$M3U8_URL" | grep -q ".m3u8") && break
echo "$LOG_PREFIX The stream is not available now."
echo "$LOG_PREFIX Retry after 30 seconds..."
sleep 30
done
# Record using MPEG-2 TS format to avoid broken file caused by interruption
FNAME="twitch_${1}_$(date +"%Y%m%d_%H%M%S").ts"
echo "$LOG_PREFIX Start recording, stream saved to \"$FNAME\"."
echo "$LOG_PREFIX Use command \"tail -f $FNAME.log\" to track recording progress."
# Start recording
ffmpeg -i "$M3U8_URL" -codec copy -f mpegts "$FNAME" > "$FNAME.log" 2>&1
# Exit if we just need to record current stream
LOG_PREFIX=$(date +"[%Y-%m-%d %H:%M:%S]")
echo "$LOG_PREFIX Live stream recording stopped."
[[ "$3" == "once" ]] && break
done