ccap(C(C)amera(CAP)ture)是一款简单易用的 C/C++ 摄像头采集库,旨在为您提供简单高效的摄像头数据访问。它支持多种摄像头接口,包括 USB、FireWire、GigE、VGA、HDMI、DVI、V4L2、V4L2+、V4L2++、V4L2++++、V4L2++++++、V4L2++++++++、V4L2++++++++++、V4L2++++++++++++、V4L2++++++++++++++、V4L2++++++++++++++++、V4L2++++++++++++++++++、V4L2++++++++++++++++++++、V4L2++++++++++++++++++++++、V4L2++++++++++++++++++++++++、V4L2++++++++++++++++++++++++++、V4L2++++++
ccap(C(C)amera(CAP)ture)是一款简单易用的 C/C++ 摄像头采集库,旨在为您提供简单高效的摄像头数据访问。它支持多种摄像头接口,包括 USB、FireWire、GigE、VGA、HDMI、DVI、V4L2、V4L2+、V4L2++、V4L2++++、V4L2++++++、V4L2++++++++、V4L2++++++++++、V4L2++++++++++++、V4L2++++++++++++++、V4L2++++++++++++++++、V4L2++++++++++++++++++、V4L2++++++++++++++++++++、V4L2++++++++++++++++++++++、V4L2++++++++++++++++++++++++、V4L2++++++++++++++++++++++++++、V4L2++++++
A high-performance, lightweight cross-platform camera capture library with hardware-accelerated pixel format conversion, supporting both camera capture and video file playback (Windows/macOS). On Windows, ccap now fully supports both DirectShow and Media Foundation, with DirectShow remaining the default for strong virtual-camera compatibility. Provides complete C++ and pure C interfaces, plus Rust bindings.
Official Website: ccap.work
auto, environment overrides, and explicit backend selectionccap::VideoWriter, ccap_video_writer_*, or CLI --record (Windows/macOS, CCAP_ENABLE_VIDEO_WRITER=ON)This repository also includes a standalone agent skill for ccap, which can be used as a standard skill entry for installation, device inspection, capture, and video inspection workflows.
Use this skill when you want an agent to work through the practical ccap flows instead of reading the source tree first, for example:
ccap on the current machineThe skill guides agents to choose among an existing installation, Homebrew on macOS, source builds, and release-binary fallback, then prefer the ccap CLI with --json where supported.
Build and install from source (on Windows, use git-bash):
git clone https://github.com/wysaid/CameraCapture.git
cd CameraCapture
./scripts/build_and_install.shIntegrate directly using CMake FetchContent:
Add the following to your CMakeLists.txt:
include(FetchContent)
FetchContent_Declare(
ccap
GIT_REPOSITORY https://github.com/wysaid/CameraCapture.git
GIT_TAG main
)
FetchContent_MakeAvailable(ccap)
target_link_libraries(your_app PRIVATE ccap::ccap)You can then use ccap headers and features directly in your project.
Install and use via Homebrew on macOS:
First, install the binary with Homebrew:
brew tap wysaid/ccap
brew install ccapThen, use it in CMake:
find_package(ccap REQUIRED)
target_link_libraries(your_app ccap::ccap)ccap provides both complete C++ and pure C language interfaces to meet different project and development requirements:
#include
int main() {
ccap::Provider provider;
// List available cameras
auto devices = provider.findDeviceNames();
for (size_t i = 0; i width, frame->height,
ccap::pixelFormatToString(frame->pixelFormat).data());
}
}
return 0;
}Starting from v1.2.0, ccap uses a global error callback system for simplified error handling across all camera operations:
…powershell
$env:CCAP_WINDOWS_BACKEND = "msmf" .\ccap --list-devices
```cpp
// Force Media Foundation explicitly on Windows
ccap::Provider msmfProvider("", "msmf");
// Force DirectShow explicitly on Windows
ccap::Provider dshowProvider("", "dshow");Rust bindings are available as a crate on crates.io:
bindings/rust/Quick install:
cargo add ccap-rsOr, if you want the crate name in code to be ccap:
[dependencies]
ccap = { package = "ccap-rs", version = "" }ccap includes a powerful command-line tool for quick camera operations and video processing without writing code:
# Build with CLI tool enabled
mkdir build && cd build
cmake .. -DCCAP_BUILD_CLI=ON
cmake --build .
# List available cameras
./ccap --list-devices
# Capture 5 images from default camera
./ccap -c 5 -o ./captures
# Real-time preview (requires GLFW)
./ccap --preview
# Play video file and extract frames
./ccap -i video.mp4 -c 30 -o ./frames
# Video preview with playback controls
./ccap -i video.mp4 --preview --speed 1.0
# Record camera stream to MP4 (Windows/macOS)
./ccap -d 0 --record ./camera_capture.mp4 --timeout 5
…
bash
mkdir build && cd build
cmake .. -DCCAP_BUILD_EXAMPLES=ON
cmake --build .
# Run examples
./0-print_camera
./1-minimal_example
# Run the pure C variants (if you built C examples)
./0-print_camera_c
./1-minimal_example_cNote: Each desktop example is available in both C++ (.cpp) and pure C (.c) variants. Use the filenames with a trailing
_c(e.g.1-minimal_example_c.c) for the C versions.
ccap provides both complete C++ and pure C interfaces to meet different project requirements.
class Provider {
public:
// Constructors
Provider();
Provider(std::string_view deviceName, std::string_view extraInfo = "");
Provider(int deviceIndex, std::string_view extraInfo = "");
// Device discovery
std::vector findDeviceNames();
// Camera lifecycle
bool open(std::string_view deviceName = "", bool autoStart = true);
bool open(int deviceIndex, bool autoStart = true);
bool isOpened() const;
void close();
// Capture control
bool start();
void stop();
bool isStarted() const;
// Frame capture
std::shared_ptr grab(uint32_t timeoutInMs = 0xffffffff);
void setNewFrameCallback(std::function&)> callback);
// Property configuration
bool set(PropertyName prop, double value);
template bool set(PropertyName prop, T value);
double get(PropertyName prop);
// Device info and advanced configuration
std::optional getDeviceInfo() const;
bool isFileMode() const; // Check if playing video file vs camera
void setFrameAllocator(std::function allocatorFactory);
void setMaxAvailableFrameSize(uint32_t size);
void setMaxCacheFrameSize(uint32_t size);
};struct VideoFrame {
// Frame data
uint8_t* data[3] = {}; // Raw pixel data planes
uint32_t stride[3] = {}; // Stride for each plane
// Frame properties
PixelFormat pixelFormat = PixelFormat::Unknown; // Pixel format
uint32_t width = 0; // Frame width in pixels
uint32_t height = 0; // Frame height in pixels
uint32_t sizeInBytes = 0; // Total frame data size
uint64_t timestamp = 0; // Frame timestamp in nanoseconds
uint64_t frameIndex = 0; // Unique incremental frame index
FrameOrientation orientation = FrameOrientation::Default; // Frame orientation
// Memory management and platform features
std::shared_ptr allocator; // Memory allocator
void* nativeHandle = nullptr; // Platform-specific handle
};enum class PropertyName {
Width, Height, FrameRate,
PixelFormatInternal, // Camera's internal format
PixelFormatOutput, // Output format (with conversion)
FrameOrientation,
// Video file playback properties (file mode only)
Duration, // Video duration in seconds (read-only)
CurrentTime, // Current playback time in seconds
FrameCount, // Total frame count (read-only)
PlaybackSpeed // Playback speed multiplier (1.0 = normal speed)
};
enum class PixelFormat : uint32_t {
Unknown = 0,
NV12, NV12f, // YUV 4:2:0 semi-planar
I420, I420f, // YUV 4:2:0 planar
RGB24, BGR24, // 24-bit RGB/BGR
RGBA32, BGRA32 // 32-bit RGBA/BGRA
};Video writing is available on Windows and macOS when CCAP_ENABLE_VIDEO_WRITER=ON.
#include
#include
ccap::Provider provider;
ccap::VideoWriter writer;
if (provider.open("", true)) {
ccap::WriterConfig cfg;
cfg.width = 1280;
cfg.height = 720;
cfg.frameRate = 30.0;
cfg.codec = ccap::VideoCodec::H264;
cfg.container = ccap::VideoFormat::MP4;
if (writer.open("camera_record.mp4", cfg)) {
while (auto frame = provider.grab(3000)) {
// timestampNs == 0 means auto timestamp generation from frameRate.
writer.writeFrame(*frame, 0);
}
writer.close();
}
}Notes:
NV12, I420, BGR24, and BGRA32.VideoFrame::orientation is honored by the writer path (including BottomToTop frames common on Windows RGB capture).CCAP_ENABLE_VIDEO_WRITER is independent from CCAP_ENABLE_FILE_PLAYBACK.namespace ccap {
// Hardware capabilities
bool hasAVX2();
bool hasAppleAccelerate();
bool hasNEON();
// Backend management
ConvertBackend getConvertBackend();
bool setConvertBackend(ConvertBackend backend);
// Format utilities
std::string_view pixelFormatToString(PixelFormat format);
// File operations
std::string dumpFrameToFile(VideoFrame* frame, std::string_view filename);
// Logging
enum class LogLevel { None, Error, Warning, Info, Verbose };
void setLogLevel(LogLevel level);
}ccap supports playing video files using the same API as camera capture (Windows and macOS only):
#include
ccap::Provider provider;
// Open video file - same API as camera
if (provider.open("/path/to/video.mp4", true)) {
// Check if in file mode
if (provider.isFileMode()) {
// Get video proper暂无开放 Issues,或尚未同步最近议题。