Skip to content

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

do stuff after start recoding

roryclear3b5ddfd74e07
diff --git a/clearcam/ViewController.m b/clearcam/ViewController.m
index a9f4e8b..76d359a 100644
--- a/clearcam/ViewController.m
+++ b/clearcam/ViewController.m
@@ -342,70 +342,67 @@ NSMutableDictionary *classColorMap;
[self.videoWriterInput markAsFinished];
[self.assetWriter finishWritingWithCompletionHandler:^{
- NSString *fileName = [self.assetWriter.outputURL lastPathComponent];
-
- AVAsset *asset = [AVAsset assetWithURL:self.assetWriter.outputURL];
- CMTime time = asset.duration;
+ AVAsset *asset = [AVAsset assetWithURL:self.assetWriter.outputURL];
+ CMTime time = asset.duration;
+
+ NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
+ [dateFormatter setDateFormat:@"yyyy-MM-dd"];
+ NSString *dateFolderName = [dateFormatter stringFromDate:self.current_file_timestamp];
+
+ // Get the documents directory
+ NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
+ NSString *segmentsDirectory = [documentsDirectory stringByAppendingPathComponent:dateFolderName];
+
+ // Create the folder if it doesn't exist
+ NSError *error = nil;
+ if (![[NSFileManager defaultManager] fileExistsAtPath:segmentsDirectory]) {
+ [[NSFileManager defaultManager] createDirectoryAtPath:segmentsDirectory withIntermediateDirectories:YES attributes:nil error:&error];
+ if (error) {
+ NSLog(@"Error creating directory: %@", error);
+ }
+ }
- NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
- [dateFormatter setDateFormat:@"yyyy-MM-dd"];
- NSString *dateFolderName = [dateFormatter stringFromDate:self.current_file_timestamp];
+ NSCalendar *calendar = [NSCalendar currentCalendar];
+ NSDateComponents *components = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour fromDate:self.current_file_timestamp];
+ NSString *dateKey = [NSString stringWithFormat:@"%04ld-%02ld-%02ld", (long)components.year, (long)components.month, (long)components.day];
- // Get the documents directory
+ if (!self.fileServer.segmentsDict[dateKey]) {
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
- NSString *segmentsDirectory = [documentsDirectory stringByAppendingPathComponent:dateFolderName];
-
- // Create the folder if it doesn't exist
- NSError *error = nil;
- if (![[NSFileManager defaultManager] fileExistsAtPath:segmentsDirectory]) {
- [[NSFileManager defaultManager] createDirectoryAtPath:segmentsDirectory withIntermediateDirectories:YES attributes:nil error:&error];
- if (error) {
- NSLog(@"Error creating directory: %@", error);
- }
- }
-
- // Path to segments.txt in the date folder
+ NSString *segmentsDirectory = [documentsDirectory stringByAppendingPathComponent:dateKey];
NSString *segmentsFilePath = [segmentsDirectory stringByAppendingPathComponent:@"segments.txt"];
-
- NSCalendar *calendar = [NSCalendar currentCalendar];
- NSDateComponents *components = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour fromDate:self.current_file_timestamp];
- NSString *dateKey = [NSString stringWithFormat:@"%04ld-%02ld-%02ld", (long)components.year, (long)components.month, (long)components.day];
-
- if (!self.fileServer.segmentsDict[dateKey]) {
- NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
- NSString *segmentsDirectory = [documentsDirectory stringByAppendingPathComponent:dateKey];
- NSString *segmentsFilePath = [segmentsDirectory stringByAppendingPathComponent:@"segments.txt"];
- if ([[NSFileManager defaultManager] fileExistsAtPath:segmentsFilePath]) {
- NSData *data = [NSData dataWithContentsOfFile:segmentsFilePath];
- NSArray *fileSegments = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
- if (fileSegments) {
- self.fileServer.segmentsDict[dateKey] = [fileSegments mutableCopy];
- }
+ if ([[NSFileManager defaultManager] fileExistsAtPath:segmentsFilePath]) {
+ NSData *data = [NSData dataWithContentsOfFile:segmentsFilePath];
+ NSArray *fileSegments = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
+ if (fileSegments) {
+ self.fileServer.segmentsDict[dateKey] = [fileSegments mutableCopy];
}
- if(!self.fileServer.segmentsDict[dateKey]) self.fileServer.segmentsDict[dateKey] = [[NSMutableArray alloc] init];
}
+ if(!self.fileServer.segmentsDict[dateKey]) self.fileServer.segmentsDict[dateKey] = [[NSMutableArray alloc] init];
+ }
- NSMutableDictionary *segmentEntry = [NSMutableDictionary dictionaryWithDictionary:@{ //does this fix last segment in hour/day?
- @"url": [NSString stringWithFormat:@"%@/%@", [[self.assetWriter.outputURL URLByDeletingLastPathComponent] lastPathComponent], self.assetWriter.outputURL.lastPathComponent],
- @"duration": @(CMTimeGetSeconds(time)),
- @"frames":[self.current_segment_squares copy]
- }];
-
- //timestamp of every segment now
- calendar = [NSCalendar currentCalendar];
- components = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:self.current_file_timestamp];
- NSDate *midnight = [calendar dateFromComponents:components];
- NSTimeInterval timeStamp = [self.current_file_timestamp timeIntervalSinceDate:midnight];
- segmentEntry[@"timeStamp"] = @(timeStamp);
-
- [self saveSegmentEntry:segmentEntry toFile:segmentsFilePath]; // Not used yet
- [self.fileServer.segmentsDict[dateKey] addObject:segmentEntry];
+ NSMutableDictionary *segmentEntry = [NSMutableDictionary dictionaryWithDictionary:@{ //does this fix last segment in hour/day?
+ @"url": [NSString stringWithFormat:@"%@/%@", [[self.assetWriter.outputURL URLByDeletingLastPathComponent] lastPathComponent], self.assetWriter.outputURL.lastPathComponent],
+ @"duration": @(CMTimeGetSeconds(time)),
+ @"frames":[self.current_segment_squares copy]
+ }];
+
+ //timestamp of every segment now
+ calendar = [NSCalendar currentCalendar];
+ components = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:self.current_file_timestamp];
+ NSDate *midnight = [calendar dateFromComponents:components];
+ NSTimeInterval timeStamp = [self.current_file_timestamp timeIntervalSinceDate:midnight];
+ segmentEntry[@"timeStamp"] = @(timeStamp);
- [self.segmentLock lock];
- [self.current_segment_squares removeAllObjects];
- [self.segmentLock unlock];
- [self startNewRecording];
+
+ [self.fileServer.segmentsDict[dateKey] addObject:segmentEntry];
+
+ [self startNewRecording];
+ NSString *segmentsFilePath = [segmentsDirectory stringByAppendingPathComponent:@"segments.txt"];
+ [self saveSegmentEntry:segmentEntry toFile:segmentsFilePath];
+ [self.segmentLock lock];
+ [self.current_segment_squares removeAllObjects];
+ [self.segmentLock unlock];
}];
} else {
NSLog(@"Cannot finish writing. Asset writer status: %ld", (long)self.assetWriter.status);
diff --git a/clearcam/player.html b/clearcam/player.html
index 002ebf0..bd05014 100644
--- a/clearcam/player.html
+++ b/clearcam/player.html
@@ -897,7 +897,8 @@
currentVideo.style.display = "block";
// Play the video
- if(lostFrames < 1){
+ if(lostFrames < 60){
+ console.log(`${lostFrames} - ${segments.length}`);
setTimeout(() => {
currentVideo.play().catch(() => {
retrySegment(currentVideo);