在开始时避免模糊&视频结束(即使使用了setPreferredVideoStabilizationMode:AVCaptureVideoStabilizationModeAuto)? [英] Avoiding blurriness at start & end of video (even after using setPreferredVideoStabilizationMode:AVCaptureVideoStabilizationModeAuto)?

查看:227
本文介绍了在开始时避免模糊&视频结束(即使使用了setPreferredVideoStabilizationMode:AVCaptureVideoStabilizationModeAuto)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

We capture video on iOS while using setPreferredVideoStabilizationMode:AVCaptureVideoStabilizationModeAuto, but the video still sometimes comes out blurry at the start and at the end (fine in the middle, though), which is very problematic because we grab the first frame as a still image (in order to enable video & photo capabilities without switching camera modes).

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

Placing the device flat on a desk removes all blurriness, so the whole video is sharp throughout. This suggests it has something to do with video stabilization, but is there another property to set?

锁定焦点模式是否重要?

Does locking the focus mode matter?

其他任何疑难解答提示?

Any other troubleshooting tips?

以下是来自PBJVision ,我们使用:

Here is the video capture function from PBJVision, which we use:

- (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并开始视频捕获:

This code configures PBJVision and starts video capture:

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准备好预览,我们就会将相机对焦于屏幕的中点。

Once PBJ is ready with its preview, we make the camera focus on the midpoint of the screen.

// 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)
}


推荐答案

我不知道关于PBJVision框架,但您可以查看 adjustFocus

I do not know about the PBJVision framework, but you could check the adjustingFocus of the AVCaptureDevice before extracting the picture you need.

显然,您可以使用PBJVision isAdjustingFocus 方法检查设备的聚焦状态。

Apparently, you can check the focusing status of the device by using PBJVision isAdjustingFocus method.

这篇关于在开始时避免模糊&视频结束(即使使用了setPreferredVideoStabilizationMode:AVCaptureVideoStabilizationModeAuto)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆