ClipService handles creating clips from recorded sessions and replay buffers. It uses FFmpeg to extract segments, apply encoding settings, and concatenate multiple selections into a single output file.
Overview
Implemented inBackend/Media/ClipService.cs, the service provides:
- Extract multiple segments from different source videos
- Concatenate segments into a single clip
- Re-encode with configurable quality settings
- Hardware and software encoding support
- Progress tracking and cancellation
- Automatic metadata generation
Core Methods
CreateClips
Creates a clip from one or more video selections.List of video segments to include in the clip. Each selection contains:
Game: Game nameFileName: Source video filenameFilePath: Full path to source video (optional, auto-resolved if missing)Type: Content type (Session, Buffer, or Clip)StartTime: Start time in secondsEndTime: End time in secondsTitle: Optional clip titleIgdbId: Optional IGDB game ID
- Validates selections and calculates total duration
- Extracts each segment to a temporary file
- Concatenates temporary files (if multiple selections)
- Generates metadata, thumbnail, and waveform
- Adds clip to content library
- Cleans up temporary files
ClipProgress messages to the frontend with:
id: Unique clip operation IDprogress: Progress percentage (0-100, or -1 for error)selections: The original selections listerror: Error message (only when progress = -1)
CancelClip
Cancels an in-progress clip operation.The unique ID of the clip operation to cancel (provided in ClipProgress messages)
- Kills all active FFmpeg processes associated with the clip ID
- Removes processes from tracking
- Sends completion message to frontend
- Cleanup of temporary files is handled by the original CreateClips task
Example Usage
Cancellation is immediate and forces FFmpeg process termination. Partial output files are automatically cleaned up.
Encoding Configuration
The service uses settings fromSettings.Instance to configure encoding:
Encoder Selection
"gpu": Use hardware acceleration"cpu": Use software encoding
Codec Selection
"h264": H.264/AVC"h265": H.265/HEVC"av1": AV1
Hardware Encoder Settings
NVIDIA (NVENC):CQ level for NVENC (lower = higher quality, typical range: 15-35)
NVENC preset:
p1 (fastest) to p7 (slowest), or Quality, Performance-cq {quality} -preset {preset}
AMD (AMF):
QP level for AMF (lower = higher quality, typical range: 15-35)
AMF usage mode:
quality, transcoding, lowlatency, ultralowlatency-rc cqp -qp_i {quality} -qp_p {quality} -usage {preset}
Intel (QuickSync):
Global quality for QSV ICQ mode (lower = higher quality, typical range: 15-35)
QSV preset:
veryfast, faster, fast, medium, slow, slower, veryslow-global_quality {quality} -preset {preset}
Software Encoder Settings
CRF value for libx264/libx265 (lower = higher quality, typical range: 18-28)
x264/x265 preset:
ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow-crf {quality} -preset {preset}
Additional Settings
Target frame rate (0 = keep source frame rate)
AAC audio bitrate (e.g.,
"128k", "192k", "256k", "320k")Codec Matrix
The service automatically selects the appropriate codec based on encoder and codec settings:| GPU Vendor | ClipCodec | FFmpeg Encoder |
|---|---|---|
| NVIDIA | h264 | h264_nvenc |
| NVIDIA | h265 | hevc_nvenc |
| NVIDIA | av1 | av1_nvenc |
| AMD | h264 | h264_amf |
| AMD | h265 | hevc_amf |
| AMD | av1 | av1_amf |
| Intel | h264 | h264_qsv |
| Intel | h265 | hevc_qsv |
| Intel | av1 | av1_qsv |
| CPU | h264 | libx264 |
| CPU | h265 | libx265 |
| CPU | av1 | Not supported (falls back to libx264) |
FFmpeg Command Examples
Single Clip Extraction
Multi-Clip Concatenation
concat_list.txt contains:
File Organization
Clips are saved to:- Video file:
{timestamp}.mp4 - Metadata:
{timestamp}.json(game, title, timestamps, IGDB ID) - Thumbnail:
{timestamp}.jpg - Waveform:
{timestamp}_waveform.png
Progress Tracking
The service tracks progress at multiple stages:| Progress % | Stage |
|---|---|
| 0-95% | Extracting and encoding segments |
| 96% | Concatenating segments |
| 97% | Verifying output file |
| 98% | Generating metadata |
| 99% | Creating thumbnail and waveform |
| 100% | Complete - loading into library |
| -1 | Error occurred |
Error Handling
The service includes comprehensive error handling:Validation Errors
- Empty selections list
- Zero or negative total duration
- FFmpeg not found
- Missing source files
Extraction Errors
- Failed to create temp clip file
- FFmpeg process errors
- Cancellation during extraction
Concatenation Errors
- Failed to create output file
- File not ready for metadata generation
Cleanup
All temporary files are cleaned up in thefinally block:
- Individual clip segments
- Concat list file
- Partial output files (on error)
Errors are logged with full stack traces and reported to the frontend via ClipProgress messages with
progress: -1 and an error description.Thread Safety
The service uses aProcessLock object to ensure thread-safe access to the active FFmpeg process dictionary:
- Adding processes during extraction
- Removing processes after completion
- Cancelling clips from frontend
Integration Points
- FFmpegService: Executes FFmpeg with progress callbacks
- ContentService: Generates thumbnails, metadata, and waveforms
- SettingsService: Loads clips into content library
- StorageService: Sanitizes game names for folder organization
- MessageService: Sends progress updates to frontend