9

这个问题与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];

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

谢谢

4

3 回答 3

25

I managed to solve this problem after many hours of testing. There are two things I needed to change. 1) add the 'precise' option when creating the asset.

 NSDictionary *options = @{AVURLAssetPreferPreciseDurationAndTimingKey:@YES};
 AVURLAsset *videoAsset3 = [AVURLAsset URLAssetWithURL:clip3URL options:options];

2) don't use

CMTime duration3 = [videoAsset3 duration];

use instead

AVAssetTrack *videoAsset3Track = [[videoAsset3 tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
CMTime duration3 = videoAsset3Track.timeRange.duration;

I found this out after I set the AVPlayer background color to Blue, then I noticed blue frames appearing, so the problem was to do with timings. Once I changed to above settings, the different videos aligned up fine when using:

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

[videoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, duration3)
                        ofTrack:videoAsset3Track
                         atTime:kCMTimeZero error:&error];
于 2013-10-10T12:31:21.720 回答
0

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

于 2012-08-20T12:27:43.493 回答
0

计算 Last对于新 的我有同样的问题并解决它CMTime非常重要:insertTimeRange:AVAsset

 func addChunk(media: AVAsset) throws {
    let duration = self.mutableComposition.tracks.last?.timeRange.end
    try mutableComposition.insertTimeRange(CMTimeRange(start: .zero, duration: media.duration), of: media, at: duration ?? .zero)
}
于 2020-10-28T14:37:16.613 回答