youtube embed - force subtitles in specific language with default

前端 未结 4 1869
执笔经年
执笔经年 2020-12-09 05:38

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

相关标签:
4条回答
  • 2020-12-09 05:57

    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.

    0 讨论(0)
  • 2020-12-09 06:06

    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
    
    0 讨论(0)
  • 2020-12-09 06:11

    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

    0 讨论(0)
  • 2020-12-09 06:15

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

    0 讨论(0)
提交回复
热议问题