How to trim the video using AVFoundation

后端 未结 4 1659
醉话见心
醉话见心 2020-12-02 17:51

Iam able to record the video by using AVFoundation or UIImagePickerController. But i am unable to trim the video from one particular second to another particular duration/ti

4条回答
  •  [愿得一人]
    2020-12-02 18:22

    You can have the UIImagePickerController enable trimming

    UIImagePickerController *videoRecorder = [[UIImagePickerController alloc]init];         
            NSArray *sourceTypes = [UIImagePickerController availableMediaTypesForSourceType:videoRecorder.sourceType];
            NSLog(@"Available types for source as camera = %@", sourceTypes);
            if (![sourceTypes containsObject:(NSString*)kUTTypeMovie] ) {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil 
                                                                message:@"Device Not Supported for video Recording."                                                                       delegate:self 
                                                      cancelButtonTitle:@"Yes" 
                                                      otherButtonTitles:@"No",nil];
                [alert show];
                [alert release];
                return;
            }
            videoRecorder.allowsEditing = YES;
    

    Unfortunately after you get back from the imagePickerController, You are forced to convert the video manually.

    -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
    {
        if ([self.popoverLibraryBrowser isPopoverVisible])
        {
            [self.popoverLibraryBrowser dismissPopoverAnimated:YES];
        }
        NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
        if ([type isEqualToString:(NSString *)kUTTypeVideo] || 
            [type isEqualToString:(NSString *)kUTTypeMovie]) { // movie != video
            NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
    
    
            NSNumber *start = [info objectForKey:@"_UIImagePickerControllerVideoEditingStart"];
            NSNumber *end = [info objectForKey:@"_UIImagePickerControllerVideoEditingEnd"];
    
            // if start and end are nil then clipping was not used.
            // You should use the entire video.
    
    
            int startMilliseconds = ([start doubleValue] * 1000);
            int endMilliseconds = ([end doubleValue] * 1000);
    
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *documentsDirectory = [paths objectAtIndex:0];
    
            NSFileManager *manager = [NSFileManager defaultManager];
    
            NSString *outputURL = [documentsDirectory stringByAppendingPathComponent:@"output"] ;
            [manager createDirectoryAtPath:outputURL withIntermediateDirectories:YES attributes:nil error:nil];
    
            outputURL = [outputURL stringByAppendingPathComponent:@"output.mp4"];
            // Remove Existing File
            [manager removeItemAtPath:outputURL error:nil];
    
    
            //[self loadAssetFromFile:videoURL];
    
            [self.recorder dismissModalViewControllerAnimated:YES];
    
            AVURLAsset *videoAsset = [AVURLAsset URLAssetWithURL:videoURL options:nil]; 
    
    
            AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:videoAsset presetName:AVAssetExportPresetHighestQuality];
            exportSession.outputURL = [NSURL fileURLWithPath:outputURL];
            exportSession.outputFileType = AVFileTypeQuickTimeMovie;
            CMTimeRange timeRange = CMTimeRangeMake(CMTimeMake(startMilliseconds, 1000), CMTimeMake(endMilliseconds - startMilliseconds, 1000));
            exportSession.timeRange = timeRange;
    
            [exportSession exportAsynchronouslyWithCompletionHandler:^{
                switch (exportSession.status) {
                    case AVAssetExportSessionStatusCompleted:
                        // Custom method to import the Exported Video
                        [self loadAssetFromFile:exportSession.outputURL];
                        break;
                    case AVAssetExportSessionStatusFailed:
                        //
                        NSLog(@"Failed:%@",exportSession.error);
                        break;
                    case AVAssetExportSessionStatusCancelled:
                        //
                        NSLog(@"Canceled:%@",exportSession.error);
                        break;
                    default:
                        break;
                }
            }];
    
    
    
            //NSData *videoData = [NSData dataWithContentsOfURL:videoURL];
            //NSString *videoStoragePath;//Set your video storage path to this variable
            //[videoData writeToFile:videoStoragePath atomically:YES];
            //You can store the path of the saved video file in sqlite/coredata here.
        }
    }
    

提交回复
热议问题