共享扩展中的AVAssetExportSession

我正在尝试在共享扩展和获取中选择的video上使用AVAssetExportSession

错误域= NSURLErrorDomain代码= -3000“无法创建文件”UserInfo = {NSLocalizedDescription =无法创建文件,NSUnderlyingError = 0x14811fdb0 {错误域= NSOSStatusErrorDomain代码= -12124“(null)”}}

但是我可以在同一个NSURL上手动创建文件而不会出错。 这是我正在使用的function

func reencodeVideo() { let videoAsset = AVURLAsset(URL: video.url) let videoTrack = videoAsset.tracksWithMediaType(AVMediaTypeVideo)[0] as AVAssetTrack print(videoTrack.estimatedDataRate) let exportSession = AVAssetExportSession(asset: videoAsset, presetName: AVAssetExportPreset1920x1080) guard let outputURL = uploadableFileURL else { return } let fileManager = NSFileManager.defaultManager() // let created = fileManager.createFileAtPath(outputURL.path!, contents: nil, attributes: nil) if let path = outputURL.path where fileManager.fileExistsAtPath(path) { print("file exists") } do { try fileManager.removeItemAtURL(outputURL) print("deleted") } catch { print(error) } exportSession?.outputURL = outputURL exportSession?.outputFileType = AVFileTypeQuickTimeMovie exportSession?.exportAsynchronouslyWithCompletionHandler{ print(exportSession?.status) } } private var uploadableFileURL: NSURL? { guard let tempFileName = video.url.lastPathComponent else { return nil } let fileManager = NSFileManager.defaultManager() guard let containerURL = fileManager.containerURLForSecurityApplicationGroupIdentifier(Constants.appGroupIdentifier) else { return nil } return containerURL.URLByAppendingPathComponent("videoFile.mov") } 

我已在同一目录中成功创建了文件,但AVAssetExportSession在那里返回错误。 我有什么想法我做错了吗?

我尝试使用AVAssetReaderAVAssetWriter ,并且AVAssetWriter在尝试启动时返回相同的错误。 如果我正在使用Documents目录,则编码过程成功完成,仅在使用共享应用程序组容器时失败。

您的问题可能与使用文档文件夹和icloud同步有关。 请参阅https://forums.developer.apple.com/message/77495#77495

如果您执行以下操作:

 guard let containerURL = fileManager.containerURLForSecurityApplicationGroupIdentifier(Constants.appGroupIdentifier) else { return nil } let libraryURL = containerURL.URLByAppendingPathComponent("Library", isDirectory: true) let cachesURL = libraryURL.URLByAppendingPathComponent("Caches", isDirectory: true) return cachesURL.URLByAppendingPathComponent("videoFile.mov") 

我无法从您的代码中找出uploadableFileURL的来源,但以下内容适用于我:

 if let video = videoAsset { let fm = NSFileManager.defaultManager().URLsForDirectory(.CachesDirectory, inDomains: .UserDomainMask) let url = fm[0].URLByAppendingPathComponent("\(NSDate())").URLByAppendingPathExtension("mp4") let exporter = AVAssetExportSession(asset: video, presetName: AVAssetExportPreset3840x2160) exporter?.outputURL = url exporter?.outputFileType = AVFileTypeMPEG4 exporter?.exportAsynchronouslyWithCompletionHandler() { _ in let data = NSData(contentsOfURL: url) } }