剪视频

2018-01-17  本文已影响0人  吾家有谷粒儿

NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:self.asset];

    if ([compatiblePresets containsObject:AVAssetExportPresetMediumQuality]) {

        self.exportSession = [[AVAssetExportSession alloc] initWithAsset:self.asset presetName:AVAssetExportPresetPassthrough];

        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

        formatter.dateFormat = @"yyyyMMddHHmmss";

        NSString *str = [formatter stringFromDate:[NSDate date]];

        NSString *videoPath =  [NSTemporaryDirectory() stringByAppendingFormat:@"%@.mp4",str];

        NSURL *furl = [NSURL fileURLWithPath:videoPath];

        self.exportSession.outputURL = furl;

        self.exportSession.outputFileType = AVFileTypeQuickTimeMovie;

        CMTime start = CMTimeMakeWithSeconds(self.startTime, self.asset.duration.timescale);

        CMTime duration = CMTimeMakeWithSeconds(self.stopTime - self.startTime, self.asset.duration.timescale);

        CMTimeRange range = CMTimeRangeMake(start, duration);

        self.exportSession.timeRange = range;

        [self.exportSession exportAsynchronouslyWithCompletionHandler:^{

            switch ([self.exportSession status]) {

                case AVAssetExportSessionStatusFailed:

                    NSLog(@"Export failed: %@", [[self.exportSession error] localizedDescription]);

                    break;

                case AVAssetExportSessionStatusCancelled:

                    NSLog(@"Export canceled");

                    break;

                default:

                    NSLog(@"NONE");

                    dispatch_async(dispatch_get_main_queue(), ^{

                         NSLog(@"剪辑成功");

                    });

                    break;

            }

        }];

    }

上一篇下一篇

猜你喜欢

热点阅读