1
votes

I made a program in visual studio that renders videos that have a static image using ffmpeg and then uploads them to youtube, but it can also upload videos not rendered by it.

And for those i would want to specify the thumbnail used, is it possible to set a thumbnail for a video with c#?

I looked at the documentation about this, but it doesn't include any c#/.net examples (https://developers.google.com/youtube/v3/docs/thumbnails/set)

2

2 Answers

2
votes

You can set a thumbnail using Youtube SDK and a Stream like this:

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

In the example, you receive an URL and set a thumbnail to the video, but you can change the stream for a local stream or any other stream.

0
votes

Assuming that you are using the Google APIs .net Client library this code should work

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


    }

Code ripped from my Google .net client sample project for youtube-Data-API ThumbnailsSample.Cs

I don't have any code for uploading the file itself but there is a very good tutorial on the Drive API it should be similar you should be able to alter that Upload Media this is unfortunately the only documentation i am aware of for media upload.