gpt4 book ai didi

xcode Swift 将录制的视频保存为 mov 或 mp4

转载 作者:行者123 更新时间:2023-11-28 06:51:01 24 4
gpt4 key购买 nike

我一直在努力寻找一种使用 AVCaptureSession 保存录制视频的方法

这是我的代码:

    @IBOutlet weak var tv: UIView!
var session:AVCaptureSession?
var output: AVCaptureVideoDataOutput?
var previewLayer: AVCaptureVideoPreviewLayer?
var out:AVCaptureMovieFileOutput!
var outPath:NSURL?

...在 View didload 函数上

        super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let filemgr = NSFileManager.defaultManager()
let documentsURL = filemgr.URLsForDirectory(.DesktopDirectory, inDomains: .UserDomainMask)[0]
outPath = documentsURL.URLByAppendingPathComponent("temp")

session = AVCaptureSession()
session!.sessionPreset = AVCaptureSessionPresetLow

let camera = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
var deviceInput:AVCaptureDeviceInput?

do{
try deviceInput = AVCaptureDeviceInput(device: camera)
}catch{
}
session?.addInput(deviceInput)
previewLayer = AVCaptureVideoPreviewLayer(session: session)
let frame = CGRect(x: 0, y: 0, width: self.tv.frame.width, height: self.tv.frame.height)
tv.layer.addSublayer(previewLayer!)
previewLayer?.frame = frame
session?.startRunning()

记录按钮 Action

        let recordingDelegate:AVCaptureFileOutputRecordingDelegate? = self
out = AVCaptureMovieFileOutput()
session?.addOutput(out)
out.startRecordingToOutputFileURL(outPath, recordingDelegate: recordingDelegate)

...停止按钮操作

        out.stopRecording()

..和委托(delegate)函数

    func captureOutput(captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAtURL outPath: NSURL!, fromConnections connections: [AnyObject]!, error: NSError!){
print("record fin")
return
}
func captureOutput(captureOutPut: AVCaptureFileOutput!, didStartRecordingTOOutputFileAtURL outPath: NSURL!, fromConnections connections: [AnyObject]!){
print("record start")
return
}

感谢您的帮助:)

编辑:这里有一些额外的细节

There are no errors or warning in the code and the app runs just fine The camera preview shows but the problem is on saving the recorded video, when the out.startRecordingToOutputFileURL(outPath, recordingDelegate: recordingDelegate) is called, it does not save any file

最佳答案

万一有人碰巧遇到和我一样的问题,这里是解决方案

在委托(delegate)函数 didfinishrecording 中添加这段代码

    let library = PHPhotoLibrary.sharedPhotoLibrary()
library.performChanges({
PHAssetChangeRequest.creationRequestForAssetFromVideoAtFileURL(outPath)!
}, completionHandler: {success, error in
NSLog("Finished saving asset. %@", (success ? "Success." : error!))
})

问候

关于xcode Swift 将录制的视频保存为 mov 或 mp4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34892254/

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