#!/usr/bin/env python3 # -*- coding: utf-8 -*- from subprocess import check_output import configparser import os import os.path import re import sys import MosDb LOCKFILE = "/tmp/n24-loudness-analyse-running.$$$" if os.path.isfile(LOCKFILE): print("ERROR: Lockfile found! Previous process seems to be still running.", file=sys.stderr) print("ERROR: If you are sure no other analyse.py is running, delete: {}".format(LOCKFILE), file=sys.stderr) sys.exit(100) # Create lockfile open(LOCKFILE, "a").close() c = configparser.ConfigParser() c.read("config.ini") LOUDNESS_SCANNER = c.get("Tools", "LOUDNESS_SCANNER") CLIP_PATH_TRANSFORM_FROM = c.get("MOS", "CLIP_PATH_TRANSFORM_FROM") CLIP_PATH_TRANSFORM_TO = c.get("MOS", "CLIP_PATH_TRANSFORM_TO") TARGET_LUFS = float(c.get("LUFS", "TARGET_VALUE")) print("Loudness-Scanner: {}".format(LOUDNESS_SCANNER)) print("Path transform FROM: {}".format(CLIP_PATH_TRANSFORM_FROM)) print("Path transform TO : {}".format(CLIP_PATH_TRANSFORM_TO)) db = MosDb.MosDb() todo = db.get_missing_lufs() print("{} files to analyse.".format(len(todo))) devnull = open(os.devnull, "w") for f in todo: objId = f[0] objPath = f[1] objFile = objPath.replace(CLIP_PATH_TRANSFORM_FROM, CLIP_PATH_TRANSFORM_TO).replace("\\", "/") if not os.path.isfile(objFile): print("ERROR: File not found: {}".format(objFile), file=sys.stderr, flush=True) continue cmd = [LOUDNESS_SCANNER, "scan", "-l", objFile] output = check_output(cmd, stderr=devnull) output = output.decode('utf-8') lines = output.split("\n") valid = re.compile(r"^\s*(-.+) LUFS, (.+) LU, (.*)$") have_lufs = False for l in lines: m = valid.match(l) if not m: continue lufs = m.group(1) lra = m.group(2) print("Found LUFS for {}: {} (LRA: {} LU)".format(objId, lufs, lra), end="", flush=True) if lufs == "-inf": lufs = -99.0 lra = 0.0 ldiff = 99.0 else: # FIXME: Add 3.0 LU to accomodate for loudness-scanner only analysing mono channel lufs = float(lufs) + 3.0 lra = float(lra) ldiff = lufs - TARGET_LUFS print(", diff to {}: {}".format(TARGET_LUFS, ldiff), flush=True) db.set_lufs(objId, lufs, lra, ldiff) have_lufs = True break if not have_lufs: print("ERROR analysing file for {}: {}".format(objId, objFile), flush=True) db.finish() os.remove(LOCKFILE)