我可以同时上传YouTube视频和缩略图吗?

时间:2016-04-05 10:01:09

标签: youtube-api youtube-data-api

我正在使用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

中上传缩略图,例如示例代码

1 个答案:

答案 0 :(得分:1)

创建全局var“VidID”并在ResponseReceived函数中设置值

static void ResponseReceived(Video video)
    {
        VidID = video.Id;
    }

应获取视频ID,然后使用该视频ID发布缩略图。