title: How to trim the video file and convert to 20 seconds video with Swift?
body: <p>I want to trim a video file. I want to just pick the video from gallery and convert it to a 15 second video. I am following <a href="https://stackoverflow.com/questions/20244456/how-to-trim-the-video-file-and-convert-to-15-seconds-video-with-ios-sdk">this</a> <a href="https://stackoverflow.com/questions/20244456/how-to-trim-the-video-file-and-convert-to-15-seconds-video-with-ios-sdk">link</a> for Objective C. It works fine for me but I'm a beginner with the Swift language. Can any one help me to convert this code in Swift?</p>
  
  <p><strong>Below is my code in Objective C:</strong></p>
  
  <pre><code>-(void)cropVideo:(NSURL*)videoToTrimURL{
      AVURLAsset *asset = [AVURLAsset URLAssetWithURL:videoToTrimURL options:nil];
      AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetHighestQuality];
  
      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
      NSString *outputURL = paths[0];
      NSFileManager *manager = [NSFileManager defaultManager];
      [manager createDirectoryAtPath:outputURL withIntermediateDirectories:YES attributes:nil error:nil];
      outputURL = [outputURL stringByAppendingPathComponent:@"output.mp4"];
      // Remove Existing File
      [manager removeItemAtPath:outputURL error:nil];
  
      //
      exportSession.outputURL = [NSURL fileURLWithPath:outputURL];
      exportSession.shouldOptimizeForNetworkUse = YES;
      exportSession.outputFileType = AVFileTypeQuickTimeMovie;
      CMTime start = CMTimeMakeWithSeconds(1.0, 600); // you will modify time range here
      CMTime duration = CMTimeMakeWithSeconds(19.0, 600);
      CMTimeRange range = CMTimeRangeMake(start, duration);
      exportSession.timeRange = range;
      [exportSession exportAsynchronouslyWithCompletionHandler:^(void)
       {
           switch (exportSession.status) {
               case AVAssetExportSessionStatusCompleted:
                   [self writeVideoToPhotoLibrary:[NSURL fileURLWithPath:outputURL]];
                   NSLog(@"Export Complete %d %@", exportSession.status, exportSession.error);
                   break;
               case AVAssetExportSessionStatusFailed:
                   NSLog(@"Failed:%@",exportSession.error);
                   break;
               case AVAssetExportSessionStatusCancelled:
                   NSLog(@"Canceled:%@",exportSession.error);
                   break;
               default:
                   break;
           }
  
           //[exportSession release];
       }];
  }
  -(void)writeVideoToPhotoLibrary:(NSURL*)aURL
  {
      NSURL *url = aURL;
      NSData *data = [NSData dataWithContentsOfURL:url];
  
      // Write it to cache directory
      NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"file.mov"];
      [data writeToFile:path atomically:YES];
  
  
      // After that use this path to save it to PhotoLibrary
  
      ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
      [library writeVideoAtPathToSavedPhotosAlbum:[NSURL fileURLWithPath:path] completionBlock:^(NSURL *assetURL, NSError *error) {
  
          if (error) {
              NSLog(@"%@", error.description);
          }else {
              NSLog(@"Done :)");
          }
  
      }];
  
  
  }
  </code></pre>