获取虎牙直播流地址,可直接使用VLC播放器播放

获取虎牙直播流地址,可直接使用VLC播放器播放,在VLC内选择Open Network,粘贴地址打开即可

// ==UserScript==
// @name         获取虎牙直播流地址,可直接使用VLC播放器播放
// @namespace    http://tampermonkey.net/
// @version      0.6
// @description  获取虎牙直播流地址,可直接使用VLC播放器播放,在VLC内选择Open Network,粘贴地址打开即可
// @author       xiaozhuai
// @include      http://www.huya.com/*
// @include      https://www.huya.com/*
// @grant        none
// ==/UserScript==

// 本人是韦神大叔粉,请大家支持韦神。
// 韦神资料:
//     绝地求生4AM队长,前英雄联盟LPL总冠军获得者。
//     2018 PUBG China Pro Invitation (PCPI) TPP模式冠军获得者。
//     4AM兄弟齐心,一千多斤!
// 韦神直播间地址:
//     https://www.huya.com/godv

(function() {
    'use strict';
    function heredoc(fn) {
        return fn.toString().split('\n').slice(1,-1).join('\n') + '\n'
    }

    var boxHtml = '';

    if(window.TT_ROOM_DATA.state==='OFF'){
        boxHtml = '状态: 未开播<br>';
    }

    if(window.TT_ROOM_DATA.state==='ON'){
        boxHtml = '状态: 正在直播<br>';
        try{
            var streamInfoList = window.hyPlayerConfig.stream.data[0].gameStreamInfoList;
            var data = [];
          
            for(var line=0; line<streamInfoList.length; line++){
                var streamInfo = streamInfoList[line];
                console.log(streamInfo);
                
                var lineNumber = streamInfo.iLineIndex;
                var url = streamInfo.sFlvUrl + '/' + streamInfo.sStreamName + '.' + streamInfo.sFlvUrlSuffix + '?' + streamInfo.sFlvAntiCode;
                var ratioList = window.hyPlayerConfig.stream.vMultiStreamInfo;

                for(var i=0; i<ratioList.length; i++){
                    var label = ratioList[i].sDisplayName;
                    var absUrl = url;
                    if(ratioList[i].iBitRate != 0){
                        absUrl = url+"&ratio="+ratioList[i].iBitRate;
                    }
                    data.push({
                        label: '(线路'+lineNumber+') '+label,
                        url: absUrl,
                    });
                }
            }
          
            for(var i=0; i<data.length; i++) {
                boxHtml += '<div class="flv-url-item"><label>'+data[i].label+'</label><input id="flv-url-'+i+'" value="'+data[i].url+'"/><a onclick="copyFlvUrl('+i+')">复制</a></div>'; //<a onclick="openFlvUrl('+i+')">VLC播放</a>
            }
            
        }catch(e){
            boxHtml += '解析流数据错误';
            console.error(e);
        }
    }

    if(window.TT_ROOM_DATA.state==='REPLAY'){
        boxHtml = '状态: 重播<br>';
        boxHtml += '当前处于重播状态,不能解析流数据';
    }


    window.toggleFlvUrlBox = function() {
        var flvUrlBoxBtn = document.getElementById('flv-url-box-btn');
        var flvUrlBox = document.getElementById('flv-url-box');
        if(flvUrlBox.style.display==='none'){
            flvUrlBox.style.display='block';
        }else{
            flvUrlBox.style.display='none';
        }
    }
    window.copyFlvUrl = function(index) {
        var input = document.getElementById('flv-url-'+index);
        input.select();
        document.execCommand("Copy");
    }
    window.openFlvUrl = function(index) {
        var input = document.getElementById('flv-url-'+index);
        var url = input.value;
        location.href = "vlc://"+url
    }
    var wrapper = document.createElement("div");
    wrapper.style.display = 'inline-block';
    wrapper.innerHTML = heredoc(function(){/*
<style>
#flv-url-box-btn {
    width: 32px;
    height: 32px;
    cursor: pointer;
    background-color: #ffffff;
    top: 9px;
    right: 40px;
    position: fixed;
    z-index: 1000000;
    border-radius: 4px;
    border: 1px solid #cccccc;
}
#flv-url-box-btn:hover{
    box-shadow: 0 0 8px #0ca4d4;
}
#flv-url-box {
    top: 54px;
    right: 40px;
    border: 1px solid #808080;
    border-radius: 6px;
    background-color: #ffffff;
    padding: 8px;
    position: fixed;
    z-index: 1000000;
}
#flv-url-box .flv-url-item{
    margin: 4px 0;
}
#flv-url-box .flv-url-item>*{
    border: 1px solid #808080;
    margin-left: -1px;
    vertical-align: top;
}
#flv-url-box .flv-url-item>*:first-child{
    border-top-left-radius: 6px;
    border-bottom-left-radius: 6px;
    margin-left: 0;
}
#flv-url-box .flv-url-item>*:last-child{
    border-top-right-radius: 6px;
    border-bottom-right-radius: 6px;
}
#flv-url-box .flv-url-item input{
    top: 0;
    width: 240px;
    height: 24px;
    padding: 0 4px;
}
#flv-url-box .flv-url-item input:focus{
    outline: none;
    border-color: #0ca4d4;
}
#flv-url-box .flv-url-item a{
    user-select: none;
    padding: 0 4px;
    cursor: pointer;
    font-size: 12px;
    line-height: 24px;
    height: 24px;
    display: inline-block;
    background-color: #ffffff;
    color: #333;
}
#flv-url-box .flv-url-item label{
    user-select: none;
    text-align: left;
    padding-left: 12px;
    width: 100px;
    font-size: 12px;
    line-height: 24px;
    height: 24px;
    display: inline-block;
    background-color: #ffffff;
    color: #333;
}
#flv-url-box .flv-url-item a:hover{
    border-color: #0ca4d4;
    color: #0ca4d4;
}
</style>
<div>
    <img id="flv-url-box-btn" onclick="toggleFlvUrlBox()" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADYAAAAvCAYAAAConDmOAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAABmJLR0QAAAAAAAD5Q7t/AAAACXBIWXMAAAB4AAAAeACd9VpgAAAAB3RJTUUH4QMTFwIhc/YnDwAABeNJREFUaN7d2k9oG9kBx/Hvm5G8sizFju38UeyuccCytcTktElocygte+mt0G6bbmkhkIRAriXgQA4LS28l0FMOxSo5BJcUcig0hxKKnCiiwoGkru21JNt1TEJQJVm2ZM94Zt7bgz3aKI6TOB5b3v7AYI1mnt/nvZn3Zt5Y8Ib86MefYZoG3d0f4/P58CpSSiYm/k15cREhhGflzs7ObtpWV+uff34O27IIBAIEmpupVitNgO5VBUKhEH5/k1fFOcAaQG9v7yZgrdl+8csvAPjoowBra+YnQojPhRCfCiFCgNppLYQQLC6WmP56EsuydlwcUAXSwF+A/7hfuDgB8MWvf4vjOKysVLWWltCvhBBfCiF6vWpaIQSVyjK5bIZKZdmrYt3MA18Bf2K9F5mdnV0/FW3bRtd1wuEDPwH+CLR5jcpmpqlUlj29tjbyMfAH1k/LuLtRc/+4lPIQcO07hnLTAvwO+F4dTNM0NE37IfDpdxDl5hPgsxpM13WamppQSvXj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NzdCDe2HSgAAAF90RVh0VGh1bWI6OlVSSQBmaWxlOi8vL2hvbWUvd3d3cm9vdC9zaXRlL3d3dy5lYXN5aWNvbi5uZXQvY2RuLWltZy5lYXN5aWNvbi5jbi9zcmMvMTIwNzcvMTIwNzcxMS5wbmcp913KAAAAAElFTkSuQmCC" />
    <div id="flv-url-box" style="display: none;">__box_html__</div>
<div>
    */}).replace('__box_html__', boxHtml);

    document.body.append(wrapper);
})();