How to play only the audio of a Youtube video using HTML 5?

前端 未结 9 583
花落未央
花落未央 2020-12-02 11:17

Is it possible to play only the audio from a YouTube video using HTML 5 and Javascript?

相关标签:
9条回答
  • 2020-12-02 11:51

    Embed the video player and use CSS to hide the video. If you do it properly you may even be able to hide only the video and not the controls below it.

    However, I'd recommend against it, because it will be a violation of YouTube TOS. Use your own server instead if you really want to play only audio.

    0 讨论(0)
  • 2020-12-02 11:54

    This may be an old post but people could still be searching for this so here you go:

    <div style="position:relative;width:267px;height:25px;overflow:hidden;">
    <div style="position:absolute;top:-276px;left:-5px">
    <iframe width="300" height="300" 
      src="https://www.youtube.com/embed/youtubeID?rel=0">
    </iframe>
    </div>
    </div>
    
    0 讨论(0)
  • 2020-12-02 11:55

    You can parse Youtube meta file for all streams available for this particular video id using this link: https://www.youtube.com/get_video_info?video_id={VID} and extract audio only streams.

    Here is an example with public Google Image proxy (but you can use any free or your own CORS proxy):

    var vid = "3r_Z5AYJJd4",
        audio_streams = {},
        audio_tag = document.getElementById('youtube');
    
    fetch("https://"+vid+"-focus-opensocial.googleusercontent.com/gadgets/proxy?container=none&url=https%3A%2F%2Fwww.youtube.com%2Fget_video_info%3Fvideo_id%3D" + vid).then(response => {
        if (response.ok) {
            response.text().then(data => {
    
                var data = parse_str(data),
                    streams = (data.url_encoded_fmt_stream_map + ',' + data.adaptive_fmts).split(',');
    
                streams.forEach(function(s, n) {
                    var stream = parse_str(s),
                        itag = stream.itag * 1,
                        quality = false;
                    console.log(stream);
                    switch (itag) {
                        case 139:
                            quality = "48kbps";
                            break;
                        case 140:
                            quality = "128kbps";
                            break;
                        case 141:
                            quality = "256kbps";
                            break;
                    }
                    if (quality) audio_streams[quality] = stream.url;
                });
    
                console.log(audio_streams);
    
                audio_tag.src = audio_streams['128kbps'];
                audio_tag.play();
            })
        }
    });
    
    function parse_str(str) {
        return str.split('&').reduce(function(params, param) {
            var paramSplit = param.split('=').map(function(value) {
                return decodeURIComponent(value.replace('+', ' '));
            });
            params[paramSplit[0]] = paramSplit[1];
            return params;
        }, {});
    }
    <audio id="youtube" autoplay controls loop></audio>

    Doesn't work for all videos, very depends on monetization settings or something like that.

    0 讨论(0)
  • 2020-12-02 11:55

    Adding to the mentions of jwplayer and possible TOS violations, I would like to to link to the following repository on github: YouTube Audio Player Generation Library, that allows to generate the following output:

    Library has the support for the playlists and PHP autorendering given the video URL and the configuration options.

    0 讨论(0)
  • 2020-12-02 12:00

    var vid = "bpt84ceWAY0",
        audio_streams = {},
        audio_tag = document.getElementById('youtube');
    
    fetch("https://"+vid+"-focus-opensocial.googleusercontent.com/gadgets/proxy?container=none&url=https%3A%2F%2Fwww.youtube.com%2Fget_video_info%3Fvideo_id%3D" + vid).then(response => {
        if (response.ok) {
            response.text().then(data => {
    
                var data = parse_str(data),
                    streams = (data.url_encoded_fmt_stream_map + ',' + data.adaptive_fmts).split(',');
    
                streams.forEach(function(s, n) {
                    var stream = parse_str(s),
                        itag = stream.itag * 1,
                        quality = false;
                    console.log(stream);
                    switch (itag) {
                        case 139:
                            quality = "48kbps";
                            break;
                        case 140:
                            quality = "128kbps";
                            break;
                        case 141:
                            quality = "256kbps";
                            break;
                    }
                    if (quality) audio_streams[quality] = stream.url;
                });
    
                console.log(audio_streams);
    
                audio_tag.src = audio_streams['128kbps'];
                audio_tag.play();
            })
        }
    });
    
    function parse_str(str) {
        return str.split('&').reduce(function(params, param) {
            var paramSplit = param.split('=').map(function(value) {
                return decodeURIComponent(value.replace('+', ' '));
            });
            params[paramSplit[0]] = paramSplit[1];
            return params;
        }, {});
    }
    <audio id="youtube" autoplay controls loop></audio>

    0 讨论(0)
  • 2020-12-02 12:07

    The answer is simple: Use a 3rd party product like jwplayer or similar, then set it to the minimal player size which is the audio player size (only shows player controls).

    Voila.

    Been using this for over 8 years.

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