it's my first question there, so sorry for mistakes and not clear description. I'm developing app that capture videos in a loop and send them to server in background. I want to sent it like separate files(post-request) and then connect them into 1 on sever side.
I'm not very experienced in using AVframework. So as a base - used AVCam project, and modify it for my features. I need to solve such kind of problem right now. I'm looking for a way how to record short video files near 2mb each, to upload them into server next. Main issue that I've got - delay between video parts after their concatenation. And how to get this videoparts without stopping recorder.
Trying few thing. First - using timer record 20sec fragments, stop by timer, and beging new record cycle. Second. My idea was to use AVURLAsset(for getting current recording video file in temp dir) and by AVAssetExportSession get last not saved video data by it's duration. It seems working, I can record few videos, but again there are freezes between them, last 1-2 sec of each video - just 1 picture. So after concatenating it's not look like 1 movie.
-(void) startRecording
{
self.videoCounter=0;
self.videoTimer=[NSTimer scheduledTimerWithTimeInterval:lenghtTimer target:self selector:@selector(endRecoringVideo) userInfo:nil repeats:YES] ;
NSLog(@"timer started");
}
if ([[UIDevice currentDevice] isMultitaskingSupported]) {
[self setBackgroundRecordingID:[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{}]];
}
[self removeFile:[[self recorder] outputFileURL]];
[[self recorder] startRecordingWithOrientation:orientation];
}
-(void)saveVideoPart
{
NSUInteger count = 0;
NSString *filePath = nil;
do {
NSString *fileName = [NSString stringWithFormat:@"buf-%@-%u", AVAssetExportPresetLowQuality, count];
filePath = NSTemporaryDirectory();
filePath = [filePath stringByAppendingPathComponent:fileName];
filePath = [filePath stringByAppendingPathExtension:@"mov"];
count++;
} while ([[NSFileManager defaultManager] fileExistsAtPath:filePath]);
NSURL *outputURL = [NSURL fileURLWithPath:filePath];
AVURLAsset *videoAsset = [AVURLAsset URLAssetWithURL:[self tempFileURL] options:nil];
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:videoAsset presetName:AVAssetExportPresetHighestQuality];
exportSession.outputURL = outputURL;
exportSession.outputFileType = AVFileTypeQuickTimeMovie;
Float64 durationSeconds = CMTimeGetSeconds([videoAsset duration]);
CMTime start;
CMTime duration;
start = CMTimeMakeWithSeconds(self.videoCounter, 1);
duration = CMTimeMakeWithSeconds(durationSeconds-self.videoCounter, 1);
self.videoCounter+=durationSeconds-self.videoCounter;
NSLog(@"duration video %f,recorded %f",durationSeconds,self.videoCounter);
CMTimeRange range = CMTimeRangeMake(start, duration);
exportSession.timeRange = range;
[exportSession exportAsynchronouslyWithCompletionHandler:^{
switch (exportSession.status) {
case AVAssetExportSessionStatusCompleted:
NSLog(@"Exported");
break;
case AVAssetExportSessionStatusFailed:
//
NSLog(@"Failed:%@",exportSession.error);
break;
case AVAssetExportSessionStatusCancelled:
//
NSLog(@"Canceled:%@",exportSession.error);
break;
default:
break;
}
}];
}
So I'll be so glad if you make proposes of algorithm or show me right direction where to move further. Main for me - divide vide to parts while recording. As an example video may be near 10 min long without pauses, I need to make few parts of it in background with 2mb long, and then upload them.