-
[Script] Check media API sidecars before importing clips into an Unraid share
A media importer can write a JSON result before the video finishes arriving. If a nightly job treats that first file as proof the clip is ready, it can try to move an incomplete pair. This small check lists the pairs and the exceptions before anything touches the library share. The input is a flat staging share with local names such as clip-001.json and clip-001.mp4. Use an ID you generate yourself and store it in the sidecar as {"clip_id":"clip-001"}. Keep the raw provider response outside the share. The report prints only the local ID and a status. It never opens a video for reading, fetches a URL, or writes to the staging share. For consistency, have the importer keep in-progress files under temporary extensions and rename each file after it's complete. I maintain EasyDown. Its API documentation is an upstream reference if your authorized clips start as public post URLs. The example runs on locally generated metadata and makes no billable requests. Codex assisted with the script and write-up. Save this as audit_intake.py. """Report local clip pairs from a staging directory, one JSON object per line.""" import argparse import json from pathlib import Path import re def status_for(clip_id, metadata, video): if metadata is None: return "missing_metadata" if metadata.is_symlink() or (video is not None and video.is_symlink()): return "symlink" if not metadata.is_file(): return "invalid_metadata" try: with metadata.open("rb") as source: data = json.loads(source.read(4097)) if source.tell() > 4096: return "invalid_metadata" except (OSError, ValueError, RecursionError): return "invalid_metadata" if not isinstance(data, dict) or data.get("clip_id") != clip_id: return "invalid_metadata" if video is None or not video.is_file(): return "missing_video" if video.stat().st_size == 0: return "empty_video" return "paired" def audit(directory): ids = {} for entry in directory.iterdir(): if entry.suffix in (".json", ".mp4") and re.fullmatch( r"clip-[0-9]{3,8}", entry.stem ): ids.setdefault(entry.stem, {})[entry.suffix] = entry for clip_id, files in sorted(ids.items()): yield { "clip_id": clip_id, "status": status_for(clip_id, files.get(".json"), files.get(".mp4")), } if __name__ == "__main__": parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("directory", type=Path) args = parser.parse_args() for row in audit(args.directory): print(json.dumps(row))First, try it in an empty staging test directory. Start with one nonempty dummy file to represent a completed download and a sidecar without a matching video. printf '%s\n' '{"clip_id":"clip-001"}' > clip-001.json printf '%s\n' 'dummy clip' > clip-001.mp4 printf '%s\n' '{"clip_id":"clip-002"}' > clip-002.json python3 /path/to/audit_intake.py .Expected result: {"clip_id": "clip-001", "status": "paired"} {"clip_id": "clip-002", "status": "missing_video"}On Unraid, point the input mount at the staging share and the script mount at the saved file. With the image available locally, this runs the check with a read-only filesystem and networking disabled. docker run --rm --network none --read-only --cap-drop ALL \ -v /mnt/user/media-staging:/intake:ro \ -v /mnt/user/appdata/intake-check/audit_intake.py:/audit_intake.py:ro \ python:3.13-alpine python /audit_intake.py /intakeTreat paired as a file-presence check. It means the ID matches and the MP4 has bytes. A downstream import can still check codec or duration with ffprobe. The script doesn't touch other names in the share, including in-progress files with a temporary extension.
zhangbo
Members
-
Joined
-
Last visited