在一定数量的视频上传到Youube后,它会停止上传

时间:2017-04-25 13:14:12

标签: c# youtube

当我上传一定数量的视频上传时,突然它停止工作,即使关闭我的程序并重新启动它仍然无法上传,我100%肯定代码工作因为它上传了一些时间,然后停止......

uploadvideo.cs

using System;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Upload;
using Google.Apis.Util.Store;
using Google.Apis.YouTube.v3;
using Google.Apis.YouTube.v3.Data;
using System.Windows;

namespace articletoyoutube
{
    /// <summary>
    /// YouTube Data API v3 sample: upload a video.
    /// Relies on the Google APIs Client Library for .NET, v1.7.0 or higher.
    /// See https://code.google.com/p/google-api-dotnet-client/wiki/GettingStarted
    /// </summary>
    class UploadVideo
    {
        // to access form controlls
        //Form1 core = new Form1();

        public async Task Run(string articlename, string filelocation)
        {
            UserCredential credential;
            using (var stream = new FileStream("secret.json", FileMode.Open, FileAccess.Read))
            {
                credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    // This OAuth 2.0 access scope allows an application to upload files to the
                    // authenticated user's YouTube channel, but doesn't allow other types of access.
                    new[] {
                        YouTubeService.Scope.YoutubeUpload
                    },
                    "user",
                    CancellationToken.None
                );
            }


            var youtubeService = new YouTubeService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = Assembly.GetExecutingAssembly().GetName().Name
            });

            var video = new Video();
            video.Snippet = new VideoSnippet();
            video.Snippet.Title = articlename;
            video.Snippet.Description = "Subscribe to get videos 24/7";
            video.Snippet.Tags = new string[] {
                "entertainment"
            };
            video.Snippet.CategoryId = "24"; // See https://developers.google.com/youtube/v3/docs/videoCategories/list
            video.Status = new VideoStatus();
            video.Status.PrivacyStatus = "public"; // or "private" or "public"
            var filePath = filelocation; // Replace with path to actual movie file.

            using (var fileStream = new FileStream(filePath, FileMode.Open))
            {
                var videosInsertRequest = youtubeService.Videos.Insert(video, "snippet,status", fileStream, "video/*");
                videosInsertRequest.ProgressChanged += videosInsertRequest_ProgressChanged;
                videosInsertRequest.ResponseReceived += videosInsertRequest_ResponseReceived;
                await videosInsertRequest.UploadAsync();
            }
        }

        void videosInsertRequest_ProgressChanged(Google.Apis.Upload.IUploadProgress progress)
        {
            /*
            switch (progress.Status)
            {
                case UploadStatus.Uploading:
                    core.prog_up.Text = "{0} bytes sent." + progress.BytesSent;
                    break;

                case UploadStatus.Failed:
                    core.status.Text = "An error prevented the upload from completing.\n{0}" + progress.Exception;
                    core.status.ForeColor = System.Drawing.Color.Red;
                    break;
            }*/
        }

        void videosInsertRequest_ResponseReceived(Video video)
        {
            //core.prog_up.Text = "Video id '{0}' was successfully uploaded." + video.Id;
        }
    }
}

我通过form1.cs上传,如下:

            try
            {
                this.Invoke((MethodInvoker)delegate
                {
                    prog_up.Visible = true;
                    prog_up.Text = "Uploading video to Youtube. Please be patient...";
                    prog_up.ForeColor = System.Drawing.Color.Green;
                });
                await new UploadVideo().Run("Video name", file);

                Directory.Delete(folder, true);
                addtoque(bitmap, file, "video name");
                await addtodb(link);


            }

我没有收到任何错误,它只是没有上传,这是为什么会发生这种情况或如何解决? 文件是我的avi文件的位置,它存在。

编辑:我不得不使用fidler调试流量,我收到错误“太多视频”这是一个api限制,这是可以理解的,但它应该向我显示错误,因为我已将上传包装在尝试并捕获阻止....没有显示错误的原因?

0 个答案:

没有答案