Skip to content

osalamon/BJJ-Analyzer-Rust

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

15 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

BJJ Video Analyzer - Rust Implementation

High-performance Brazilian Jiu-Jitsu video analysis and processing system written in Rust. This is a complete rewrite of the Python version, designed for production use with significant performance improvements.

πŸš€ Performance Improvements

  • 10-50x faster video processing compared to Python
  • 50-80% less memory usage
  • Native concurrency with Tokio async runtime
  • Single binary deployment (no Python runtime needed)
  • Superior error handling with Rust's type system

🎯 Features

Core Processing

  • Video Analysis: Extract metadata, duration, resolution, format information
  • Audio Extraction: High-quality audio extraction optimized for transcription
  • Batch Processing: Parallel processing with configurable worker pools
  • Scene Detection: Automatic chapter detection using FFmpeg analysis
  • Audio Enhancement: Noise reduction and optimization for better transcription

Transcription Integration

  • Multiple Providers: OpenAI, AssemblyAI, Google Cloud, Azure, Local Whisper
  • πŸ†• Remote GPU Whisper: Offload transcription to dedicated GPU server (5-10x faster)
  • Automatic Fallback: Remote β†’ Local β†’ Error with configurable fallback
  • Retry Logic: Automatic retry with exponential backoff
  • Quality Optimization: Audio preprocessing for better transcription accuracy
  • Chunking Support: Large file handling with intelligent segmentation

Production Features

  • Configuration Management: TOML/environment variable configuration
  • Comprehensive Logging: Structured logging with configurable levels
  • Health Checks: Service health monitoring and validation
  • Metrics: Performance monitoring and statistics
  • Error Recovery: Graceful failure handling and recovery

Additional Tools

  • πŸ–₯️ Web UI: Browser-based interface for video management and series organization (ui/)
  • πŸ” Chapter Finder: Tool to discover BJJ Fanatics product pages from video filenames (tools/chapter-finder/)
  • πŸš€ Remote GPU Server: Docker container for high-performance Whisper transcription (remote-whisper-server/)
  • 🐍 Python Integration: Bridge for Python applications and scripts (examples/)

πŸ“¦ Installation

Prerequisites

# Install Rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

# Install FFmpeg (required)
# macOS
brew install ffmpeg

# Ubuntu/Debian
sudo apt install ffmpeg

# Install additional dependencies
sudo apt install build-essential pkg-config libssl-dev

Build from Source

# Clone the repository
git clone https://github.com/tigreroll/bjj-analyzer-rust.git
cd bjj-analyzer-rust

# Build release version
cargo build --release

# Run tests
cargo test

# Install globally
cargo install --path .

πŸ”§ Usage

Command Line Interface

# Basic usage
bjj-analyzer --video-dir ./videos --output-dir ./output

# With custom settings
bjj-analyzer \
  --video-dir ./videos \
  --output-dir ./output \
  --workers 8 \
  --verbose

# Show help
bjj-analyzer --help

Configuration File

Create bjj-analyzer.toml:

[processing]
supported_extensions = ["mp4", "mkv", "avi", "mov"]
max_file_size = 0  # No limit
skip_existing = true
validate_videos = true
enable_scene_detection = true

[audio]
target_sample_rate = 16000
target_format = "wav"
enable_enhancement = true
enhancement_filters = "highpass=f=80,lowpass=f=8000,volume=1.2"
cleanup_temp_files = true

[transcription]
provider = "Local"  # or "OpenAI", "AssemblyAI"
model = "base"
auto_detect_language = true
max_retries = 3
timeout = 300

[performance]
max_workers = 8
memory_limit_mb = 1024
enable_monitoring = true
enable_caching = true

[output]
base_dir = "./output"
enable_logging = true
log_level = "info"
save_metadata = true
export_formats = ["JSON"]

πŸš€ Remote GPU Transcription

For maximum performance, use a dedicated GPU server for transcription:

# 1. Start GPU server (on machine with NVIDIA GPU)
cd remote-whisper-server/
./deploy.sh start

# 2. Configure client to use remote server
cp config/remote-gpu-example.toml config/bjj-analyzer.toml

# 3. Process videos (5-10x faster!)
cargo run -- --video-dir ./videos

Benefits:

  • 5-10x faster transcription with GPU acceleration
  • Support for larger models (large-v3) requiring more GPU memory
  • Automatic fallback to local processing if remote unavailable
  • Lightweight Docker container (~500MB) for easy deployment

See REMOTE_GPU_SETUP.md for detailed setup instructions.

Python Integration

import bjj_analyzer_rust

# Process videos from Python
result = bjj_analyzer_rust.process_videos_rust(
    video_dir="./videos",
    output_dir="./output",
    workers=4,
    transcription_provider="openai",
    api_key="your-api-key"
)

print(f"Processed {result['successful']} videos successfully")

# Get video information
info = bjj_analyzer_rust.get_video_info_rust("./video.mp4")
print(f"Duration: {info['duration']}s, Resolution: {info['width']}x{info['height']}")

# Extract audio only
audio_path = bjj_analyzer_rust.extract_audio_rust("./video.mp4", "./output")
print(f"Audio extracted to: {audio_path}")

# Check system requirements
reqs = bjj_analyzer_rust.check_system_requirements()
if not reqs['ffmpeg_available']:
    print("FFmpeg not found - please install FFmpeg")

Advanced Python Usage

from bjj_analyzer_rust import PyBatchProcessor, PyConfig

# Create custom configuration
config = PyConfig.from_dict({
    "workers": 8,
    "sample_rate": 44100,
    "enable_enhancement": True,
    "enable_caching": True
})

# Set transcription provider
config.set_transcription_provider("openai", "your-api-key")

# Validate configuration
config.validate()

# Create processor
processor = PyBatchProcessor(workers=8)

# Process videos
result = processor.process_directory("./videos", "./output")

# Get statistics
stats = processor.get_stats()
print(f"Workers: {stats['max_workers']}, Available: {stats['available_permits']}")

πŸ› οΈ Additional Tools

Web UI (ui/)

Browser-based interface for managing video processing and series organization:

# Start the backend with API enabled
cargo run --features api -- --video-dir ./videos --api-port 8080

# Serve the UI (any web server)
cd ui && python -m http.server 3000
# Visit: http://localhost:3000

Features:

  • πŸ“Ή Video library browser with processing status
  • πŸ“š Series management and metadata editing
  • ✏️ Correction interface for series mapping
  • πŸ“Š Real-time processing status updates

Chapter Finder (tools/chapter-finder/)

Automatically discover BJJ Fanatics product pages from video filenames:

cd tools/chapter-finder
pip install -r requirements.txt

# Process a directory
python bjj_fanatics_finder.py "/path/to/videos"

# Process specific files
python bjj_fanatics_finder.py "JustStandUpbyCraigJones1.mp4"

Features:

  • πŸ” Automatic Google search for BJJ Fanatics pages
  • πŸ“ Saves results to product-pages.txt
  • πŸ•°οΈ Rate limiting to avoid search restrictions
  • πŸ“ Recursive directory processing

πŸ—οΈ Architecture

Core Components

  1. Video Processor (src/video.rs)

    • Video metadata extraction
    • Scene detection
    • Thumbnail generation
    • Format validation
  2. Audio Extractor (src/audio.rs)

    • High-quality audio extraction
    • Audio enhancement and filtering
    • Silence detection
    • Audio chunking
  3. Batch Processor (src/processing.rs)

    • Async parallel processing
    • Worker pool management
    • Progress tracking
    • Error recovery
  4. Configuration (src/config.rs)

    • TOML configuration parsing
    • Environment variable support
    • Validation and defaults
  5. API Integration (src/api.rs)

    • Multi-provider transcription
    • Retry logic and error handling
    • Health checking
  6. Python Bridge (src/python_bridge.rs)

    • PyO3-based Python bindings
    • Type conversion and error handling
    • Async runtime integration

Performance Characteristics

Operation Python (original) Rust Improvement
Video Analysis ~2.5s ~0.05s 50x faster
Audio Extraction ~8.2s ~0.3s 27x faster
Batch Processing (10 videos) ~45s ~2.1s 21x faster
Memory Usage ~1.2GB ~150MB 8x less memory
Startup Time ~3.5s ~0.1s 35x faster

πŸ§ͺ Testing

# Run all tests
cargo test

# Run with output
cargo test -- --nocapture

# Run specific test
cargo test test_video_discovery

# Run benchmarks
cargo bench

# Test Python bindings
cd python_bindings && python -m pytest tests/

πŸ“Š Benchmarking

# Benchmark video processing
bjj-analyzer benchmark --video-path ./sample.mp4 --iterations 10

# Compare with Python version
python benchmark_comparison.py

Example benchmark results:

Rust Implementation:
- Average processing time: 0.087s per video
- Memory usage: 45MB peak
- Throughput: 11.5 videos/second

Python Implementation:
- Average processing time: 2.34s per video  
- Memory usage: 380MB peak
- Throughput: 0.43 videos/second

Performance improvement: 26.9x faster, 8.4x less memory

πŸ”Œ Integration Examples

Drop-in Python Replacement

# Replace existing Python calls
# OLD:
# from src.adaptive.flexible_pipeline import process_videos_adaptive
# result = process_videos_adaptive(video_dir, mode='standalone')

# NEW:
import bjj_analyzer_rust
result = bjj_analyzer_rust.process_videos_rust(
    video_dir=video_dir,
    output_dir="./output",
    workers=8
)

Docker Integration

FROM rust:1.87-slim as builder
COPY . .
RUN cargo build --release

FROM debian:bookworm-slim
RUN apt-get update && apt-get install -y ffmpeg
COPY --from=builder /target/release/bjj-analyzer /usr/local/bin/
CMD ["bjj-analyzer", "--help"]

Web API Integration

use axum::{routing::post, Router, Json};
use bjj_analyzer_rust::{BatchProcessor, Config};

async fn process_endpoint(
    Json(request): Json<ProcessRequest>
) -> Json<ProcessResponse> {
    let processor = BatchProcessor::new(Config::default(), 4).await.unwrap();
    let result = processor.process_directory(
        request.video_dir.into(),
        request.output_dir.into()
    ).await.unwrap();
    
    Json(ProcessResponse { 
        successful: result.successful,
        failed: result.failed,
        total_time: result.total_time.as_secs_f64()
    })
}

let app = Router::new().route("/process", post(process_endpoint));

πŸš€ Production Deployment

Systemd Service

[Unit]
Description=BJJ Video Analyzer
After=network.target

[Service]
Type=simple
User=bjj-analyzer
ExecStart=/usr/local/bin/bjj-analyzer --video-dir /data/videos --output-dir /data/output
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target

Kubernetes Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: bjj-analyzer
spec:
  replicas: 3
  selector:
    matchLabels:
      app: bjj-analyzer
  template:
    metadata:
      labels:
        app: bjj-analyzer
    spec:
      containers:
      - name: bjj-analyzer
        image: tigreroll/bjj-analyzer-rust:latest
        resources:
          requests:
            memory: "256Mi"
            cpu: "250m"
          limits:
            memory: "512Mi"
            cpu: "500m"
        volumeMounts:
        - name: videos
          mountPath: /data/videos
        - name: output
          mountPath: /data/output
      volumes:
      - name: videos
        persistentVolumeClaim:
          claimName: bjj-videos-pvc
      - name: output
        persistentVolumeClaim:
          claimName: bjj-output-pvc

πŸ› Troubleshooting

Common Issues

  1. FFmpeg not found

    # Install FFmpeg
    sudo apt install ffmpeg  # Ubuntu/Debian
    brew install ffmpeg      # macOS
  2. Permission denied

    # Fix file permissions
    chmod +x /usr/local/bin/bjj-analyzer
  3. Memory issues

    # Reduce workers in config
    [performance]
    max_workers = 2
    memory_limit_mb = 512
  4. Python binding issues

    # Rebuild with Python support
    cargo build --features python-bindings

Debug Mode

# Enable debug logging
export RUST_LOG=bjj_analyzer_rust=debug
bjj-analyzer --verbose --video-dir ./videos

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Write tests for new functionality
  4. Ensure all tests pass: cargo test
  5. Submit a pull request

πŸ“„ License

MIT License - see LICENSE file for details.

πŸ™ Acknowledgments

  • Built with Rust
  • Async runtime by Tokio
  • Python bindings via PyO3
  • Video processing powered by FFmpeg
  • Inspired by the original Python BJJ Video Analyzer

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Rust 70.4%
  • JavaScript 12.2%
  • Python 7.3%
  • HTML 5.2%
  • CSS 2.8%
  • Shell 1.4%
  • Other 0.7%