使用C#.NET和youtube数据API v3检索我的每个youtube视频的持续时间
是否可以使用C#NET和YouTube数据API v3(而不是JavaScript或任何其他客户端语言)获取我的每个YouTube视频的持续时间 我已经搜索了好几天了,我唯一想到的是谷歌在他们的网站上的一个例子,它只显示了如何获取playlitems.list。但是,这并没有给出contentDeatils中视频及其相关持续时间的列表 请帮我弄清楚。使用C#.NET和youtube数据API v3检索我的每个youtube视频的持续时间,c#,.net,video,youtube-api,youtube-data-api,C#,.net,Video,Youtube Api,Youtube Data Api,是否可以使用C#NET和YouTube数据API v3(而不是JavaScript或任何其他客户端语言)获取我的每个YouTube视频的持续时间 我已经搜索了好几天了,我唯一想到的是谷歌在他们的网站上的一个例子,它只显示了如何获取playlitems.list。但是,这并没有给出contentDeatils中视频及其相关持续时间的列表 请帮我弄清楚。 谢谢大家。也遇到过类似的情况,我需要更新所有上传内容的描述。请参见此处的隐藏宝石: 在projectGoogle.api.YouTube.Samp
谢谢大家。也遇到过类似的情况,我需要更新所有上传内容的描述。请参见此处的隐藏宝石: 在project
Google.api.YouTube.Samples.UpdateVideos
中,您将发现一个循环,您可以稍微修改它并使用它来获取每个视频的持续时间
foreach (var channel in channelsListResponse.Items)
{
var uploadsListId = channel.ContentDetails.RelatedPlaylists.Uploads;
Console.WriteLine("Videos in list {0}", uploadsListId);
var nextPageToken = "";
while (nextPageToken != null)
{
var playlistItemsListRequest = youtubeService.PlaylistItems.List("snippet");
playlistItemsListRequest.PlaylistId = uploadsListId;
playlistItemsListRequest.MaxResults = 50;
playlistItemsListRequest.PageToken = nextPageToken;
// Retrieve the list of videos uploaded to the authenticated user's channel.
var playlistItemsListResponse = await playlistItemsListRequest.ExecuteAsync();
foreach (var playlistItem in playlistItemsListResponse.Items)
{
var videoRequest = youtubeService.Videos.List("snippet");
videoRequest.Id = playlistItem.Snippet.ResourceId.VideoId;
videoRequest.MaxResults = 1;
var videoItemRequestResponse = await videoRequest.ExecuteAsync();
// Get the videoID of the first video in the list
var video = videoItemRequestResponse.Items[0];
var duration = video.ContentDetails.Duration;
}
nextPageToken = playlistItemsListResponse.NextPageToken;
}
}
好的,这是我最终如何解决这个问题的解释 我希望通过YouTube数据API实现的目标是根据用户名或频道ID从任何YouTube频道检索视频列表 理想情况下,我们应该能够向YouTube索要特定YouTube频道的所有视频。然而,它似乎不是这样工作的。 最后,我们需要向YouTubeService.Videos.list方法发送一个视频列表请求。这将允许我们检索视频对象列表的内容详细信息、片段和统计信息。然而,这种方法需要几个参数。一个是VideoListRequest.ID参数,它是要检索的视频集合中视频ID的字符串数组。另一个是VideoListRequest.MaxResults参数。这将是您希望撤回的最大视频数 要检索视频ID列表,我们需要再次调用YouTube API,并从YouTubeService.playlitems.list方法检索播放列表项列表。但是,此方法需要UploadsListID,必须通过再次调用YouTube API的YouTubeService.Channels.List方法来获取该UploadsListID 因此,我们需要对YouTube API进行3次调用,如下图所示 第一步是根据用户名或频道ID获取频道列表。UploadsListId将来自ChannelListResponse:channelsListResponse.Items[0]。ContentDetails.RelatedPlaylists.Uploads 第二步是使用上一步中的UploadsListID获取播放列表项目列表。这允许我们检索上传视频播放列表中每个视频的视频ID,并将其放入字符串列表中 最后,第三步是根据前面字符串列表中的视频ID获取视频列表。通过这个响应,我们可以检索每个视频中的持续时间,并将YouTube的HMS格式转换为“可用”Timespan格式的字符串(h:mm:ss) 这是我用来完成上述描述的代码:
public async Task<List<Video>> GetVideoListAsync(ChannelListMethod Method, string MethodValue, int? MaxVideos)
{
// Define variables needed for this method
List<string> videoIdList = new List<string>();
List<Video> videoList = new List<Video>();
string uploadsListId = null;
// Make sure values passed into the method are not null or empty.
if (MaxVideos == null)
{
throw new ArgumentNullException(nameof(MaxVideos));
}
if (string.IsNullOrEmpty(MethodValue))
{
return videoList;
}
// Create the service.
using (YouTubeService youtubeService = new YouTubeService(new BaseClientService.Initializer
{
ApiKey = _apiKey,
ApplicationName = _appName
}))
{
// Step ONE is to get a list of channels for a specified YouTube user or ChannelID.
// Create the FIRST Request object to get a list of YouTube Channels and get their contentDetails
// based on either ForUserName or ChannelID.
ChannelsResource.ListRequest channelsListRequest = youtubeService.Channels.List("contentDetails");
if (Method == ChannelListMethod.ForUserName)
{
// Build the ChannelListRequest using UserName
channelsListRequest.ForUsername = MethodValue;
}
else
{
// Build the ChannelListRequest using ChannelID
channelsListRequest.Id = MethodValue;
}
// This is the FIRST Request to the YouTube API.
// Retrieve the contentDetails part of the channel resource to get a list of channel IDs.
// We are only interested in the Uploads playlist of the first channel.
try
{
ChannelListResponse channelsListResponse = await channelsListRequest.ExecuteAsync();
uploadsListId = channelsListResponse.Items[0].ContentDetails.RelatedPlaylists.Uploads;
}
catch (Exception ex)
{
ErrorException = ex;
return videoList;
}
// Step TWO is to get a list of playlist items from the Uploads playlist.
// From the API response, use the Uploads playlist ID (uploadsListId) to be used to get list of videos
// from the videos uploaded to the user's channel.
string nextPageToken = "";
while (nextPageToken != null)
{
// Create the SECOND Request object for requestring a list of Playlist items
// from the channel's Uploads playlist.
// Limit the list to maxVideos items and continue to iterate through the pages.
PlaylistItemsResource.ListRequest playlistItemsListRequest = youtubeService.PlaylistItems.List("contentDetails");
playlistItemsListRequest.PlaylistId = uploadsListId;
playlistItemsListRequest.MaxResults = MaxVideos;
playlistItemsListRequest.PageToken = nextPageToken;
// This is the SECOND Request to YouTube and get a Response object containing
// the playlist items in the channel's Uploads playlist.
// Then iterate through the Response items to build a string list of the video IDs
try
{
PlaylistItemListResponse playlistItemsListResponse = await playlistItemsListRequest.ExecuteAsync();
foreach (PlaylistItem playlistItem in playlistItemsListResponse.Items)
{
videoIdList.Add(playlistItem.ContentDetails.VideoId.ToString());
}
}
catch (Exception ex)
{
ErrorException = ex;
return videoList;
}
// Now that we have a collection (string array) of video IDs,
// Step THREE is to retrieve the snippet, contentDetails, and statistics parts of the
// list of videos uploaded to the authenticated user's channel.
try
{
// Create the THIRD Request object for requestring a list of videos and their associated metadata.
var VideoListRequest = youtubeService.Videos.List("snippet, contentDetails, statistics");
// The next line converts the list of video Ids to a comma seperated string array of the video IDs
VideoListRequest.Id = String.Join(",", videoIdList);
VideoListRequest.MaxResults = MaxVideos;
var VideoListResponse = await VideoListRequest.ExecuteAsync();
// This is the THIRD Request to the YouTube API to get a Response object
// containing Collect each Video duration and convert to a usable time format.
foreach (var video in VideoListResponse.Items)
{
video.ContentDetails.Duration = HMSToTimeSpan(video.ContentDetails.Duration).ToString();
videoList.Add(video);
}
// request next page
nextPageToken = VideoListResponse.NextPageToken;
}
catch (Exception ex)
{
ErrorException = ex;
return videoList;
}
}
return videoList;
}
}
公共异步任务GetVideoListAsync(ChannelListMethod方法、string方法值、int?MaxVideos)
{
//定义此方法所需的变量
List videoIdList=新列表();
List videoList=新列表();
字符串uploadsListId=null;
//确保传递到方法中的值不为null或空。
如果(MaxVideos==null)
{
抛出新ArgumentNullException(nameof(MaxVideos));
}
if(string.IsNullOrEmpty(MethodValue))
{
返回视频列表;
}
//创建服务。
使用(YouTubeService YouTubeService=new YouTubeService(new BaseClientService.Initializer
{
ApiKey=\u ApiKey,
ApplicationName=\u appName
}))
{
//第一步是获取指定YouTube用户或ChannelID的频道列表。
//创建第一个请求对象以获取YouTube频道列表并获取其内容详细信息
//基于ForUserName或ChannelID。
ChannelsResource.ListRequest channelsListRequest=youtubeService.Channels.List(“内容详细信息”);
if(Method==ChannelListMethod.ForUserName)
{
//使用用户名构建ChannelListRequest
channelsListRequest.ForUsername=MethodValue;
}
其他的
{
//使用ChannelID构建ChannelListRequest
channelsListRequest.Id=MethodValue;
}
//这是对YouTube API的第一个请求。
//检索频道资源的contentDetails部分以获取频道ID列表。
//我们只对第一个频道的上传播放列表感兴趣。
尝试
{
ChannelListResponse channelsListResponse=等待channelsListRequest.ExecuteAsync();
uploadsListId=channelsListResponse.Items[0]。ContentDetails.RelatedPlaylists.Uploads;
}
捕获(例外情况除外)
{
ErrorException=ex;
返回视频列表;
}
//第二步是从上传的播放列表中获取播放列表项的列表。
//在API响应中,使用Uploads播放列表ID(uploadsListId)来获取视频列表
//从上传到用户频道的视频。
字符串nextPageToken=“”;
while(nextPageToken!=null)
{
//创建第二个请求对象,用于请求播放列表项的列表
//从频道的上传播放列表。
//将列表限制为maxVideos项目,并继续遍历页面。
PlayItemsResource.ListRequest PlayItemsListRequest=youtubeService.PlayItems.List(“内容详细信息”);
playlItemsListRequest.playliId=上传的sListId;
playlitemsListrequest.MaxResults=MaxVid