media-processor

module
v0.0.39 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 26, 2026 License: AGPL-3.0

README

media-processor

media-processor sits transparently between a download client and Sonarr/Radarr. When a file lands in the watched download directory, it is automatically transcoded to H.265 in a Matroska container and the appropriate library service is notified to import the result.

[!NOTICE] A large part of the reason why I worked on this project was to improve how I ues LLMs. All code was reviewed by me, but very little was hand-written by me. Use at your own risk.

How it works

Two binaries run as separate processes and coordinate through Temporal, a self-hosted workflow engine:

  • The watcher (bin/watcher) — scans configured filesystem paths on a configurable interval and starts a media-processing workflow execution on Temporal for each file found.
  • The worker (bin/worker) — polls a Temporal task queue and processes the workflows it receives. It probes each file with FFmpeg, transcodes it, and notifies Radarr or Sonarr when the output is ready.

Hardware-accelerated encoding is supported via Intel QSV (oneVPL) and VAAPI on Linux.

The bind-mount arrangement

The key to making this transparent to Sonarr/Radarr is a single bind mount:

Path Visible to
/downloads Download client, watcher, worker
/processed-output watcher, worker
/downloads (bind-mounted from /processed-output) Sonarr/Radarr

Sonarr/Radarr's /downloads is bind-mounted from /processed-output on the host. This means Sonarr/Radarr never sees the raw download — it only sees files after they have been processed. The download client reports /downloads/… to Sonarr/Radarr; because /processed-output is mounted there, the path resolves to the transcoded output automatically.

End-to-end flow
  1. User requests media via Sonarr or Radarr.
  2. Sonarr/Radarr sends the release to the download client.
  3. The download client saves the completed file to /downloads and reports the path back to Sonarr/Radarr.
  4. The watcher detects the new file and starts a Temporal workflow execution.
  5. The worker picks up the job: it probes the file, detects black-bar crop, and writes an MKV output to the directory specified by output.path in the watcher config (mirroring the input's subdirectory under that path when input.path is a parent of the input file). Non-H.264/H.265 video is re-encoded to H.265; H.264 or H.265 sources already in MKV are remuxed without re-encode unless a crop is being applied.
  6. The worker calls the Radarr or Sonarr API to trigger a library rescan using the transcoded output file path. When output.remotePath is configured, the output.path prefix in that path is replaced by output.remotePath to produce the path as Sonarr/Radarr sees it (e.g., local /processed-output/movies/film.mkv becomes /downloads/movies/film.mkv when output.remotePath is /downloads).
  7. Sonarr/Radarr scans the notified path, finds the processed file, and imports it into the library.

Building

Requires Go 1.26 and FFmpeg 8 shared libraries. Use the Nix dev shell to get everything:

nix develop
make build

Binaries are written to bin/watcher and bin/worker.

Container deployment

Container images for the watcher and worker can be built locally via make build-images. See docs/container-deployment.md for volume mounts, environment variables, and hardware device passthrough.

Configuration

Watcher

The watcher is configured by a YAML file (passed via --config, defaults to config.yaml) and a small set of environment variables. See docs/configuration.md for the full reference.

Minimal watcher config:

watches:
  - name: movies
    input:
      path: /downloads/movies
    mediaType: movie
    output:
      path: /processed/movies
  - name: shows
    input:
      path: /downloads/tv
    mediaType: show
    output:
      path: /processed/tv
Worker

The worker is configured entirely via environment variables. Required variables:

Variable Description
TEMPORAL_ADDRESS Temporal frontend host:port (e.g. temporal-frontend:7233)
TEMPORAL_NAMESPACE Temporal namespace the workflows execute in (e.g. default)
TEMPORAL_TASK_QUEUE Task queue the worker polls and the watcher dispatches to (e.g. media-processor)
RADARR_URL Radarr base URL (e.g. http://radarr:7878)
RADARR_API_KEY Radarr API key
SONARR_URL Sonarr base URL (e.g. http://sonarr:8989)
SONARR_API_KEY Sonarr API key

TEMPORAL_TASK_QUEUE, RADARR_*, and SONARR_* are explicitly checked as non-empty at startup; TEMPORAL_ADDRESS and TEMPORAL_NAMESPACE silently fall back to the Temporal Go SDK defaults (localhost:7233 and default) when empty, so production deployments must set them explicitly. See docs/configuration.md for the full reference including optional variables.

Metrics and observability

Both binaries always serve a Prometheus pull endpoint on /metrics (worker :9090, watcher :9091 by default). See docs/metrics.md.

Kubernetes deployment

A Helm chart is available for deploying the watcher and worker to Kubernetes. See docs/helm.md for installation instructions, a full values reference, and a worked example.

For the full list of network connections made by each pod (required when writing NetworkPolicy resources), see docs/network-policies.md.

For KEDA-driven 0..N autoscaling of the worker (recommended for the transcode activity), see docs/autoscaling.md.

Hardware acceleration

Intel QSV and VAAPI are supported. See docs/hardware-acceleration.md.

Development

make fmt         # go fmt
make lint        # golangci-lint
make test        # unit tests
make test-integration   # starts a local Temporal server (Postgres-backed) via `make temporal-up` and runs integration tests against it
make test-e2e    # end-to-end tests (requires Docker; first run downloads ~700 MB fixture)

Directories

Path Synopsis
cmd
watcher command
worker command
internal
envvar
Package envvar centralises the small env-var validation helpers used by the watcher and worker entry points.
Package envvar centralises the small env-var validation helpers used by the watcher and worker entry points.
pkg
ffmpeg
Package ffmpeg provides a builder-pattern API for constructing and running FFmpeg transcoding jobs in-process via go-astiav (libavcodec/libavformat CGo bindings).
Package ffmpeg provides a builder-pattern API for constructing and running FFmpeg transcoding jobs in-process via go-astiav (libavcodec/libavformat CGo bindings).
ffprobe
Package ffprobe wraps the go-astiav CGo bindings to inspect media files.
Package ffprobe wraps the go-astiav CGo bindings to inspect media files.
loadprobe
Package loadprobe samples worker load metrics — Intel i915 GPU video-engine utilization or container CPU usage — and exposes a smoothed reading plus a fallback signal to callers gating admission decisions on observed load.
Package loadprobe samples worker load metrics — Intel i915 GPU video-engine utilization or container CPU usage — and exposes a smoothed reading plus a fallback signal to callers gating admission decisions on observed load.
logging
Package logging configures the global slog handler from a level string.
Package logging configures the global slog handler from a level string.
medialib
Package medialib provides higher-level abstractions for media library services.
Package medialib provides higher-level abstractions for media library services.
medialib/internal/arrcommand
Package arrcommand provides shared polling logic for waiting on Sonarr/Radarr long-running commands (e.g.
Package arrcommand provides shared polling logic for waiting on Sonarr/Radarr long-running commands (e.g.
medialib/internal/artwork
Package artwork provides shared utilities for fetching poster images from arr-style media library services (Radarr, Sonarr).
Package artwork provides shared utilities for fetching poster images from arr-style media library services (Radarr, Sonarr).
medialib/radarr
Package radarr provides a Radarr client implementing medialib.ArrLibrary.
Package radarr provides a Radarr client implementing medialib.ArrLibrary.
medialib/sonarr
Package sonarr provides a Sonarr client implementing medialib.ArrLibrary.
Package sonarr provides a Sonarr client implementing medialib.ArrLibrary.
temporalclient
Package temporalclient builds a Temporal SDK client configured via the envconfig package, with two extensions on top:
Package temporalclient builds a Temporal SDK client configured via the envconfig package, with two extensions on top:
transcodelimiter
Package transcodelimiter implements a Temporal SlotSupplier that gates transcode-activity admission by a load-probe-driven controller.
Package transcodelimiter implements a Temporal SlotSupplier that gates transcode-activity admission by a load-probe-driven controller.
webhook
Package webhook provides an HTTP client for posting failure notifications to a configured webhook endpoint.
Package webhook provides an HTTP client for posting failure notifications to a configured webhook endpoint.
workflows
media
Package media provides the Temporal workflow definition for processing media files (movies and TV episodes) using a single parameterised workflow.
Package media provides the Temporal workflow definition for processing media files (movies and TV episodes) using a single parameterised workflow.
media/types
Package types defines the shared workflow name and input payload for the media processing workflow.
Package types defines the shared workflow name and input payload for the media processing workflow.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL