大佬教程收集整理的这篇文章主要介绍了ios – Swift – 压缩视频文件,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
func compressvideo(inputURL: NSURL,outputURL: NSURL,handler:(session: AVAssetExportSession)-> Void) { let urlAsset = AVURLAsset(URL: inputURL,options: nil) let exportSession = AVAssetExportSession(asset: urlAsset,presetName: AVAssetExportPresetMediumQuality) exportSession!.outputURL = outputURL exportSession!.outputFileType = AVFileTypeQuickTimeMovie exportSession!.shouldoptimizeForNetworkUse = true exportSession!.exportAsynchronouslyWithCompletionHandler { () -> Void in handler(session: exportSession!) } }
当我在2秒内录制视频时,大小为4.3 MB,当我在6秒内录制视频时,文件大小为9,3 MB.
任何减小尺寸的提示?
我使用基于Swift版本的这些扩展:
对于OP(Swift 2.2):
extension PreviewVideoViewController: AVCaptureFiLeoutputRecordingDelegate { func captureOutput(captureOutput: AVCaptureFiLeoutput!,didFinishRecordingToOutputFileAtURL outputFileURL: NSURL!,fromConnections connections: [AnyObject]!,error: NSError!) { let data = NSData(contentsOfURL: outputFileURL) print("File size before compression: \(Double(data!.length / 1048576)) mb") let compressedURL = NSURl.fileURLWithPath(NstemporaryDirectory() + NSUUID().UUIDString + ".m4v") compressvideo(outputFileURL,outputURL: compressedURL) { (session) in switch session.status { case .UnkNown: break case .WaiTing: break case .ExporTing: break case .Completed: let data = NSData(contentsOfURL: compressedURL) print("File size after compression: \(Double(data!.length / 1048576)) mb") case .Failed: break case .Cancelled: break } } } private func compressvideo(inputURL: NSURL,handler:(session: AVAssetExportSession)-> Void) { let urlAsset = AVURLAsset(URL: inputURL,options: nil) if let exportSession = AVAssetExportSession(asset: urlAsset,presetName: AVAssetExportPresetMediumQuality) { exportSession.outputURL = outputURL exportSession.outputFileType = AVFileTypeQuickTimeMovie exportSession.shouldoptimizeForNetworkUse = true exportSession.exportAsynchronouslyWithCompletionHandler { () -> Void in handler(session: exportSession) } } } }
对于在Swift 3.0中需要它的人:
extension PreviewVideoViewController: AVCaptureFiLeoutputRecordingDelegate { func capture(_ captureOutput: AVCaptureFiLeoutput!,didFinishRecordingToOutputFileAt outputFileURL: URL!,fromConnections connections: [Any]!,error: Error!) { guard let data = NSData(contentsOf: outputFileURL as URL) else { return } print("File size before compression: \(Double(data.length / 1048576)) mb") let compressedURL = NSURl.fileURL(withPath: NstemporaryDirectory() + NSUUID().uuidString + ".m4v") compressvideo(inputURL: outputFileURL as URL,outputURL: compressedURL) { (exportSession) in guard let session = exportSession else { return } switch session.status { case .unkNown: break case .waiTing: break case .exporTing: break case .completed: guard let compressedData = NSData(contentsOf: compressedURL) else { return } print("File size after compression: \(Double(compressedData.length / 1048576)) mb") case .Failed: break case .cancelled: break } } } func compressvideo(inputURL: URL,outputURL: URL,handler:@escaping (_ exportSession: AVAssetExportSession?)-> Void) { let urlAsset = AVURLAsset(url: inputURL,options: nil) guard let exportSession = AVAssetExportSession(asset: urlAsset,presetName: AVAssetExportPresetMediumQuality) else { handler(nil) return } exportSession.outputURL = outputURL exportSession.outputFileType = AVFileTypeQuickTimeMovie exportSession.shouldoptimizeForNetworkUse = true exportSession.exportAsynchronously { () -> Void in handler(exportSession) } } }
以上是大佬教程为你收集整理的ios – Swift – 压缩视频文件全部内容,希望文章能够帮你解决ios – Swift – 压缩视频文件所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。