ios - AVCaptureSession的设置与iPhone相机内置的设置相同

标签 ios objective-c avcapturesession avcapturedevice ios-camera

我已经坚持了很长时间。当我在iPhone上使用内置的相机应用程序进行录制时,似乎有一个变焦系数。但是,当我在应用程序内部使用AVCaptureSession时,似乎无法获得相同的结果。这是我在说的一个例子。顶部是通过iPhone相机应用程序记录的,底部是使用AVCaptureSession记录在我的应用程序内部的。就像发生了鱼眼效应。我的设置相机代码如下。

Default iPhone camera recording

AVCaptureSession recording

- (void)configureCameraSession{
_captureSession = [AVCaptureSession new];

[_captureSession beginConfiguration];

_device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

if(!_device){
    [self noValidCamera];
    return;
}

AVCaptureDeviceFormat *currentFormat;
int frameRate = 60;
for (AVCaptureDeviceFormat *format in _device.formats)
{
    NSArray *ranges = format.videoSupportedFrameRateRanges;
    AVFrameRateRange *frameRates = ranges[0];

    //CMVideoFormatDescriptionGet
    int resolutionWidth = CMVideoFormatDescriptionGetDimensions(format.formatDescription).width;
    if(frameRates.maxFrameRate > 59 && resolutionWidth >= 1920){
        CameraFormat *cformat = [[CameraFormat alloc] init];
        cformat.format = format;
        cformat.fps = 60;
        if(frameRates.maxFrameRate > 119){
            cformat.fps = 120;
        }
        if(frameRates.maxFrameRate > 239){
            cformat.fps = 240;
        }

        NSString *resolution;

        if(resolutionWidth > 1920){
            resolution = @"4K ";
        }
        else{
            resolution = [[NSNumber numberWithInt:CMVideoFormatDescriptionGetDimensions(format.formatDescription).height] stringValue];
            resolution = [resolution stringByAppendingString:@"p "];
        }

        //stringValue];
        NSString *fps = [[NSNumber numberWithInt:cformat.fps] stringValue];
        fps = [fps stringByAppendingString:@" FPS"];
        cformat.label = [resolution stringByAppendingString:fps];

        BOOL isUniqueFormat = YES;
        for(int i = 0; i < [_formatList count]; i++){
            if([_formatList[i].label isEqualToString:cformat.label]){
                isUniqueFormat = NO;
                break;
            }
        }
        if(isUniqueFormat){
            [_formatList addObject:cformat];
            frameRate = cformat.fps;
            currentFormat = cformat.format;
        }
    }
}

if(!currentFormat){
    [self noValidCamera];
    return;
}

_currentCameraFormat = _analysisViewController.currentCameraFormat;
if(_currentCameraFormat.fps == 0){
    _currentCameraFormatIndex = 0;
    _currentCameraFormat = _formatList[_currentCameraFormatIndex];
    _analysisViewController.currentCameraFormat = _currentCameraFormat;
    currentFormat = _currentCameraFormat.format;
    frameRate = _currentCameraFormat.fps;
}
else{
    currentFormat = _currentCameraFormat.format;
    frameRate = _currentCameraFormat.fps;
}

NSString *resolution;
if(CMVideoFormatDescriptionGetDimensions(currentFormat.formatDescription).width > 1920){
    resolution = @"4K ";
}
else{
    resolution = [[NSNumber numberWithInt:CMVideoFormatDescriptionGetDimensions(currentFormat.formatDescription).height] stringValue];
    resolution = [resolution stringByAppendingString:@"p "];
}
NSString *fps = [[NSNumber numberWithInt:frameRate] stringValue];
fps = [fps stringByAppendingString:@" FPS  ▼"];

[self.videoLabelButton setTitle:[resolution stringByAppendingString:fps] forState:UIControlStateNormal];

[_device lockForConfiguration:nil];
_device.activeFormat = currentFormat;
_device.activeVideoMinFrameDuration = CMTimeMake(1, frameRate);
_device.activeVideoMaxFrameDuration = CMTimeMake(1, frameRate);
[_device unlockForConfiguration];

//Input
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:_device error:nil];
[_captureSession addInput:input];

//Output
_movieFileOutput = [[AVCaptureMovieFileOutput alloc] init];

if([_captureSession canAddOutput:_movieFileOutput]){
    [_captureSession addOutput:_movieFileOutput];
}

[self setMovieOutputOrientation];

//Preview Layer
_previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_captureSession];
_previewView = [[UIView alloc] initWithFrame:self.imageView.bounds];
[self addFocusToView:_previewView];
[_previewView addSubview:self.imageView];

_previewLayer.frame = _previewView.bounds;
_previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[_previewView.layer addSublayer:_previewLayer];

_previewLayer.connection.videoOrientation = [self videoOrientationFromCurrentDeviceOrientation];

AVCaptureVideoStabilizationMode stabilizationMode = AVCaptureVideoStabilizationModeCinematic;
if ([_device.activeFormat isVideoStabilizationModeSupported:stabilizationMode]) {
    [_previewLayer.connection setPreferredVideoStabilizationMode:stabilizationMode];
}

[self.view addSubview:_previewView];
[self.view addSubview:self.topInfoBar];
[self.view addSubview:self.recordButton];
[self.view addSubview:self.doneButton];

[_captureSession commitConfiguration];
[_captureSession startRunning];

}

最佳答案

好吧,我终于明白了。问题是,在某些格式上支持视频稳定,因此Apple会尽可能使用它。问题是我没有在可能的时候打开它。基本上检查该选项是否适用于设备的 Activity 格式,然后将其打开。

 AVCaptureConnection *videoConnection = nil;
for (AVCaptureConnection *connection in [_movieFileOutput connections]){
    for ( AVCaptureInputPort *port in [connection inputPorts]){
        if ([[port mediaType] isEqual:AVMediaTypeVideo]){
            videoConnection = connection;
        }
    }
}

if([videoConnection isVideoOrientationSupported]){
    [videoConnection setVideoOrientation:[self videoOrientationFromCurrentDeviceOrientation]];
}


//Check if we can use video stabilization and if so then set it to auto
if (videoConnection.supportsVideoStabilization) {
    videoConnection.preferredVideoStabilizationMode = AVCaptureVideoStabilizationModeAuto;
}

关于ios - AVCaptureSession的设置与iPhone相机内置的设置相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51509252/

相关文章:

iphone - 用于音频和视频组合的 AVCaptureSession - 音频部分提供 EXC_BAD_ACCESS

objective-c - 如何在可见 map 矩形中选择特定类的注释

ios - UISearchBar 未设置为 UITableView.tableHeaderView

ios - "This application is modifying the autolayout engine"错误(Swift iOS)

ios - 发送到 setDetailModal 实例的无法识别的选择器

php - JSON + MySQL 查询

ios - 为什么重写prepareForSegue会显示重写只能在类成员上指定

ios - 一旦我在 View Controller 中调整 Storyboard View 的大小,我的其他 View 就会消失

iphone - iOS : camera orientation

ios - AVCaptureSession 和 AVCaptureMovieFileOutput 帧时间戳