Trong ứng dụng của tôi, tôi đã sửa kích thước hiển thị thành phần là 1280 x 720. Vì vậy, nếu sẽ nhập bất kỳ video chân dung nào thì tôi phải hiển thị nền mờ với tô màu và khung cạnh của video ở giữa. Tương tự như thế này:Hình nền mờ trong Video
https://www.youtube.com/watch?v=yCOrqUA0ws4
tôi đã đạt được để chơi cả video sử dụng AVMtableComposition
, nhưng tôi không biết làm thế nào để làm mờ một ca khúc nền cụ thể. Tôi đã sau trong mã của tôi:
self.composition = [AVMutableComposition composition];
AVAsset *firstAsset = [AVAsset assetWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"ScreenFlow_Blend" ofType:@"mp4"]]];
[self addAsset:firstAsset toComposition:self.composition withTrackID:1];
[self addAsset:firstAsset toComposition:self.composition withTrackID:2];
// [self addAsset:ThirdAsset toComposition:self.composition withTrackID:3];
AVAssetTrack *backVideoTrack = [firstAsset tracksWithMediaType:AVMediaTypeVideo][0];;
self.videoComposition = [AVMutableVideoComposition videoComposition];
self.videoComposition.renderSize = CGSizeMake(1280, 720);
self.videoComposition.frameDuration = CMTimeMake(1, 30);
AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
instruction.timeRange = [backVideoTrack timeRange];
CGFloat scale = 1280/backVideoTrack.naturalSize.width;
CGAffineTransform t = CGAffineTransformMakeScale(scale, scale);
t = CGAffineTransformTranslate(t, 0, -backVideoTrack.naturalSize.height/2 + self.videoComposition.renderSize.height/2);
AVMutableVideoCompositionLayerInstruction *frontLayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstruction];
frontLayerInstruction.trackID = 1;
[frontLayerInstruction setTransform:t atTime:kCMTimeZero];
CGFloat scaleSmall = 720/backVideoTrack.naturalSize.height;
CGAffineTransform translate = CGAffineTransformMakeTranslation(self.videoComposition.renderSize.width/2 - ((backVideoTrack.naturalSize.width/2)*scaleSmall),0);
CGAffineTransform scaleTransform = CGAffineTransformMakeScale(scaleSmall,scaleSmall);
CGAffineTransform finalTransform = CGAffineTransformConcat(scaleTransform, translate);
CGAffineTransform t1 = CGAffineTransformMakeScale(scaleSmall,scaleSmall);
t1 = CGAffineTransformTranslate(t1,1280, 0);
AVMutableVideoCompositionLayerInstruction *backLayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstruction];
backLayerInstruction.trackID = 2;
[backLayerInstruction setTransform:finalTransform atTime:kCMTimeZero];
// AVMutableVideoCompositionLayerInstruction *maskLayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstruction];
// maskLayerInstruction.trackID = 3;
// [maskLayerInstruction setTransform:t atTime:kCMTimeZero];
instruction.layerInstructions = @[backLayerInstruction,frontLayerInstruction];
self.videoComposition.instructions = @[ instruction ];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:self.composition];
playerItem.videoComposition = self.videoComposition;
self.player = [AVPlayer playerWithPlayerItem:playerItem];
AVPlayerLayer *newPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:[self player]];
[newPlayerLayer setFrame:[[[self playerView] layer] bounds]];
// [newPlayerLayer setHidden:YES];
[[[self playerView] layer] addSublayer:newPlayerLayer];
[self setPlayerLayer:newPlayerLayer];
Sử dụng mã ở trên tôi có thể đạt được điều này:
https://drive.google.com/open?id=0B2jCvCt5fosyOVNOcGZ1MU1laEU
tôi biết về lớp customVideoCompositor
để lọc khung thành phần. Tôi đã thử nó nhưng nếu tôi sử dụng customVideoCompositor
sau đó tôi mất sự biến đổi của tôi trên các lớp thành phần. Ngoài ra, từ customVideoCompositor
Tôi không biết cách lọc id theo dõi cụ thể.
Nếu ai đó có bất kỳ liên kết hoặc đề xuất tài liệu nào thì điều đó thực sự đánh giá cao trong tương lai.
bạn có thể hiển thị thực hiện của bạn của 'addAsset: toComposition:' và 'setPlayerLayer:'? –
video của bạn là gì 'backVideoTrack.naturalSize'? –
backVideoTrack, naturalSize là 1920 x 1080 –