Skip to content

Latest commit

 

History

History
170 lines (123 loc) · 6.91 KB

README.md

File metadata and controls

170 lines (123 loc) · 6.91 KB

Find the Exact Moment in a Video — With a Text Query

VideoQuery is a semantic video-search backend for the question: “Where in this video does this appear?” Upload a video, search in plain language, and receive the matching keyframes with timestamps.

For example, search for:

  • a person holding a phone
  • a red car entering the frame
  • a slide with a bar chart
  • a worker wearing a hard hat

VideoQuery is designed for teams that need to find a visual moment without manually scrubbing through long recordings: product and user-research footage, support calls, security video, event recordings, media libraries, or internal demos.

Search terms: semantic video search, natural-language video search, CLIP video retrieval, visual search, keyframe search, video intelligence pipeline.

What it does

  1. Accepts a video upload through a FastAPI endpoint.
  2. Splits a long video into time-based chunks.
  3. Extracts sampled frames from each chunk in parallel.
  4. Embeds frames with CLIP and indexes them as vectors.
  5. Embeds a text query with the same model and retrieves the nearest visual matches.
  6. Returns the matching frame, score, and exact video timestamp so a client can jump directly to the relevant moment.
flowchart LR
    A["Upload video"] --> B["S3 / local storage"]
    B --> C["Splitter: ffprobe + time chunks"]
    C --> D["SQS / local queue"]
    D --> E["Parallel workers"]
    E --> F["FFmpeg sampled frames"]
    F --> G["CLIP image embeddings"]
    G --> H["OpenSearch k-NN index"]
    I["Natural-language query"] --> J["CLIP text embedding"]
    J --> H
    H --> K["Top keyframes + timestamps"]
Loading

Why this is more than frame extraction

The system is built around the operational problem of searching long videos at scale:

  • Parallel video processing: a lightweight splitter uses ffprobe to determine duration, then publishes independent time-range tasks such as 0–60s, 60–120s, and 120–180s.
  • Worker isolation: each worker downloads and processes only its assigned interval, avoiding a single long-running process for the full video.
  • Semantic retrieval: CLIP places images and text in the same vector space, allowing a text description to retrieve visually related frames.
  • Production-oriented storage: frames are retained in object storage and indexed in OpenSearch as knn_vector documents with timestamps and source metadata.
  • Local-to-cloud path: the same interfaces support a deterministic local backend for plumbing tests and an AWS path using S3, SQS, OpenSearch, Lambda, or containerized workers.

Architecture

Layer Implementation
API FastAPI upload and search endpoints
Video metadata ffprobe duration probing
Frame sampling ffmpeg, configurable sampling FPS
Work distribution SQS in AWS; JSONL-backed queue locally
Embeddings OpenCLIP in semantic mode; deterministic hash provider for tests
Vector search OpenSearch k-NN in AWS; JSONL-backed cosine search locally
Object storage S3 in AWS; filesystem storage locally
Infrastructure AWS SAM template and Docker worker image

Quick start: semantic search locally

VideoQuery requires Python 3.11+ and ffmpeg / ffprobe on your PATH.

git clone https://github.com/GuoShuyin/VideoQuery.git
cd VideoQuery

python3.11 -m venv .venv
. .venv/bin/activate
pip install -e ".[clip,dev]"

# macOS
brew install ffmpeg

export VIDEOQUERY_BACKEND=local
export VIDEOQUERY_LOCAL_AUTOPROCESS=true
export VIDEOQUERY_EMBEDDING_BACKEND=clip
uvicorn videoquery.api:app --reload

In a second terminal, upload a video and query it:

curl -F "file=@/path/to/video.mp4" http://127.0.0.1:8000/videos

curl "http://127.0.0.1:8000/search?q=person%20holding%20a%20phone&k=5"

The search response includes each candidate's timestamp, similarity score, and frame_s3_key.

Local plumbing mode

The default local embedding provider is hash. It is deterministic, fast, and useful for testing upload, queuing, indexing, and retrieval wiring—but it is not semantic search. Set VIDEOQUERY_EMBEDDING_BACKEND=clip to perform real text-to-frame retrieval.

AWS deployment model

The AWS reference stack is defined in infra/aws-sam.yaml:

  1. An S3 ObjectCreated event invokes the splitter function.
  2. The splitter publishes chunk tasks to SQS.
  3. Workers consume tasks, sample frames, store them in S3, and index embeddings in OpenSearch.
  4. For large videos or CLIP model warmup, the worker can run in ECS/Fargate using docker/worker.Dockerfile.

Configure the AWS backend through environment variables. See .env.example for the expected names.

API

POST /videos

Uploads a video and either queues its chunks or processes them immediately in local auto-process mode.

curl -F "file=@/path/to/video.mp4" http://127.0.0.1:8000/videos

GET /search?q=<query>&k=<count>

Searches indexed video frames with a natural-language query.

curl "http://127.0.0.1:8000/search?q=red%20car&k=5"

Example response shape:

{
  "query": "red car",
  "results": [
    {
      "video_id": "b7a2...",
      "chunk_id": "000001",
      "timestamp": 73.0,
      "score": 0.82,
      "frame_s3_key": "frames/b7a2/000001/frame_000014_73.000.jpg"
    }
  ]
}

Project layout

Tests

PYTHONPATH=src python3.11 -m unittest discover -s tests

The unit tests cover time-range chunking, deterministic embedding behavior, and local cosine-similarity retrieval.

Practical tradeoffs

  • A higher sampling FPS increases recall but also storage, inference, and indexing cost.
  • CLIP returns visually relevant candidate frames; applications may add reranking, tracking, or temporal grouping for stronger end-user results.
  • The repository is a backend reference implementation. A production product would typically add a browser UI, signed frame URLs, authentication, retention policies, and monitoring.

Privacy

Do not commit private footage, personal data, or copyrighted video without permission. Use synthetic or fully authorized sample videos for demos.