IOS square video output

Is there a way to get square video output on AVFoundationin iOS?

I use OpenGL to process every frame ( CMSampleBuffer) of video. Each frame rotates, so I need to crop and rotate CMSampleBuffer. But I don’t know how to do this, so I believe that there is a way to get already cropped and rotated frames by setting properties ( videoSettings) in AVCaptureVideoDataOutput.

I googled, googled and googled it but found nothing. Sample code in swift would be great.

Update:

My complete final solution in Swift:

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    captureSession = AVCaptureSession()
    captureSession!.sessionPreset = AVCaptureSessionPreset640x480

    let backCamera = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
    var error: NSError?
    var input: AVCaptureDeviceInput!
    do {
        input = try AVCaptureDeviceInput(device: backCamera)
    } catch let error1 as NSError {
        error = error1
        input = nil
    }
    if error == nil && captureSession!.canAddInput(input) {
        captureSession!.addInput(input)
        stillImageOutput = AVCaptureStillImageOutput()
        stillImageOutput!.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG, kCVPixelBufferPixelFormatTypeKey: Int(kCVPixelFormatType_32BGRA)]
        if captureSession!.canAddOutput(stillImageOutput) {
            captureSession!.addOutput(stillImageOutput)
        }
    }
    videoOutput = AVCaptureVideoDataOutput()
    videoOutput!.videoSettings = [kCVPixelBufferPixelFormatTypeKey: Int(kCVPixelFormatType_32BGRA), AVVideoWidthKey : 100, AVVideoHeightKey: 100]
    videoOutput!.setSampleBufferDelegate(self, queue: dispatch_queue_create("sample buffer delegate", DISPATCH_QUEUE_SERIAL))

    if captureSession!.canAddOutput(self.videoOutput) {
        captureSession!.addOutput(self.videoOutput)
    }

    videoOutput!.connectionWithMediaType(AVMediaTypeVideo).videoOrientation = AVCaptureVideoOrientation.PortraitUpsideDown
    videoOutput!.connectionWithMediaType(AVMediaTypeVideo).videoMirrored = true
    captureSession!.startRunning();
}

This mirroring and rotation of the video output is perfect for me! But this is not circumcision!

+4
source share
2

CMSampleBuffer, Apple:

https://developer.apple.com/library/ios/qa/qa1744/_index.html

, ( ), ...

, :

- (void)captureOutput:(AVCaptureOutput *)captureOutput 
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer 
       fromConnection:(AVCaptureConnection *)connection 

:

    [connection setVideoOrientation:AVCaptureVideoOrientationPortraitUpsideDown];

.

, AVAssetWriterInput, .

:

NSDictionary *videoSettings = @{
      AVVideoCodecKey  : AVVideoCodecH264
    , AVVideoWidthKey  : @(100)
    , AVVideoHeightKey : @(100)
         };

:

  self.assetWriterVideoInput = [[AVAssetWriterInput alloc]
                               initWithMediaType:AVMediaTypeVideo
                                  outputSettings:videoSettings];

100 x 100 px, , .

AVVideoSettings.h

+3

, GPUImage

-1

Source: https://habr.com/ru/post/1617770/


All Articles