【问题标题】:Adding a thumbnail with youtube api using c#?使用 c# 添加带有 youtube api 的缩略图?
【发布时间】:2017-09-30 19:33:46
【问题描述】:

我在visual studio中做了一个程序,它使用ffmpeg渲染具有静态图像的视频,然后将它们上传到youtube,但它也可以上传不是由它渲染的视频。

对于那些我想指定使用的缩略图的人,是否可以使用 c# 为视频设置缩略图?

我查看了有关此的文档,但它不包含任何 c#/.net 示例 (https://developers.google.com/youtube/v3/docs/thumbnails/set)

【问题讨论】:

    标签: c# video youtube youtube-api youtube-data-api


    【解决方案1】:

    您可以像这样使用 Youtube SDK 和 Stream 设置缩略图:

        public async Task SetThumbnail(String url)
        {
            System.Net.WebClient theClient = new WebClient();
            using (var fileStream = theClient.OpenRead(url))
            {
                var videosInsertRequest = this.Service.Thumbnails.Set(this.Video.Id, fileStream, "image/jpeg");
                await videosInsertRequest.UploadAsync();
            }
            Console.WriteLine("Thumbnail " + url + " set to video: " + this.Video.Id);
        }
    

    在示例中,您会收到一个 URL 并为视频设置缩略图,但您可以将流更改为本地流或任何其他流。

    【讨论】:

      【解决方案2】:

      假设您使用的是 Google APIs .net 客户端库,此代码应该可以工作

      public class ThumbnailsSetOptionalParms
      {
           /// Note: This parameter is intended exclusively for YouTube content partners. The onBehalfOfContentOwner parameter indicates that the request's authorization credentials identify a YouTube CMS user who is acting on behalf of the content owner specified in the parameter value. This parameter is intended for YouTube content partners that own and manage many different YouTube channels. It allows content owners to authenticate once and get access to all their video and channel data, without having to provide authentication credentials for each individual channel. The actual CMS account that the user authenticates with must be linked to the specified YouTube content owner.
          public string OnBehalfOfContentOwner { get; set; }  
              
      }
       
      /// <summary>
      /// Uploads a custom video thumbnail to YouTube and sets it for a video. 
      /// Documentation https://developers.google.com/youtube/v3/reference/thumbnails/set
      /// Generation Note: This does not always build corectly.  Google needs to standardise things I need to figuer out which ones are wrong.
      /// </summary>
      /// <param name="service">Authenticated YouTube service.</param>  
      /// <param name="videoId">The videoId parameter specifies a YouTube video ID for which the custom video thumbnail is being provided.</param>
      /// <param name="optional">Optional paramaters.</param>        
      /// <returns>ThumbnailSetResponseResponse</returns>
      public static ThumbnailSetResponse Set(YouTubeService service, string videoId, ThumbnailsSetOptionalParms optional = null)
      {
          try
          {
              // Initial validation.
              if (service == null)
                  throw new ArgumentNullException("service");
      
              if (videoId == null)
                  throw new ArgumentNullException(videoId);
      
              // Building the initial request.
              var request = service.Thumbnails.Set(videoId);
      
              // Applying optional parameters to the request.                
              request = (ThumbnailsResource.SetRequest)SampleHelpers.ApplyOptionalParms(request, optional);
      
              // Requesting data.
              return request.Execute();
          }
          catch (Exception ex)
          {
              throw new Exception("Request Thumbnails.Set failed.", ex);
          }
      }
      

      SampleHelper 类如下所示:

      public static class SampleHelpers
      {
          /// <summary>
          /// Using reflection to apply optional parameters to the request.  
          /// 
          /// If the optonal parameters are null then we will just return the request as is.
          /// </summary>
          /// <param name="request">The request. </param>
          /// <param name="optional">The optional parameters. </param>
          /// <returns></returns>
          public static object ApplyOptionalParms(object request, object optional)
          {
              if (optional == null)
                  return request;
      
              System.Reflection.PropertyInfo[] optionalProperties = (optional.GetType()).GetProperties();
      
              foreach (System.Reflection.PropertyInfo property in optionalProperties)
              {
                  // Copy value from optional parms to the request.  They should have the same names and datatypes.
                  System.Reflection.PropertyInfo piShared = (request.GetType()).GetProperty(property.Name);
                  if (property.GetValue(optional, null) != null) // TODO Test that we do not add values for items that are null
                      piShared.SetValue(request, property.GetValue(optional, null), null);
              }
      
              return request;
          }
      }
      

      从我的 youtube-Data-API ThumbnailsSample.Cs 的 Google .net 客户端示例项目中提取的代码

      我没有用于上传文件本身的任何代码,但有一个关于 Drive API 的非常好的教程,它应该是类似的,你应该能够更改 Upload Media 不幸的是,这是我知道的唯一文档用于媒体上传。

      【讨论】:

      • 谢谢,虽然像这样重要的东西几乎没有记录在案,尽管它是网站的重要组成部分,但它真的很糟糕
      • Google 没有时间,所以我尝试用 C# 来做。 Github 项目包含所有 API 的代码。 Google 已开始将我的一些代码用于示例,但并非用于所有 API,我仍在使用它们来更新所有内容。
      • 这是一个有价值的事业,我希望你能成功。
      猜你喜欢
      • 2011-10-26
      • 1970-01-01
      • 2016-03-26
      • 2013-07-15
      • 2020-04-15
      • 2014-06-17
      • 2021-07-09
      • 2015-09-13
      • 2017-01-03
      相关资源
      最近更新 更多