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?
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?
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.)
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