Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVAsset "tracksWithMediaType" returns an empty array

Tags:

ios

avasset

These is the beginning o a method I am using to merge videos together

-(void) mergeVideosAndAudio:(AVAsset *)audioAsset{

    //Load Video Assets

    NSError *error;
    NSArray *dirFiles;
    if ((dirFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[self documentsDirectory] error:&error]) == nil) {
        // handle the error
    };
    // find all the temp files
    NSArray *movFiles = [dirFiles filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self BEGINSWITH 'temp'"]];
    NSLog(@"The are %i temp files",movFiles.count);

    //Create assets array
    NSMutableArray *assets = [[NSMutableArray alloc]init];

    for (int i = 0; i < movFiles.count; i++) {
        NSString *videoURL = [[self documentsDirectory] stringByAppendingPathComponent:
                              [NSString stringWithFormat:@"temp%i.mov", i]];

        NSURL *url = [NSURL fileURLWithPath:videoURL];

        AVURLAsset *videoAsset = [AVURLAsset URLAssetWithURL:url options:nil];
        [assets addObject:videoAsset];

    }
        NSLog(@"assets:%i ", assets.count);
    // a second way
    for (id obj in assets)
        NSLog(@"obj: %@", obj);

    //Create the composition
    AVMutableComposition *mixComposition = [[AVMutableComposition alloc] init];

    // 1 - Video track
    AVMutableCompositionTrack *firstTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo
                                                                        preferredTrackID:kCMPersistentTrackID_Invalid];
    CMTime videoTrackDuration;
    for (int j = 0; j < assets.count; j++) {
        AVURLAsset *currentAsset = assets[j];
        videoTrackDuration = CMTimeAdd(videoTrackDuration, currentAsset.duration);
        CMTime time;
        if (j == 0) {
            time = kCMTimeZero;

        }else{
            AVURLAsset *previousAsset = assets[j-1];
            time = previousAsset.duration;
        }

        AVAssetTrack *assetTrack = [[currentAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];

        [firstTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, currentAsset.duration) ofTrack:assetTrack atTime:time error:nil];
    }

The problem I am having is that the tracksWithMediaType property of currentAsset is an empty array. Here is the console

enter image description here

Any help will be greatly appreciated. Thanks

like image 294
Kaitis Avatar asked Oct 22 '14 13:10

Kaitis


1 Answers

Have you seen this link?

I'm working on a solution now that KVO's the tracks key:

[item addObserver:self forKeyPath:kTracksKey options:opts context:nil];
like image 185
MagicFlow Avatar answered Nov 08 '22 17:11

MagicFlow