Project

General

Profile

Random video under same channel number - streamlink called from python and stdout piped

Added by Tivi Hed 11 months ago

Hi All,

I'm trying to achieve situation where opening channel, let's say X, it calls streamlink to play random video from Youtube.
I've script which gets random URL for streamlink, so this portion is easy.

Where it gets complicated is that it should be random URL on each call and not statically written to m3u file which would be used by IPTV Automatic.

Problem is about piping, I'm failing to properly call streamlink and pipe it's output back to tvheadend.

Does any of you would be able to advise or point me on how to fix it?

Other option is that maybe I'm over complicating it and there's another solution to the problem? The only requirement is that it needs to be different video on each call. I've list of URL to pick from generated, let's say every day. Can also supply random URL on the call - just couldn't find a way to have either streamlink calling command to get URL, nor a way to execute a command prior calling streamlink within single channel opening process.

m3u entry looks like this:
```
pipe:///usr/bin/env /config/streamlinker/streamlink_piped.py /usr/bin/streamlink --stdout --hls-live-edge 6 --ringbuffer-size 32M -4 --default-stream 1080p,720p,best --url /config/streamlinker/long-list.m3u
```

The streamlink_piped.py is what calls long-list.m3u, picks random URL and then calls streamlink (as shown below) - this is where I fail.
Other idea was to call command prior streamlink, though none of tricks known to me worked.

Thanks for help.

```
def run_command(command, args):
full_command = [command] + args
  1. sys.stderr.write("Full command: {}\n".format(full_command)) # Debug print to stderr
    buffer_size = 16 * 1024 * 1024 # 16 megabytes
    try:
    with subprocess.Popen(full_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as process:
    while True: # chunk = process.stdout.read(buffer_size)
    chunk = process.stdout.read(1024)
    if not chunk:
    break
    sys.stdout.buffer.write(chunk)
    sys.stdout.flush()
  1. Wait for Streamlink to finish
    process.wait()
    except Exception as e:
    print(f"Error: {e}")
    finally:
    sys.stdout.flush()
    sys.stderr.flush()

```