Youtube embed - force subtitles in a specific language by default

I embed a youtube video with subtitles in a specific language (in Hebrew, in my case). im using:

hl=He&cc_load_policy=1 

to show Jewish subtitles, and it works great.

However, if there are no subtitles in my language, I would like English by default (if any). is there any way to force this?

+7
source share
4 answers

You can use subtitles and language with the options cc_load_policy and cc_lang_pref with

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

+19
source

I have not found this anywhere in my api docs, but with your youtube player object you can 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 enable it via js:

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

to disable it:

 player.unloadModule("captions"); //Works for html5 ignored by AS3 player.unloadModule("cc"); //Works for AS3 ignored by html5 
+3
source

I believe that it would be better to just leave hl = completely. (In fact, this is not one of our officially supported player settings .) Subtitles will default to the user's preferences of the video, and my assumption is that he will return to English if there are no subtitles in the preferred viewing language.

+2
source

The only way I found is to change the URI from

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

to this circuit

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

In bash / Linux, you can simply copy the URI with this structure and then run this command (hard-coded Spanish code) to convert the contents of the clipboard (you can create 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 

Using youtube-dl you can list the available ISO 639-1 subtitle language codes :

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

The only drawback is that the video will cover the entire screen ... which can be useful to stop putting off on similar videos :)

0
source

Source: https://habr.com/ru/post/926726/


All Articles