# shivammitra/gaming-moment-detector architecture

> Architecture diagram of the GitHub repository shivammitra/gaming-moment-detector, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/shivammitra/gaming-moment-detector
- Repository: https://github.com/shivammitra/gaming-moment-detector

## Overview

#### Purpose and entry points
This is a Python batch video-processing tool that identifies motion-heavy gameplay moments and produces a stitched highlight reel. The primary CLI is `detector.py`, accepting either a local video path or `--youtube` URL. `generate_test_video.py` supplies synthetic input for end-to-end validation.

Paths: `detector.py`, `youtube_source.py`, `generate_test_video.py`

#### Source acquisition boundary
The pipeline accepts two interchangeable source types:
- Local video files passed directly to the detector.
- YouTube URLs resolved into temporary direct CDN stream URLs without downloading source media.

`youtube_source.py` encapsulates yt-dlp resolution; downstream OpenCV and MoviePy consume the resulting video source uniformly. Stream URLs are external, time-limited infrastructure and may expire during unusually long processing.

Paths: `youtube_source.py`, `detector.py`

#### Motion-signal analysis
The detector reads frames sequentially with OpenCV, downsamples them, converts to grayscale, and computes differences between consecutive frames. This yields a per-frame motion-intensity signal, the central data product used for ranking candidate highlights.

The design intentionally measures visual movement rather than game semantics; camera pans, shake, or cutscenes can therefore score as action.

Paths: `detector.py`

#### Signal conditioning and moment selection
The raw signal is edge-padded and moving-average smoothed to suppress flicker while avoiding artificial boundary peaks. SciPy peak detection then applies prominence and minimum-distance constraints, ranks high-motion peaks, and selects up to `top_n` moments. A flat/no-motion source is treated as an error rather than yielding a false highlight.

Each selected peak becomes a fixed clip interval: roughly four seconds before and three seconds after, clamped to source-duration boundaries.

Paths: `detector.py`, `test_detector.py`

#### Rendering and artifact generation
MoviePy opens the same source video, extracts selected windows, overlays ordered “Moment” labels, and concatenates clips into the final MP4. The pipeline also emits inspection and machine-readable artifacts: a motion-signal plot with peaks and per-clip timestamps/scores.

Committed example outputs demonstrate the artifact contract.

Paths: `detector.py`, `output/highlight_reel.mp4`, `output/clips_metadata.json`

#### Runtime and dependencies
The runtime is Python 3.9+ with OpenCV for frame analysis, NumPy/SciPy for signal processing, MoviePy 2.x for clip rendering, Matplotlib for visualization, and yt-dlp for YouTube stream resolution. MoviePy’s 2.x API is an implementation compatibility boundary.

Paths: `requirements.txt`, `detector.py`, `youtube_source.py`

#### Primary control flow
CLI arguments select a source, optionally resolve YouTube, analyze frames into motion values, smooth and detect ranked peaks, translate peaks into bounded time windows, render/stitch those windows, then write reel, plot, and metadata under the chosen output location. There is no server, database, queue, or persistent source-media ingestion layer; processing is single-run and file-oriented.

Paths: `detector.py`, `output/clips_metadata.json`

Main entry points: [`detector.py`](https://github.com/shivammitra/gaming-moment-detector/blob/main/detector.py) (Highlight renderer), [`youtube_source.py`](https://github.com/shivammitra/gaming-moment-detector/blob/main/youtube_source.py) (YouTube resolver), [`requirements.txt`](https://github.com/shivammitra/gaming-moment-detector/blob/main/requirements.txt) (Python media runtime), [`output/clips_metadata.json`](https://github.com/shivammitra/gaming-moment-detector/blob/main/output/clips_metadata.json) (Plot & clip metadata)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_source["Source Acquisition"]
  node_cli["Detector CLI<br/>Python entry point<br/>[detector.py]"]
  node_local_video["Local video<br/>file source"]
  node_youtube_resolver{{"YouTube resolver<br/>yt-dlp adapter<br/>[youtube_source.py]"}}
  node_stream_url["Direct stream URL<br/>external CDN source"]
  node_synthetic_generator["Synthetic video generator<br/>validation utility"]
end

subgraph group_analysis["Motion Analysis"]
  node_frame_reader["Sequential frame reader<br/>OpenCV stage<br/>[detector.py]"]
  node_motion_signal["Motion signal<br/>per-frame data<br/>[detector.py]"]
end

subgraph group_selection["Moment Selection"]
  node_signal_conditioning["Signal smoothing<br/>NumPy processing<br/>[detector.py]"]
  node_peak_selection["Peak ranking<br/>SciPy selection<br/>[detector.py]"]
  node_clip_windows["Bounded clip windows<br/>time intervals<br/>[detector.py]"]
end

subgraph group_rendering["Rendering &amp; Artifacts"]
  node_reel_renderer["Highlight renderer<br/>MoviePy stage<br/>[detector.py]"]
  node_highlight_reel["Highlight reel<br/>MP4 artifact<br/>[highlight_reel.mp4]"]
  node_inspection_artifacts["Plot &amp; clip metadata<br/>output artifacts"]
end

node_runtime{{"Python media runtime<br/>dependency boundary<br/>[requirements.txt]"}}

node_cli -->|"accepts path"| node_local_video
node_cli -->|"--youtube"| node_youtube_resolver
node_youtube_resolver -->|"resolves"| node_stream_url
node_local_video -->|"video source"| node_frame_reader
node_stream_url -->|"video source"| node_frame_reader
node_frame_reader -->|"frame differences"| node_motion_signal
node_motion_signal -->|"raw values"| node_signal_conditioning
node_signal_conditioning -->|"smoothed signal"| node_peak_selection
node_peak_selection -->|"top_n peaks"| node_clip_windows
node_local_video -->|"same source"| node_reel_renderer
node_stream_url -->|"same source"| node_reel_renderer
node_clip_windows -->|"extract windows"| node_reel_renderer
node_reel_renderer -->|"writes MP4"| node_highlight_reel
node_motion_signal -->|"plot"| node_inspection_artifacts
node_peak_selection -->|"scores and timestamps"| node_inspection_artifacts
node_synthetic_generator -.->|"generates input"| node_local_video
node_runtime -.->|"runs"| node_cli
node_runtime -.->|"yt-dlp"| node_youtube_resolver

click node_cli "https://github.com/shivammitra/gaming-moment-detector/blob/main/detector.py"
click node_youtube_resolver "https://github.com/shivammitra/gaming-moment-detector/blob/main/youtube_source.py"
click node_frame_reader "https://github.com/shivammitra/gaming-moment-detector/blob/main/detector.py"
click node_motion_signal "https://github.com/shivammitra/gaming-moment-detector/blob/main/detector.py"
click node_signal_conditioning "https://github.com/shivammitra/gaming-moment-detector/blob/main/detector.py"
click node_peak_selection "https://github.com/shivammitra/gaming-moment-detector/blob/main/detector.py"
click node_clip_windows "https://github.com/shivammitra/gaming-moment-detector/blob/main/detector.py"
click node_reel_renderer "https://github.com/shivammitra/gaming-moment-detector/blob/main/detector.py"
click node_highlight_reel "https://github.com/shivammitra/gaming-moment-detector/blob/main/output/highlight_reel.mp4"
click node_inspection_artifacts "https://github.com/shivammitra/gaming-moment-detector/blob/main/output/clips_metadata.json"
click node_synthetic_generator "https://github.com/shivammitra/gaming-moment-detector/blob/main/generate_test_video.py"
click node_runtime "https://github.com/shivammitra/gaming-moment-detector/blob/main/requirements.txt"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_cli,node_local_video,node_youtube_resolver,node_stream_url,node_synthetic_generator toneBlue
class node_frame_reader,node_motion_signal toneAmber
class node_signal_conditioning,node_peak_selection,node_clip_windows toneMint
class node_reel_renderer,node_highlight_reel,node_inspection_artifacts toneRose
class node_runtime toneNeutral
```

## Components

### Source Acquisition

- [Detector CLI](https://github.com/shivammitra/gaming-moment-detector/blob/main/detector.py): Python entry point · Accepts a local video path or --youtube URL. · `detector.py`
- **Local video**: file source
- [YouTube resolver](https://github.com/shivammitra/gaming-moment-detector/blob/main/youtube_source.py): yt-dlp adapter · Resolves URLs to temporary direct stream URLs. · `youtube_source.py`
- **Direct stream URL**: external CDN source · Time-limited source consumed without downloading media.
- [Synthetic video generator](https://github.com/shivammitra/gaming-moment-detector/blob/main/generate_test_video.py): validation utility · Produces synthetic input for end-to-end validation. · `generate_test_video.py`

### Motion Analysis

- [Sequential frame reader](https://github.com/shivammitra/gaming-moment-detector/blob/main/detector.py): OpenCV stage · Downsamples frames and converts them to grayscale. · `detector.py`
- [Motion signal](https://github.com/shivammitra/gaming-moment-detector/blob/main/detector.py): per-frame data · Consecutive-frame differences measure visual movement. · `detector.py`

### Moment Selection

- [Signal smoothing](https://github.com/shivammitra/gaming-moment-detector/blob/main/detector.py): NumPy processing · Edge padding and moving average suppress flicker. · `detector.py`
- [Peak ranking](https://github.com/shivammitra/gaming-moment-detector/blob/main/detector.py): SciPy selection · Applies prominence and distance constraints; flat signals error. · `detector.py`
- [Bounded clip windows](https://github.com/shivammitra/gaming-moment-detector/blob/main/detector.py): time intervals · Selected peaks become roughly -4s/+3s source intervals. · `detector.py`

### Rendering & Artifacts

- [Highlight renderer](https://github.com/shivammitra/gaming-moment-detector/blob/main/detector.py): MoviePy stage · Extracts labeled clips and concatenates the final reel. · `detector.py`
- [Highlight reel](https://github.com/shivammitra/gaming-moment-detector/blob/main/output/highlight_reel.mp4): MP4 artifact · `output/highlight_reel.mp4`
- [Plot & clip metadata](https://github.com/shivammitra/gaming-moment-detector/blob/main/output/clips_metadata.json): output artifacts · Writes the motion plot plus timestamps and scores. · `output/clips_metadata.json`

### Other components

- [Python media runtime](https://github.com/shivammitra/gaming-moment-detector/blob/main/requirements.txt): dependency boundary · Python 3.9+ with OpenCV, SciPy, MoviePy 2.x, Matplotlib, and yt-dlp. · `requirements.txt`

## Connections

- Detector CLI → Local video: accepts path
- Detector CLI → YouTube resolver: --youtube
- YouTube resolver → Direct stream URL: resolves
- Local video → Sequential frame reader: video source
- Direct stream URL → Sequential frame reader: video source
- Sequential frame reader → Motion signal: frame differences
- Motion signal → Signal smoothing: raw values
- Signal smoothing → Peak ranking: smoothed signal
- Peak ranking → Bounded clip windows: top_n peaks
- Local video → Highlight renderer: same source
- Direct stream URL → Highlight renderer: same source
- Bounded clip windows → Highlight renderer: extract windows
- Highlight renderer → Highlight reel: writes MP4
- Motion signal → Plot & clip metadata: plot
- Peak ranking → Plot & clip metadata: scores and timestamps
- Synthetic video generator → Local video: generates input
- Python media runtime → Detector CLI: runs
- Python media runtime → YouTube resolver: yt-dlp
