Auto stream capture not detecting live broadcasts

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?

Your subprocess command has the wrong streamlink syntax. When you run subprocess.check_output(["streamlink", "--json", f"https://www.twitch.tv/{channel_name}", video_quality]), streamlink doesn’t return JSON about stream availability - it tries to process the stream and crashes if it’s offline. I ran into this same issue building my capture setup. First check if the stream’s live with subprocess.check_output(["streamlink", "--json", f"https://www.twitch.tv/{channel_name}"]) (drop the quality parameter). Parse that response to see if streams exist, then run your actual recording command. Also double-check that streamlink has the right auth tokens if those streams need them.

you’re using the wrong streamlink approach. i had the same issues - turns out streamlink --json doesn’t work like you’d expect. skip the subprocess mess and just run streamlink --retry-streams 5 https://twitch.tv/channelname best -o filename.mp4. the --retry-streams flag will automatically check if the stream’s offline and start recording when it goes live.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.