2

The YouTube iFrame API's captions functionality does not work for auto-generated captions.

Is there a plan to address this flaw or is there another way to enable auto-generated captions within the iFrame API?

teeger
  • 31
  • 1
  • Same need here, this doesnt work: didnt work, https://www.youtube.com/embed/VIDEOID?cc_load_policy=1&cc_lang_pref=en&autohide=1&controls=1&loop=0&rel=0&fs=1&wmode=opaque&showinfo=1&modestbranding=1&iv_load_policy=1&theme=light&color=red&playsinline=0&enablejsapi=1&origin=http%3A%2F%2F2017.public.onecnc.proj&widgetid=1&yt:cc=on – Daniel Brose Jun 22 '18 at 04:55

2 Answers2

3

As always, there is a way.

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 tag = document.createElement('script')
tag.src = 'https://www.youtube.com/iframe_api'
const firstScriptTag = document.getElementsByTagName('script')[0]
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag)

let player

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

const onReady = _ => {   
  player.playVideo()
}

function onYouTubePlayerAPIReady() {
  player = new YT.Player('player', {
    height: '360',
    width: '640',
    videoId: 'q2C0EO0zzAY',
    playerVars: {
      cc_load_policy: 1
    },
    events: {
      onReady,
      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.)

blumi
  • 190
  • 2
  • 6
-1

I have the same need - more and more videos are being posted with Auto cptions and it would be good if they can also be supported in the Iframe