首页 > 解决方案 > iOS13 AVAssetExportSession 错误试图减少设备上的视频大小但不是模拟器

问题描述

我正在尝试使用以下方法减小视频大小:

func reduceVideoSize(_ originalVideoURL : String, completion: @escaping (_ data: Data?) ->()) {

        let VideoFilePath = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("mergeVideo\(arc4random()%1000)d").appendingPathExtension("mp4").absoluteString
        if FileManager.default.fileExists(atPath: VideoFilePath) {
            do {
                try FileManager.default.removeItem(atPath: VideoFilePath)
            } catch { }
        }

        let savePathUrl =  URL(string: VideoFilePath)!

        let sourceAsset = AVURLAsset(url: URL(string: originalVideoURL)! , options: nil)

        let assetExport: AVAssetExportSession = AVAssetExportSession(asset: sourceAsset, presetName: AVAssetExportPresetMediumQuality)!
        assetExport.outputFileType = AVFileType.mov
        assetExport.outputURL = savePathUrl
        assetExport.exportAsynchronously { () -> Void in

            switch assetExport.status {
            case AVAssetExportSession.Status.completed:
                DispatchQueue.main.async(execute: {
                    do {
                        let videoData = try Data(contentsOf: savePathUrl, options: NSData.ReadingOptions())
                        //print("MB - \(Double(videoData.count) / (1024.0 * 1024.0))")
                        completion(videoData)
                    } catch {
                        print(error)
                        completion(nil)
                    }
                })
            case  AVAssetExportSession.Status.failed:
                print("failed \(String(describing: assetExport.error))")
            case AVAssetExportSession.Status.cancelled:
                print("cancelled \(String(describing: assetExport.error))")
            default:
                print("complete")
            }
        }
    }

这在 iOS 13 之前运行良好,即使 iOS 13 在模拟器上运行良好,但是当我在我的设备 iPhone 11 Pro Max 上尝试时,我得到 AVAssetExportSession.Status.failed 并显示“操作无法完成”

我尝试将质量更改为 AVAssetExportPresetLowQuality 和高质量但同样的错误。

此函数从 imagePicker 委托调用:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    if let mediaType = info[.mediaType] as? NSString {
        if mediaType == kUTTypeMovie {

            // video
            if let videoURL = info[.mediaURL] as? URL {
                self.mediaHelper.reduceVideoSize(videoURL.absoluteString, completion: { (data) in
                    if data != nil {
                        self.videoData = data
                        self.sideImage = image
                    }
                })
            }
        }
    }

    picker.dismiss(animated: true) {
        self.layoutIfNeeded()
    }
}

标签: iosswiftxcodeavassetexportsession

解决方案


推荐阅读