我们在 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)
}