获取YouTube视频格式

时间:2018-10-03 14:02:54

标签: javascript youtube

我有一个YouTube URL(ID),我需要根据该视频的视频格式调整一些脚本。

我们如何在拥有ID的YouTube上找出视频的格式(16:9或其他格式)和分辨率?

1 个答案:

答案 0 :(得分:2)

如果您只需要视频的长宽比,则可以使用https://noembed.com/之类的东西(或者您可以直接使用YouTube的oEmbed API;请参见https://oembed.com/)。< / p>

示例:

GET http://noembed.com/embed?url=https://www.youtube.com/watch?v=ysk2OzngYIg

{  
  "height":270,
  "thumbnail_url":"https://i.ytimg.com/vi/ysk2OzngYIg/hqdefault.jpg",
  "thumbnail_width":480,
  "author_name":"Jamie Syme",
  "url":"http://www.youtube.com/watch?v=ysk2OzngYIg",
  "title":"test: sample coast",
  "width":480,
  "provider_name":"YouTube",
  "thumbnail_height":360,
  "provider_url":"https://www.youtube.com/",
  "type":"video",
  "author_url":"https://www.youtube.com/channel/UC7x7nMO0Y9Fq0g7q5zyDC2Q",
  "html":"\n<iframe width=\" 480\" height=\"270\" src=\"https://www.youtube.com/embed/ysk2OzngYIg?feature=oembed\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"></iframe>\n",
  "version":"1.0"
}

width除以height可以得到宽高比。

但是,如果您需要上传视频的实际分辨率,则需要经过YouTube's Video List API,并且您必须是视频的所有者。

示例:

GET https://www.googleapis.com/youtube/v3/videos?id=ysk2OzngYIg&part=fileDetails

/**
 * API response
 */
{
  "kind": "youtube#videoListResponse",
  "etag": "\"XI7nbFXulYBIpL0ayR_gDh3eu1k/nkmtubhIzKeVfaCtEq3-HIJf0mw\"",
  "pageInfo": {
    "totalResults": 1,
    "resultsPerPage": 1
  },
  "items": [
    {
      "kind": "youtube#video",
      "etag": "\"XI7nbFXulYBIpL0ayR_gDh3eu1k/er5srVttLDLwfS6A6I4VKsvOmH4\"",
      "id": "ysk2OzngYIg",
      "fileDetails": {
        "fileName": "sample-coast.mp4",
        "fileSize": "13586259",
        "fileType": "video",
        "container": "mov",
        "videoStreams": [
          {
            "widthPixels": 1920,
            "heightPixels": 1080,
            "frameRateFps": 29.97002997002997,
            "aspectRatio": 1.7777777777777777,
            "codec": "h264",
            "bitrateBps": "4238925"
          }
        ],
        "durationMs": "24290",
        "bitrateBps": "4238925"
      }
    }
  ]
}

请注意,以上响应是通过其文档中的嵌入式API测试器收到的。