我正在使用AVAssetWriterInputPixelBufferAdaptor保存从iPad Air相机捕获的视频,但是随机地,保存的视频文件有时会显示奇怪的噪音,就像您可以看到here
这是源代码:
- (NSBlockOperation *) videoWritingOperation{
NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
NSError *error = nil;
NSArray *paths = [[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
NSURL *documentsURL = [paths lastObject];
// generate UUID string for filename
NSString *filename = [NSString stringWithFormat:@"%@.mov",[[NSUUID UUID] UUIDString]];
NSURL *outputFileURL = [documentsURL URLByAppendingPathComponent:filename];
if ([[NSFileManager defaultManager] fileExistsAtPath:[outputFileURL path]])
[[NSFileManager defaultManager] removeItemAtURL:outputFileURL error:NULL];
self.tempFileName = [outputFileURL path];
self.assetWriter = [AVAssetWriter assetWriterWithURL:outputFileURL fileType:AVFileTypeQuickTimeMovie error:&error];
if (!self.assetWriter || error) {
NSLog(@"Cannot create asset writer, error: %@", error);
return;
}
NSDictionary *codecSettings = @{AVVideoAverageBitRateKey:@(1100000),AVVideoMaxKeyFrameIntervalKey:@(5)};
NSDictionary *videoCompressionSettings = @{AVVideoCodecKey:AVVideoCodecH264,AVVideoWidthKey:@(self.currentVideoDimensions.width),AVVideoHeightKey:@(self.currentVideoDimensions.height),AVVideoCompressionPropertiesKey:codecSettings};
self.assetWriterVideoInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:videoCompressionSettings];
self.assetWriterVideoInput.expectsMediaDataInRealTime = YES;
// create a pixel buffer adaptor for the asset writer; we need to obtain pixel buffers for rendering later from its pixel buffer pool
self.assetWriterInputPixelBufferAdaptor = [AVAssetWriterInputPixelBufferAdaptor assetWriterInputPixelBufferAdaptorWithAssetWriterInput:self.assetWriterVideoInput sourcePixelBufferAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInteger:kCVPixelFormatType_420YpCbCr8BiPlanarFullRange], (id)kCVPixelBufferPixelFormatTypeKey,
[NSNumber numberWithUnsignedInteger:self.currentVideoDimensions.width], (id)kCVPixelBufferWidthKey,
[NSNumber numberWithUnsignedInteger:self.currentVideoDimensions.height], (id)kCVPixelBufferHeightKey,
(id)kCFBooleanTrue, (id)kCVPixelFormatOpenGLESCompatibility,
nil]];
//UIDeviceOrientation orientation = ((FHAppDelegate *)[UIApplication sharedApplication].delegate).realDeviceOrientation;
UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
// give correct orientation information to the video
if (self.captureDevice.position == AVCaptureDevicePositionFront)
self.assetWriterVideoInput.transform = IPDGetTransformForDeviceOrientation(orientation, YES);
else
self.assetWriterVideoInput.transform = IPDGetTransformForDeviceOrientation(orientation, NO);
BOOL canAddInput = [self.assetWriter canAddInput:self.assetWriterVideoInput];
if (!canAddInput) {
NSLog(@"Cannot add asset writer video input");
self.assetWriterVideoInput = nil;
return;
}
[self.assetWriter addInput:self.assetWriterVideoInput];
}];
return operation;
}
是否有人可以帮助我了解会发生什么?