How to get YouTube video aspect ratio

前端 未结 6 1371
滥情空心
滥情空心 2020-12-14 20:16

I\'d like to get the aspect ratio of a YouTube video, to resize the player accordingly. I\'m programming the YT player using JavaScript.

6条回答
  •  孤城傲影
    2020-12-14 21:04

    Here is how I do it. I get the aspect ratio from the youtube image.

    
    
    

    I pull all the video information from the youtube api and then store all the video information in a database beforehand, so if you are doing this on the fly, you might have to hide the image on the page and then get the aspect ratio that way.

    edit** Another option, and probably the best, would be to use youtube's api. Search for a video, and check if the data->items->aspectRatio is set. I don't think it's set on 4:3 video, but on 16:9 it is set to widescreen. Should be as simple as if (data->items->aspectRatio) {ratio= "16:9"} else {ratio="4:3"}

提交回复
热议问题