ios - Swift - 压缩视频文件

标签 ios swift xcode video avassetexportsession

所以,目前我正在使用它来压缩视频:

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 .

有什么减小尺寸的技巧吗?

最佳答案

此扩展专注于将其导出到较低质量的设置(在本例中为“中”)并使用 mp4 容器,而不是 iOS 青睐的 mov 容器。这可能会导致质量下降,但您可以尝试使用更高的输出设置和不同的格式来微调输出。

extension PreviewVideoViewController: AVCaptureFileOutputRecordingDelegate {
    func fileOutput(_ output: AVCaptureFileOutput,
                    didFinishRecordingTo outputFileURL: URL,
                    from connections: [AVCaptureConnection],
                    error: Error?) {
        guard let data = try? Data(contentsOf: outputFileURL) else {
            return
        }

        print("File size before compression: \(Double(data.count / 1048576)) mb")

        let compressedURL = NSURL.fileURL(withPath: NSTemporaryDirectory() + UUID().uuidString + ".mp4")
        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 = try? Data(contentsOf: compressedURL) else {
                    return
                }

                print("File size after compression: \(Double(compressedData.count / 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 = .mp4
        exportSession.exportAsynchronously {
            handler(exportSession)
        }
    }
}

关于ios - Swift - 压缩视频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40470637/

相关文章:

ios - 对数组进行排序,其中包含日期,月份

ios - 什么是 iOS 包 ID

ios - 我可以为 4.7 和 5.5 设置特定的自动布局约束吗

ios - 我真的需要单例上的共享实例吗?

c++ - 此哈希仅适用于枚举类型

iphone - sizeWithFont 是否考虑自动收缩?

ios - 如果我将我的应用程序移到后台然后在 iOS 中进入前台,则呈现的 View Controller 会被解散

ios - 从iPhone 5s获取CellID,LAC,PCI,RSRP,RSRQ,SINR 高通MDM9615M基带芯片

ios - 将图像 (UIImageView) 叠加到照片中

ios - 修复了滚动时消失的按钮/单元格