以下是我的代码
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];
在日志中:
_strThumbForegroundVideo 的值为:
/private/var/mobile/Containers/Data/Application/80C71B40-87DA-449F-832C-1D27722727D8/tmp/90219F63-073E-4586-A1F6-DD68B627C3D1.mov
firstAsset 的值为:
AVURLAsset: 0x1702362c0, URL = file:///private/var/mobile/Containers/Data/Application/80C71B40-87DA-449F-832C-1D27722727D8/tmp/90219F63-073E-4586-A1F6-DD68B627C3D1.mov
firstTrack 的值为:
AVMutableCompositionTrack: 0x174033b20 trackID = 1, mediaType = vide, editCount = 0
在执行“insertTimeRange”时出现此错误,
-[__NSArrayM objectAtIndex:]; // index 0 beyond bounds for empty array' while using insertTimeRange in AVMutableComposition
我正在尝试使用此链接上的教程合并 2 个视频。唯一的区别是,在本教程中,他们从照片中导入了视频,我一直在将最近录制的视频路径转换为 AVAsset,然后完成相同的功能。我录制的视频可以在图库中找到,路径与我相同。
在这里,无论如何我都需要视频资产,因为它不是可选的。因此,不能使用类似的东西
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];
实现在 Xcode 8.1 中完成