CIFilters和AVVideoCompositionInstructions

时间:2016-10-12 14:37:43

标签: ios objective-c swift avfoundation

在我的应用中,我必须在播放时应用过滤器。我这样做是通过使用AVVideoComposition并在每个帧上应用过滤器。

由于某些原因,当我更改AVPlayerItem的videoComposition时,视频会旋转。我试图通过使用转换来修复此问题并将其添加到videoComposition指令中。旋转问题已修复,但过滤器不再应用。

如何在应用过滤器的情况下修复旋转?

以下代码:

- (void)setVideoComposition:(AVMutableVideoComposition *)videoComposition {
_videoComposition = videoComposition;

AVMutableComposition *composition = [AVMutableComposition composition];
AVMutableCompositionTrack *videoCompositionTrack =
    [composition addMutableTrackWithMediaType:AVMediaTypeVideo
        preferredTrackID:kCMPersistentTrackID_Invalid];

AVAssetTrack *videoAssetTrack = [_avAsset tracksWithMediaType:AVMediaTypeVideo].lastObject;

CMTimeRange range = CMTimeRangeMake(kCMTimeZero, videoAssetTrack.timeRange.duration);

[videoCompositionTrack insertTimeRange:range
    ofTrack:videoAssetTrack
    atTime:kCMTimeZero
    error:nil];

AVMutableVideoCompositionInstruction *instruction =
    [AVMutableVideoCompositionInstruction videoCompositionInstruction];
instruction.timeRange = range;

AVMutableVideoCompositionLayerInstruction *layerInstruction =
    [AVMutableVideoCompositionLayerInstruction
        videoCompositionLayerInstructionWithAssetTrack:videoCompositionTrack];

[layerInstruction setTransform:videoAssetTrack.preferredTransform
    atTime:kCMTimeZero];



NSMutableArray *instructions = [[NSMutableArray alloc] init];
for (id instr in _videoComposition.instructions) {
    [instructions addObject:instr];
}

[instructions insertObject:instruction atIndex:0];

_videoComposition.instructions = instructions;

// Hook it to the item player.
if (self.playerItem) {
    self.playerItem.videoComposition = _videoComposition;
}

0 个答案:

没有答案