Black frames in AVMutableComposition

This question is quite related to AVMutableComposition - Blank / Black frame between video objects , but since I do not use AVAssetExportSession, the answers do not correspond to my problem.

I use AVMutableComposition to create a video composition, and I read it using AVAssetReader (I need to have frame data, I cannot use AVPlayer), but I often have black frames between my pieces of video (there is no glitch in the sound).

I create my composition as

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]; 

I also tried to insert each track manually into my composition, but I have the same phenomenon.

thanks

+7
source share
2 answers

I managed to solve this problem after many hours of testing. I need to change two things. 1) add the “exact” option when creating the asset.

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

2) do not use

 CMTime duration3 = [videoAsset3 duration]; 

use

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

I found out about this after setting the background color of AVPlayer to blue, then I noticed the appearance of blue frames, so the problem is related to timing. After I changed the settings above, the various videos aligned when using:

  AVMutableCompositionTrack *videoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid]; [videoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, duration3) ofTrack:videoAsset3Track atTime:kCMTimeZero error:&error]; 
+19
source

Try creating two video tracks, and then alternate between the two when adding clips.

0
source

All Articles