Unable to trim a video using AVAssetExportSession

后端 未结 1 1829
孤独总比滥情好
孤独总比滥情好 2020-12-29 16:39

I want to trim a video:

-(void)trimVideo:(NSURL*)outputURL
{
    //[[NSFileManager defaultManager] removeItemAtURL:outputURL error:nil];
    AVURLAsset *asse         


        
相关标签:
1条回答
  • 2020-12-29 16:47

    This did the trick:

    -(void)trimVideo:(NSURL*)videoToTrimURL
    {
        //[[NSFileManager defaultManager] removeItemAtURL:outputURL error:nil];
        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);
        CMTime duration = CMTimeMakeWithSeconds(3.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];
         }];
    }
    
    0 讨论(0)
提交回复
热议问题