Skip to content

Add Stash Video Editor plugin - #822

Open
speckofthecosmos wants to merge 2 commits into
stashapp:mainfrom
speckofthecosmos:add-stash-video-editor
Open

speckofthecosmos wants to merge 2 commits into
stashapp:mainfrom
speckofthecosmos:add-stash-video-editor

Conversation

@speckofthecosmos

Copy link
Copy Markdown
Contributor

I was jumping through lots of hoops to edit videos in my library and thought it should be doable right through the interface. Here’s the result. You can crop and trim videos. It lets you remove black bars and cut out title screens for example. Keeps the old version as a second file on the same listing (like a merge). Has the option to use remote encoding for faster processing. Details below.

Claude Fable 5.2 (medium) and Claude Opus 5.5 (encode hook)

Adds plugins/StashVideoEditor: crop, stretch or trim a scene's video from the scene page, and crop or stretch image clips from the image page. The backend is Python (standard library only) and runs ffmpeg with Stash's own transcode settings.

Behavior (what this adds for users)

  • Two buttons on the scene page, Crop & re-encode and Trim.
  • Crop: a box with 8 handles over the video. Auto-crop bars finds the sharp picture area, so it handles black bars and blurred padding. Switch to Stretch resizes the cropped region to a different shape.
  • A magnifier shows the edge being dragged at 1:1 source resolution, with the removed side marked. Arrow keys nudge the last-touched handle by 2 source pixels (Shift: 10).
  • Trim: an in handle and an out handle on a timeline that reuses the scene's sprite sheet when one exists.
    • Lossless copies the streams. The cut starts at the keyframe at or before the in point.
    • Precision re-encodes for a frame-accurate cut.
  • The edit is written to a new file next to the original and becomes the scene's primary file. The original stays on the scene as a second file. Nothing is overwritten or deleted.
  • After a trim, the scene's markers move to the new timeline, and markers whose moment was cut away are removed.
  • On the image page the same crop button appears for image clips (.vclip). The cropped file replaces the clip in place and the original is kept beside it as <name>.sve-bak. Cropping again leaves that backup as the first original.
  • When the browser cannot decode the file, the editor says so and disables submit.
  • When Stash does not queue the task, the dialog stays open and shows the reason.

How the swap works

Stash's job queue is serial, so a task cannot wait for a scan it queued. The work is split in two:

  1. The task writes a record of the edit to sve-pending.json in the plugin folder, encodes to <name>.edited.<ext> (or .edited-2, .edited-3 when the name is taken), queues a scan of that file, and exits.
  2. The scan creates a scene for the new file. A Scene.Create.Post hook merges it into the source scene with sceneMerge, sets the edited file as primary with sceneUpdate, moves the markers after a trim, removes the record, and queues metadataGenerate with overwrite.

The hook acts only on recorded paths, so a file a user named something.edited.mp4 is left alone. With no records the hook makes no GraphQL calls. A Merge edited scenes task finishes any recorded edit the hook did not, and does nothing when run again.

Images have no imageAssignFile or imageMerge, so the image-clip crop encodes to a sibling file, moves the original to .sve-bak, moves the edit onto the clip's path (rolling back if that fails), then queues a scan and a generate.

Encode hook (optional)

A setting, Encode hook (executable path), hands the re-encodes to another program, for example to use another machine's GPU or a cloud encoder. Blank, the default, means Stash's ffmpeg does them, as before.

  • Crop, stretch, image-clip crop and precision trim go to the hook. Lossless trim is a stream copy and always runs locally.
  • The plugin runs the executable with a JSON job on stdin: the op, the scene or image id, source and output paths as Stash sees them, the crop box and output size or the in and out points, Stash's transcode settings, and the full ffmpeg argv the plugin would run itself. A hook can run that argv elsewhere or translate it for another encoder.
  • The hook writes to the same in-progress .sve-partial name a local encode uses (the job names its muxer), so a scan never sees a half-written file, even when the task is cancelled while the hook runs. Exit 0 means the hook wrote it. The plugin checks that the file exists and is not empty, moves it into place and carries on as after a local encode.
  • A non-zero exit, no output, an empty output, a timeout (Encode hook timeout, default 3600 seconds, capped at a day) or an executable that cannot start is logged with its reason and no path. The plugin deletes whatever the hook wrote and encodes locally. The hook runs in its own process group, which is killed whenever the hook exits or times out.
  • The hook's stdout and stderr are discarded, so they cannot put file names in the Stash log.
  • Plugins already run code on the Stash host, so a configured executable adds no new privilege.
  • The README documents the job format and has two short examples: one that passes the argv to an ffmpeg-over-ip client, and one that sends the job to an HTTP encode service.
  • Prior art checked for a hook contract to match: Unmanic's external script plugin is the nearest (it is handed the input and output paths and returns an exec_command); Tdarr and Jellyfin's rffmpeg replace the ffmpeg binary instead. None defines a job format a Stash plugin could reuse, so the names here follow the plugin's own (src, dst).

Implementation notes (open to revision)

  • No dependencies: no pip packages, and no other plugin is required.
  • The re-encode uses ffmpegPath, transcodeInputArgs and transcodeOutputArgs from Stash's configuration. With no output arguments set it uses libx264 -crf 18 -preset slow.
  • An .mp4, .m4v, .mov or .mkv source keeps its container and its audio is copied. Any other source is written as .mp4 with AAC audio.
  • A re-encode maps the first video stream and the audio streams only.
  • An encode in progress is written to *.sve-partial, which Stash does not scan, and renamed when ffmpeg succeeds.
  • Log lines carry scene and file ids, never file paths. ffmpeg's error text is logged with paths removed.
  • Requests and stream URLs are built from the page's <base href>.
  • cropMath.js adapts coordinate helpers from react-easy-crop (MIT). The licence notice is in the file.
  • The folder and manifest name are StashVideoEditor; happy to rename to the repository's preferred style.
  • url: points at this folder; I can change it to a forum topic.

Testing

  • Unit tests: 100 Python (pytest) and 70 JavaScript (node --test), all passing.
  • End to end against a throwaway Stash in Docker with synthetic ffmpeg clips:
    • On v0.32.0-12 (development): crop, precision and lossless trim with marker remap, a user's own .edited file left alone, WebM, a MOV with a timecode track, a second edit from the original, image-clip crop twice with the backup checksum unchanged, tasks run with no arguments, an out-of-bounds crop, and the hook with nothing pending.
    • On v0.32.0 (latest) and v0.31.1: crop, both trims and the second edit. The image-clip crop was also run on v0.31.1.
  • Encode hook, end to end on v0.31.1, v0.32.0 and v0.32.0-12, 34 checks each, all passing: hook off; a hook that runs the given argv; a hook that exits 1 after writing a partial file; one that exits 0 and writes nothing; one that overruns a 3-second timeout (it and its child process are killed, no partial file left); precision trim through the hook; lossless trim never reaching the hook; an image-clip crop through the hook; the README's two examples (the ffmpeg-over-ip one with the container's ffmpeg standing in for the client, the HTTP one against a stub service, and again with the service down); a task cancelled while the hook runs (no scene is made from the hook's output); and no file path in any plugin log line.
  • A scripted browser pass (headless Chromium) on v0.32.0-12 and v0.31.1: both buttons render, both dialogs open and load the video, the crop button appears on an image clip and not on a still image. One crop was submitted through the dialog.
  • validator/validate.js --ci passes.
  • Not tested: Stash with authentication on, Stash under a base path, hardware-accelerated or custom transcode arguments, a killed encode, an unwritable plugin folder, Windows, and browsers other than headless Chromium. Auto-crop, stretch, the magnifier and the arrow keys are covered by unit tests only in this version.
  • Used by hand on a real library: the author runs this build on their own Stash, with the encode hook sending re-encodes to another machine's hardware encoder through ffmpeg-over-ip, and has cropped a scene that way. Before this version the author used an earlier private build with the same editor and a different swap mechanism.
  • The test files are not included, so they are not installed with the plugin; they can be added if preferred.

Limits

  • The generate step after an edit replaces the scene's cover, including a cover set by hand (covers with overwrite).
  • A re-encode does not carry subtitle streams.
  • After a lossless trim a marker can be off by up to one keyframe interval.
  • An ffmpeg failure shows only in the Stash log; the dialog closes once the task is queued.
  • A killed task can leave a *.sve-partial file. It is safe to delete.
  • Updating the plugin while an edit is between encode and scan loses that edit's record; the edited file then stays as its own scene.
  • Editing a scene whose primary file is already an edit names the result <name>.edited.edited.<ext>.

LLM-assisted contribution

Written with Claude Code (an LLM) under the author's direction, per this repository's rule for LLM-assisted contributions. An LLM code review was run over the whole plugin; its ten findings are fixed in this version, apart from what is listed under Limits. A second LLM review covered the encode hook; its four findings are fixed. The tests above were run after those fixes.

The author specified the behaviour, looked over the code and tested it on their own library, and takes responsibility for the code.

🤖 Generated with Claude Code

speckofthecosmos and others added 2 commits November 27, 2026 13:14
Crop, stretch or trim a scene's video from the scene page; crop and
stretch image clips from the image page. The edit becomes the primary
file and the original is kept. An optional encode hook hands the
re-encodes to another program, with a local encode as the fallback.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant