|
| 1 | +import stashapi.log as log |
| 2 | +from stashapi.stashapp import StashInterface |
| 3 | +import sys |
| 4 | +import json |
| 5 | +from pathlib import Path |
| 6 | +import os |
| 7 | + |
| 8 | +settings = {"hash_type": "oshash", "transcodes_dir": "/generated/transcodes/"} |
| 9 | + |
| 10 | + |
| 11 | +def run_ffmpeg(file, hash): |
| 12 | + dest = Path(settings["transcodes_dir"]) / "transcodes" / str(hash + ".mp4") |
| 13 | + if not dest.exists(): |
| 14 | + log.debug( |
| 15 | + "running ffmpeg on %s to generate %s" |
| 16 | + % ( |
| 17 | + file, |
| 18 | + dest, |
| 19 | + ) |
| 20 | + ) |
| 21 | + command = ( |
| 22 | + "ffmpeg -f lavfi -i color=c=blue:s=1280x720 -i %s -shortest -fflags +shortest %s" |
| 23 | + % (file, dest) |
| 24 | + ) |
| 25 | + log.debug("about to run command: %s " % (command,)) |
| 26 | + os.system(command) |
| 27 | + else: |
| 28 | + log.debug( |
| 29 | + "transcode already exists %s - %s" |
| 30 | + % ( |
| 31 | + dest, |
| 32 | + file, |
| 33 | + ) |
| 34 | + ) |
| 35 | + |
| 36 | + |
| 37 | +def processScene(s): |
| 38 | + for f in s["files"]: |
| 39 | + if f["width"] == 0: |
| 40 | + log.debug("needs to transcode") |
| 41 | + for h in f["fingerprints"]: |
| 42 | + if h["type"] == settings["hash_type"]: |
| 43 | + file = f["path"] |
| 44 | + hash = h["value"] |
| 45 | + run_ffmpeg(file, hash) |
| 46 | + |
| 47 | + |
| 48 | +def processAll(): |
| 49 | + scenes = stash.find_scenes( |
| 50 | + f={"resolution": {"modifier": "LESS_THAN", "value": "VERY_LOW"}} |
| 51 | + ) |
| 52 | + for s in scenes: |
| 53 | + processScene(s) |
| 54 | + |
| 55 | + |
| 56 | +json_input = json.loads(sys.stdin.read()) |
| 57 | + |
| 58 | +FRAGMENT_SERVER = json_input["server_connection"] |
| 59 | +stash = StashInterface(FRAGMENT_SERVER) |
| 60 | +config = stash.get_configuration() |
| 61 | + |
| 62 | +log.debug(config) |
| 63 | +settings["transcodes_dir"] = config["general"]["generatedPath"] |
| 64 | +settings["hash_type"] = config["general"]["videoFileNamingAlgorithm"].lower() |
| 65 | +log.debug(settings) |
| 66 | + |
| 67 | +if "mode" in json_input["args"]: |
| 68 | + PLUGIN_ARGS = json_input["args"]["mode"] |
| 69 | + log.debug(json_input) |
| 70 | + if "processScenes" == PLUGIN_ARGS: |
| 71 | + processAll() |
| 72 | + |
| 73 | +elif "hookContext" in json_input["args"]: |
| 74 | + _id = json_input["args"]["hookContext"]["id"] |
| 75 | + _type = json_input["args"]["hookContext"]["type"] |
| 76 | + if _type == "Scene.Create.Post": |
| 77 | + scene = stash.find_scene(_id) |
| 78 | + processScene(scene) |
0 commit comments