raman
raman

Reputation: 73

Concatenating two videos in ios

I am trying to merge two videos, but it always throws this exception:

-[NSURL tracksWithMediaType:]: unrecognized selector sent to instance 0x935cf10

2012-08-09 16:26:59.492 videoTest[3920:17903] Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSURL tracksWithMediaType:]:

Here is the code:

AVMutableComposition *mixComposition = [AVMutableComposition composition];
AVMutableCompositionTrack *compositionTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
NSError * error = nil;
NSMutableArray * timeRanges = [NSMutableArray arrayWithCapacity:videoClipPaths.count];
NSMutableArray * tracks = [NSMutableArray arrayWithCapacity:videoClipPaths.count];
for (int i=0; i<[videoClipPaths count]; i++) {
    AVURLAsset *assetClip = [videoClipPaths objectAtIndex:i];
    AVAssetTrack *clipVideoTrackB = [[assetClip tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
        
    [timeRanges addObject:[NSValue valueWithCMTimeRange:CMTimeRangeMake(kCMTimeZero, assetClip.duration)]];
    [tracks addObject:clipVideoTrackB];
}
[compositionTrack insertTimeRanges:timeRanges ofTracks:tracks atTime:kCMTimeZero error:&error];
    
AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPreset1280x720];
NSParameterAssert(exporter != nil);
NSArray *t;
NSString *u;
    
t = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
u = [t objectAtIndex:0];
NSString *finalPath = [u stringByAppendingPathComponent:@"final.mov"];
NSURL *lastURL = [NSURL fileURLWithPath:finalPath];
exporter.outputFileType = AVFileTypeQuickTimeMovie;
exporter.outputURL = lastURL;
[exporter exportAsynchronouslyWithCompletionHandler:^(void){
    switch (exporter.status) {
        case AVAssetExportSessionStatusFailed:
            NSLog(@"exporting failed");
            break;
        case AVAssetExportSessionStatusCompleted:
            NSLog(@"exporting completed");
            //UISaveVideoAtPathToSavedPhotosAlbum(filePath, self, nil, NULL);
            break;
        case AVAssetExportSessionStatusCancelled:
            NSLog(@"export cancelled");
            break;
    }
}]; 

Upvotes: 4

Views: 2611

Answers (2)

raman
raman

Reputation: 73

I fixed it by replacing this code:

AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPreset1280x720]; NSParameterAssert(exporter != nil);

with:

AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetHighestQuality]; 

Upvotes: 3

Dustin
Dustin

Reputation: 6803

assetClip is an AVURLAsset. But it looks like you're assigning a NSURL object to it. Then you call tracksWithMediaType on it, which is a method NSURL doesn't have. That's why you're getting "unrecognized selector".

I'm not very familiar with this particular family of classes, but it might fix your problem if you replace

    AVURLAsset *assetClip = [videoClipPaths objectAtIndex:i];

with

    AVURLAsset *assetClip = [AVURLAsset URLAssetWithURL:[videoClipPaths objectAtIndex:i] options:nil];

Upvotes: 1

Related Questions