ios对视频长短进行截取

2018-01-26  本文已影响581人  今年27

+(void)trimVideo:(AVURLAsset*)asset needCrop:(BOOL)needCrop andComplete:(void (^) (NSError* error)) complete

{

    //[[NSFileManager defaultManager] removeItemAtURL:outputURL error:nil];

//    AVURLAsset *asset = [AVURLAsset URLAssetWithURL:videoToTrimURL options:nil];

    AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetMediumQuality];

    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:@"Movie.mp4"];

    // Remove Existing File

    [manager removeItemAtPath:outputURL error:nil];

    exportSession.outputURL = [NSURL fileURLWithPath:outputURL];

    exportSession.shouldOptimizeForNetworkUse = YES;

    exportSession.outputFileType = AVFileTypeQuickTimeMovie;

    exportSession.videoComposition = [self getVideoComposition:asset];

    if (needCrop) {

        CMTime start = CMTimeMakeWithSeconds(0.0, 600);

        CMTime duration = CMTimeMakeWithSeconds(15.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);

                complete(nil);

            }

                break;

            case AVAssetExportSessionStatusFailed:

            {

                NSLog(@"Export Complete %d %@", exportSession.status, exportSession.error);

                complete(exportSession.error);

            }

                break;

            case AVAssetExportSessionStatusCancelled:

                {

                    NSLog(@"Failed:%@",exportSession.error);

                    complete(exportSession.error);

                }

                break;

            default:

                break;

        }

        //[exportSession release];

    }];

}

上一篇下一篇

猜你喜欢

热点阅读