13

我们在 iOS 上使用 捕获视频setPreferredVideoStabilizationMode:AVCaptureVideoStabilizationModeAuto,但视频有时仍然在开始和结束时出现模糊(不过中间很好),这是非常有问题的,因为我们将第一帧作为静止图像(为了无需切换相机模式即可启用视频和照片功能)。

将设备平放在桌子上可以消除所有模糊,因此整个视频始终清晰。这表明它与视频稳定有关,但还有其他属性需要设置吗?

锁定对焦模式重要吗?

还有其他故障排除技巧吗?

这是我们使用的PBJVision的视频捕获功能:

- (void)startVideoCapture
{
    if (![self _canSessionCaptureWithOutput:_currentOutput] || _cameraMode != PBJCameraModeVideo) {
        [self _failVideoCaptureWithErrorCode:PBJVisionErrorSessionFailed];
        DLog(@"session is not setup properly for capture");
        return;
    }

    DLog(@"starting video capture");

    [self _enqueueBlockOnCaptureVideoQueue:^{

        if (_flags.recording || _flags.paused)
            return;

        NSString *guid = [[NSUUID new] UUIDString];
        NSString *outputFile = [NSString stringWithFormat:@"video_%@.mp4", guid];

        if ([_delegate respondsToSelector:@selector(vision:willStartVideoCaptureToFile:)]) {
            outputFile = [_delegate vision:self willStartVideoCaptureToFile:outputFile];

            if (!outputFile) {
                [self _failVideoCaptureWithErrorCode:PBJVisionErrorBadOutputFile];
                return;
            }
        }

        NSString *outputDirectory = (_captureDirectory == nil ? NSTemporaryDirectory() : _captureDirectory);
        NSString *outputPath = [outputDirectory stringByAppendingPathComponent:outputFile];
        NSURL *outputURL = [NSURL fileURLWithPath:outputPath];
        if ([[NSFileManager defaultManager] fileExistsAtPath:outputPath]) {
            NSError *error = nil;
            if (![[NSFileManager defaultManager] removeItemAtPath:outputPath error:&error]) {
                [self _failVideoCaptureWithErrorCode:PBJVisionErrorOutputFileExists];

                DLog(@"could not setup an output file (file exists)");
                return;
            }
        }

        if (!outputPath || [outputPath length] == 0) {
            [self _failVideoCaptureWithErrorCode:PBJVisionErrorBadOutputFile];

            DLog(@"could not setup an output file");
            return;
        }

        if (_mediaWriter) {
            _mediaWriter.delegate = nil;
            _mediaWriter = nil;
        }
        _mediaWriter = [[PBJMediaWriter alloc] initWithOutputURL:outputURL];
        _mediaWriter.delegate = self;

        AVCaptureConnection *videoConnection = [_captureOutputVideo connectionWithMediaType:AVMediaTypeVideo];
        [self _setOrientationForConnection:videoConnection];

        _startTimestamp = CMClockGetTime(CMClockGetHostTimeClock());
        _timeOffset = kCMTimeInvalid;

        _flags.recording = YES;
        _flags.paused = NO;
        _flags.interrupted = NO;
        _flags.videoWritten = NO;

        _captureThumbnailTimes = [NSMutableSet set];
        _captureThumbnailFrames = [NSMutableSet set];

        if (_flags.thumbnailEnabled && _flags.defaultVideoThumbnails) {
            [self captureVideoThumbnailAtFrame:0];
        }

        [self _enqueueBlockOnMainQueue:^{                
            if ([_delegate respondsToSelector:@selector(visionDidStartVideoCapture:)])
                [_delegate visionDidStartVideoCapture:self];
        }];
    }];
}

此代码配置 PBJVision 并开始视频捕获:

private func initPBJVision() {
    // Configure PBJVision
    pbj.delegate = self
    pbj.cameraMode = .Video
    pbj.cameraOrientation = .Portrait
    pbj.focusMode = .AutoFocus
    pbj.outputFormat = .Preset
    pbj.cameraDevice = .Back
    pbj.thumbnailEnabled = false

    // Log status
    print("Configured PBJVision")

    pbj.startVideoCapture()
}

PBJ 准备好预览后,我们将相机聚焦在屏幕的中点。

// Called when PBJVision preview begins
func visionSessionDidStartPreview(vision: PBJVision) {
    // Focus screen at midpoint
    let focus_x = CGFloat(0.5)
    let focus_y = CGFloat(0.5)
}
4

2 回答 2

2

我不了解 PBJVision 框架,但是您可以在提取所需图片之前检查 AVCaptureDevice的adjustingFocus 。


显然,您可以使用 PBJVisionisAdjustingFocus方法检查设备的对焦状态。

于 2016-01-28T14:33:56.457 回答
2

视频本质上是模糊的。每秒 24 或 30 帧的视频在镜头中总是会有一些模糊,因为这是我们的眼睛被欺骗相信图片实际上在移动的方式。较长的快门速度可以让相机给人以运动的印象。

照片使用更短的快门速度(1/60 秒或更短)来提供清晰、静止的照片。

这里有一些关于如何在录制视频时拍照的信息:

如何在录制视频时以编程方式拍照

于 2016-01-26T19:48:31.867 回答