167 lines
3.9 KiB
Python
Executable File
167 lines
3.9 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
import sys
|
|
|
|
oldexcepthook = sys.excepthook
|
|
def newexcepthook(type,value,traceback):
|
|
oldexcepthook(type,value,traceback)
|
|
input("Press ENTER to quit.")
|
|
sys.excepthook = newexcepthook
|
|
|
|
import os
|
|
p = os.path.join
|
|
pUp = os.path.dirname
|
|
s = False
|
|
if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
|
|
s = os.path.realpath(sys.executable)
|
|
else:
|
|
s = os.path.realpath(__file__)
|
|
sp = pUp(s)
|
|
|
|
import platform
|
|
import configparser
|
|
import subprocess
|
|
import json
|
|
import shutil
|
|
import datetime
|
|
import math
|
|
|
|
print("Grabbing system info...")
|
|
pid = os.getpid()
|
|
osName = platform.system().lower()
|
|
if osName == "darwin": osName = "macos"
|
|
jsonSettings = [
|
|
"DEFAULT/output",
|
|
"DEFAULT/input"
|
|
]
|
|
|
|
def configToDict(conf):
|
|
out = {}
|
|
for group in conf:
|
|
out[group] = {}
|
|
for key in conf[group]:
|
|
out[group][key] = conf[group][key]
|
|
return out
|
|
|
|
def makeEnvironment(config = False):
|
|
print("Creating environment...")
|
|
cenv = {}
|
|
cenv["SP"] = sp
|
|
cenv["OS"] = osName
|
|
cenv["PID"] = pid
|
|
if config:
|
|
for group in config:
|
|
for key in config[group]:
|
|
value = config[group][key]
|
|
path = group+ "/" +key
|
|
cenv["CONF:" +path] = value
|
|
|
|
for env in os.environ:
|
|
cenv["SYS:" +env] = os.environ[env]
|
|
|
|
print(json.dumps(cenv,indent=2))
|
|
print("")
|
|
return cenv
|
|
|
|
def applyEnvironment(cenv,config):
|
|
print("Applying environment...")
|
|
found = True
|
|
while found == True:
|
|
found = False
|
|
for group in config:
|
|
for key in config[group]:
|
|
value = config[group][key]
|
|
for var in cenv:
|
|
varStr = "$" +var+ "$"
|
|
if varStr in value:
|
|
found = True
|
|
rpl = str(cenv[var])
|
|
if group + "/" + key in jsonSettings:
|
|
rpl = rpl.replace("\\","\\\\")
|
|
value = value.replace(varStr,rpl)
|
|
config[group][key] = value
|
|
print(json.dumps(configToDict(config),indent=2))
|
|
print("")
|
|
|
|
def waitForInput():
|
|
inp = sys.stdin.read(1)
|
|
return
|
|
|
|
def getTimeString():
|
|
now = datetime.datetime.now()
|
|
nowl = [
|
|
str(now.year),str(now.month),str(now.day),
|
|
str(now.hour),str(now.minute),str(now.second),
|
|
str(math.floor(now.microsecond / 10000))
|
|
]
|
|
|
|
index = 0
|
|
while (index < 7):
|
|
value = nowl[index]
|
|
if len(value) < 2: nowl[index] = "0" + value
|
|
index = index + 1
|
|
|
|
return "-".join(nowl[0:3]) + "_" + "-".join(nowl[3:6]) + "." +nowl[6]
|
|
|
|
def main():
|
|
# Setup
|
|
print("Loading configs...")
|
|
configPath = os.path.splitext(s)[0]
|
|
configPathOS = configPath + "-" +osName+ ".ini"
|
|
if not os.path.isfile(configPathOS):
|
|
print("Warning: No OS-specific config found (" +configPathOS+ "), falling back to linux defaults")
|
|
configPathOS = configPath + "-linux.ini"
|
|
configPath += ".ini"
|
|
|
|
config = configparser.ConfigParser(interpolation=None)
|
|
config.optionxform = str
|
|
config.read(configPath)
|
|
if os.path.isfile(configPathOS): config.read(configPathOS)
|
|
|
|
ffrEnv = makeEnvironment(config)
|
|
applyEnvironment(ffrEnv,config)
|
|
|
|
if os.path.isfile(config["DEFAULT"]["userConfig"]):
|
|
print("Reading user-specific config...")
|
|
config.read(config["DEFAULT"]["userConfig"])
|
|
ffrEnv = makeEnvironment(config)
|
|
applyEnvironment(ffrEnv,config)
|
|
|
|
cmdBinary = config["DEFAULT"]["binary"]
|
|
cmdInput = json.loads(config["DEFAULT"]["input"])
|
|
cmdOutput = json.loads(config["DEFAULT"]["output"])
|
|
|
|
# Main
|
|
if os.path.isdir(config["DEFAULT"]["temp"]):
|
|
shutil.rmtree(config["DEFAULT"]["temp"])
|
|
|
|
proc = False
|
|
try:
|
|
while True:
|
|
os.makedirs(config["DEFAULT"]["temp"])
|
|
proc = subprocess.Popen(
|
|
[cmdBinary] + cmdInput + cmdOutput,
|
|
stdin=subprocess.PIPE
|
|
)
|
|
|
|
waitForInput()
|
|
timeString = getTimeString()
|
|
proc.communicate(input=b'q')
|
|
proc.wait()
|
|
|
|
proc = subprocess.Popen([cmdBinary,"-f","concat","-safe","0","-i",p(config["DEFAULT"]["temp"],"segments.ffcat"),"-c","copy","-map","0",config["DEFAULT"]["outputPath"].replace("$TIME$",timeString) + ".mp4"])
|
|
proc.wait()
|
|
shutil.rmtree(config["DEFAULT"]["temp"])
|
|
except:
|
|
try:
|
|
proc.communicate(input=b'q')
|
|
proc.wait()
|
|
except:
|
|
pass
|
|
|
|
try:
|
|
shutil.rmtree(config["DEFAULT"]["temp"])
|
|
except Exception as e2:
|
|
raise e2
|
|
raise
|
|
|
|
main() |