我使用c#,我可以成功上传视频到youtube我的频道。现在我想上传包含缩略图的视频。我在调试中没有出错,但缩略图没有上传,这是我的代码:
private static async Task Upload(FileInfo Vidf)
{
//2.1 Get credentials
UserCredential credentials;
//2.1.1 Use https://console.developers.google.com/ to get the json file (Credential section)
using (var stream = new FileStream(APIjson, FileMode.Open, FileAccess.Read))
{
credentials = await GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
new[] { YouTubeService.Scope.YoutubeUpload },
"user",
CancellationToken.None);
}
//2.2 Create a YoutubeService instance using our credentials
var youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
HttpClientInitializer = credentials,
ApplicationName = Assembly.GetExecutingAssembly().GetName().Name
});
//2.3 Create a video object
var video = new Video()
{
Id = Vidf.Name,
Status = new VideoStatus
{
PrivacyStatus = "private"
},
Snippet = new VideoSnippet
{
Title = "...",
Description = "...",
}
};
var filePath = Vidf.FullName;
filesize = Vidf.Length;
//2.4 Read and insert the video in youtubeService
using (var fileStream = new FileStream(filePath, FileMode.Open))
{
var videosInsertRequest = youtubeService.Videos.Insert(video, "snippet,status", fileStream, "video/*");
videosInsertRequest.ProgressChanged += ProgressChanged;
videosInsertRequest.ResponseReceived += ResponseReceived;
//2.4.1 Wait for the upload process
await videosInsertRequest.UploadAsync();
}
var tpath = Vidf.FullName.Replace("mp4", "jpg");
using (var tStream = new FileStream(tpath, FileMode.Open))
{
var tInsertRequest = youtubeService.Thumbnails.Set(video.Id, tStream, "image/jpeg");
tInsertRequest.ProgressChanged += ProgressChanged;
await tInsertRequest.UploadAsync();
}
}
我必须先上传视频,然后等待youtube处理完视频。然后上传缩略图,如此链接中的示例代码https://developers.google.com/youtube/v3/docs/thumbnails/set#examples
创建一个全局var"VidID"并在ResponseReceived函数中设置值
static void ResponseReceived(Video video)
{
VidID = video.Id;
}
应该获取视频ID,然后发布带有该视频ID的缩略图。