-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy path5-play_video_c.c
More file actions
197 lines (165 loc) · 7.13 KB
/
5-play_video_c.c
File metadata and controls
197 lines (165 loc) · 7.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/**
* @file 5-play_video_c.c
* @brief Example demonstrating video file playback using ccap C interface
* @author wysaid (this@wysaid.org)
* @date 2025-12
*
* This example demonstrates how to use ccap C interface to play video files.
* The same API works for both camera capture and video file playback.
*/
#include "ccap_c.h"
#include "ccap_utils_c.h"
#include "utils/helper.h"
#include <ctype.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Error callback function
void error_callback(CcapErrorCode errorCode, const char* errorDescription, void* userData) {
(void)userData; // Unused parameter
printf("Error - Code: %d, Description: %s\n", (int)errorCode, errorDescription);
}
int main(int argc, char** argv) {
printf("ccap C Interface Video Playback Example\n");
printf("Version: %s\n\n", ccap_get_version());
ExampleCommandLine commandLine = { 0 };
initExampleCommandLine(&commandLine, argc, argv);
#ifdef __linux__
fprintf(stderr, "\n[WARNING] Video playback is currently not supported on Linux.\n");
fprintf(stderr, "This feature may be implemented in a future version.\n");
fprintf(stderr, "Currently supported platforms: Windows, macOS\n\n");
return 0;
#endif
const char* videoPath = NULL;
if (commandLine.argc < 2) {
// Check if test.mp4 exists in current directory
const char* defaultVideo = "test.mp4";
FILE* testFile = fopen(defaultVideo, "rb");
if (testFile != NULL) {
fclose(testFile);
printf("No video path provided, using default: %s\n", defaultVideo);
videoPath = defaultVideo;
} else {
fprintf(stderr, "Usage: %s <video_file_path>\n", commandLine.argv[0]);
fprintf(stderr, "Example: %s /path/to/video.mp4\n", commandLine.argv[0]);
fprintf(stderr, "\nNote: You can also place a test.mp4 file in the same directory as this executable.\n");
return -1;
}
} else {
videoPath = commandLine.argv[1];
}
// Enable verbose log to see debug information
ccap_set_log_level(CCAP_LOG_LEVEL_VERBOSE);
// Set error callback to receive error notifications
ccap_set_error_callback(error_callback, NULL);
// Get current working directory and create capture directory
char cwd[1024];
if (commandLine.argc > 0 && commandLine.argv[0][0] != '.') {
strncpy(cwd, commandLine.argv[0], sizeof(cwd) - 1);
cwd[sizeof(cwd) - 1] = '\0';
// Find last slash
char* lastSlash = strrchr(cwd, '/');
if (!lastSlash) {
lastSlash = strrchr(cwd, '\\');
}
if (lastSlash) {
*lastSlash = '\0';
}
} else {
if (getCurrentWorkingDirectory(cwd, sizeof(cwd)) != 0) {
strncpy(cwd, ".", sizeof(cwd) - 1);
cwd[sizeof(cwd) - 1] = '\0';
}
}
char captureDir[1024 + 32]; // Extra space for "/video_frames" and safety margin
snprintf(captureDir, sizeof(captureDir), "%s/video_frames", cwd);
createDirectory(captureDir);
// Create provider
CcapProvider* provider = ccap_provider_create();
if (!provider) {
printf("Failed to create provider\n");
return -1;
}
// Set output format (works for both camera and video file)
ccap_provider_set_property(provider, CCAP_PROPERTY_PIXEL_FORMAT_OUTPUT, CCAP_PIXEL_FORMAT_RGB24);
// Open the video file - the same open() method works for both camera and file
printf("Opening video file: %s\n", videoPath);
if (!ccap_provider_open(provider, videoPath, true)) {
fprintf(stderr, "Failed to open video file!\n");
ccap_provider_destroy(provider);
return -1;
}
// Check if we are in file mode
if (ccap_provider_is_file_mode(provider)) {
printf("Provider is in FILE mode\n");
// Get video properties (only available in file mode)
double duration = ccap_provider_get_property(provider, CCAP_PROPERTY_DURATION);
double frameCount = ccap_provider_get_property(provider, CCAP_PROPERTY_FRAME_COUNT);
double frameRate = ccap_provider_get_property(provider, CCAP_PROPERTY_FRAME_RATE);
int width = (int)ccap_provider_get_property(provider, CCAP_PROPERTY_WIDTH);
int height = (int)ccap_provider_get_property(provider, CCAP_PROPERTY_HEIGHT);
printf("Video properties:\n");
printf(" Duration: %.2f seconds\n", duration);
printf(" Frame count: %.0f\n", frameCount);
printf(" Frame rate: %.2f fps\n", frameRate);
printf(" Resolution: %dx%d\n", width, height);
// Set playback speed (only works in file mode)
ccap_provider_set_property(provider, CCAP_PROPERTY_PLAYBACK_SPEED, 1.0);
} else {
printf("Provider is in CAMERA mode (this shouldn't happen with a file path)\n");
}
if (!ccap_provider_is_started(provider)) {
fprintf(stderr, "Failed to start playback!\n");
ccap_provider_destroy(provider);
return -1;
}
printf("Playback started. Capturing first 30 frames...\n");
// Grab frames from the video file
int maxFrames = 30;
int frameCount = 0;
while (frameCount < maxFrames) {
CcapVideoFrame* frame = ccap_provider_grab(provider, 3000);
if (frame) {
CcapVideoFrameInfo frameInfo;
if (ccap_video_frame_get_info(frame, &frameInfo)) {
double currentTime = ccap_provider_get_property(provider, CCAP_PROPERTY_CURRENT_TIME);
printf("Frame %d: width=%d, height=%d, bytes=%d, time=%.2fs\n",
(int)frameInfo.frameIndex, frameInfo.width, frameInfo.height,
(int)frameInfo.sizeInBytes, currentTime);
// Save every 10th frame
if (frameInfo.frameIndex % 10 == 0) {
char outputPath[2048];
int result = ccap_dump_frame_to_directory(frame, captureDir, outputPath, sizeof(outputPath));
if (result >= 0) {
printf(" Frame saved to: %s\n", outputPath);
}
}
}
ccap_video_frame_release(frame);
frameCount++;
} else {
printf("No more frames or timeout\n");
break;
}
}
printf("Captured %d frames, stopping...\n", frameCount);
// Demonstrate seeking (only works in file mode)
if (ccap_provider_is_file_mode(provider)) {
printf("\nDemonstrating seek functionality...\n");
// Seek to middle of video
double duration = ccap_provider_get_property(provider, CCAP_PROPERTY_DURATION);
double seekTime = duration / 2.0;
printf("Seeking to %.2f seconds...\n", seekTime);
if (ccap_provider_set_property(provider, CCAP_PROPERTY_CURRENT_TIME, seekTime)) {
double currentTime = ccap_provider_get_property(provider, CCAP_PROPERTY_CURRENT_TIME);
printf("Seek successful. Current time: %.2f seconds\n", currentTime);
}
}
// Cleanup
ccap_provider_stop(provider);
ccap_provider_close(provider);
ccap_provider_destroy(provider);
printf("Video playback completed.\n");
return 0;
}