I need help with my Python script that should automatically capture live streams. The script keeps saying the channel is offline even when I can see it’s actually streaming.
import subprocess
import time
import os
import json
channel_name = "example_streamer"
video_quality = "720p"
output_directory = "/home/user/recordings"
if not os.path.exists(output_directory):
os.makedirs(output_directory)
while True:
try:
result = subprocess.check_output(["streamlink", "--json", f"https://www.twitch.tv/{channel_name}", video_quality])
json_data = json.loads(result.decode("utf-8"))
video_stream = json_data["streams"][video_quality]["url"]
timestamp = time.strftime('%Y%m%d_%H%M%S')
output_file = f"{channel_name}_{timestamp}.mp4"
full_path = os.path.join(output_directory, output_file)
print("Found active stream, beginning capture...")
subprocess.call(["streamlink", video_stream, video_quality, "-o", full_path])
except:
print("No active stream found, retrying in 60 seconds...")
time.sleep(60)
The problem is that it always shows “No active stream found” message and never starts recording. I already tried reinstalling Python and all the packages but nothing changed. What could be wrong with my approach?