Skip to content

Add object detection, tracking, mobile notifications, and search to any security camera.

use hardcoded segments for now

roryclearee24b27af2fe
diff --git a/clearcam/FileServer.m b/clearcam/FileServer.m
index 79f6478..149b391 100644
--- a/clearcam/FileServer.m
+++ b/clearcam/FileServer.m
@@ -97,107 +97,27 @@
if ([filePath isEqualToString:@"/"]) filePath = @"";
// Handle /get-segments API endpoint
- if ([filePath isEqualToString:@"get-segments"]) { //TODO, this gets very slow I think, just use a txt file
+ if ([filePath isEqualToString:@"get-segments"]) {
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
- NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsPath error:nil];
- NSPredicate *mp4Filter = [NSPredicate predicateWithFormat:@"SELF ENDSWITH '.mp4'"];
- NSArray *mp4Files = [files filteredArrayUsingPredicate:mp4Filter];
-
- // Sort .mp4 files by numeric values
- NSArray *sortedFiles = [mp4Files sortedArrayUsingComparator:^NSComparisonResult(NSString *file1, NSString *file2) {
- NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"_(\\d+)_"
- options:0
- error:nil];
- NSTextCheckingResult *match1 = [regex firstMatchInString:file1 options:0 range:NSMakeRange(0, file1.length)];
- NSTextCheckingResult *match2 = [regex firstMatchInString:file2 options:0 range:NSMakeRange(0, file2.length)];
-
- NSInteger num1 = match1 ? [[file1 substringWithRange:[match1 rangeAtIndex:1]] integerValue] : 0;
- NSInteger num2 = match2 ? [[file2 substringWithRange:[match2 rangeAtIndex:1]] integerValue] : 0;
-
- return [@(num1) compare:@(num2)];
- }];
-
- if (sortedFiles.count > 1) { // Exclude last 2 files
- sortedFiles = [sortedFiles subarrayWithRange:NSMakeRange(0, sortedFiles.count - 2)];
- }
-
- // Prepare JSON response
- NSMutableArray *responseArray = [NSMutableArray array];
- NSString *lastFile = nil;
- for (NSString *file in sortedFiles) {
- NSString *filePath = [documentsPath stringByAppendingPathComponent:file];
-
- // Retrieve cached duration or calculate it
- NSNumber *cachedDuration = self.durationCache[file];
- Float64 duration;
- if (cachedDuration) {
- duration = [cachedDuration doubleValue];
- } else {
- // Get duration using AVAsset
- AVAsset *asset = [AVAsset assetWithURL:[NSURL fileURLWithPath:filePath]];
- CMTime time = asset.duration;
- duration = CMTimeGetSeconds(time);
-
- if (isfinite(duration)) {
- duration = round(duration * 1000) / 1000.0; // Round to 3 decimal places
- self.durationCache[file] = @(duration);
- }
- }
-
- // Retrieve cached timeDifference or calculate it
- Float64 timeDifference = 0;
- if (lastFile) {
- NSNumber *cachedTimeDifference = self.timeDifferenceCache[lastFile];
- if (cachedTimeDifference) {
- timeDifference = [cachedTimeDifference doubleValue];
- } else {
- // Extract timestamps
- NSRegularExpression *timestampRegex = [NSRegularExpression regularExpressionWithPattern:@"_(\\d{2}:\\d{2}:\\d{2}:\\d{3})\\.mp4$"
- options:0
- error:nil];
- NSTextCheckingResult *lastMatch = [timestampRegex firstMatchInString:lastFile options:0 range:NSMakeRange(0, lastFile.length)];
- NSTextCheckingResult *currentMatch = [timestampRegex firstMatchInString:file options:0 range:NSMakeRange(0, file.length)];
-
- NSString *lastTimestamp = lastMatch ? [lastFile substringWithRange:[lastMatch rangeAtIndex:1]] : nil;
- NSString *currentTimestamp = currentMatch ? [file substringWithRange:[currentMatch rangeAtIndex:1]] : nil;
-
- if (lastTimestamp && currentTimestamp) {
- NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
- [formatter setDateFormat:@"HH:mm:ss:SSS"];
- NSDate *lastDate = [formatter dateFromString:lastTimestamp];
- NSDate *currentDate = [formatter dateFromString:currentTimestamp];
-
- if (lastDate && currentDate) {
- timeDifference = [currentDate timeIntervalSinceDate:lastDate];
- self.timeDifferenceCache[lastFile] = @(timeDifference);
- }
- }
- }
- }
-
- // Prepare segment data
- NSMutableDictionary *segmentData = [@{
- @"url": file,
- @"duration": @(duration)
- } mutableCopy];
- if (timeDifference > 0) {
- segmentData[@"timeDifference"] = @(timeDifference);
- }
-
- [responseArray addObject:segmentData];
- lastFile = file; // Update last file
+ NSString *segmentsFilePath = [documentsPath stringByAppendingPathComponent:@"segments.txt"];
+
+ if ([[NSFileManager defaultManager] fileExistsAtPath:segmentsFilePath]) {
+ // Read contents of segments.txt
+ NSData *jsonData = [NSData dataWithContentsOfFile:segmentsFilePath];
+ NSString *httpHeader = @"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n";
+ send(clientSocket, [httpHeader UTF8String], httpHeader.length, 0);
+ send(clientSocket, jsonData.bytes, jsonData.length, 0);
+ return;
+ } else {
+ // If file does not exist, send an empty response
+ NSString *httpHeader = @"HTTP/1.1 404 Not Found\r\nContent-Type: application/json\r\n\r\n";
+ NSString *errorMessage = @"{\"error\": \"segments.txt not found\"}";
+ send(clientSocket, [httpHeader UTF8String], httpHeader.length, 0);
+ send(clientSocket, [errorMessage UTF8String], errorMessage.length, 0);
+ return;
}
-
- // Send JSON response
- NSData *jsonData = [NSJSONSerialization dataWithJSONObject:responseArray options:0 error:nil];
- NSString *httpHeader = @"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n";
- send(clientSocket, [httpHeader UTF8String], httpHeader.length, 0);
- send(clientSocket, jsonData.bytes, jsonData.length, 0);
- return;
}
-
-
NSString *fullPath = [basePath stringByAppendingPathComponent:filePath];
BOOL isDirectory = NO;
if (![[NSFileManager defaultManager] fileExistsAtPath:fullPath isDirectory:&isDirectory]) {
diff --git a/segments.txt b/segments.txt
new file mode 100644
index 0000000..0b8e785
--- /dev/null
+++ b/segments.txt
@@ -0,0 +1,296 @@
+[
+ {
+ "url": "output_2025-01-16_00000_11:04:43:061.mp4",
+ "duration": 0
+ },
+ {
+ "url": "output_2025-01-16_00001_11:04:53:338.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.277000069618225
+ },
+ {
+ "url": "output_2025-01-16_00002_11:05:03:433.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.09499990940094
+ },
+ {
+ "url": "output_2025-01-16_00003_11:05:13:526.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.093000054359436
+ },
+ {
+ "url": "output_2025-01-16_00004_11:05:23:622.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.095999956130981
+ },
+ {
+ "url": "output_2025-01-16_00005_11:05:33:727.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.105000019073486
+ },
+ {
+ "url": "output_2025-01-16_00006_11:05:43:800.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.072999954223633
+ },
+ {
+ "url": "output_2025-01-16_00007_11:05:53:894.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.094000101089478
+ },
+ {
+ "url": "output_2025-01-16_00008_11:06:03:979.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.084999918937683
+ },
+ {
+ "url": "output_2025-01-16_00009_11:06:14:074.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.095000028610229
+ },
+ {
+ "url": "output_2025-01-16_00010_11:06:24:169.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.095000028610229
+ },
+ {
+ "url": "output_2025-01-16_00011_11:06:34:270.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.10099995136261
+ },
+ {
+ "url": "output_2025-01-16_00012_11:06:44:347.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.077000021934509
+ },
+ {
+ "url": "output_2025-01-16_00013_11:06:54:454.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.10699999332428
+ },
+ {
+ "url": "output_2025-01-16_00014_11:07:04:562.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.108000040054321
+ },
+ {
+ "url": "output_2025-01-16_00015_11:07:14:667.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.105000019073486
+ },
+ {
+ "url": "output_2025-01-16_00016_11:07:24:780.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.11299991607666
+ },
+ {
+ "url": "output_2025-01-16_00017_11:07:34:891.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.111000061035156
+ },
+ {
+ "url": "output_2025-01-16_00018_11:07:45:013.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.121999979019165
+ },
+ {
+ "url": "output_2025-01-16_00019_11:07:55:121.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.108000040054321
+ },
+ {
+ "url": "output_2025-01-16_00020_11:08:05:210.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.08899998664856
+ },
+ {
+ "url": "output_2025-01-16_00021_11:08:15:330.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.120000004768372
+ },
+ {
+ "url": "output_2025-01-16_00022_11:08:25:419.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.08899998664856
+ },
+ {
+ "url": "output_2025-01-16_00023_11:08:35:501.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.082000017166138
+ },
+ {
+ "url": "output_2025-01-16_00024_11:08:45:603.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.101999998092651
+ },
+ {
+ "url": "output_2025-01-16_00025_11:08:55:728.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.125
+ },
+ {
+ "url": "output_2025-01-16_00026_11:09:05:813.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.084999918937683
+ },
+ {
+ "url": "output_2025-01-16_00027_11:09:15:891.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.078000068664551
+ },
+ {
+ "url": "output_2025-01-16_00028_11:09:25:966.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.074999928474426
+ },
+ {
+ "url": "output_2025-01-16_00029_11:09:36:039.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.073000073432922
+ },
+ {
+ "url": "output_2025-01-16_00030_11:09:46:153.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.113999962806702
+ },
+ {
+ "url": "output_2025-01-16_00031_11:09:56:270.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.116999983787537
+ },
+ {
+ "url": "output_2025-01-16_00032_11:10:06:362.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.092000007629395
+ },
+ {
+ "url": "output_2025-01-16_00033_11:10:16:464.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.101999998092651
+ },
+ {
+ "url": "output_2025-01-16_00034_11:10:26:546.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.082000017166138
+ },
+ {
+ "url": "output_2025-01-16_00035_11:10:36:654.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.108000040054321
+ },
+ {
+ "url": "output_2025-01-16_00036_11:10:46:765.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.110999941825867
+ },
+ {
+ "url": "output_2025-01-16_00037_11:10:56:884.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.11899995803833
+ },
+ {
+ "url": "output_2025-01-16_00038_11:11:06:972.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.088000059127808
+ },
+ {
+ "url": "output_2025-01-16_00039_11:11:17:046.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.074000000953674
+ },
+ {
+ "url": "output_2025-01-16_00040_11:11:27:121.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.075000047683716
+ },
+ {
+ "url": "output_2025-01-16_00041_11:11:37:206.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.084999918937683
+ },
+ {
+ "url": "output_2025-01-16_00042_11:11:47:319.mp4",
+ "duration": 10.053000000000001,
+ "timeDifference": 10.11300003528595
+ },
+ {
+ "url": "output_2025-01-16_00043_11:11:57:429.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.110000014305115
+ },
+ {
+ "url": "output_2025-01-16_00044_11:12:07:530.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.10099995136261
+ },
+ {
+ "url": "output_2025-01-16_00045_11:12:17:607.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.077000021934509
+ },
+ {
+ "url": "output_2025-01-16_00046_11:12:27:727.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.120000004768372
+ },
+ {
+ "url": "output_2025-01-16_00047_11:12:37:828.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.10099995136261
+ },
+ {
+ "url": "output_2025-01-16_00048_11:12:47:931.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.103000044822693
+ },
+ {
+ "url": "output_2025-01-16_00049_11:12:58:059.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.128000020980835
+ },
+ {
+ "url": "output_2025-01-16_00050_11:13:08:194.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.134999990463257
+ },
+ {
+ "url": "output_2025-01-16_00051_11:13:18:291.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.097000002861023
+ },
+ {
+ "url": "output_2025-01-16_00052_11:13:28:401.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.110000014305115
+ },
+ {
+ "url": "output_2025-01-16_00053_11:13:38:501.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.100000023841858
+ },
+ {
+ "url": "output_2025-01-16_00054_11:13:48:724.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.222999930381775
+ },
+ {
+ "url": "output_2025-01-16_00055_11:13:58:798.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.074000000953674
+ },
+ {
+ "url": "output_2025-01-16_00056_11:14:08:912.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.113999962806702
+ },
+ {
+ "url": "output_2025-01-16_00057_11:14:19:051.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.139000058174133
+ },
+ {
+ "url": "output_2025-01-16_00058_11:14:29:122.mp4",
+ "duration": 10.039999999999999,
+ "timeDifference": 10.070999979972839
+ }
+]
\ No newline at end of file