在ios中连接两个视频

时间:2012-08-09 11:20:38

标签: objective-c ios xcode avfoundation

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

  

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

2 个答案:

答案 0 :(得分:3)

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

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

使用:

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

答案 1 :(得分:1)

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

我对这个特定的类系列并不是很熟悉,但如果你替换它可能会修复你的问题

    AVURLAsset *assetClip = [videoClipPaths objectAtIndex:i];

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