-
Notifications
You must be signed in to change notification settings - Fork 29
feat: add video file writing support for macOS and Windows #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
c841c1f
feat: add video file writing support for macOS and Windows
wysaid 3d57d3b
fix: restructure video writer to fix Windows build and address review…
wysaid ba6a43a
fix: remove codecapi.h include to fix MinGW build
wysaid 4feb1da
feat: add camera-to-video recording demo and CLI --record support
wysaid 30eb121
chore: add Run 6-record_video tasks for Debug and Release
wysaid d5eb708
fix: use steady_clock for video writer timestamps to avoid first-fram…
wysaid 2bb278b
fix: respect VideoFrame::orientation in shared video writer NV12 conv…
wysaid 3a116a1
fix: finalize writer PR hardening and docs alignment
wysaid e9984e9
fix: address review feedback for video writer and CLI parsing
wysaid 52f074c
docs/tests: address follow-up review comments
wysaid 3126c0c
cli: ensure --record triggers capture mode (include recordVideoPath i…
wysaid e060286
fix(cli): propagate frame timestamps to VideoWriter; add preview+reco…
wysaid 4467e78
test/cli: add regression tests and update CLI help and VSCode tasks f…
wysaid 87bd68b
fix: remove unreachable null check on getFreeFrame() and document non…
wysaid 5a0d820
feat: add C interface video recording example
wysaid 98b7ab2
fix(writer): align auto bitrate with YouTube official recommended set…
wysaid e1e1f04
ci: add video writer test execution to macOS and Windows CI
wysaid File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| /** | ||
| * @file ccap_writer.h | ||
| * @author wysaid (this@wysaid.org) | ||
| * @brief Video writer header file for ccap. | ||
| * @date 2025-05 | ||
| * | ||
| * @note Requires CCAP_ENABLE_VIDEO_WRITER to be defined. | ||
| * Only available on Windows and macOS. | ||
| */ | ||
|
|
||
| #ifndef __cplusplus | ||
| #error "ccap_writer.h is for C++ only. For C language, please use ccap_writer_c.h instead." | ||
| #endif | ||
|
|
||
| #pragma once | ||
| #ifndef CCAP_WRITER_H | ||
| #define CCAP_WRITER_H | ||
|
|
||
| #include "ccap_def.h" | ||
|
|
||
| #include <memory> | ||
| #include <string_view> | ||
|
|
||
| namespace ccap { | ||
|
|
||
| /** | ||
| * @brief Video codec for encoding. | ||
| */ | ||
| enum class VideoCodec { | ||
| HEVC, ///< H.265 / HEVC (preferred, better compression) | ||
| H264, ///< H.264 / AVC (fallback, wider compatibility) | ||
| }; | ||
|
|
||
| /** | ||
| * @brief Video container format. | ||
| */ | ||
| enum class VideoFormat { | ||
| MP4, ///< MP4 container | ||
| MOV, ///< MOV container | ||
| }; | ||
|
|
||
| /** | ||
| * @brief Configuration for video writer. | ||
| */ | ||
| struct WriterConfig { | ||
| VideoCodec codec = VideoCodec::HEVC; ///< Preferred codec; auto-fallback to H.264 if unavailable | ||
| VideoFormat container = VideoFormat::MP4; | ||
| uint32_t width = 0; ///< Frame width in pixels | ||
| uint32_t height = 0; ///< Frame height in pixels | ||
| double frameRate = 30.0; ///< Target frame rate; 0 = variable rate | ||
| uint64_t bitRate = 5'000'000; ///< Target bit rate in bits/s; 0 = auto | ||
|
LeeGoDamn marked this conversation as resolved.
Outdated
|
||
| }; | ||
|
|
||
| /** | ||
| * @brief Video file writer. Captures frames and encodes them into a video file. | ||
| * @note This class is not thread-safe. Use it in a single thread or protect with a mutex. | ||
| */ | ||
| class CCAP_EXPORT VideoWriter { | ||
| public: | ||
| VideoWriter(); | ||
| ~VideoWriter(); | ||
|
|
||
| /// Move-only | ||
| VideoWriter(VideoWriter&&) noexcept; | ||
| VideoWriter& operator=(VideoWriter&&) noexcept; | ||
| VideoWriter(const VideoWriter&) = delete; | ||
| VideoWriter& operator=(const VideoWriter&) = delete; | ||
|
|
||
| /** | ||
| * @brief Open writer to a file path. | ||
| * @param filePath Output file path (e.g., "output.mp4") | ||
| * @param config Writer configuration (width, height, codec, etc.) | ||
| * @return true on success, false on failure. | ||
| */ | ||
| bool open(std::string_view filePath, const WriterConfig& config); | ||
|
|
||
| /// Close and finalize the file. | ||
| void close(); | ||
| bool isOpened() const; | ||
|
|
||
| /** | ||
| * @brief Write a single frame. | ||
| * @param frame The video frame to write. Pixel format will be converted to NV12 internally. | ||
| * @param timestampNs Optional timestamp in nanoseconds. If 0, auto-increment based on frameRate. | ||
| * @return true on success, false on failure. | ||
| */ | ||
| bool writeFrame(const VideoFrame& frame, uint64_t timestampNs = 0); | ||
|
|
||
| /// Query the actual codec being used (may differ from config due to fallback). | ||
| VideoCodec actualCodec() const; | ||
|
|
||
| uint32_t width() const; | ||
| uint32_t height() const; | ||
| double frameRate() const; | ||
|
|
||
| struct Impl; | ||
|
|
||
| private: | ||
| void* m_impl; | ||
| }; | ||
|
|
||
| } // namespace ccap | ||
|
|
||
| #endif // CCAP_WRITER_H | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| /** | ||
| * @file ccap_writer_c.h | ||
| * @author wysaid (this@wysaid.org) | ||
| * @brief Pure C interface for ccap video writer. | ||
| * @date 2025-05 | ||
| * | ||
| * @note Requires CCAP_ENABLE_VIDEO_WRITER to be defined. | ||
| * Only available on Windows and macOS. | ||
| */ | ||
|
|
||
| #pragma once | ||
| #ifndef CCAP_WRITER_C_H | ||
| #define CCAP_WRITER_C_H | ||
|
|
||
| #include "ccap_c.h" | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| /* ========== Forward Declarations ========== */ | ||
|
|
||
| /** @brief Opaque pointer to ccap::VideoWriter C++ object */ | ||
| typedef struct CcapVideoWriter CcapVideoWriter; | ||
|
|
||
| /* ========== Enumerations ========== */ | ||
|
|
||
| /** @brief Video codec enumeration */ | ||
| typedef enum { | ||
| CCAP_VIDEO_CODEC_HEVC = 0, ///< H.265 / HEVC (preferred) | ||
| CCAP_VIDEO_CODEC_H264 = 1, ///< H.264 / AVC (fallback) | ||
| } CcapVideoCodec; | ||
|
|
||
| /** @brief Video container format */ | ||
| typedef enum { | ||
| CCAP_VIDEO_FORMAT_MP4 = 0, | ||
| CCAP_VIDEO_FORMAT_MOV = 1, | ||
| } CcapVideoFormat; | ||
|
|
||
| /* ========== Data Structures ========== */ | ||
|
|
||
| /** @brief Video writer configuration */ | ||
| typedef struct { | ||
| CcapVideoCodec codec; ///< Preferred codec | ||
| CcapVideoFormat container; ///< Container format | ||
| uint32_t width; ///< Frame width | ||
| uint32_t height; ///< Frame height | ||
| double frameRate; ///< Target frame rate (0 = variable) | ||
| uint64_t bitRate; ///< Target bit rate in bits/s (0 = auto) | ||
| } CcapWriterConfig; | ||
|
|
||
| /* ========== Writer Lifecycle ========== */ | ||
|
|
||
| /** | ||
| * @brief Create a new video writer instance | ||
| * @return Pointer to CcapVideoWriter instance, or NULL on failure | ||
| */ | ||
| CCAP_EXPORT CcapVideoWriter* ccap_video_writer_create(void); | ||
|
|
||
| /** | ||
| * @brief Destroy a video writer instance and finalize the output file | ||
| * @param writer Pointer to CcapVideoWriter instance | ||
| */ | ||
| CCAP_EXPORT void ccap_video_writer_destroy(CcapVideoWriter* writer); | ||
|
|
||
| /** | ||
| * @brief Open writer to a file path | ||
| * @param writer Pointer to CcapVideoWriter instance | ||
| * @param filePath Output file path (e.g., "output.mp4") | ||
| * @param config Writer configuration | ||
| * @return true on success, false on failure | ||
| */ | ||
| CCAP_EXPORT bool ccap_video_writer_open(CcapVideoWriter* writer, const char* filePath, | ||
| const CcapWriterConfig* config); | ||
|
|
||
| /** | ||
| * @brief Close and finalize the output file | ||
| * @param writer Pointer to CcapVideoWriter instance | ||
| */ | ||
| CCAP_EXPORT void ccap_video_writer_close(CcapVideoWriter* writer); | ||
|
|
||
| /** | ||
| * @brief Check if writer is opened | ||
| * @param writer Pointer to CcapVideoWriter instance | ||
| * @return true if opened, false otherwise | ||
| */ | ||
| CCAP_EXPORT bool ccap_video_writer_is_opened(const CcapVideoWriter* writer); | ||
|
|
||
| /** | ||
| * @brief Write a single frame | ||
| * @param writer Pointer to CcapVideoWriter instance | ||
| * @param frameInfo Frame data to write (must match configured width/height) | ||
| * @param timestampNs Timestamp in nanoseconds (0 for auto-increment) | ||
| * @return true on success, false on failure | ||
| */ | ||
| CCAP_EXPORT bool ccap_video_writer_write_frame(CcapVideoWriter* writer, | ||
| const CcapVideoFrameInfo* frameInfo, | ||
| uint64_t timestampNs); | ||
|
|
||
| /** | ||
| * @brief Get the actual codec being used (may differ from config due to fallback) | ||
| * @param writer Pointer to CcapVideoWriter instance | ||
| * @return Actual codec enum value | ||
| */ | ||
| CCAP_EXPORT CcapVideoCodec ccap_video_writer_actual_codec(const CcapVideoWriter* writer); | ||
|
LeeGoDamn marked this conversation as resolved.
|
||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
| #endif /* CCAP_WRITER_C_H */ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.