Post

Replies

Boosts

Views

Activity

Reply to finishWritingWithCompletionHandler can't callback
just one,but i make the process in another thread my code is just this: (BOOL)saveVideo { 		if (!self.videoPath self.videoPath.length == 0 !self.buffer) { 				return NO; 		} 		NSURL *url = [NSURL fileURLWithPath:self.videoPath]; 		NSError *error = nil; 		AVAssetWriter *videoWriter = [[AVAssetWriter alloc] initWithURL:url fileType:AVFileTypeQuickTimeMovie error:&error]; 		if (error) { 				Log(@"%s error = %@",PRETTY_FUNCTION,error); 				return NO; 		} 		NSDictionary *videoSetting = @{ 				AVVideoCodecKey:AVVideoCodecTypeH264, 				AVVideoWidthKey:@(CVPixelBufferGetWidth(self.buffer)), 				AVVideoHeightKey:@(CVPixelBufferGetHeight(self.buffer)) 		}; 		AVAssetWriterInput *writerInput = [[AVAssetWriterInput alloc] initWithMediaType:AVMediaTypeVideo outputSettings:videoSetting]; 		AVAssetWriterInputPixelBufferAdaptor *adaptor = [[AVAssetWriterInputPixelBufferAdaptor alloc] initWithAssetWriterInput:writerInput sourcePixelBufferAttributes:nil]; 		if (![videoWriter canAddInput:writerInput]) { 				return NO; 		} 		[videoWriter addInput:writerInput]; 		[videoWriter startWriting]; 		[videoWriter startSessionAtSourceTime:kCMTimeZero]; 		CMTime presentTime = CMTimeMake(0, 600); 		while (1) { 				if ([writerInput isReadyForMoreMediaData]) { 						[adaptor appendPixelBuffer:self.buffer withPresentationTime:presentTime]; 						[writerInput markAsFinished]; 						__block BOOL isFinish = NO; 						dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); 						[videoWriter finishWritingWithCompletionHandler:^{ 								isFinish = YES; 								dispatch_semaphore_signal(semaphore); 						}]; 						dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, 2.0f * NSEC_PER_SEC); 						dispatch_semaphore_wait(semaphore, time); 						if (!isFinish) { 								Log(@"%s create video error",PRETTY_FUNCTION); 						} 						break; 				} 		} 		return YES; }
Dec ’20