OritK
OritK

Reputation: 544

youtube embed - force subtitles in specific language with default

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?

Upvotes: 16

Views: 39115

Answers (4)

Pablo Bianchi
Pablo Bianchi

Reputation: 2038

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 :)

Upvotes: 2

i--
i--

Reputation: 4360

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

Upvotes: 32

James Irwin
James Irwin

Reputation: 429

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

Upvotes: 5

Jeff Posnick
Jeff Posnick

Reputation: 56084

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.

Upvotes: 4

Related Questions