#!/usr/bin/env python3
"""Pull ffmpeg's loudnorm measurement out of its stderr, as colon-joined fields.

Kept as a file rather than inline in the runner: a python heredoc nested inside
a shell heredoc collides on the terminator, and the failure mode is a shell
parse error a long way from the cause.
"""
import json
import re
import sys

match = re.search(r"\{[^{]*input_i.*?\}", sys.stdin.read(), re.S)
data = json.loads(match.group(0)) if match else {}
fields = ("input_i", "input_tp", "input_lra", "input_thresh", "target_offset")
# .strip() is load-bearing. ffmpeg pads these for sign alignment and emits
# `"target_offset" : "-0.82 "` — a TRAILING SPACE inside the JSON string. The
# caller interpolates the value straight into an ffmpeg filter
# (`offset=$m_off,linear=true`), and IFS=':' does not strip a space, so the
# graph becomes `offset=-0.82 ,linear=true` and ffmpeg refuses the whole
# filter: "Unable to parse offset option value". The film then fails to
# deliver, several steps away from the cause.
print(":".join(str(data.get(key, "")).strip() for key in fields))
