diff --git a/scripts/readme.md b/scripts/readme.md index 7b0f55f..175175b 100644 --- a/scripts/readme.md +++ b/scripts/readme.md @@ -4,17 +4,24 @@ Tetris and OCR related scripts will be found in this folder and documented in th `scripts/restream.py` is a script to allow a restreamer to quickly get started to OCR from a CTM Stencil-ready twitch stream. -This can alow a restreamer to compute stats and redraw a stream, or even render a 1v1 competition while computing score differentials against players. +This can allow a restreamer to compute stats and redraw a stream, or even render a 1v1 competition while computing score differential between players. + +To run this script, you are expected to have downloaded and installed the following CLI tools: +* [streamlink](https://streamlink.github.io/install.html) +* [vlc](https://www.videolan.org/index.html) +* [ffmpeg](https://ffmpeg.org/download.html) + The script can be invoked from NESTrisOCR's root folder as: -``` +```bash python -m scripts.restream PLAYER_1_TWITCH_USER_NAME 1 # and for player 2 python -m scripts.restream PLAYER_1_TWITCH_USER_NAME 2 ``` -To test this, visit https://www.twitch.tv/directory/game/Tetris to see who is streaming with Stencil. +To test this, visit https://www.twitch.tv/directory/game/Tetris to see who is streaming with CTM Stencil. Select the twitch user name of a player and use it in the command for player 1 above. + The script does the following: @@ -26,12 +33,20 @@ The script does the following: * Starts NESTrisOCR with that config -The VLC servers will be accessible from the following 2 URLs (for player 1 and 2 respectively) +The VLC servers will be accessible locally from the following 2 URLs (for player 1 and 2 respectively): * http://localhost:8081 for player 1 * http://localhost:8082 for player 2 -NESTrisOCR will push the game data to ports 4001 and 4002 for player 1 and player 2 respectively, where a suitable renderer may be listening (like [NESTrisStatsUI](https://github.com/timotheeg/NESTrisStatsUI)) . + +NESTrisOCR will push the game data to ports 4001 and 4002 for player 1 and player 2 respectively, where a suitable renderer may be listening (like [NESTrisStatsUI](https://github.com/timotheeg/NESTrisStatsUI)). + + +Note: running the local VLC server on windows doesn't seem to work very well (for some reason???). You can forgo VLC and have NESTrisOCR read straight from the twitch stream. This can be done by adding the CLI arg `--novlc` like this: + +```bash +python -m scripts.restream --novlc PLAYER_1_TWITCH_USER_NAME 1 +``` diff --git a/scripts/restream.py b/scripts/restream.py index 81a144d..823ffda 100644 --- a/scripts/restream.py +++ b/scripts/restream.py @@ -1,6 +1,6 @@ -import sys import os from subprocess import Popen, PIPE +import argparse import time import re @@ -18,11 +18,11 @@ ] -def setup_player(twitch_name, player_num): - local_stream_port, ocr_dest_port = PLAYER_SETTINGS[player_num - 1] +def setup_player(twitch_name, player_num, local_vlc=True): + print("Setting up player", twitch_name, player_num, local_vlc) + local_stream_port, ocr_dest_port = PLAYER_SETTINGS[player_num - 1] twitch_url = "twitch.tv/{}".format(twitch_name) - local_url = "http://localhost:{}".format(local_stream_port) fps = -1 @@ -39,23 +39,47 @@ def setup_player(twitch_name, player_num): # ================================== # 1. run local restreamer over http - Popen( - [ - "streamlink", - twitch_url, - ",".join(resolutions), - "--player", - "vlc --intf dummy --sout '#standard{access=http,mux=mkv,dst=localhost:" - + str(local_stream_port) - + "}'", - ] - ) - time.sleep(10) + if local_vlc: + source_url = "http://localhost:{}".format(local_stream_port) + + Popen( + [ + "streamlink", + twitch_url, + ",".join(resolutions), + "--hds-live-edge", + "1", + "--hls-live-edge", + "1", + "--player", + "vlc --intf dummy --sout '#standard{access=http,mux=mkv,dst=localhost:" + + str(local_stream_port) + + "}'", + ] + ) + + time.sleep(10) + + else: + p = Popen( + ["streamlink", twitch_url, ",".join(resolutions), "--stream-url"], + stdout=PIPE, + stderr=PIPE, + ) + + stdout, stderr = p.communicate() + + source_url = stdout.decode("UTF-8").strip() + + if not source_url: + raise Exception("No Stream found") + + print("Twitch UR found", source_url) # ================================== # 2. read stream details with ffmpeg - p = Popen(["ffmpeg", "-i", local_url], stdout=PIPE, stderr=PIPE) + p = Popen(["ffmpeg", "-i", source_url], stdout=PIPE, stderr=PIPE) stdout, stderr = p.communicate() @@ -98,7 +122,7 @@ def setup_player(twitch_name, player_num): round(height * CAP_RATIOS[1]), ] config["network.port"] = ocr_dest_port - config["capture.source_id"] = local_url + config["capture.source_id"] = source_url config.save() @@ -113,4 +137,15 @@ def setup_player(twitch_name, player_num): if __name__ == "__main__": - setup_player(sys.argv[1], int(sys.argv[2])) + parser = argparse.ArgumentParser() + parser.add_argument( + "--novlc", + default=False, + action="store_true", + help="Don't run a local VLC server from the twitch stream", + ) + parser.add_argument("twitch_name") + parser.add_argument("player_num", type=int) + args = parser.parse_args() + + setup_player(args.twitch_name, args.player_num, local_vlc=(not args.novlc))