Get YouTube Video dimensions (width/height)

前端 未结 5 938
孤街浪徒
孤街浪徒 2020-12-06 01:08

I’m trying to get the size of a YouTube video. I’m using a Gdata API call to retrieve the basic informations (title, URLs, thumbnails and categories) but I can’t find the vi

5条回答
  •  抹茶落季
    2020-12-06 01:46

    If you load the youtube video like this, you can trigger a javascript function only after the video has loaded.

    
        

    resizeHeroVideo is called after the video is loaded.

    var player = null;
    $( document ).ready(function() {
        resizeHeroVideo();
    } );
    
    $(window).resize(function() {
        resizeHeroVideo();
    });
    
    function resizeHeroVideo() {
        var content = $('#hero');
        var contentH = viewportSize.getHeight();
        contentH -= 158;
        content.css('height',contentH);
    
        if(player != null) {
            var iframe = $('.videoWrapper iframe');
            var iframeH = contentH - 150;
            if (isMobile) {
                iframeH = 163; 
            }
            iframe.css('height',iframeH);
            var iframeW = iframeH/9 * 16;
            iframe.css('width',iframeW);
        }
    }
    

    and then we calculate the sizes to maintain it's position in the center of the page adhering to the appropriate 16:9 aspect ratio.

    Complete gist here. Live example here.

提交回复
热议问题