有没有办法在iOS中通过AVFoundation
获得方形视频输出?
我使用OpenGL处理视频的每一帧(CMSampleBuffer
)。每一帧
旋转,所以我需要裁剪和旋转CMSampleBuffer
。但我不知道该怎么做,所以我相信有一种方法可以通过在videoSettings
中设置属性(AVCaptureVideoDataOutput
)来获取已裁剪和旋转的帧。
更新
我在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();
}
它为我完美地镜像和旋转视频输出!但它并没有裁剪!
答案 0 :(得分:3)
要旋转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
以获取完整的密钥列表
答案 1 :(得分:-1)
Thread致力于使用GPUImage进行视频裁剪和旋转