InViZz
InViZz

Reputation: 109

Cutting video on iPhone

I have a video file from the device camera -- stored as /private/var/mobile/Media/DCIM/100APPLE/IMG_0203.MOV, for example, and I need to cut the first 10 seconds of this video. What API or libraries I can use?

Upvotes: 0

Views: 1954

Answers (1)

InViZz
InViZz

Reputation: 109

I found solution with standard API: AVAssetExportSession

- (void)getTrimmedVideoForFile:(NSString *)filePath withInfo:(NSArray *)info
{
//[[NSFileManager defaultManager] removeItemAtURL:outputURL error:nil];
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:filePath] options:nil];
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetLowQuality];

// NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSAllDomainsMask, YES);
// NSString *outputURL = paths[0];
NSFileManager *manager = [NSFileManager defaultManager];
// [manager createDirectoryAtPath:outputURL withIntermediateDirectories:YES attributes:nil error:nil];
// outputURL = [outputURL stringByAppendingPathComponent:@"output.mp4"];
NSString *outputURL = [NSString stringWithFormat:@"/tmp/%@.mp4", [info objectAtIndex:2]];
NSLog(@"OUTPUT: %@", outputURL);
// Remove Existing File
// [manager removeItemAtPath:outputURL error:nil];

if (![manager fileExistsAtPath:outputURL]) {
    exportSession.outputURL = [NSURL fileURLWithPath:outputURL];
    exportSession.shouldOptimizeForNetworkUse = YES;
    exportSession.outputFileType = AVFileTypeQuickTimeMovie;
    CMTime start = kCMTimeZero;
    CMTime duration = kCMTimeIndefinite;
    if ([[NSString stringWithFormat:@"%@", [info objectAtIndex:3]] floatValue] > 20.0) {
        start = CMTimeMakeWithSeconds(1.0, 600);
        duration = CMTimeMakeWithSeconds(10.0, 600);
    }

    CMTimeRange range = CMTimeRangeMake(start, duration);
    exportSession.timeRange = range;
    [exportSession exportAsynchronouslyWithCompletionHandler:^(void) {
        switch (exportSession.status) {
            case AVAssetExportSessionStatusCompleted:
                NSLog(@"Export Complete %d %@", exportSession.status, exportSession.error);
                [self sendVideoPreview:info];
                break;
            case AVAssetExportSessionStatusFailed:
                NSLog(@"Failed:%@",exportSession.error);
                // [self addToDelayed:info withAction:@"add"];
                break;
            case AVAssetExportSessionStatusCancelled:
                NSLog(@"Canceled:%@",exportSession.error);
                // [self addToDelayed:info withAction:@"add"];
                break;
            default:
                break;
        }
    }];
} else {
    [self sendVideoPreview:info];
}

Upvotes: 1

Related Questions