AVMutableComposition中的黑框

chu*_*hub 7 avfoundation ios avmutablecomposition

这个问题与AVMutableComposition - 视频资产之间的空白/黑框相关,但由于我没有使用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];
Run Code Online (Sandbox Code Playgroud)

我也尝试在我的作品中手动插入每个音轨,但我有同样的现象.

谢谢

elp*_*prl 22

经过数小时的测试,我设法解决了这个问题.我需要改变两件事.1)在创建资产时添加"精确"选项.

 NSDictionary *options = @{AVURLAssetPreferPreciseDurationAndTimingKey:@YES};
 AVURLAsset *videoAsset3 = [AVURLAsset URLAssetWithURL:clip3URL options:options];
Run Code Online (Sandbox Code Playgroud)

2)不要使用

CMTime duration3 = [videoAsset3 duration];
Run Code Online (Sandbox Code Playgroud)

改用

AVAssetTrack *videoAsset3Track = [[videoAsset3 tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
CMTime duration3 = videoAsset3Track.timeRange.duration;
Run Code Online (Sandbox Code Playgroud)

在我将AVPlayer背景颜色设置为蓝色后,我发现了这一点,然后我注意到蓝色框架出现,所以问题与时间有关.一旦我更改为上述设置,使用时不同的视频就可以正常对齐:

 AVMutableCompositionTrack *videoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo
                                                                        preferredTrackID:kCMPersistentTrackID_Invalid];

[videoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, duration3)
                        ofTrack:videoAsset3Track
                         atTime:kCMTimeZero error:&error];
Run Code Online (Sandbox Code Playgroud)

  • 谢谢@elprl ..这很完美 (3认同)

Mik*_*ael 0

尝试创建两个视频轨道,然后在添加剪辑时在两个视频轨道之间交替。