Adding Native Video Input to LLM Inference Services: A Practical Guide

    This article walks through the engineering work required to add native video input to an existing multimodal LLM inference service. It explains why image support alone is insufficient, breaks the pipeline into request parsing, decoding and frame sampling, temporal chunking with position encoding, visual token projection, and post-processing for timestamp localization. It also covers a plugin-based design that keeps text and image paths untouched, deployment on Baidu AI Cloud using object storage, container orchestration, gateway rate limiting, and observability, plus capacity planning metrics and common pitfalls such as sparse or overly dense frame sampling and silent truncation of long videos.

    1. Background: Why “Can See Images” Does Not Mean “Can See Video”

    Open-source multimodal LLMs released over the past year or two generally support text and image input, and inference frameworks handle interleaved text-and-image requests reasonably well. But when a business team asks for something like “upload a 30-second screen recording and have the model pinpoint when the error occurred,” many teams discover that running the request against an existing inference service fails during preprocessing, or the video is treated as a set of unrelated images and the answer clearly lacks temporal information.

    The gap breaks down into three layers:

    1. Protocol gap. Public commercial APIs usually expose video as a standalone content type. In a self-built pipeline, the video field in the request body often has no parsing branch and is either dropped or rejected.
    2. Preprocessing gap. An image is self-contained and can go straight into the vision encoder after decoding. Video requires demuxing, decoding, frame sampling, and temporal organization, and this entire set of steps is missing in a text-and-image-only version.
    3. Semantic gap. Even if a video is split into frames and fed to the model, without explicit modeling of frame order, shot changes, and action continuity, the output is a description of a collection of images rather than a description of a process.

    Closing the video input gap is therefore not a matter of adding a file upload button. It means integrating a complete processing pipeline into the existing inference runtime while keeping the already stable text and image paths intact.

    2. Pipeline Breakdown: From a Request to Visual Feature Fusion

    A working video input pipeline typically contains the following stages. Understanding the responsibility of each stage is the prerequisite for plugin-based design and performance tuning.

    2.1 Request Reception and Multimodal Content Parsing

    The inference service must first recognize the different content blocks in a request (text, image, video) and run basic validation: whether the container format is supported, whether resolution and duration fall within acceptable ranges, and whether the file size exceeds the limit. In practice, validation should be pushed forward to the gateway or ingress layer so that invalid requests never reach the expensive decoding stage.

    An illustrative request structure (structure only, not a specific API):

    1. {
    2. "messages": [
    3. {
    4. "role": "user",
    5. "content": [
    6. { "type": "text", "text": "Describe why the operation failed in this screen recording" },
    7. { "type": "video", "video": {"path": "oss://bucket/demo.mp4", "fps": 1} }
    8. ]
    9. }
    10. ]
    11. }

    2.2 Decoding and Frame Sampling Strategy

    Frame sampling is the most important cost-performance lever. Three common approaches:

    • Fixed frame rate sampling: take a fixed number of frames per second. Timing is uniform, which suits action and workflow content.
    • Fixed frame count sampling: always take N frames regardless of video length. Token cost stays constant, which suits cost-sensitive scenarios.
    • Scene-based sampling: detect shot boundaries from frame differences and sample at key frames. This suits edited and instructional long videos.

    In practice the three are usually combined: scene splitting sets the upper bound first, then uniform sampling happens within each segment, and finally a hard cap is applied to the total frame count. Decoding should run in a separate process or service to avoid blocking the main inference loop.

    2.3 Temporal Chunking and Position Encoding

    Flattening all frames into a single sequence and feeding them to the model at once creates two problems: the number of visual tokens explodes, and temporal relationships get diluted. A workable approach is to split the frame sequence into alternating frame groups, preserving spatial detail within a group and expressing order between groups through position information along the time dimension. This compresses the token count and lets the model distinguish what happened first from what happened later.

    2.4 Visual Token Generation and Projection Alignment

    Each frame group produces a feature map through the vision encoder, which a projection module then maps to the hidden dimension of the language model, forming a visual token sequence. This sequence is concatenated with text tokens and fed into the language model together. The core constraint here is that the projection module’s input and output must align strictly with the existing vision tower. Adding a video branch must not change the parameters or preprocessing of the original image branch, or shipped capabilities will regress.

    2.5 Generation and Post-Processing

    After the model generates output, if the business needs timestamp localization, post-processing can map token indices back to frame indices and then to second-level timestamps, producing actionable conclusions such as “an anomaly appears around the 12-second mark.”

    3. Plugin-Based Design: Separating Adaptation from the Main Codebase

    Modifying the inference framework’s mainline directly is the fastest short-term option and a maintenance disaster long term: every framework upgrade forces the changes to be redone. A more robust approach is plugin-based design:

    • Stable interface: define a unified “multimodal input processor” abstraction that takes a raw request and outputs a token sequence and mask ready for the model.
    • Capability registration: the video processor registers with the runtime as a plugin. Without the plugin installed, behavior is exactly as before; once installed, it automatically takes over video content blocks.
    • Dependency isolation: decoding libraries, frame sampling parameters, and concurrency limits live inside the plugin and are exposed through configuration rather than polluting global config.
    • Rollback safety: on plugin failure, the system degrades to rejecting video requests with a clear error instead of taking the whole service down.

    The direct benefit is that the regression test scope for the text and image pipelines stays unchanged, while video capability can be rolled out and rolled back independently.

    4. Deployment and Launch on Baidu AI Cloud

    Once adaptation is complete, a production environment that can be operated and maintained can be assembled from general-purpose cloud products:

    1. Model and artifact storage: model weights and plugin packages live in object storage with versioned directories so deployments are reproducible.
    2. Containerized runtime: the inference service and the decoding process are containerized separately, with a container platform handling orchestration, scaling, and health checks.
    3. Ingress and rate limiting: load balancing and a gateway handle authentication, quotas, and request body size limits to keep oversized videos from overwhelming nodes.
    4. Observability: a logging service collects metrics such as frame sampling latency, decoding failure rate, and time to first token, while monitoring and alerting apply thresholds to latency and error rate.
    5. One-click deployment recipe: images, startup parameters, plugin switches, and resource configuration are fixed into a deployment template that can be enabled directly after installation on Baidu AI Cloud, reducing drift from manual configuration.

    For capacity planning, three metrics matter: average frames per request, the share of visual tokens, and decoding concurrency. The first two determine GPU memory and latency; the last determines CPU and memory levels.

    5. Quality Evaluation and Common Pitfalls

    • Frame sampling too sparse: key moments are missed and the model answers the wrong question. Mitigation: use scene splitting to fill in key frames.
    • Frame sampling too dense: token limits are exceeded and cost spikes. Mitigation: set a hard cap on total frames and lower the resolution.
    • Loss of temporal information: the answer becomes a stitched description of multiple images. Mitigation: introduce frame groups and temporal position encoding.
    • Silent truncation of long videos: users do not know the model only saw the first N seconds. The response should return the actual sampled interval.
    • Regression risk: a new branch modifies shared preprocessing. Avoid this through plugin isolation and automated regression testing.

    6. Summary

    Adding native video input is essentially integrating the decode-sample-organize-project-fuse pipeline into an existing inference runtime as a plugin, reusing the vision encoder and language model without touching the text and image paths. Combined with general-purpose Baidu AI Cloud capabilities such as object storage, container platforms, logging, and monitoring, plus a fixed one-click deployment recipe, teams can run video understanding aligned with commercial APIs in their own environments and keep tuning frame sampling and cost strategies as business needs evolve.