Skip to content
sv2devPublic

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

1 watching

Forks

Repository files navigation

m4k - media toolkit

A Bun media optimizer based on ffmpeg and sharp/libvips.

The docker image is available at ghcr.io/sv2dev/media-optimizer. It uses Bun as runtime.

Usage

Running the server

The @m4k/server can be deployed as a docker container.

docker run -p 3000:3000 -v $(pwd)/output:/output ghcr.io/sv2dev/m4k:0.2.1

Alternatively, you can install the server as a local package and run it:

bun add @m4k/server

Note: This server implementation is using Bun as runtime, so it can only be run on a system that has Bun installed.

Then, run the server:

bun run @m4k/server

Or import the server as a module and embed it in your project:

import { serveOpts } from "@m4k/server";

Bun.serve(serveOpts);

Image classification and embedded previews

The optional classification API runs a pinned CPU CLIP model on the M4K host. GET /info advertises clip-image-labels version 1 and embedded-preview/1. Existing conversion behaviour is unchanged unless the new options/endpoints are selected. These changes require deploying this server version, not just upgrading a client.

import { classifyImage } from "@m4k/client";

const result = await classifyImage("http://localhost:3000", Bun.file("preview.jpg"), {
  classifierId: "clip-image-labels",
  classifierVersion: "1",
  labels: ["landscape", "receipt", "family photo"],
});

POST /images/classify uploads the selected representation with JSON X-Options. It returns classifier/version, the uploaded bytes' SHA-256 and every candidate's score. Labels are a complete ordered candidate set (2–32 distinct labels, 1–80 characters each). Scores are softmax rankings within that set, not independent probabilities; changing the candidates changes their meaning. The fixed model is Xenova/clip-vit-base-patch32 revision 6b7c523703ae26e39be4b03774966f8571c7f523 (MIT), Transformers.js 3.8.1, CPU/q8, with prompt This is a photo of {}. Changing this profile requires a new classifier version. Only static JPEG/PNG/WebP previews up to 8 MiB / 1,048,576 pixels are accepted. One inference runs per process; busy requests receive 429. Cancellation while waiting for model loading releases the request slot immediately; the shared load continues and is reused by the next request. Once native inference starts, cancellation discards results without freeing its slot before inference finishes. There is no model, original or cloud fallback.

The model is downloaded on first use and remains in memory. M4K_MODEL_CACHE selects the persistent model-cache directory (default ${M4K_TMP_DIR:-/tmp/m4k}/models); ensure the service user can write it. Mount that directory to retain weights across container replacements. Model downloads do not upload input media. The quantized model cache is approximately 149 MiB in the tested configuration; this is not a runtime memory limit. CPU ONNX binaries are packaged by the dependency; optional CUDA postinstall/downloads are not required. The server bundle keeps Transformers.js external so native runtime assets resolve from installed dependencies.

For RAW classification preparation, processImage(..., { embeddedPreview: true, format: "jpeg" }) uses only a TIFF IFD1 JPEG thumbnail, never full RAW development. Input is limited to 128 MiB, each of IFD0/IFD1 to 256 entries, and the JPEG to 8 MiB / 1,048,576 pixels. Both TIFF byte orders are supported; missing/malformed thumbnails fail without developing or passing through the source. Source orientation is applied by normal conversion; metadata preservation options are forbidden in this mode. Callers must require embedded-preview/1 before uploading to avoid older servers silently ignoring the option. No broad camera-format or physical-device compatibility is claimed; extractor tests use synthetic TIFF/DNG containers.

Classifying while converting

POST /images/process accepts an optional classify property on each image option. As soon as that entry's output has been produced, the server classifies exactly those output bytes and streams the result as an application/json part, so a RAW file is uploaded once and the converted preview never travels back and forth. Add discard: true to receive only the classification.

import { processImage, ProcessedFile } from "@m4k/client";

for await (const event of processImage("http://localhost:3000", Bun.file("IMG_0001.DNG"), [
  {
    embeddedPreview: true,
    format: "jpeg",
    resize: { width: 512, height: 512 },
    classify: { classifierId: "clip-image-labels", classifierVersion: "1", labels: ["landscape", "receipt"] },
    discard: true,
  },
  { embeddedPreview: true, format: "jpeg", resize: { width: 2048, height: 2048 }, name: "web.jpg" },
])) {
  if (event instanceof ProcessedFile) await Array.fromAsync(event.stream);
  else if ("classification" in event) console.log(event.classification.index, event.classification.labels);
}

The part is {"classification": {"index": 0, "classifierId", "classifierVersion", "sha256", "labels"}}; index is the position of the producing entry in the options array, sha256 is the hash of the classified output. Every classified entry needs an explicit jpeg, png or webp format within the classifier limits (8 MiB / 1,048,576 pixels), which is why a resize is usually appropriate. Several entries share one upload; each classified entry separately decodes its converted output. Inline inference admits at most eight waiting callers; excess requests fail rather than retaining unbounded preview buffers. Unlike the standalone endpoint, a classification queues for the single inference slot instead of failing with 429, because the conversion has already been done. There is no fallback: a failing classification ends the stream with an error. Servers advertise inline-classification/1 in imageProfiles; the client requires it (and the pinned classifier) before uploading and fails if the server omits a requested classification. classify is only available as JSON options (X-Options or options), not as a flat query parameter.

Using the client

The client is available as a standalone package @m4k/client. You can use it to connect to the m4k server.

Example, using the client on Bun:

import { ProcessedFile, processVideo } from "@m4k/client";

for await (const value of processVideo("http://localhost:3000", Bun.file("fixtures/video.mp4"), {
  format: "mp4",
  videoCodec: "libx265",
  output: "output/video.mp4",
})) {
  if (value instanceof ProcessedFile) {
    // handle the processed file
  } else {
    // handle the status update
  }
}

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages