1

i am trying to merge.mp4 and .caf file so i am using the following code,

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

but i am getting the error

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array' *** First throw call stack: (0x32c4d2a3 0x3aae397f 0x32b98b75 0x748e7 0x74e61 0x34b400c5 0x34b40077 0x34b40055 0x34b3f90b 0x34b3fe01 0x34a685f1 0x34a55801 0x34a5511b 0x367495a3 0x32c22683 0x32c21ee9 0x32c20cb7 0x32b93ebd 0x32b93d49 0x367482eb 0x34aa9301 0x6f767 0x6f708) libc++abi.dylib: terminate called throwing an exception


all require framework and .dylib file is included.

Greg
  • 9,068
  • 6
  • 49
  • 91
utkal patel
  • 1,321
  • 1
  • 15
  • 24
  • 2
    might be the issue because of [[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]... [videoAsset tracksWithMediaType:AVMediaTypeVideo] may be returning nil. Check – iCoder Jun 12 '13 at 11:54

1 Answers1

2

Print your array count. It must be an empty array that's why your app is getting crashed. Check that. NSlog(@"array count => %d",[videoAsset tracksWithMediaType:AVMediaTypeVideo]);

and replace your code by below code to prevent crashes.

 AVMutableCompositionTrack *compositionVideoTrack = 
    [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo
                                preferredTrackID:kCMPersistentTrackID_Invalid];
    NSArray *dataSourceArray = [NSArray arrayWithArray: [videoAsset tracksWithMediaType:AVMediaTypeVideo];
   NSlog(@"array count => %d",[dataSourceArray count]);
    [compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration) 
                                   ofTrack:([dataSourceArray count]>0)?[dataSourceArray objectAtIndex:0]:nil
                                                                     atTime:kCMTimeZero
                                                                      error:nil]; 
Divyu
  • 1,325
  • 9
  • 21
  • how can i create a video if `dataSourceArray` was empty? – Muruganandham K Dec 26 '13 at 07:34
  • @iTroyd23 Please check your video file whether valid? Its's size is zero? – Hugo Oct 24 '14 at 07:07
  • @Divyu you are Right that this must be an Empty Array, BUT in my case i have VIDEOASSET that containing Audio but dont know why i'm getting Empty Array? IF i make a VIDEO for 3 to 5 seconds then i dont get any Error because Array is not Empty BUT if i create a Video that is more then 5, 6 seconds and this Video also Containing AUDIo then in this case i'm getting EMPTY ARRAY(INDEX OUT OF BOUND) – Ahtazaz Apr 09 '19 at 11:24