Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customizing subtitles with AVPlayer

I was able to display a subtitle track with AVPlayer on iOS 6, but I am not able to customize it. It just shows the same style (a small font size, in white).

Here's how I select the subtitle:

AVMediaSelectionGroup *subtitle = [asset mediaSelectionGroupForMediaCharacteristic: AVMediaCharacteristicLegible];
[self.videoPlayer.currentItem selectMediaOption:subtitle.options[0] inMediaSelectionGroup: subtitle];

And how I'm trying to customize the subtitle:

AVTextStyleRule *rule = [[AVTextStyleRule alloc] initWithTextMarkupAttributes:@{
                         (id)kCMTextMarkupAttribute_ForegroundColorARGB : @[ @1, @1, @0, @0 ],
                         (id) kCMTextMarkupAttribute_ItalicStyle : @(YES)}];

self.videoPlayer.currentItem.textStyleRules = @[rule];

No matter if I put this snippet before or after selecting the subtitle, the result is the same.

The AVPlayer is created with a local (file) URL (a mp4 file).

Any thoughts on how to do this?

like image 615
Marcelo Fabri Avatar asked Aug 07 '13 20:08

Marcelo Fabri


2 Answers

I asked this question on Apple Developer Forums and I got an answer from an Apple employee:

The textStyleRules property only applies to WebVTT content. Your local file is probably carrying subtitles in TX3G format.

You're right that the documentation doesn't mention this limitation, so you should file a bug so we can get our documentation updated.

So, I'll open a radar to ask that they update the docs and I'll post its number here if someone wants to dupe it.

EDIT:

I created rdar://14923673 to ask Apple to update the docs about this current limitation. I also created rdar://14923755 to ask them to provide support to subtitles in TX3G format.

Please dupe them if you're affected by this issue.

like image 81
Marcelo Fabri Avatar answered Oct 19 '22 04:10

Marcelo Fabri


I found workaround to modify text foreground color and background correctly. Just separate styles to multiple AVTextStyleRule.

func initSubtitleStyle() {
    let textStyle:AVTextStyleRule = AVTextStyleRule(textMarkupAttributes: [
        kCMTextMarkupAttribute_CharacterBackgroundColorARGB as String: [0.2,0.3,0.0,0.3]
        ])!


    let textStyle1:AVTextStyleRule = AVTextStyleRule(textMarkupAttributes: [
        kCMTextMarkupAttribute_ForegroundColorARGB as String: [0.2,0.8,0.4,0.0]
        ])!

    let textStyle2:AVTextStyleRule = AVTextStyleRule(textMarkupAttributes: [
        kCMTextMarkupAttribute_BaseFontSizePercentageRelativeToVideoHeight as String: 20,
        kCMTextMarkupAttribute_CharacterEdgeStyle as String: kCMTextMarkupCharacterEdgeStyle_None
        ])!

    player.currentItem?.textStyleRules = [textStyle, textStyle1, textStyle2]
}

please do not ask me why, that solution come from try and error XD

like image 5
Hsiao-Ting Avatar answered Oct 19 '22 04:10

Hsiao-Ting