How lvzhenbo/screc works
Project Type and Purpose
• This is an open-source, AI-driven, Rust-based CLI tool (“screc”) for monitoring and recording live streams from StripChat.
• It provides automated state polling, HLS segment downloading, decryption, retry logic, FFmpeg conversion, multi-user concurrency, proxy support, and a robust logging system.
File Structure Analysis
• Top level:
• Cargo.toml / src/ : Rust project
• .github/workflows : CI and release pipelines
• src/ modules:
• main.rs — entry point, CLI parsing (clap)
• config.rs — loads merges CLI args + JSON config (serde)
• stripchat.rs — StripChat API client, state machine, recording logic
• downloader.rs — HLS segment fetcher, retry, decryption
• utils.rs — shared helpers (URL parsing, time, logging setup)
• External integrations: StripChat REST API, HLS/CDN, FFmpeg CLI, filesystem, proxies, environment variables.
Key Components
A. CLI Layer
• User invokes screc with flags or --generate-config
• clap parser → dispatch to config loader or generator.
B. Configuration Manager
• Loads JSON file + merges CLI, env vars (proxy settings, cookies, resolution, intervals).
C. Scheduler / State Monitor
• Tokio runtime spawns concurrent tasks per username
• Polls StripChat API (reqwest) periodically
• State machine (Public, Private, Offline, Error, etc.)
D. Recording Pipeline
1. HLS Playlist Fetch
• downloader.fetch_playlist(url)
2. Segment Download & Decrypt
• downloader.download_segment() + MOUFLON decrypt
3. Retry Logic & Backoff
4. FFmpeg Merge & Conversion
• Shell out to ffmpeg for final MP4
E. I/O and Persistence
• File system: output_dir/<username>/<username>-<timestamp>.mp4
F. External Services
• StripChat API (HTTP + cookies)
• CDN / HLS servers
• FFmpeg binary
• Proxy endpoints (HTTP/HTTPS/SOCKS5)
Relationships and Flow
• The CLI triggers the Configuration Manager → yields a Config object.
• The Scheduler spawns one Worker Task per username.
• Each Worker loops: call StripChat API → if live, initiate Recording Pipeline → download segments → decrypt → pass to FFmpeg.
• Errors bubble up to retry logic; SIGINT triggers graceful shutdown broadcast.
Architectural Patterns and Principles
• Modular design: each Rust module has a single responsibility (SRP).
• Asynchronous concurrency: Tokio + futures for multi-stream recording.
• State machine for stream status transitions.
• Dependency inversion: HTTP clients (reqwest) and file I/O behind traits in utils.
• Resilience: retry/backoff strategy, skip logic, graceful shutdown.
Technologies & Libraries
• Language / Runtime: Rust 2024 + Tokio async
• HTTP & Proxies: reqwest
• JSON: serde / serde_json
• CLI: clap
• HLS parsing: m3u8-rs
• Error handling: anyhow
• Crypto: sha2 / base64 (MOUFLON decrypt)
• Time: chrono
• URL handling: url
• Build / CI: Cargo, GitHub Actions
Essential Elements to Capture
• CLI entry point and flag handling
• Config load + merging order (file → env → CLI)
• Concurrency model (one task per user)
• State machine driving recording logic
• Downloader + decryptor + FFmpeg chain
• Logging and graceful shutdown mechanism
• External dependencies and network flows