xcode Swift 将录制的视频保存为 mov 或 mp4
xcode Swift Save Recorded Video as mov or mp4
我一直在寻找一种使用 AVCaptureSession 保存录制视频的方法
这是我的代码:
@IBOutlet weak var tv: UIView!
var session:AVCaptureSession?
var output: AVCaptureVideoDataOutput?
var previewLayer: AVCaptureVideoPreviewLayer?
var out:AVCaptureMovieFileOutput!
var outPath:NSURL?
...在视图 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()
录制按钮操作
let recordingDelegate:AVCaptureFileOutputRecordingDelegate? = self
out = AVCaptureMovieFileOutput()
session?.addOutput(out)
out.startRecordingToOutputFileURL(outPath, recordingDelegate: recordingDelegate)
...停止按钮操作
out.stopRecording()
..和委托函数
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
万一有人遇到和我一样的问题,这里是解决方案
在委托函数 didfinishrecording 中添加这段代码
let library = PHPhotoLibrary.sharedPhotoLibrary()
library.performChanges({
PHAssetChangeRequest.creationRequestForAssetFromVideoAtFileURL(outPath)!
}, completionHandler: {success, error in
NSLog("Finished saving asset. %@", (success ? "Success." : error!))
})
问候
我一直在寻找一种使用 AVCaptureSession 保存录制视频的方法
这是我的代码:
@IBOutlet weak var tv: UIView!
var session:AVCaptureSession?
var output: AVCaptureVideoDataOutput?
var previewLayer: AVCaptureVideoPreviewLayer?
var out:AVCaptureMovieFileOutput!
var outPath:NSURL?
...在视图 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()
录制按钮操作
let recordingDelegate:AVCaptureFileOutputRecordingDelegate? = self
out = AVCaptureMovieFileOutput()
session?.addOutput(out)
out.startRecordingToOutputFileURL(outPath, recordingDelegate: recordingDelegate)
...停止按钮操作
out.stopRecording()
..和委托函数
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
万一有人遇到和我一样的问题,这里是解决方案
在委托函数 didfinishrecording 中添加这段代码
let library = PHPhotoLibrary.sharedPhotoLibrary()
library.performChanges({
PHAssetChangeRequest.creationRequestForAssetFromVideoAtFileURL(outPath)!
}, completionHandler: {success, error in
NSLog("Finished saving asset. %@", (success ? "Success." : error!))
})
问候