ios - 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()
}
}
解决方案
推荐阅读
- javascript - 为什么我的 drawImage 不接受高度参数的“0+常数”?
- python - 如何找到列的名称以及最大值
- javascript - 存根函数返回 .promise() 可接受的内容
- php - 检查 URL 是否包含 PHP 中的内容
- python - 获取熊猫每个月的最后一个非NaN值
- r - 计算日期范围间隔的部分重叠数
- java - 迭代 Thymeleaf 中的地图列表
- ruby-on-rails - 将angularjs与ruby api集成
- javascript - 后删除内容。从字符串
- ruby-on-rails - 使用 Ping Federate 实现 Rails SSO