使用 c# 添加带有 youtube api 的缩略图?

Adding a thumbnail with youtube api using c#?

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

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

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

假设您使用的是 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 class 看起来像这样:

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;
    }
}

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

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

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

    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 并为视频设置了缩略图,但您可以将流更改为本地流或任何其他流。