从Swift中的AVCaptureSession获取输出以发送到服务器
问题描述:
我已经设法编写一些代码来打开相机并预览视频。我现在想捕捉从输出帧发送到理想的编码为H.264从Swift中的AVCaptureSession获取输出以发送到服务器
这里的服务器是我得到了什么:
import UIKit
import AVFoundation
class ViewController: UIViewController {
let captureSession = AVCaptureSession()
var previewLayer : AVCaptureVideoPreviewLayer?
// If we find a device we'll store it here for later use
var captureDevice : AVCaptureDevice?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
captureSession.sessionPreset = AVCaptureSessionPresetHigh
let devices = AVCaptureDevice.devices()
// Loop through all the capture devices on this phone
for device in devices {
// Make sure this particular device supports video
if (device.hasMediaType(AVMediaTypeVideo)) {
// Finally check the position and confirm we've got the back camera
if(device.position == AVCaptureDevicePosition.Back) {
captureDevice = device as? AVCaptureDevice
if captureDevice != nil {
println("Capture device found")
beginSession()
}
}
}
}
}
func beginSession() {
var err : NSError? = nil
captureSession.addInput(AVCaptureDeviceInput(device: captureDevice, error: &err))
if err != nil {
println("error: \(err?.localizedDescription)")
}
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
self.view.layer.addSublayer(previewLayer)
previewLayer?.frame = self.view.layer.frame
captureSession.startRunning()
}
}
这种开放式的相机成功,我可以预览镜头。
我发现这个Objective C代码看起来像它得到的输出,但我不知道如何将其转换为swift。它使用AVCaptureVideoDataOutput,AVAssetWriter,AVAssetWriterInput和AVAssetWriterInputPixelBufferAdaptor将帧写入H.264编码的电影文件。
Can use AVCaptureVideoDataOutput and AVCaptureMovieFileOutput at the same time?
有人可以帮助转换,或给我指针,以如何获得帧离开我当前的代码?
答
Apple在ObjectiveC中有一个示例项目AVCam,可以处理这些事情。
Here's关于在Swift中使用AVCamera的SO的另一个问题。
我个人使用这个https://github.com/alex-chan/AVCamSwift,这很好。我只需要将它转换为Xcode中的最新Swift语法,并且它工作正常。
另一个建议是使用您找到的ObjectiveC代码,并通过桥接头将其导入Swift代码中。
这些东西的文档缺乏迅速。这非常令人沮丧。 – 2015-02-19 16:10:06
您是否找到解决方案? – 2015-05-08 09:13:40
你想转换代码[可以同时使用AVCaptureVideoDataOutput和AVCaptureMovieFileOutput?](http://stackoverflow.com/questions/4944083/can-use-avcapturevideodataoutput-and-avcapturemoviefileoutput-at-the-same-time )迅速? – 2015-05-09 15:20:10