2017-02-17 47 views
5

我有持續時間爲4:00的視頻。現在我想根據視頻幀在視頻文件中添加文本。例如,從00:30到1:50,我想添加文字「歡迎」。現在從3點到4點視頻持續時間我想添加文字「真棒」。如何實現這一功能。我在下面提到了教程。它在整個視頻中添加文本,而不是一段視頻的時間。 https://www.raywenderlich.com/30200/avfoundation-tutorial-adding-overlays-and-animations-to-videos如何在iOS SDK中添加一段視頻的文本

任何幫助將appriciated。

我對整個視頻添加的代碼行的附加文本:

- (void)applyVideoEffectsToComposition:(AVMutableVideoComposition *)composition size:(CGSize)size 
{ 
    // 1 - Set up the text layer 
    CATextLayer *subtitle1Text = [[CATextLayer alloc] init]; 
    [subtitle1Text setFont:@"Helvetica-Bold"]; 
    [subtitle1Text setFontSize:36]; 
    [subtitle1Text setFrame:CGRectMake(0, 0, size.width, 100)]; 
    [subtitle1Text setString:_subTitle1.text]; 
    [subtitle1Text setAlignmentMode:kCAAlignmentCenter]; 
    [subtitle1Text setForegroundColor:[[UIColor whiteColor] CGColor]]; 

    // 2 - The usual overlay 
    CALayer *overlayLayer = [CALayer layer]; 
    [overlayLayer addSublayer:subtitle1Text]; 
    overlayLayer.frame = CGRectMake(0, 0, size.width, size.height); 
    [overlayLayer setMasksToBounds:YES]; 

    CALayer *parentLayer = [CALayer layer]; 
    CALayer *videoLayer = [CALayer layer]; 
    parentLayer.frame = CGRectMake(0, 0, size.width, size.height); 
    videoLayer.frame = CGRectMake(0, 0, size.width, size.height); 
    [parentLayer addSublayer:videoLayer]; 
    [parentLayer addSublayer:overlayLayer]; 

    composition.animationTool = [AVVideoCompositionCoreAnimationTool 
           videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer]; 

} 
+0

你可以這樣做,這是可能的,顯示你的代碼 –

+0

我已經嘗試按照上面的示例代碼。 –

+0

你能告訴我怎麼做嗎? –

回答

0

編輯這是否answer爲你工作?您將不得不添加多個文本圖層,並使用CABasicAnimation在適當的時間顯示/隱藏每個圖層(使用setBeginTime:)。

原來的答案 基本上,只要保持這部分代碼在CATextLayer參考,並使用一個NSTimer稱爲每秒更新您的文字:在相關聯的網站

// 1 - Set up the text layer 
CATextLayer *subtitle1Text = [[CATextLayer alloc] init]; 
self.textLayer = subtitle1Text; // ### Keep a reference to this object and update it in timerDidFire 
... 

- (void)viewDidLoad{ 
    ... 
    // Add a timer at some point. Don't forget to invalidate it later 
    NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(timerDidFire) userInfo:nil repeats:YES]; 
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode]; 
    ... 
} 

- (void)timerDidFire{ 
    if (currentPlaybackPosition <= some_value){ 
     [self.textLayer setText:@"Welcome"]; 
    } else if (currentPlaybackPosition <= some_bigger_value){ 
     [self.textLayer setText:@"Awesome"]; 
    } 
    ... 
} 
+0

我必須導出那個視頻 –

+0

@PayalManiyar抱歉,誤解了你。我添加了一個鏈接到我相信做你想要的答案。 – arsenius

+0

我試過了,它不工作。 –

0

看:https://www.raywenderlich.com/30200/avfoundation-tutorial-adding-overlays-and-animations-to-videos 只需使用2個CATextLayer,如圖所示。設置他們的文本。

將第一個beginTime屬性設置爲30秒,並將其duration屬性設置爲80秒。第二個180秒的beginTime和60秒的duration

結果將按照視頻播放器中的顯示導出。

+0

感謝您的回覆。它在給定的時間顯示文本,但在持續時間後它不會消失。如何讓文字消失? –

+0

文本消失總是在我的解決方案的持續時間結束(除非它太短,意味遠遠<1秒)。我不知道爲什麼它不能在你的代碼中工作。 – Jan

+0

你能提供一些代碼行嗎? –

相關問題