我正在尝试在 PyCharm 中编写代码,以便在我最喜欢的 twitch 流媒体广播时录制流,使用以下代码:
import subprocess
import time
import os
import json
streamer_name = "pobr4tski"
quality = "best"
save_folder = "C:/Users/letha/twitch"
if not os.path.exists(save_folder):
os.mkdir(save_folder)
while True:
try:
channels = subprocess.check_output(["streamlink", "--json", f"https://www.twitch.tv/{streamer_name}", quality])
stream_data = json.loads(channels.decode("utf-8"))
stream_url = stream_data["streams"][quality]["url"]
file_name = f"{streamer_name} - {time.strftime('%Y-%m-%d %H-%M-%S')}.mp4" # Create a file name with the current date and time
file_path = os.path.join(save_folder, file_name)
print("Stream is live, starting recording...")
subprocess.call(["streamlink", stream_url, quality, "-o", file_path])
except:
print("Stream is offline, checking again in 60 seconds...")
time.sleep(60)
但是我只有输出
Stream is offline, checking again in 60 seconds...
,即使他在线并且正在广播,提前谢谢你
我已经卸载了 python 并再次下载了所有需要的 pip,但这并没有改变我的情况
第一步是检查触发异常的原因:
except Exception as e:
print("Stream is offline, checking again in 60 seconds...")
print(str(e))
time.sleep(60)
这将打印异常文本。请检查它的内容,也许可以将其添加到问题中来跟进。