Nelson Tucker
Nelson Tucker

Reputation: 21

-[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array' while using insertTimeRange in AVMutableComposition

Following is my code

firstAsset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:_strThumbForegroundVideo] options:nil];

AVMutableComposition* mixComposition = [[AVMutableComposition alloc] init];
AVMutableCompositionTrack *firstTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];

[firstTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, firstAsset.duration) ofTrack:[[firstAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:kCMTimeZero error:nil];

In logs:

Value for _strThumbForegroundVideo is :

/private/var/mobile/Containers/Data/Application/80C71B40-87DA-449F-832C-1D27722727D8/tmp/90219F63-073E-4586-A1F6-DD68B627C3D1.mov

Value for firstAsset is :

AVURLAsset: 0x1702362c0, URL = file:///private/var/mobile/Containers/Data/Application/80C71B40-87DA-449F-832C-1D27722727D8/tmp/90219F63-073E-4586-A1F6-DD68B627C3D1.mov

Value of firstTrack is :

AVMutableCompositionTrack: 0x174033b20 trackID = 1, mediaType = vide, editCount = 0

While doing "insertTimeRange" I get this error,

-[__NSArrayM objectAtIndex:]; // index 0 beyond bounds for empty array' while using insertTimeRange in AVMutableComposition

I am trying to merge 2 videos using tutorial on this link. The only difference is that in this tutorial, they have imported video from Photos and I have been converting recently recorded video's path into AVAsset and then getting the same functionality done. The video I record can be found in gallery and with the same path that I have.

Here, I need video assets anyhow as it is not optional. Hence, Can not use something like

NSArray *dataSourceArray = [NSArray arrayWithArray: [firstAsset tracksWithMediaType:AVMediaTypeVideo]];
[firstTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, firstAsset.duration)
                                                                   ofTrack:([dataSourceArray count]>0)?[dataSourceArray objectAtIndex:0]:nil
                                                                    atTime:kCMTimeZero
                                                                     error:nil]; 

The implementation is done in Xcode 8.1

Upvotes: 0

Views: 602

Answers (1)

Dhiru
Dhiru

Reputation: 3060

Use the Below code , This may work for you

NSURL *videoURL = [NSURL fileURLWithPath:path]  // Don't use URLWithString if any one using .

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

AVMutableCompositionTrack *firstTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo
                                                                            preferredTrackID:kCMPersistentTrackID_Invalid];
[firstTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, firstAsset.duration)
                        ofTrack:[[firstAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:kCMTimeZero error:nil];

Please check before you insertTrack if your asset is containing That type of media tracks , here i am assuming that My assets contains atleast one video type media track AVMediaTypeVideo .

if you are not sure than , check how many tracks of containing your assets. use below code

NSArray *videoTracks = [NSArray arrayWithArray: [secondAsset tracksWithMediaType:AVMediaTypeVideo]];
        NSLog(@"videoTracks count => %ld",[videoTracks count]);

        [firstTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, firstAsset.duration)
                            ofTrack:([videoTracks count]>0)?[videoTracks objectAtIndex:0]:nil
                             atTime:kCMTimeZero
                              error:nil];

Upvotes: 1

Related Questions