使用AVMutableVideoCompositionLayerInstruction修改跟踪,大小和位置偏差

时间:2016-01-12 07:45:21

标签: ios objective-c video avfoundation avmutablecomposition

设置Transform后,我使用AVMutableVideoCompositionLayerInstruction修改大小和位置。 “iPhone 5”可能正常,但“iPhone6”显示屏有偏差。经过测试,总是偏移约30左右。就像上面的图片一样,它应该显示在矩形中有一个绿色边框。下面是我的代码,我想知道这是怎么回事?

我的英语非常糟糕,也许我没有明确说明问题,对不起。谢谢。

Biased screenshot.

    ----------

要做好准备工作,请初始化compositionvideoComposition

_composition = [[AVMutableComposition composition] init];
_audioMix = [[AVMutableAudioMix alloc] init];
_videoComposition = [[AVMutableVideoComposition alloc] init];
_videoComposition.frameDuration = CMTimeMake(1, 30);
_videoComposition.renderSize = CGSizeMake(320, 320);
_videoComposition.renderScale = 1.0;

制作视频片段,用它来合成新视频:

-(void)initializationMutableTrackWithComposition:(AVMutableComposition *)composition {
self.mutableTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];

CMTimeRange timeRange = CMTimeRangeMake(CMTimeMake(1, 1), self.asset.duration);
AVAssetTrack * resourceTrack = [[self.asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];

BOOL  insertedResult =  [self.mutableTrack insertTimeRange:timeRange
                                                    ofTrack:resourceTrack
                                                     atTime:kCMTimeZero
                                                      error:nil];
self.mutableTrack.preferredTransform = CGAffineTransformIdentity;
NSLog(@"插入结果是------>%@",insertedResult ? @"成功" : @"失败");
}

创建AVMutableVideoCompositionLayerInstruction,缩放为naturalSize第一个跟踪资源和绿色矩形来计算大小,移动x和y的绿色矩形。

-(void)initializationVideoLayer {
    AVMutableVideoCompositionLayerInstruction * layer = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:self.mutableTrack];

    CGAffineTransform preferredTransform = CGAffineTransformConcat(self.elementAttribute.scale, self.elementAttribute.move);

    [layer setTransform:preferredTransform atTime:kCMTimeZero];

    //设置 Video 透明度
    //[layer setOpacity:0.1f atTime:kCMTimeZero];

    self.videoLayer = layer;
}

下一步是分配layerInstruction AVMutableVideoCompositionInstruction并创建AVPlayerItem并播放。

-(void)refreshVideoConpositiom:(NSNotification *) notify{
    _compositionInstruction.timeRange = CMTimeRangeMake(kCMTimeZero,CMTimeMake(16, 1));

    NSMutableArray * layerInstructions = [NSMutableArray array];
    NSMutableArray * audioParameters = [NSMutableArray array];

    for (CompositionLayerInstruction *layer in NSArray){
        [layerInstructions addObject:layer];
    }

    self.compositionInstruction.layerInstructions = [layerInstructions copy];

    for (AVMutableAudioMixInputParameters * audioParameter in NSArray){
        [audioParameter addObject:audioParameter];
    }

    self.audioMix.inputParameters = audioParameters;
    [self makePlayerItem];
}

-(void)makePlayerItem{
    self.videoComposition.instructions = @[[self.compositionInstruction]];

    //Create playerItem.
    AVPlayerItem * playerItem =  [AVPlayerItem playerItemWithAsset:self.composition];
    playerItem.videoComposition = self.videoComposition;
    playerItem.audioMix = self.audioMix;

    //The result callback to display layer.
    if ([NSThread isMainThread])
        [self callBackPlayerItemToDelegateWithPlayerItem:playerItem];
    else
        [self performSelector:@selector(callBackPlayerItemToDelegateWithPlayerItem:)
                         onThread:[NSThread mainThread]
                       withObject:playerItem
                    waitUntilDone:NO];
}  

1 个答案:

答案 0 :(得分:0)

我之所以发现这个问题,是因为renderSize属性大小和计算VideoCompostion大小的缩放和移动是不一致的,导致应用程序中的Transform偏差。我猜,是使用默认大小AVMutableVideoCompositionLayerInstruction的VideoCompostion的renderSize,所以如果renderSize不一致计算大小和比例,就会出现偏差。

感谢上帝。 ^〜^