4

我正在尝试合并两个视频,但它总是抛出这个异常:

-[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:]:

这是代码:

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;
    }
}]; 
4

2 回答 2

3

我通过替换此代码来修复它:

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

和:

AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetHighestQuality]; 
于 2012-08-10T07:37:44.273 回答
1

assetClip是一个AVURLAsset。但看起来你正在NSURL为它分配一个对象。然后你调用tracksWithMediaType它,这是一个方法NSURL没有。这就是你得到“无法识别的选择器”的原因。

我对这个特定的类不是很熟悉,但如果你替换它可能会解决你的问题

    AVURLAsset *assetClip = [videoClipPaths objectAtIndex:i];

    AVURLAsset *assetClip = [AVURLAsset URLAssetWithURL:[videoClipPaths objectAtIndex:i] options:nil];
于 2012-08-09T12:19:16.307 回答