美文网首页
ios对视频长短进行截取

ios对视频长短进行截取

作者: 今年27 | 来源:发表于2018-01-26 18:49 被阅读581次

+(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];

    }];

}

相关文章

网友评论

      本文标题:ios对视频长短进行截取

      本文链接:https://www.haomeiwen.com/subject/gvvwaxtx.html