title: AVAssetExportSession fails to export videos downloaded from iCloud
body: <p>I'm trying to create a scaled down version of a video selected from the users photo album. The max dimensions of the output will be 720p. Therefore, when retrieving the video, I'm using the <code>.mediumQualityFormat</code> as the <code>deliveryMode</code>. This causes iOS to retrieve a 720p video from iCloud if the original video or its medium quality version don't exist in the users device.</p>
  <pre><code>let videoRequestOptions = PHVideoRequestOptions()
  videoRequestOptions.deliveryMode = .mediumQualityFormat
  videoRequestOptions.isNetworkAccessAllowed = true
  
  PHImageManager.default().requestAVAsset(forVideo: asset, options: videoRequestOptions) { (asset, audioMix, info) in
      // Proceess the asset
  }
  </code></pre>
  <p>The problem is, when I use <code>AVAssetExportSession</code> to create a scaled down version of the asset, if the asset is a medium variant and not the original version, the export process fails immediately with the following error:</p>
  <pre><code>Error Domain=AVFoundationErrorDomain Code=-11800 &quot;The operation could not be completed&quot; UserInfo={NSLocalizedFailureReason=An unknown error occurred (-17507), NSLocalizedDescription=İşlem tamamlanamadı, NSUnderlyingError=0x283bbcf60 {Error Domain=NSOSStatusErrorDomain Code=-17507 &quot;(null)&quot;}}
  </code></pre>
  <p>I couldn't find anything about the meaning of this error anywhere.</p>
  <p>When I set the <code>deliveryMode</code> property to <code>.auto</code> or <code>.highQualityFormat</code>, everything is working properly.</p>
  <p>When I checked the asset url's, I noticed that when the video has been retrieved from iCloud, its filename has a &quot;.medium&quot; postfix like in this example:</p>
  <p><code>file:///var/mobile/Media/PhotoData/Metadata/PhotoData/CPLAssets/group338/191B2348-5E19-4A8E-B15C-A843F9F7B5A3.medium.MP4</code></p>
  <p>The weird thing is, if I use <code>FileManager</code> to copy the video in this url to another directory, create a new <code>AVAsset</code> from that file, and use that asset when creating the <code>AVExportSession</code> instance, the problem goes away.</p>
  <p>I'd really appreciate if someone could provide some insight about what the problem could be.</p>
  <p>This is how I use <code>AVAssetExportSession</code> to  create a scaled down version of the original video.</p>
  <pre><code>let originalVideoURL = // The url of the asset retrieved from requestAVAsset
  
  let outputVideoPath = NSTemporaryDirectory() + &quot;encodedVideo.mp4&quot;
  let outputVideoURL = URL(fileURLWithPath: outputVideoPath)
  
  guard
          let exportSession = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetHighestQuality),
          let videoTrack = asset.tracks(withMediaType: .video).first else {
          // Error handling
      return
  }
  
  let videoComposition = AVMutableVideoComposition()
  videoComposition.renderSize = scaledSize // How this is calculated is irrelevant
  videoComposition.frameDuration = CMTimeMake(value: 1, timescale: 30)
  
  let layerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: videoTrack)
  let transform = videoTrack.preferredTransform
  
  layerInstruction.setTransform(transform, at: .zero)
  
  let instruction = AVMutableVideoCompositionInstruction()
  instruction.timeRange = CMTimeRangeMake(start: .zero, duration: asset.duration)
  instruction.layerInstructions = [layerInstruction]
  videoComposition.instructions = [instruction]
  
  exportSession.videoComposition = videoComposition
  exportSession.outputURL = outputVideoURL
  exportSession.outputFileType = .mp4
  exportSession.shouldOptimizeForNetworkUse = true
  
  exportSession.exportAsynchronously(completionHandler: {[weak self] in
      guard let self = self else { return }
  
      if let url = exportSession.outputURL, exportSession.status == .completed {
          // Works for local videos
      } else {
          // Fails with error code 17507 when loading videos with delivery size &quot;Medium&quot;
      }
  })
  </code></pre>