cc_load_policy=1 doesn't work with videos that have only auto generated captions
Asked Answered
P

1

7

When I use google's YTPlayer to display a video with cc_load_policy=1, and if the video in question has just a English (auto-generated) subtitle, captions still don't appear for me - shouldn't the English (auto-generated) one still be displayed in such a case?

I've tried setting the cc_lang_pref=en as well as the hl=en parameters but to no avail.

The cc_load_policy=1 parameter does work well with videos that do have an English subtitle, so I don't think the problem has anything to with code per se.

Is there perhaps a special language code for English (auto-generated) that I should be using instead?

new YT.Player("ytplayer_placeholder", {
    width: "100%",
    videoId: "4Uzbpj1UCEY",
    playerVars: {cc_load_policy: 1},
    events: {
        'onReady': player_ready,
        'onStateChange': schedule_buffering,
        'onError': error_handler
    }
});

The sample code above includes the youtube videoid that has such a problem. It should have displayed the video with the English (auto-generated) captions but nothing appears. I can manually click the CC button to get the captions to appear though, but I need that to happen automatically.

Portie answered 14/8, 2019 at 18:14 Comment(0)
N
6

There is no official or documented way to force auto-generated captions in embedded videos. However there is a solution with the setOption method which works now, but there is no guarantee it will work in the future as this is a non documented call of the method:

const onApiChange = _ => {   
  if (typeof player.setOption === 'function') {
    player.setOption('captions', 'track', {languageCode: 'en'}) // undocumented call
  }  
}

function onYouTubePlayerAPIReady() {
  player = new YT.Player('player', {
    height: '360',
    width: '640',
    videoId: '4Uzbpj1UCEY',
    playerVars: {
      cc_load_policy: 1
    },
    events: {
      onApiChange
    }
  })
}

Working jsFiddle is here.

You have to wait for an onApiChange event before using the setOption function. (See: https://developers.google.com/youtube/iframe_api_reference#Events) According to the docs only the 'fontSize' and the 'reload' parameters are supported. However, changing the captions track works too and it turns ON the captions as a side-effect. I tried only the 'en' languageCode, of course this will change to the normal english captions track if there is one available, but will display the auto-generated english captions in the absence of a predefined track.

(You can also query the active captions track with the getOption method, but it will return nothing if the auto-generated captions are used.)

Nealon answered 25/3, 2020 at 6:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.