x d*_*x d 3 video-streaming twitch
我正在寻找一种方法来将 Twitch 流“通过管道”(抱歉,如果我在这里滥用术语)将其传输到正在传输的文件中。我知道可以在流完成后下载 VOD,但这不适用于我的用例。
我看过一个名为streamlink的库,它可以让我获得给定流的确切URL,但我有点迷失了从这里去哪里
这是一个对我有用的解决方案:
首先,安装Streamlink。然后只需运行这个命令
streamlink -o <file name>.mkv <URL of the Twitch stream> best
将流保存到本地文件。
如果您想以编程方式实现此目的,您可以将 Streamlink pip 模块 ( ) 与ffmpegpip
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()
Run Code Online (Sandbox Code Playgroud)