大家好,我找不到这个问题的解决方案,想知道你们能否帮助我。我正在寻找一种工具,它可以在后台运行并观看 twitch 流 URL,当直播开始时,它会下载到我的本地机器上。非常感谢您的帮助。
答案1
就您问题的下载部分而言,您可以使用直播者,例如:
livestreamer <livestream-url> best -o vod.mp4
通过 Twitch API 监控直播活动其实并不太难。例如,您可以执行一个简单的curl
请求,通过管道来grep
识别直播是否离线或是否存在其他类型的错误:
curl -s https://api.twitch.tv/kraken/streams/totalbiscuit | grep '"stream":null'
如果此刻没有流正在运行,则这将返回 true。
考虑到这一点,您可以设计一个简单的循环,每隔几分钟检查一次活动流,例如:
#!/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
或者,稍微复杂一点,并进行更多的健全性检查:
#!/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
要试用此脚本,请将上面的代码块复制并粘贴到一个新的空文本文件中,将其另存为twitch_poller.sh
或类似名称,然后通过文件管理器的“属性”菜单使其可执行(右键单击 → 属性 → 权限 →允许作为程序执行文件)。
确保已安装 livestreamer,然后从终端运行脚本,同时提供您想要监控的 twitch 频道,例如:
$ './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)
Interval
您可以通过设置脚本中的变量来调整轮询间隔。