2016-09-04 13 views
11

W mojej aplikacji mam rozmiar renderowania kompozycji 1280 x 720. Jeśli więc zaimportuję dowolny portret wideo, to muszę pokazać tło rozmycia z wypełnieniem i ramką obrazu w środku. Tak samo jak to:AVfoundation rozmycie tła w Wideo

https://www.youtube.com/watch?v=yCOrqUA0ws4

osiągnąłem grać zarówno wideo za pomocą AVMtableComposition, ale nie wiem jak rozmycie tła konkretny utwór. Ja robiłem następujących w moim kodu:

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

Stosując powyższy kod można to osiągnąć:

https://drive.google.com/open?id=0B2jCvCt5fosyOVNOcGZ1MU1laEU

wiem o klasie customVideoCompositor filtrowania ramek składu. Próbowałem, ale jeśli używam customVideoCompositor, tracę moją transformację na warstwach kompozycji. Dodatkowo od customVideoCompositor nie wiem, jak filtrować określony identyfikator ścieżki.

Jeśli ktoś ma jakieś linki lub sugestie do dokumentów, to naprawdę doceniam to w tej sprawie.

+0

można pokazać swoją realizację 'addAsset: toComposition:' a 'setPlayerLayer:'? –

+0

czym jest "backVideoTrack.naturalSize" Twojego filmu? –

+0

backVideoTrack, naturalSize jest 1920 x 1080 –

Odpowiedz

-1

Sposób osiągnąć, który za pomocą 2 różne AVPlayer S i a blur overlay view: backgroundLayer ->bluer overlay view ->frontLayer. Musisz tylko upewnić się, że zarówno gracz rozpoczyna i kończy w tym samym czasie.

Inną opcją jest użycie 1 AVPlayer i time observer. Wyodrębnij bieżący obraz frontLayer w każdej klatce, zamień go i wyświetl w postaci backgroundLayer. Funkcję rozmycia można znaleźć w tym samym łączu, który podałem powyżej.

+0

Proszę zrozumieć pytanie. On chce zrobić to środowisko wykonawcze przy użyciu 'AVMutablecomposition'. –

+0

Czy zrozumiałeś również jego pytanie? Poprosił o jakąkolwiek sugestię, która może pomóc. I nie wspomniał też, że chce używać tylko 'AVMutablecomposition'. – sahara108

0

Przed dodaniem drugą warstwę wideo, który jest na środku ekranu, dodać ten kod

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

w 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

Chcę grać ze ścieżkami wideo. Będzie jak wiele 5 ścieżek wideo. Dynamiczne rozmycie będzie się zmieniać w czasie wykonywania wielu ścieżek i tego samego, co chcę wyeksportować na końcu jako wideo. więc twoje rozwiązanie jest dostępne tylko w przypadku, gdy obsługa interfejsu użytkownika nie jest rozmyta, a nie edycja wideo w czasie rzeczywistym. –

+0

Musisz sprawdzić i zmienić efekt rozmycia, kiedy chcesz, dynamiczne rozmycie zostanie wykonane za pomocą własnych algorytmów, które mieszczą twój efekt wizualny. Zobacz link do linku, który opublikowałeś w serwisie youtube, miał prosty efekt rozmycia. Chcesz rozmycia dynamicznego, dodaj algorytmy. – gurmandeep

Powiązane problemy