【问题标题】:How to get Duration of all Youtube Videos by calling YouTube API in C#如何通过在 C# 中调用 YouTube API 来获取所有 Youtube 视频的持续时间
【发布时间】:2021-01-26 16:32:16
【问题描述】:

我想在调用youtubeService.Search.List("snippet");后获取列表中每个视频的时长 以下是我的代码

var youtubeService = new YouTubeService(new BaseClientService.Initializer()
            {
                ApiKey = "API Key",
                ApplicationName = this.GetType().ToString()
            });

            var searchListRequest = youtubeService.Search.List("snippet");
            searchListRequest.MaxResults = 30;
            var searchListResponse = searchListRequest.Execute();

我读到持续时间在视频的内容详细信息中,但每个视频的内容详细信息为空。

【问题讨论】:

    标签: c# asp.net-mvc google-api youtube-data-api google-api-dotnet-client


    【解决方案1】:

    查看search.list 的文档后,您会看到它返回了search#resource 的列表

    {
      "kind": "youtube#searchResult",
      "etag": etag,
      "id": {
        "kind": string,
        "videoId": string,
        "channelId": string,
        "playlistId": string
      },
      "snippet": {
        "publishedAt": datetime,
        "channelId": string,
        "title": string,
        "description": string,
        "thumbnails": {
          (key): {
            "url": string,
            "width": unsigned integer,
            "height": unsigned integer
          }
        },
        "channelTitle": string,
        "liveBroadcastContent": string
      }
    }
    

    其中不包含有关视频时长的任何信息。此方法似乎不会返回您要查找的信息。

    视频列表

    只有包含video.resourcevideos.list 才能为您提供视频的时长

     {
          "kind": "youtube#video",
          "etag": "U-t7IORpXf52WZSn6hBzfgm6WLo",
          "id": "XOIZZEdfrfA",
          "contentDetails": {
            "duration": "PT8M51S",
            "dimension": "2d",
            "definition": "hd",
            "caption": "false",
            "licensedContent": true,
            "contentRating": {},
            "projection": "rectangular"
          }
    

    【讨论】:

    • 你知道通过什么方法可以得到视频的时长吗?
    • 只有包含 video.resourcevideos.list 才能为您提供视频的时长
    • 非常感谢,我尝试了 youtubeService.Videos.List("contentDetails") ,然后我才得到视频的详细信息,例如持续时间。
    猜你喜欢
    • 2021-12-10
    • 2017-12-10
    • 2017-07-14
    • 1970-01-01
    • 2021-03-13
    • 2014-06-09
    • 1970-01-01
    • 2018-06-28
    • 2020-06-05
    相关资源
    最近更新 更多