Reformat files

This commit is contained in:
Santiago Lo Coco 2024-11-21 00:30:00 +01:00
parent 1ea363ecbd
commit 255961b944
2 changed files with 28 additions and 14 deletions

View File

@ -1,6 +1,7 @@
import os
import re
def load_env(filename=".env"):
env_vars = {}
with open(filename) as f:
@ -10,14 +11,16 @@ def load_env(filename=".env"):
env_vars[key] = value
return env_vars
def count_servers(env_vars):
server_keys = [key for key in env_vars if re.match(r"PORT_\d+", key)]
return len(server_keys)
def generate_docker_compose(env_vars, num_servers):
ip = env_vars.get("IP")
services = []
for i in range(1, num_servers + 1):
port = env_vars.get(f"PORT_{i}")
listener_port = env_vars.get(f"LISTENER_PORT_{i}")
@ -34,32 +37,35 @@ def generate_docker_compose(env_vars, num_servers):
- ./mediamtx/mediamtx_{i}.yml:/mediamtx.yml
"""
services.append(service)
compose_content = f"""
name: streaming
services:
{''.join(services)}
"""
with open("docker-compose.yml", "w") as f:
f.write(compose_content)
def generate_mediamtx_files(env_vars, num_servers):
# os.makedirs("mediamtx")
mediamtx_dir = "mediamtx"
if not os.path.exists(mediamtx_dir):
os.makedirs(mediamtx_dir)
with open("templates/mediamtx.yml", "r") as template_file:
template_content = template_file.read()
for i in range(1, num_servers + 1):
port = env_vars.get(f"PORT_{i}")
listener_port = env_vars.get(f"LISTENER_PORT_{i}")
generated_content = template_content.replace("{{WEBRTC_PORT}}", port).replace("{{WEBRTC_LISTENER_PORT}}", listener_port)
generated_content = template_content.replace("{{WEBRTC_PORT}}", port).replace(
"{{WEBRTC_LISTENER_PORT}}", listener_port
)
with open(f"mediamtx/mediamtx_{i}.yml", "w") as f:
f.write(generated_content)
@ -71,20 +77,21 @@ def generate_mediamtx_files(env_vars, num_servers):
os.remove(f"mediamtx/{file}")
print(f"Removed extra file: {file}")
def generate_start_bat(num_servers):
with open("templates/start.bat", "r") as template_file:
bat_template = template_file.read()
obs_instances = ""
for i in range(1, num_servers + 1):
obs_instance = f"""
obs_instance = f""" # noqa: E501
echo Launching OBS instance {i}
start /MIN "OBS{i}" /D "C:\\Program Files\\obs-studio\\bin\\64bit" "obs64.exe" --profile "Profile{i}" --collection "Profile{i}" --multi --startstreaming
timeout /t 1 >nul
"""
obs_instances += obs_instance
bat_content = bat_template.replace("{{OBS_INSTANCES}}", obs_instances)
with open("start.bat", "w") as f:
f.write(bat_content)

View File

@ -1,13 +1,20 @@
import os
from config import *
from config import (
count_servers,
generate_docker_compose,
generate_mediamtx_files,
generate_start_bat,
load_env,
)
def main():
env_vars = load_env(".env")
num_servers = count_servers(env_vars)
generate_docker_compose(env_vars, num_servers)
generate_mediamtx_files(env_vars, num_servers)
generate_start_bat(num_servers)
if __name__ == "__main__":
main()