2016-09-04 24 views
11

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.

+0

bạn có thể hiển thị thực hiện của bạn của 'addAsset: toComposition:' và 'setPlayerLayer:'? –

+0

video của bạn là gì 'backVideoTrack.naturalSize'? –

+0

backVideoTrack, naturalSize là 1920 x 1080 –

Trả lời

-1

Một cách để đạt được điều đó là sử dụng 2 khác nhau AVPlayer s và một a blur overlay view: backgroundLayer ->bluer overlay view ->frontLayer. Bạn chỉ cần đảm bảo cả hai người chơi đều bắt đầu và dừng lại cùng một lúc.

Một tùy chọn khác đang sử dụng 1 AVPlayertime observer. Trích xuất hình ảnh hiện tại của frontLayer trên mọi khung hình, làm mờ nó và hiển thị trong một backgroundLayer. Có thể tìm thấy hàm mờ trong cùng một liên kết mà tôi đã cung cấp ở trên.

+0

Hãy hiểu câu hỏi. Anh ta muốn thực hiện nó bằng cách sử dụng 'AVMutablecomposition'. –

+0

Bạn có hiểu câu hỏi của mình không? Anh ta yêu cầu bất cứ đề nghị nào có thể giúp đỡ. Và anh cũng không đề cập đến việc anh chỉ muốn sử dụng 'AVMutablecomposition'. – sahara108

0

Trước khi thêm lớp video thứ hai đó là trên trung tâm của màn hình, thêm mã này

UIVisualEffect *blurEffect; 
blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];//Change the effect which you want. 

UIVisualEffectView *visualEffectView; 
visualEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect]; 

visualEffectView.frame = self.view.bounds; 
[self.view addSubview:visualEffectView]; 

Trong Swift

let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Light) //Change the style which suites you 
    let blurEffectView = UIVisualEffectView(effect: blurEffect) 
    blurEffectView.frame = view.bounds 
    blurEffectView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight] // for supporting device rotation 
    view.addSubview(blurEffectView) 
+0

Tôi muốn phát bằng các bản nhạc video. Sẽ có nhiều video 5 bài hát. Nhòe động sẽ thay đổi theo thời gian chạy cho nhiều bản nhạc và cùng một thứ tôi muốn xuất ở cuối video dưới dạng video. vì vậy giải pháp của bạn chỉ dành cho việc xử lý giao diện người dùng mờ không dành cho chỉnh sửa video thời gian thực. –

+0

Bạn cần phải kiểm tra và thay đổi hiệu ứng mờ khi bạn muốn, Làm mờ động sẽ được thực hiện bằng các thuật toán của riêng bạn để tạo hiệu ứng hình ảnh. Xem liên kết video bạn đăng lên youtube có hiệu ứng mờ đơn giản. Bạn muốn có Dynamic blur, thêm thuật toán. – gurmandeep

Các vấn đề liên quan