quxingzhe
quxingzhe

Reputation: 41

Use AVMutableVideoCompositionLayerInstruction modify Track, size and position deviation

I use AVMutableVideoCompositionLayerInstruction modify the size and position, after setting Transform. The 'iPhone 5' can be normal, but there are deviations at 'iPhone6' display. After testing, always more offset about 30 or so. Like the picture above, it should be displayed in the rectangle has a green border.Below is my code,I wonder if this is how it?

My English is very bad, maybe I did not put a clear description of the problem, I'm sorry. Thank you.

Biased screenshot.

    ----------

To do the preparatory work,initialization composition and videoComposition:

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

Create a video track, use it to synthesize new video:

-(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 ? @"成功" : @"失败");
}

Create AVMutableVideoCompositionLayerInstruction, scale is naturalSize first track asset and green rectangles to calculate the size, move the green rectangle of x and 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;
}

The next step is to assign layerInstruction AVMutableVideoCompositionInstruction and create AVPlayerItem and play.

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

Upvotes: 3

Views: 1219

Answers (1)

quxingzhe
quxingzhe

Reputation: 41

The reason I have found the problem, because renderSize property size and computing VideoCompostion size scaling and movement are inconsistent, leading to Transform deviation in the application. I guess, is to use the default size AVMutableVideoCompositionLayerInstruction VideoCompostion of renderSize, so if renderSize inconsistent calculate the size and scale, there will be deviations.

Thank God. ^~^

Upvotes: 1

Related Questions