-2

How can I save a video selected from the camera roll to a specific destination? In my case I want to save the selected video to destinationPath. Also how can I get the duration of the video?

Code:

- (void) imagePickerController: (UIImagePickerController *) picker didFinishPickingMediaWithInfo: (NSDictionary *) info
{

    // Video Path
    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd_HH-mm-ss"];
    NSString *destinationPath = [documentsDirectory stringByAppendingFormat:@"/output_%@.mov", [dateFormatter stringFromDate:[NSDate date]]];

    // Save Video

    // Get Video Duration        

    [self dismissViewControllerAnimated:YES completion:nil];

}
Renfei Song
  • 2,941
  • 2
  • 25
  • 30
SpaceShroomies
  • 1,635
  • 2
  • 17
  • 23

1 Answers1

0
- (void) imagePickerController: (UIImagePickerController *) picker didFinishPickingMediaWithInfo: (NSDictionary *) info
  {
    NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
    AVAsset *avAsset = [AVURLAsset URLAssetWithURL:videoURL options:nil];
    // Video Path
    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd_HH-mm-ss"];
    NSString *destinationPath = [documentsDirectory stringByAppendingFormat:@"/output_%@.mov", [dateFormatter stringFromDate:[NSDate date]]];
    NSURL *url = [NSURL fileURLWithPath:destinationPath];
    // Save Video
    AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:avAsset presetName:AVAssetExportPresetHighestQuality];
    exportSession.outputURL = url;
    [exportSession exportAsynchronouslyWithCompletionHandler:nil];
    [binaryImageData writeToFile:destinationPath atomically:YES];
    // Get Video Duration        
    CMTime duration = avAsset.duration;
    [self dismissViewControllerAnimated:YES completion:nil];
  }
Britto Thomas
  • 2,092
  • 1
  • 15
  • 28