下载实时 Twitch 流

Downloading a live Twitch stream

我正在寻找一种方法来 "pipe"(抱歉,如果我在这里使用术语)将 Twitch 流式传输到正在流式传输的文件中。我知道可以在流完成后下载 VOD,但这不适用于我的用例。

我看过一个名为 streamlink 的库,它可以让我获得给定流的确切 url,但我有点不知道从这里去哪里

这是一个对我有用的解决方案:

首先,安装Streamlink。然后只需 运行 这个命令

streamlink -o <file name>.mkv <URL of the Twitch stream> best

将流保存到本地文件。

如果您想以编程方式实现此目的,您可以将 Streamlink pip 模块 (pip install streamlink) 与 ffmpeg.

结合使用

代码可能如下所示(在 Python 3 中):

import streamlink
from subprocess import Popen
from time import sleep

# get the URL of .m3u8 file that represents the stream
stream_url = streamlink.streams('https://www.twitch.tv/forsen')['best'].url
print(stream_url)

# now we start a new subprocess that runs ffmpeg and downloads the stream
ffmpeg_process = Popen(["ffmpeg", "-i", stream_url, "-c", "copy", 'stream.mkv'])

# we wait 60 seconds
sleep(60)

# terminate the process, we now have ~1 minute video of the stream
ffmpeg_process.kill()

youtube-dl 提供从流URL 获取播放列表的接口。以下 1 个衬里效果很好:

ffmpeg -i $( youtube-dl -f best --get-url twitch.tv/host ) -codec copy "out.mp4"

还有一个twitch-dl实用工具我没试过。