Video 使用AvassetWriterInputPixelBufferAdapter保存的视频文件中存在噪波

Video 使用AvassetWriterInputPixelBufferAdapter保存的视频文件中存在噪波,video,avfoundation,noise,Video,Avfoundation,Noise,我正在用AvassetWriterInputPixelBufferAdapter保存一段从iPad Air camera捕获的视频,但有时保存的视频文件会随机显示奇怪的噪音,如您所见 这是源代码: - (NSBlockOperation *) videoWritingOperation{ NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{ NSError *error = nil;

我正在用AvassetWriterInputPixelBufferAdapter保存一段从iPad Air camera捕获的视频,但有时保存的视频文件会随机显示奇怪的噪音,如您所见

这是源代码:

- (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;
}


是否有人可以帮助我了解发生的情况?

您的适配器需要kCVPixelFormatTypeƤYPCBCCR8BIPLANARFULLRANGE YUV图像。这就是您提供的吗?是的,这是传递给AVCaptureVideoDataOutput实例的outputSettings字典:NSDictionary*outputSettings=@{idkCVPixelBufferPixelFormatTypeKey:[NSNumber numberWithInteger:kCVPixelFormatType\U 420YpCbCr8BiPlanarFullRange];你能提供一些可运行的代码吗?问题在iPad Air手机上出现的频率更高,而在iPad Air手机上出现的频率更低,而且是随机的……这是一个可以找到源代码的链接: