16

i'm embedding youtube videos with subtitles in specific language (Hebrew, in my case). im using:

hl=He&cc_load_policy=1

to show the hebrew subtitles and that works fine.

However, if there are no subs in my language, i would like to see the English one (if there are any) as a default. is there a way to force that?

OritK
  • 554
  • 1
  • 7
  • 16
  • The subtitles can be automatically translated, how can I load the video with the automatic translation of the subtitles in english to another language activated, in my case portuguese !? – digfish Jan 13 '19 at 11:48

4 Answers4

31

You can force the captions and language using cc_load_policy & cc_lang_pref options via

URL:

http://www.youtube.com/embed/M7lc1UVf-VE?cc_load_policy=1&cc_lang_pref=en

API:

var ytPlayer = new YT.Player(
    ...
    playerVars: {
        cc_load_policy: 1,
        cc_lang_pref: 'en'
    },
    ....
});

Credits: https://webapps.stackexchange.com/questions/27669/is-there-any-way-to-force-subtitles-in-a-youtube-video

Community
  • 1
  • 1
i--
  • 4,349
  • 2
  • 27
  • 35
  • Thanks - the cc_lang_pref is new to me. ill check it. – OritK Apr 13 '14 at 03:02
  • Well detailed and the most accurate now (2015-05-18), this answer should be upvoted. And if you want to force the **captions not to show up**, [I found a way to deal with it](http://stackoverflow.com/questions/2287774/how-to-link-youtube-video-without-captions/30302467#30302467) but it's very limited. – Armfoot May 18 '15 at 14:29
  • 4
    It doesn't seem to work any more. Even if it would work, I am looking for a way to start playing with "auto-generated and translated" subtitles. Using your method I wouldn't know how to do that. – user643011 May 08 '19 at 06:58
  • Props for the example URL which is also an overview of the API params – MediaFormat Nov 10 '21 at 19:00
5

I have not found this anywhere in their api docs, but with your youtube player object you should be able to do the following to change it to english captions:

player.setOption("captions", "track", {"languageCode": "en"});  //Works for html5 ignored by AS3
player.setOption("cc", "track", {"languageCode": "en"});  //Works for AS3 ignored by html5

You can also do:

var module;
if (testplayer.getOptions().indexOf("cc") !== -1) {
    module = "cc";
} else if (testplayer.getOptions().indexOf("captions") != -1) {{
    module = "captions";
}
var tracklist = testplayer.getOption(module, "tracklist");

// then iterate through the tracklist to see if "he" or "en" is there.

You have cc_load_policy=1 but you can also turn it on via js by:

player.loadModule("captions");  //Works for html5 ignored by AS3
player.loadModule("cc");  //Works for AS3 ignored by html5

to turn it off:

player.unloadModule("captions");  //Works for html5 ignored by AS3
player.unloadModule("cc");  //Works for AS3 ignored by html5
James Irwin
  • 419
  • 5
  • 4
4

I believe it would be better to just leave out the hl= entirely. (It's not actually one of our officially supported Player parameters.) The subtitles will default to the language preference of the viewer of the video, and my assumption is that it will fall back on English if there are no subtitles in the viewer's preferred language.

Jeff Posnick
  • 53,580
  • 14
  • 141
  • 167
  • thanks,but - when i remove the "hl" tag, i get the English subtitles even if my preferred language is Hebrew and there are Hebrew subtitles available. – OritK Oct 04 '12 at 07:42
  • That sounds like a bug to me, then. Could you use https://code.google.com/p/gdata-issues/issues/entry?template=YouTube%20(Defect%20Report) to open a bug there so that we can track it? And where is your language preference set—I think there's a different location based on whether you have a Google+ account linked to your YouTube account or not. – Jeff Posnick Oct 04 '12 at 17:26
  • Opened - https://code.google.com/p/gdata-issues/issues/detail?id=3046 i referred to the language setting in the bug description – OritK Oct 07 '12 at 08:07
  • In my case, I had 2 languages in my language settings. One preferred language and one secondary language. Every time a youtube video was embedded on a site, it would show up in my secondary language. And everytime a logged in to a Google service, the UI would be in my secondary language. I had to remove my secondary language from the language settings, that stopped this problem. – 17xande Oct 06 '22 at 08:20
2

The only way I found is changing the URI from

https://www.youtube.com/watch?v=2s3aJfRr9gE

to this pattern

"https://www.youtube-nocookie.com/embed/" + video_id + "?hl=" lang_code

On bash/Linux you can just copy the URI with that structure and then run this command (Spanish code hardcoded) to transform clipboard content (you can make an alias):

xclip -selection c -o | echo "$(cat -)?&hl=es-419" | sed "s|youtube.com/watch?v=|youtube-nocookie.com/embed/|1" | xclip -selection c

You can list the available subtitles ISO 639-1 language codes with youtube-dl:

youtube-dl --list-subs "{video_id or url}"

The only drawback is that the video will cover the complete screen... which might be good thing to stop procrastinating with related videos :)

Pablo Bianchi
  • 1,824
  • 1
  • 26
  • 30