title: scaleTimeRange for Video Preventing Export of Video in Swift 2.0+ on Xcode 7
body: <p>I am using AVFoundation to record a video in a custom camera application. The following code below successfully captures video from a previewView and turns it into a video asset that is saved to the device's Photo Library. I want to have the app use an effect to turn the video in to a slow motion video and have heard people say that <code>AVMutableComposition</code>'s <code>scaleTimeRange</code> can do the trick. But when I try to implement <strong>as shown in the code below</strong> the video is not exported and the effect is not in slow motion. </p>
  
  <pre><code>func captureOutput(captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAtURL outputFileURL: NSURL!, fromConnections connections: [AnyObject]!, error: NSError!) {
  
      if(error != nil){
          print(error)
      }
  
      self.lockInterfaceRotation = false
  
      let backgroundRecordId: UIBackgroundTaskIdentifier = self.backgroundRecordId
      self.backgroundRecordId = UIBackgroundTaskInvalid
  
      //create mutable composition of video and slow it down
  
      let videoAsset = AVURLAsset(URL: outputFileURL, options: nil)
      let mixComposition = AVMutableComposition()
      let videoTrack = mixComposition.addMutableTrackWithMediaType(AVMediaTypeVideo, preferredTrackID: CMPersistentTrackID(kCMPersistentTrackID_Invalid))
      let audioTrack = mixComposition.addMutableTrackWithMediaType(AVMediaTypeAudio, preferredTrackID: CMPersistentTrackID(kCMPersistentTrackID_Invalid))
  
      let sourceVideoTrack = videoAsset.tracksWithMediaType(AVMediaTypeVideo)[0] 
      let sourceAudioTrack = videoAsset.tracksWithMediaType(AVMediaTypeAudio)[0]
      do{
          try videoTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero, videoAsset.duration), ofTrack: sourceVideoTrack, atTime: kCMTimeZero)
  
          try audioTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero, videoAsset.duration), ofTrack: sourceAudioTrack, atTime: kCMTimeZero)
      }catch _ {
          print("ERROR could not insert time range")
          return
      }
  
      /* ** THE PART THAT ISN'T WORKING - When I add this section everything breaks **
      //slow video down for slow mo effect
      let videoDuration = videoAsset.duration;
      let videoScaleFactor = 2 * videoDuration.value
  
      videoTrack.scaleTimeRange(CMTimeRangeMake(kCMTimeZero, videoDuration), toDuration: CMTimeMake(videoScaleFactor, videoDuration.timescale))
      */
  
      // Make mutable combination
      // -- Create instruction
      let instruction = AVMutableVideoCompositionInstruction()
      instruction.timeRange = CMTimeRangeMake(kCMTimeZero, videoAsset.duration)
      let videoLayerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: videoTrack)
      instruction.layerInstructions = [videoLayerInstruction]
  
      let mutableComposition = AVMutableVideoComposition()
      mutableComposition.renderSize = videoTrack.naturalSize
      mutableComposition.frameDuration = CMTimeMake(1, 20)
      mutableComposition.instructions = [instruction]
  
      // -- Get path
      let fileName = "/editedVideo-\(arc4random() % 10000).mp4"
      let allPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
      let docsPath = allPaths[0] as NSString
      let exportPath = docsPath.stringByAppendingFormat(fileName)
      let exportUrl = NSURL.fileURLWithPath(exportPath as String)
  
      print("Tracks before export: \(mixComposition.tracks.count). File URL: \(exportUrl)")
  
      // -- Remove old video if exists
      if NSFileManager.defaultManager().fileExistsAtPath(exportPath as String) {
          print("Deleting existing file\n")
          do{
              try NSFileManager.defaultManager().removeItemAtPath(exportPath as String)
          }catch _ {
              print("Error deleting old video file")
          }
  
      }
  
      // -- Create exporter
      let exporter = AVAssetExportSession(asset: mixComposition, presetName: AVAssetExportPresetHighestQuality)
      exporter!.videoComposition = mutableComposition
      exporter!.outputFileType = AVFileTypeMPEG4
      exporter!.outputURL = exportUrl
      exporter!.shouldOptimizeForNetworkUse = true
  
      // -- Export video
      exporter!.exportAsynchronouslyWithCompletionHandler({
          self.exportDidFinish(exporter!)
      })
  
  }
  
  func exportDidFinish(exporter: AVAssetExportSession) {
  
      // Save video to photo album
      let assetLibrary = ALAssetsLibrary()
      assetLibrary.writeVideoAtPathToSavedPhotosAlbum(exporter.outputURL, completionBlock: {(url: NSURL!, error: NSError!) in
          print("Saved video to album \(exporter.outputURL)")
          if (error != nil) {
              print("Error saving video")
          }
      })
  
      // Check asset tracks
      print("SUCCESS exporting video")
  }
  </code></pre>
  
  <p>Any ideas would be appreciated! Thanks</p>