【发布时间】:2014-11-29 15:16:07
【问题描述】:
我设法编写了一些代码来打开相机并预览视频。我现在想从输出中捕获帧以发送到理想编码为 H.264 的服务器
这是我得到的:
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?
有人可以帮助转换它或给我指点如何从我当前的代码中获取框架吗?
【问题讨论】:
-
swift 缺少这类事情的文档。这很令人沮丧。
-
您找到解决方案了吗?
-
您是否正在尝试将代码从 Can use AVCaptureVideoDataOutput and AVCaptureMovieFileOutput at the same time? 转换为 swift?
-
也许您真正的问题是如何从您的 iPhone 流式传输视频,对吗?您尝试过 G-Streamer、FFmpeg 还是 live555?我很确定它会成功,只是要小心许可证......
-
标签: ios video swift stream avcapturesession