Hi guys im not been able to find a solution to this issue and wonder if you can help me. Im looking for a tool that will sit in the background and watch a twitch stream URL and when the live broadcast starts it will download to my local machine. your help would be greatly appreciated.
1 Answer
As far as the downloading part of your question is concerned you can use livestreamer, e.g.:
livestreamer <livestream-url> best -o vod.mp4
For installation instructions check out this answer by @henry.
It actually shouldn't be too difficult to monitor stream activity via the Twitch API. For instance, you could perform a simple curl
request piped to grep
to identify if the stream is offline or if there's another type of error:
curl -s https://api.twitch.tv/kraken/streams/totalbiscuit | grep '"stream":null'
This will return as true if no stream is running at the moment.
With that in mind you could design a simple loop that would check for active streams every few minutes, e.g.:
#!/bin/bash
Channel="totalbiscuit"
while sleep 60; do
if ! curl -s "https://api.twitch.tv/kraken/streams/$Channel" | grep -q '"stream":null'; then
echo "$Channel is live. Downloading stream..."
livestreamer "http://www.twitch.tv/$Channel" best -o "${Channel}_livestream.mp4"
else
echo "$Channel is offline"
fi
done
Or, slightly more complex and with more sanity checks:
#!/bin/bash
# Simple Twitch Poller
# Author: Glutanimate
# License: GPL v3
# Dependencies: livestreamer
#
# Description: Polls twitch channel status and downloads stream if user is online
Usage="$0 <space-separated list of twitch channels>"
Channels=($@)
Interval="60" # polling interval in seconds
if [[ -z "$Channels" ]]; then
echo "Error: No channels provided"
echo "Usage: $Usage"
exit 1
fi
while true; do
for i in ${Channels[@]}; do
StreamData="$(curl -s "https://api.twitch.tv/kraken/streams/$i")"
if echo "$StreamData" | grep -q '"status":404'; then # 404 Error
echo "Error: $i does not exist."
break 2
elif echo "$StreamData" | grep -q '"stream":null'; then # Channel offline
echo "$i is offline."
else # Channel online
echo "$i is live. Downloading stream..."
livestreamer "http://www.twitch.tv/$i" best -o "$(date +"${i}_TwitchVOD_%Y-%m-%d_%H%M%S.mp4")"
fi
done
sleep "$Interval"
done
To try this script out, copy and paste the codeblock above into a new empty text file, save it as twitch_poller.sh
or something similar and make it executable via the Properties menu of your file manager (Right click → Properties → Permissions → Allow executing file as program).
Make sure you have livestreamer installed, then run the script from a terminal while providing the twitch channels you want to monitor, e.g.:
$ './twitch_poller.sh' totalbiscuit TSM_Dyrus
totalbiscuit is offline.
TSM_Dyrus is live. Downloading stream...
[cli][info] Found matching plugin twitch for URL http://www.twitch.tv/TSM_Dyrus
[cli][info] Available streams: audio, high, low, medium, mobile (worst), source (best)
[cli][info] Opening stream: source (hls)
[download][..D_2014-11-07_001503.mp4] Written 3.1 MB (6s @ 460.6 KB/s)
You can adjust the polling interval by setting the Interval
variable in the script.
-
Hi thanks so much for the answer could you possibly place this on a service such as GitHub so I can do a tutorial? Nov 9, 2014 at 20:48
-
@MatthewParker Sure thing! I've placed it in my miscellaneous repo on Github. Nov 9, 2014 at 22:02