Capcodec media codec
Capcodec is Cap’s H.264 video codec for screen recordings. It turns normalized video frames into a compact H.264 stream and can package that stream for file or segmented delivery. The media server uses it as an explicit alternative to its normal FFmpeg and libx264 path.
Codec role
The codec project aims to reduce the CPU cost of Cap video encoding while keeping screen content clear at a quality close to the x264 veryfast baseline. It is a standalone executable with its own encoder and container writers, while the media server supplies input frames, audio, process control, and final storage output (capcodec/README.md• L1-7).
The project treats screen recordings as a distinct workload. The encoder can use unchanged areas, text, scrolling regions, and motion structure to avoid work that does not improve the output. This focus explains why Capcodec is an opt-in path rather than a general replacement for every video encoder (capcodec/DECISIONS.md• L5-10).
References: capcodec/README.md• L1-7 capcodec/DECISIONS.md• L5-10
Media processing pipeline
Cap media processing separates video encoding from audio handling and container assembly. FFmpeg decodes and normalizes the source, Capcodec produces video-only H.264, and FFmpeg combines that video with the source audio into the service’s final MP4 output (apps/…/media-video.ts• L1489-1691).
The flow preserves a common media contract:
- Input video is scaled into the service’s maximum dimensions and converted to limited-range BT.709 4:2:0 frames.
- Capcodec receives exact frame dimensions and a frame-rate fraction.
- The codec writes video to a fragmented MP4 intermediate file.
- FFmpeg remuxes the video with copied or AAC audio and applies fast start metadata.
- The service reports progress, enforces a shared timeout, and removes temporary files after completion.
This diagram shows the boundary between the codec project and the runtime service.
flowchart LR
A[Source video] --> B[FFmpeg decode and normalize]
B --> C[Capcodec H.264 encode]
C --> D[Fragmented MP4 video]
A --> E[Source audio]
D --> F[FFmpeg remux]
E --> F
F --> G[Final MP4 for Cap storage or delivery]
References: apps/…/capcodec-encoder.ts• L177-238 apps/…/capcodec-encoder.ts• L286-375 apps/…/media-video.ts• L1694-1776
Frame preparation
Frame preparation gives the codec a stable input format. The service keeps aspect ratio within its size limits, makes both dimensions even, and uses a fixed YUV pixel format so the codec does not need to handle many source formats (apps/…/capcodec-encoder.ts• L129-159, apps/…/capcodec-encoder.ts• L177-207).
The frame rate is passed as an exact fraction when possible. This keeps timestamps stable for ordinary rates such as 23.976, 29.97, and 59.94 rather than relying on a rounded decimal (apps/…/capcodec-encoder.ts• L149-169).
References: apps/…/capcodec-encoder.ts• L129-169 apps/…/capcodec-encoder.test.ts• L84-114
Video encoding
The Capcodec encoder accepts raw frames on standard input and writes an H.264 video stream to a requested output path. Its main work is to compare the current frame with reference frames, choose intra or inter coding for changed regions, reconstruct the result, and write H.264 slices; these stages support both screen-specific work reduction and normal moving content (capcodec/src/encoder.tov#L1-L30, capcodec/src/encoder.tov#L556-L680).
The encoder supports keyframes, predictive frames, optional B-frames, several speed and quality presets, and both quality- and bitrate-oriented modes. The runtime uses a small preset set and maps familiar FFmpeg preset names into that set so job settings remain compatible with the rest of Cap (apps/…/capcodec-encoder.ts• L6-17, apps/…/capcodec-encoder.ts• L34-53).
The detailed encoder stages, command-line behavior, bitstream rules, and benchmark method are covered in Capcodec encoder implementation and benchmarking.
References: capcodec/…/encoder.tov• L1-30 capcodec/…/encoder.tov• L556-680 apps/…/capcodec-encoder.ts• L6-17 apps/…/capcodec-encoder.ts• L34-53
Container assembly
Capcodec’s muxing layer turns encoded H.264 access units into media containers. It records decode order, presentation order, keyframes, reference status, and frame durations so streams with reordered frames still play at the intended times (capcodec/src/mux/mp4.tov#L43-L129).
The MP4 writer supports progressive files and fragmented MP4. Its composition offsets and edit lists account for decoder reordering, which is required when B-frames are enabled. The HLS writer adds an initialization segment and a VOD media playlist that points to independent media segments (capcodec/src/mux/mp4.tov#L281-L427, capcodec/src/mux/mp4.tov#L430-L480, capcodec/src/mux/hls.tov#L1-L38).
References: capcodec/…/mp4.tov• L43-129 capcodec/…/mp4.tov• L281-427 capcodec/…/mp4.tov• L430-480 capcodec/…/hls.tov• L1-38
Runtime integration
The media server selects Capcodec through an environment-controlled contract. The default remains libx264; Capcodec is used only when CAP_MEDIA_VIDEO_ENCODER=capcodec, and the selected binary must be mounted into the runtime image because published images do not include it (apps/…/capcodec.md• L1-16).
The service combines job settings with optional environment overrides. A job normally supplies its CRF and preset, while deployment settings can select the binary and override quality, speed, keyframe spacing, or change-detection noise (apps/…/capcodec-encoder.ts• L78-104).
The integration boundary is intentionally narrow:
| Concern | Owner | Result |
|---|---|---|
| Source fetch, probing, and audio | Media server | Valid input and final audio track |
| Frame conversion and progress | FFmpeg plus media server | Raw YUV frames and job progress |
| H.264 video | Capcodec | Video-only encoded stream |
| Final MP4 and storage handoff | FFmpeg plus media server | Playable output file |
Runtime selection
Video processing first decides whether a transcode is needed. If the source already meets the requested video rules, the service can copy or remux it; if a video transcode is needed and the selector enables Capcodec, the service validates the executable and starts the Capcodec path (apps/…/media-video.ts• L1497-1552).
A missing or non-executable binary is a configuration error. The service reports that error before starting the encode and points operators to the mount and binary settings (apps/…/capcodec-encoder.ts• L107-117).
References: apps/…/media-video.ts• L1497-1552 apps/…/capcodec-encoder.ts• L78-117 apps/…/capcodec-encoder.test.ts• L15-53 apps/…/capcodec-encoder.test.ts• L116-121
Job control
The runtime treats decoding and encoding as one cancellable job. It registers both processes, forwards cancellation to them, reads progress from FFmpeg, checks both exit codes, and rejects an empty output (apps/…/capcodec-encoder.ts• L286-375).
The encode and mux share the job timeout. The mux receives only the time that remains after encoding, so a slow encode cannot silently consume the full processing budget and leave the final assembly without a deadline (apps/…/media-video.ts• L1711-1776, apps/…/capcodec-encoder.ts• L120-127).
References: apps/…/capcodec-encoder.ts• L286-375 apps/…/capcodec-encoder.ts• L120-127 apps/…/media-video.ts• L1711-1776
Deployment contract
The Capcodec executable is a deployment artifact rather than a package built into the published media-server image. Operators must provide a binary for the image architecture and set its path before enabling the selector (apps/…/capcodec.md• L3-12).
The service’s supported runtime settings are summarized below. Invalid values are ignored and replaced by safe defaults rather than passed to the codec.
| Setting | Purpose |
|---|---|
CAP_MEDIA_VIDEO_ENCODER | Selects Capcodec instead of libx264 |
CAPCODEC_BIN | Names the mounted executable |
CAPCODEC_CRF and CAPCODEC_PRESET | Override job quality and speed |
CAPCODEC_KEYINT and CAPCODEC_NOISE | Control keyframe spacing and change detection |
References: apps/…/capcodec.md• L3-16 apps/…/capcodec-encoder.ts• L55-104 apps/…/capcodec-encoder.test.ts• L55-82
Quality and performance
Capcodec is measured against x264 veryfast at matched quality. The benchmark results show the intended trade: strong speed and memory results on screen clips, with results that vary by content type and preset (apps/…/capcodec-benchmarks.md• L1-20).
The benchmark page is a measurement record, not a runtime guarantee. It records the corpus, quality comparison, speed method, encoder revisions, and known differences between measured and pending branches. Use Capcodec encoder implementation and benchmarking for the benchmark harness and reporting model, and Media delivery services for the wider service context.
References: apps/…/capcodec-benchmarks.md• L1-26 apps/…/capcodec-benchmarks.md• L142-193
Related systems
Capcodec sits between Cap’s media processing service and its delivery systems. The codec page explains the codec boundary; Media delivery services explains the service layer around video jobs, and Media server and web cluster explains the deployment and web-serving environment.
The child page Capcodec encoder implementation and benchmarking covers the internal encoder modules, bitstream and container generation, command-line surface, corpus, harness, and performance reports.
References: apps/…/capcodec.md• L14-16 apps/…/media-video.ts• L1489-1776
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the raw-frame format, color tags, dimensions, and frame-rate contract aligned between FFmpeg decode and the Capcodec CLI.
- Keep preset and environment-name changes aligned between runtime parsing, operator documentation, and deployment images.
- When frame reordering changes, update MP4 composition offsets, edit lists, fragmented initialization data, and HLS playback behavior together.
- When the binary build target changes, update the architecture-specific mount and release instructions.
- When audio or mux behavior changes, check that the video-only intermediate remains compatible with the final audio remux.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Capcodec is enabled without a mounted executable | The video job fails before encoding | Validate the executable and mount it through CAPCODEC_BIN |
| FFmpeg cannot decode or normalize the input | No raw frames reach the encoder | Check decoder exit status and retain recent stderr |
| Capcodec or the final mux exits non-zero | The output job fails and its temporary output is removed | Check both process exit codes and use the shared timeout |
| Reordered frames are packaged without matching composition timing | Players show delayed or out-of-order video | Keep MP4 edit lists and composition offsets in step with B-frame settings |
| The encoder produces an empty intermediate or final file | The job returns no usable media | Check file size after encoding and after muxing |
Key files
| File | Why to read it |
|---|---|
| capcodec/README.md | Defines the codec project’s role and how its binary relates to the media server |
capcodec/src/encoder.tov | Shows the codec’s high-level frame and reference model |
capcodec/src/mux/mp4.tov | Shows how H.264 samples become progressive and fragmented MP4 |
capcodec/src/mux/hls.tov | Shows the codec’s segmented delivery output |
| apps/…/capcodec-encoder.ts | Defines the runtime boundary, options, process flow, and cancellation behavior |
| apps/…/media-video.ts | Shows transcode selection, audio remux, time limits, and final output handling |
| apps/…/capcodec.md | Gives the operator-facing deployment contract |
| apps/…/capcodec-benchmarks.md | Records the quality, speed, memory, and latency comparison method |