我正在寻找一种方法来“管道”一个Twitch流到一个文件中,因为它正在被流传送(抱歉,如果我在这里胡乱使用术语)。我知道可以在流完成后下载VODs,但这在我的用例中不适用。
我看过一个名为streamlink的库,它可以让我获得给定流的确切url,但是我有点迷茫,不知道从哪里开始
发布于 2019-03-17 00:06:48
这是一个对我有效的解决方案:
首先,安装Streamlink。然后只需运行以下命令
streamlink -o <file name>.mkv <URL of the Twitch stream> best
将流保存到本地文件。
如果你想通过编程来实现这一点,你可以结合ffmpeg使用Streamlink pip模块(pip install streamlink)。
下面是代码可能的样子(在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()发布于 2020-10-10 10:38:16
youtube-dl提供了用于从流URL获取播放列表的接口。下面的1个衬垫运行良好:
ffmpeg -i $( youtube-dl -f best --get-url twitch.tv/host ) -codec copy "out.mp4"
还有一个我还没有尝试过的twitch-dl实用程序。
https://stackoverflow.com/questions/54843726
复制相似问题