How shivammitra/gaming-moment-detector works
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 Highlight renderer · youtube_source.py YouTube resolver · requirements.txt Python media runtime · clips_metadata.json Plot & clip metadata
Components 14
Source Acquisition
- Detector CLI · Accepts a local video path or --youtube URL.
detector.py - Local video
- YouTube resolver · Resolves URLs to temporary direct stream URLs.
youtube_source.py - Direct stream URL · Time-limited source consumed without downloading media.
- Synthetic video generator · Produces synthetic input for end-to-end validation.
generate_test_video.py
Motion Analysis
- Sequential frame reader · Downsamples frames and converts them to grayscale.
detector.py - Motion signal · Consecutive-frame differences measure visual movement.
detector.py
Moment Selection
- Signal smoothing · Edge padding and moving average suppress flicker.
detector.py - Peak ranking · Applies prominence and distance constraints; flat signals error.
detector.py - Bounded clip windows · Selected peaks become roughly -4s/+3s source intervals.
detector.py
Rendering & Artifacts
- Highlight renderer · Extracts labeled clips and concatenates the final reel.
detector.py - Highlight reel
output/highlight_reel.mp4 - Plot & clip metadata · Writes the motion plot plus timestamps and scores.
output/clips_metadata.json
Other components
- Python media runtime · Python 3.9+ with OpenCV, SciPy, MoviePy 2.x, Matplotlib, and yt-dlp.
requirements.txt
Connections 18
- 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