Skip to content

Commit 6cf074d

Browse files
committed
feat: Implement robust batch processing using isolated subprocesses to prevent VRAM fragmentation and OOM errors.
1 parent ea86934 commit 6cf074d

17 files changed

Lines changed: 73 additions & 13 deletions

File tree

.github/workflows/linting.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ jobs:
3434
run: pip install -e .
3535

3636
- name: Run Ruff
37-
run: ruff check .
37+
run: |
38+
ruff check .
39+
ruff check --select D --config 'lint.pydocstyle.convention="google"' src/
3840
3941
- name: Run Mypy
4042
run: mypy src/shorts_maker

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ For the original CPU-only version, please visit [Shorts Maker](https://github.co
2929
- **Video Analysis**: Zero-copy GPU memory streaming for stable motion estimation (replaces heavy frame indices).
3030
- **Image Processing**: Native PyTorch operators used for heavy operations like blurring backgrounds (separable convolutions).
3131
- **Rendering**: Custom PyTorch+NVENC engine for high-performance rendering (MoviePy removed from render path).
32+
- **Robust Batch Processing**: Video processing runs in fully isolated subprocesses, completely clearing CUDA contexts between files to prevent VRAM fragmentation and OOM crashes (especially in Docker/WSL).
3233
- Audio + video action scoring:
3334
- Combined ranking with tunable weights (defaults: audio 0.6, video 0.4).
3435
- Scenes ranked by combined action score rather than duration.

docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ For the original CPU-only version, please visit [Shorts Maker](https://github.co
2626
- **Video Analysis**: Zero-copy GPU memory streaming for stable motion estimation (replaces heavy frame indices).
2727
- **Image Processing**: Native PyTorch operators used for heavy operations like blurring backgrounds (separable convolutions).
2828
- **Rendering**: Custom PyTorch+NVENC engine for high-performance rendering (MoviePy removed from render path).
29+
- **Robust Batch Processing**: Video processing runs in fully isolated subprocesses, completely clearing CUDA contexts between files to prevent VRAM fragmentation and OOM crashes (especially in Docker/WSL).
2930
- Audio + video action scoring:
3031
- Combined ranking with tunable weights (defaults: audio 0.6, video 0.4).
3132
- Scenes ranked by combined action score rather than duration.

src/shorts_maker/__init__.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
"""
2-
Shorts Maker GPU: A high-performance video processing library.
3-
"""
1+
"""Shorts Maker GPU: A high-performance video processing library."""
42
import logging
53

64
from .config import ProcessingConfig
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Audio and video analysis modules for generating action profiles."""

src/shorts_maker/analysis/audio.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"""Audio analysis module for computing an action profile based on loudness and spectral flux."""
2+
13
import logging
24
from pathlib import Path
35
from typing import Tuple

src/shorts_maker/analysis/video.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"""Video analysis module for computing an action profile from frame-by-frame pixel differences."""
2+
13
import logging
24
from pathlib import Path
35
from typing import Tuple
@@ -34,7 +36,6 @@ def compute_video_action_profile(
3436
- times (np.ndarray): Array of timestamps (in seconds) for each evaluated frame.
3537
- score (np.ndarray): Array of normalized, smoothed action scores.
3638
"""
37-
3839
# 1) Get metadata and calculate dimensions
3940
try:
4041
dmx = nvc.PyFFmpegDemuxer(str(video_path))

src/shorts_maker/cli.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
"""Command Line Interface for Shorts Maker, orchestrating the entire processing pipeline."""
2+
13
import logging
4+
import multiprocessing
25
from pathlib import Path
36

47
import typer
@@ -9,6 +12,14 @@
912

1013
app = typer.Typer(help="GPU-accelerated shorts generator.")
1114

15+
def _process_video_worker(config: ProcessingConfig, video_file: Path, output_dir: Path) -> None:
16+
"""Isolated worker for processing a single video.
17+
18+
Ensures that PyTorch and VPF memory is entirely cleared upon exit.
19+
"""
20+
processor = VideoProcessor(config)
21+
processor.process_video(video_file, output_dir)
22+
1223

1324
@app.command()
1425
def process(
@@ -53,15 +64,27 @@ def process(
5364
logger.warning(f"No '{input_dir}' directory found. Exiting.")
5465
raise typer.Exit(code=1)
5566

56-
processor = VideoProcessor(config)
57-
5867
for video_file in input_dir.iterdir():
5968
if video_file.is_file() and video_file.suffix.lower() in [
6069
".mp4",
6170
".mkv",
6271
".mov",
6372
]:
64-
processor.process_video(video_file, output_dir)
73+
logger.info(f"\n--- Spawning isolated process for: {video_file.name} ---")
74+
75+
# Using 'spawn' guarantees a clean process without inherited CUDA contexts
76+
ctx = multiprocessing.get_context("spawn")
77+
p = ctx.Process(
78+
target=_process_video_worker,
79+
args=(config, video_file, output_dir)
80+
)
81+
p.start()
82+
p.join()
83+
84+
if p.exitcode != 0:
85+
logger.error(f"Processing failed for {video_file.name} with exit code {p.exitcode}")
86+
if p.exitcode in (-9, 137):
87+
logger.error("Process was likely OOM killed by Docker/WSL.")
6588

6689

6790
if __name__ == "__main__":

src/shorts_maker/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"""Configuration definitions and loaded settings for the Shorts Maker pipeline."""
2+
13
from pydantic import Field
24
from pydantic_settings import BaseSettings, SettingsConfigDict
35

src/shorts_maker/core/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Core processing logic orchestrating scene detection, scoring, and rendering."""

0 commit comments

Comments
 (0)