微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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)
        }
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐