您可以使用以下代码将录制的视频转换为MP4:
func enpreVideo(videoURL: NSURL) {let avAsset = AVURLAsset(URL: videoURL, options: nil)var startDate = NSDate()//Create Export sessionexportSession = AVAssetExportSession(asset: avAsset, presetName: AVAssetExportPresetPassthrough)// exportSession = AVAssetExportSession(asset: composition, presetName: mp4Quality)//Creating temp path to save the converted videolet documentsDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .UserDomainMask, true)[0]let mydocumentPath = NSURL(fileURLWithPath: documentsDirectory).URLByAppendingPathComponent("temp.mp4").absoluteStringlet url = NSURL(fileURLWithPath: mydocumentPath)let documentsDirectory2 = NSFileManager.defaultManager().URLsForDirectory(.documentDirectory, inDomains: .UserDomainMask)[0] as NSURLlet filePath = documentsDirectory2.URLByAppendingPathComponent("rendered-Video.mp4")deleteFile(filePath)//Check if the file already exists then remove the previous fileif NSFileManager.defaultManager().fileExistsAtPath(mydocumentPath) { do { try NSFileManager.defaultManager().removeItemAtPath(mydocumentPath) } catch let error { print(error) }} urlexportSession!.outputURL = filePathexportSession!.outputFileType = AVFileTypeMPEG4exportSession!.shouldOptimizeForNetworkUse = truevar start = CMTimeMakeWithSeconds(0.0, 0)var range = CMTimeRangeMake(start, avAsset.duration)exportSession.timeRange = rangeexportSession!.exportAsynchronouslyWithCompletionHandler({() -> Void in switch self.exportSession!.status { case .Failed: print("%@",self.exportSession?.error) case .Cancelled: print("Export canceled") case .Completed: //Video conversion finished var endDate = NSDate() var time = endDate.timeIntervalSinceDate(startDate) print(time) print("Successful!") print(self.exportSession.outputURL) default: break }})}func deleteFile(filePath:NSURL) {guard NSFileManager.defaultManager().fileExistsAtPath(filePath.path!) else { return}do { try NSFileManager.defaultManager().removeItemAtPath(filePath.path!)}catch{ fatalError("Unable to delete file: (error) : (__FUNCTION__).")}}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)