Question

In my app I am recording video using the device's camera and AVCaptureSession to save. Long story short, I need to be able to cut out the first x seconds of this recorded clip. I do not want the user to be presented with an 'edit video'-view, and it is not a "fixed" amount of time per say, but at the end of the day, I am left here with a CMTime with a value of exactly how much I have to cut out from the beginning of the clip. I have been looking at AVAssetWriter etc, but no luck. For playback, I guess [player seekToTime:time]; will do, but I need the actual video to be time duration shorter, cut out from the beginning. What method, or where can I get documentation for this?

Was it helpful?

Solution

Have you tried AVMutableComposition? It will have processing time though. Something like:

// get your asset
AVAsset *asset = [AVAsset assetWithURL:yourURL];

// get asset tracks
AVAssetTrack *assetTrackVideo = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];

AVAssetTrack *assetTrackAudio = [[asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];

// create composition
AVMutableComposition *composition = [AVMutableComposition composition];

AVMutableCompositionTrack *trackVideo = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];

AVMutableCompositionTrack *trackAudio = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];

// YOUR_DURATION is something like CMTimeSubstruct(asset.duration, YOURTIME_START);
[trackVideo insertTimeRange:CMTimeRangeMake(YOURTIME_START, YOUR_DURATION)
                    ofTrack:assetTrackVideo
                     atTime:kCMTimeZero
                      error:nil];

[trackAudio insertTimeRange:CMTimeRangeMake(YOURTIME_START, YOUR_DURATION)
                    ofTrack:assetTrackAudio
                     atTime:kCMTimeZero
                      error:nil];

// do the orientation change if needed

NSString* filename = [NSString stringWithFormat:@"videoFileName-%d.mov",arc4random() % 1000];
NSString* path = [NSTemporaryDirectory() stringByAppendingPathComponent:filename];

NSURL *exporterURL = [NSURL fileURLWithPath:path];

// Create exporter
AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:composition presetName:AVAssetExportPresetHighestQuality];
exporter.outputURL = exporterURL;
exporter.outputFileType = AVFileTypeQuickTimeMovie;
exporter.shouldOptimizeForNetworkUse = YES;

[exporter exportAsynchronouslyWithCompletionHandler:^{

        // NSLog(@"Finished Output composition with error '%@' reason '%@'",   exporter.error.localizedDescription,exporter.error.localizedFailureReason);
    }];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top