这个问题与
AVMutableComposition – Blank/Black frame between videos assets非常相关,但由于我没有使用AVAssetExportSession,答案不适合我的问题.
我正在使用AVMutableComposition来创建视频合成,我正在使用AVAssetReader读取它(我需要拥有帧数据,我不能使用AVPlayer)但我的视频块之间经常有黑框(有音频中没有明显的故障).
我创作我的作品
AVMutableCompositionTrack *compositionVideoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid]; AVMutableCompositionTrack *compositionAudioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid]; NSMutableArray* durationList = [NSMutableArray array]; NSMutableArray* videoList= [NSMutableArray array]; NSMutableArray* audioList= [NSMutableArray array]; for (NSInteger i = 0; i < [clips count]; i++) { AVURLAsset *myasset = [clips objectAtIndex:i]; AVAssetTrack *clipVideoTrack = [[myasset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]; [videoList addObject:clipVideoTrack]; AVAssetTrack *clipAudioTrack = [[myasset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]; [audioList addObject:clipAudioTrack]; CMTime clipDuration = [myasset duration]; CMTimeRange clipRange = CMTimeRangeMake(kCMTimeZero,clipDuration); [durationList addObject:[NSValue valueWithCMTimeRange:clipRange]]; } [compositionVideoTrack insertTimeRanges:durationList ofTracks:videoList atTime:kCMTimeZero error:nil]; [compositionAudioTrack insertTimeRanges:durationList ofTracks:audioList atTime:kCMTimeZero error:nil];
我也尝试在我的作品中手动插入每首曲目,但我有同样的现象.
谢谢
解决方法
经过几个小时的测试,我设法解决了这个问题.我需要改变两件事.
1)在创建资产时添加“精确”选项.
1)在创建资产时添加“精确”选项.
NSDictionary *options = @{AVURLAssetPreferPreciseDurationAndTimingKey:@YES}; AVURLAsset *videoAsset3 = [AVURLAsset URLAssetWithURL:clip3URL options:options];
2)不要使用
CMTime duration3 = [videoAsset3 duration];
改用
AVAssetTrack *videoAsset3Track = [[videoAsset3 tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]; CMTime duration3 = videoAsset3Track.timeRange.duration;
在我将AVPlayer背景颜色设置为蓝色后,我发现了这一点,然后我注意到蓝色框架出现,所以问题与时间有关.一旦我更改为上述设置,使用时不同的视频就可以正常对齐:
AVMutableCompositionTrack *videoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid]; [videoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero,duration3) ofTrack:videoAsset3Track atTime:kCMTimeZero error:&error];