title: Wrong color of video after using AVAssetExportSession
body: <p>I'm generating a video and add <code>backgroundView</code> on a background of it.</p>
  
  <p><code>BackgroundView</code> contains color or image.</p>
  
  <p>While using <code>AVAssetExportSession</code> we can't add a background to the video. That's why I generate background video from <code>backgroundView</code>.</p>
  
  <pre><code>guard let export = AVAssetExportSession(asset: mixComposition,presetName: AVAssetExportPresetHighestQuality) else { return }
  export.videoComposition = mainComposition
  export.outputFileType = .mp4
  export.outputURL = exportURL
  export.shouldOptimizeForNetworkUse = true
  </code></pre>
  
  <p>After that add <code>AVURLAsset</code> via instructions on the background to the video. And generate the final one.</p>
  
  <pre><code>guard let track = composition.addMutableTrack(withMediaType: .video, preferredTrackID: kCMPersistentTrackID_Invalid) else { return nil }
  do {
      try track.insertTimeRange(CMTimeRange(start: .zero, end: asset.duration),
                                        of: asset.tracks(withMediaType: .video).first!, at: .zero)
  } catch {
      print(error)
  }
  return AVMutableVideoCompositionLayerInstruction(assetTrack: track)
  </code></pre>
  
  <p>And the problem that I've got incorrect color. After generating from image it's correct, but after adding as instructions it became lighter.</p>
  
  <p><strong>Update</strong></p>
  
  <p>Found that after <code>AVAssetExportSession</code> change the color of the video. It doesn't depend on instructions, it also changed if add the video to <code>videoLayer</code>.</p>
  
  <p>I have a video with solid color added<a href="https://i.stack.imgur.com/UAVIQ.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UAVIQ.jpg" alt="enter image description here"></a></p>
  
  <p>and after export it changed to <a href="https://i.stack.imgur.com/LFT5b.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LFT5b.jpg" alt="enter image description here"></a></p>
  
  <p>Video for test: <a href="http://s000.tinyupload.com/?file_id=51847253429896053354" rel="nofollow noreferrer">link for download</a></p>
  
  <h1>Update:</h1>
  
  <p>Added mainComposition and mixComposition</p>
  
  <pre><code>var mixComposition = AVMutableComposition()
  let mainInstruction = AVMutableVideoCompositionInstruction()
  </code></pre>
  
  <p>Using func for adding backgroundAsset like inout param.</p>
  
  <pre><code>    func addBackgroundAsset(_ asset: AVURLAsset?,
                              composition: inout AVMutableComposition) -&gt; AVMutableVideoCompositionLayerInstruction? {
          guard let asset = asset else { return nil}
          // Block with adding Background Image
          guard let track = composition.addMutableTrack(withMediaType: .video, preferredTrackID: kCMPersistentTrackID_Invalid),
              let assetTrack = asset.tracks(withMediaType: .video).first else { return nil }
          do {
              try track.insertTimeRange(CMTimeRange(start: .zero, end: asset.duration),
                                        of: assetTrack, at: .zero)
          } catch {
              print(error)
          }
          let instruction = AVMutableVideoCompositionLayerInstruction(assetTrack: track)
          let transform = assetTrack.preferredTransform
          instruction.setTransform(transform, at: .zero)
          return instruction
      }
  
          let mainInstruction = mainInstruction
          mainInstruction.timeRange = CMTimeRange(start: .zero, end: duration)
          mainInstruction.layerInstructions = instructions
          let mainComposition = AVMutableVideoComposition()
          mainComposition.instructions = [mainInstruction]
          mainComposition.frameDuration = CMTime(value: 1, timescale: 30)
          mainComposition.renderSize = CGSize(width: 1080, height: 1920)
          mainComposition.animationTool = AVVideoCompositionCoreAnimationTool(postProcessingAsVideoLayer: videoLayer,
                                                                              in: outputLayer)
  </code></pre>