gpt4 book ai didi

ios - 使用 AVMutableVideoCompositionLayerInstruction 修改 Track,尺寸和位置偏差

转载 作者:行者123 更新时间:2023-12-01 15:47:55 24 4
gpt4 key购买 nike

设置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 首先跟踪 Assets 和绿色矩形计算大小,移动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];
}

最佳答案

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

感谢上帝。 ^~^

关于ios - 使用 AVMutableVideoCompositionLayerInstruction 修改 Track,尺寸和位置偏差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34738063/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com