gpt4 book ai didi

ios - 使用 AVFoundation Swift 保存视频

转载 作者:IT王子 更新时间:2023-10-29 05:22:43 32 4
gpt4 key购买 nike

您好,我学习了 Jared Davidson 的类(class),使用 AVFoundation 创建自定义相机 View 并保存图片。 https://www.youtube.com/watch?v=w0O3ZGUS3pk

但是我想录制和保存视频而不是图像。有人可以帮我吗?我确信它很简单,但苹果的文档是用 Objective-C 编写的,我无法解读它。

这是我的代码。谢谢。

import UIKit
import AVFoundation

class ViewController: UIViewController {

var captureSession = AVCaptureSession()
var sessionOutput = AVCaptureStillImageOutput()
var previewLayer = AVCaptureVideoPreviewLayer()


@IBOutlet var cameraView: UIView!

override func viewWillAppear(animated: Bool) {

let devices = AVCaptureDevice.devicesWithMediaType(AVMediaTypeVideo)
for device in devices {
if device.position == AVCaptureDevicePosition.Front{


do{

let input = try AVCaptureDeviceInput(device: device as! AVCaptureDevice)

if captureSession.canAddInput(input){

captureSession.addInput(input)
sessionOutput.outputSettings = [AVVideoCodecKey : AVVideoCodecJPEG]

if captureSession.canAddOutput(sessionOutput){

captureSession.addOutput(sessionOutput)
captureSession.startRunning()

previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
previewLayer.connection.videoOrientation = AVCaptureVideoOrientation.Portrait
cameraView.layer.addSublayer(previewLayer)

previewLayer.position = CGPoint(x: self.cameraView.frame.width / 2, y: self.cameraView.frame.height / 2)
previewLayer.bounds = cameraView.frame


}

}

}
catch{

print("Error")
}

}
}

}


@IBAction func TakePhoto(sender: AnyObject) {

if let videoConnection = sessionOutput.connectionWithMediaType(AVMediaTypeVideo){

sessionOutput.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: {
buffer, error in

let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(buffer)
UIImageWriteToSavedPhotosAlbum(UIImage(data: imageData)!, nil, nil, nil)

})

}

}

}

最佳答案

您可以通过创建 AVCaptureMovieFileOutput 并将其添加到您的捕获 session ,并使您的 ViewController 符合 AVCaptureFileOutputRecordingDelegate.

此示例将 5 秒的视频录制到应用程序文档目录中名为“output.mov”的文件中。

class ViewController: UIViewController, AVCaptureFileOutputRecordingDelegate {

var captureSession = AVCaptureSession()
var sessionOutput = AVCaptureStillImageOutput()
var movieOutput = AVCaptureMovieFileOutput()
var previewLayer = AVCaptureVideoPreviewLayer()

@IBOutlet var cameraView: UIView!

override func viewWillAppear(animated: Bool) {
self.cameraView = self.view

let devices = AVCaptureDevice.devicesWithMediaType(AVMediaTypeVideo)
for device in devices {
if device.position == AVCaptureDevicePosition.Front{


do{

let input = try AVCaptureDeviceInput(device: device as! AVCaptureDevice)

if captureSession.canAddInput(input){

captureSession.addInput(input)
sessionOutput.outputSettings = [AVVideoCodecKey : AVVideoCodecJPEG]

if captureSession.canAddOutput(sessionOutput){

captureSession.addOutput(sessionOutput)

previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
previewLayer.connection.videoOrientation = AVCaptureVideoOrientation.Portrait
cameraView.layer.addSublayer(previewLayer)

previewLayer.position = CGPoint(x: self.cameraView.frame.width / 2, y: self.cameraView.frame.height / 2)
previewLayer.bounds = cameraView.frame


}

captureSession.addOutput(movieOutput)

captureSession.startRunning()

let paths = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
let fileUrl = paths[0].URLByAppendingPathComponent("output.mov")
try? NSFileManager.defaultManager().removeItemAtURL(fileUrl)
movieOutput.startRecordingToOutputFileURL(fileUrl, recordingDelegate: self)

let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(5 * Double(NSEC_PER_SEC)))
dispatch_after(delayTime, dispatch_get_main_queue()) {
print("stopping")
self.movieOutput.stopRecording()
}
}

}
catch{

print("Error")
}

}
}

}

func captureOutput(captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAtURL outputFileURL: NSURL!, fromConnections connections: [AnyObject]!, error: NSError!) {
print("FINISHED \(error)")
// save video to camera roll
if error == nil {
UISaveVideoAtPathToSavedPhotosAlbum(outputFileURL.path!, nil, nil, nil)
}
}

}

关于ios - 使用 AVFoundation Swift 保存视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39431390/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com