有没有办法在iOS中通过AVFoundation获得方形视频输出?
我使用OpenGL处理视频的每一帧(CMSampleBuffer).每一帧
旋转,所以我需要裁剪和旋转CMSampleBuffer.但我不知道该怎么做,所以我相信有一种方法可以通过在AVCaptureVideoDataOutput中设置属性(videoSettings)来获取已裁剪和旋转的帧.
我用谷歌搜索,用Google搜索谷歌,但没有发现任何东西.
swift中的代码示例很棒.
更新:
我在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();
}
这对我来说是完美的镜像和旋转视频输出!但它不是裁剪!
最佳答案 要旋转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,您可以使用videoSettings字典设置裁剪.
例如:
NSDictionary *videoSettings = @{
AVVideoCodecKey : AVVideoCodecH264
, AVVideoWidthKey : @(100)
, AVVideoHeightKey : @(100)
};
用于这里:
self.assetWriterVideoInput = [[AVAssetWriterInput alloc]
initWithMediaType:AVMediaTypeVideo
outputSettings:videoSettings];
会给你一个大小为100 x 100像素的视频,全宽,但裁剪高度到正方形.
查看AVVideoSettings.h以获取完整的密钥列表