Skip to content

Commit

Permalink
Merge branch 'master' into retron_fix
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-ong committed Jul 19, 2020
2 parents 8ae9e8c + 220f666 commit 7d70e54
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 24 deletions.
25 changes: 20 additions & 5 deletions scripts/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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
```



Expand Down
73 changes: 54 additions & 19 deletions scripts/restream.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys
import os
from subprocess import Popen, PIPE
import argparse
import time
import re

Expand All @@ -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

Expand All @@ -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()

Expand Down Expand Up @@ -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()

Expand All @@ -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))

0 comments on commit 7d70e54

Please sign in to comment.