ios – Swift – 压缩视频文件

ios – Swift – 压缩视频文件,第1张

概述所以,目前我正在使用它来压缩视频: func compressVideo(inputURL: NSURL, outputURL: NSURL, handler:(session: AVAssetExportSession)-> Void) { let urlAsset = AVURLAsset(URL: inputURL, options: nil) le 所以,目前我正在使用它来压缩视频:

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 – 压缩视频文件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1068652.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存