【问题标题】:How can i upload more then one video file to youtube at the same time?如何同时将多个视频文件上传到 youtube?
【发布时间】:2015-09-10 11:12:10
【问题描述】:
private void UploadVideo(string FileName, string VideoTitle, string VideoDescription)
        {
            try
            {
                var youtubeService = new YouTubeService(new BaseClientService.Initializer()
                {
                    HttpClientInitializer = credential,
                    ApplicationName = Assembly.GetExecutingAssembly().GetName().Name
                });

                video.Snippet = new VideoSnippet();
                video.Snippet.Title = VideoTitle;
                video.Snippet.Description = VideoDescription;
                video.Snippet.Tags = new string[] { "tag1", "tag2" };
                video.Status = new VideoStatus();
                video.Status.PrivacyStatus = "public";
                using (var fileStream = new FileStream(FileName, FileMode.Open))
                {

                    const int KB = 0x400;
                    var minimumChunkSize = 256 * KB;

                    var videosInsertRequest = youtubeService.Videos.Insert(video,
                        "snippet,status", fileStream, "video/*");
                    videosInsertRequest.ProgressChanged +=
                        videosInsertRequest_ProgressChanged;
                    videosInsertRequest.ResponseReceived +=
                        videosInsertRequest_ResponseReceived;
                    // The default chunk size is 10MB, here will use 1MB.
                    videosInsertRequest.ChunkSize = minimumChunkSize * 3;
                    dt = DateTime.Now;
                    totalBytes = fileStream.Length;
                    videosInsertRequest.Upload();
                    videosInsertRequest.UploadAsync();
                }
            }
            catch (Exception errors)
            {
                string errorss = errors.ToString();
            }
        }

直到现在我只使用以下行上传视频文件:

videosInsertRequest.Upload();

在上传的描述中说:将内容上传到服务器。这个方法是同步的,会一直阻塞,直到上传完成。

UploadAsync 描述说:将内容异步上传到服务器。

如果我有一个视频文件列表,并且我想同时上传它们,我的意思是同时上传一些视频,我该怎么做?

或者,如果我将使用 UploadAsync,那么如果我单击一个按钮,并且每次它会上传不同文件名的视频文件,它们将同时并行上传?

换句话说,我认为:我的问题应该是如何将多个视频文件同时上传到 youtube?

【问题讨论】:

  • 如果您尝试同时上传多个文件,您不会遇到用户配额问题吗?如果我一次上传 10 个视频,我仍然只能每秒发送 10 个请求。所以每个视频每秒只能上传一个块,就像你一次一个视频一样,你每秒会上传 10 个块。
  • DalmTo ok 也许是另一种方式。每次我点击一个按钮上传一个新的视频文件时,它都会以某种方式将视频文件添加到队列中,并自动上传视频文件我的意思是,一旦完成上传一个文件,它将从队列中取出下一个视频文件,并将上传它等等。也许我可以使用 List 或队列以某种方式我的一般想法是拥有一批视频文件,我将单击一个按钮并“忘记”视频文件,如果不是同时上传它们。

标签: c# .net winforms google-api google-api-dotnet-client


【解决方案1】:
var mResumableUploader = new ResumableUploader(chunkSize);
        mResumableUploader.AsyncOperationCompleted += MResumableUploaderAsyncOperationCompleted;
        mResumableUploader.AsyncOperationProgress += MResumableUploaderAsyncOperationProgress;
mResumableUploader.AsyncOperationCompleted += new AsyncOperationCompletedEventHandler(ru_AsyncOperationCompleted);
        var youTubeAuthenticator = new ClientLoginAuthenticator(appName, ServiceNames.YouTube, uName, passWord);
        youTubeAuthenticator.DeveloperKey = devKey;

        newVideo = new Video();

        newVideo.Title = "video";
        newVideo.Tags.Add(new MediaCategory("Entertainment", YouTubeNameTable.CategorySchema));
        newVideo.Keywords = "video";
        newVideo.Description = "video";
        newVideo.YouTubeEntry.Private = false;
        newVideo.YouTubeEntry.MediaSource = new MediaFileSource(fileName, fileContType);

        var link = new AtomLink("http://uploads.gdata.youtube.com/resumable/feeds/api/users/default/uploads");
        link.Rel = ResumableUploader.CreateMediaRelation;
        newVideo.YouTubeEntry.Links.Add(link);

        Console.WriteLine("Starting upload: ");
        mResumableUploader.InsertAsync(youTubeAuthenticator, newVideo.YouTubeEntry, "inserter");

有完成事件:

void ru_AsyncOperationCompleted(object sender, AsyncOperationCompletedEventArgs e)
        {

        //upload complete
        YouTubeRequestSettings ytSettings = new YouTubeRequestSettings("myApp", googleDevKey, ytUsername, ytPassword);
        Video v = ytRequest.ParseVideo(e.ResponseStream);
        string videoId = v.VideoId;
        string watchPage = v.WatchPage.ToString();

    }

【讨论】:

  • 问题是。 “同步和异步有什么区别”这并不能回答这个问题。
  • 看问题的最后一行:换句话说,我认为:我的问题应该是如何将多个视频文件同时上传到 youtube?
  • 我随后更新了他的问题以更好地反映实际问题。
  • 您的答案仍然使用 YouTube api v2 (gdata)。他正在使用当前版本的 YouTube API v3。您的代码不适用于它的两个不同的客户端库。而且 V2 已经被弃用了一段时间,现在真的不推荐使用它。
猜你喜欢
  • 2017-02-27
  • 2016-03-07
  • 2012-09-25
  • 1970-01-01
  • 2015-08-31
  • 2014-01-26
  • 2014-11-02
  • 2011-07-24
  • 2011-10-29
相关资源
最近更新 更多