【问题标题】:Black frames in AVMutableCompositionAVMutableComposition 中的黑框
【发布时间】:2012-02-01 09:19:14
【问题描述】:

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

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

谢谢

【问题讨论】:

  • 你解决过这个问题吗?我遇到了类似的问题。
  • @elprl 我查看了我的代码,它几乎保持不变。我认为我们通过调整(关键帧间隔、帧速率……)视频格式输入(我们可以控制它)来“解决”这个问题。那是很久以前的事了,我不能确定是什么参数改善了这种情况。

标签: ios avfoundation avmutablecomposition


【解决方案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];

【讨论】:

  • 旧但黄金解决方案
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    计算最后一个CMTimeinsertTimeRange: 非常重要,对于新的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)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多