0

I'm trying to upload large video files to youtube via the C# API using the ResumableUploader

The code looks like this:

            var settings = new YouTubeRequestSettings(Configuration.YouTubeApplicationName, Configuration.YouTubeApplicationKey, Configuration.YouTubeUsername, Configuration.YouTubePassword);
            settings.Timeout = int.MaxValue;

            var newVideo = new Video();

            newVideo.Title = title.Trim();
            newVideo.Tags.Add(new MediaCategory("Games", YouTubeNameTable.CategorySchema));
            newVideo.Keywords = keywords;
            newVideo.Description = description;
            newVideo.YouTubeEntry.Private = false;
            newVideo.Tags.Add(new MediaCategory("StarCraft2, Replay", YouTubeNameTable.DeveloperTagSchema));
            var contentType = MediaFileSource.GetContentTypeForFileName(filePathToUpload);
            newVideo.YouTubeEntry.MediaSource = new MediaFileSource(filePathToUpload, contentType);

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

            var resumableUploader = new ResumableUploader(256); //chunksize 256 kilobyte
            resumableUploader.AsyncOperationCompleted += resumableUploader_AsyncOperationCompleted;
            resumableUploader.AsyncOperationProgress += resumableUploader_AsyncOperationProgress;

            var youTubeAuthenticator = new ClientLoginAuthenticator(Configuration.YouTubeApplicationName, ServiceNames.YouTube, Configuration.YouTubeUsername, Configuration.YouTubePassword);
            youTubeAuthenticator.DeveloperKey = Configuration.YouTubeApplicationKey;

           resumableUploader.InsertAsync(youTubeAuthenticator, newVideo.YouTubeEntry, new object());

I try to get the video ID when the async opertion is finished like this:

    private static void resumableUploader_AsyncOperationCompleted(object sender, AsyncOperationCompletedEventArgs e)
{
    try
    {
        var settings = new YouTubeRequestSettings(Configuration.YouTubeApplicationName, Configuration.YouTubeApplicationKey, Configuration.YouTubeUsername, Configuration.YouTubePassword);
        var request = new YouTubeRequest(settings);
        Video v = request.ParseVideo(e.ResponseStream);        

    }
    catch (Exception ex)
    {
        //Upload has been disturbed.
    }  

}

The problem is. Sometime there is a problem with the upload, shaky connection or stuff like that. When an error occurs AsyncOperationCompleted is getting called.

Now the question is. How do I resume the upload? I can get the current position via the AsyncOperationProgress event. But how do I proceed to continue the upload?

Emil C
  • 1,315
  • 4
  • 15
  • 27

1 Answers1

0

I know this is an old question but I found it while trying to do the same thing with V3 of the YouTube API. This looks like code from an earlier version of the API so my question asking the same thing for V3 of the API may help if you upgrade.

I've pasted some older code of mine if anyone is struggling with this but I'm switching to the new version and replacing all this code. The youtube_upload object is my own rolled class and not part of the api. I'm also using ReSharper now and following their naming conventions.

//Grab the rendered YouTube video ready for upload
string content_type = MediaFileSource.GetContentTypeForFileName(youtube_upload.YouTubeFileName);
MediaFileSource media_file_source = new MediaFileSource(youtube_upload.YouTubeFileName, content_type);

//Generate a media stream and move to the currently uploaded position within it
media_stream_resume = media_file_source.GetDataStream();
media_stream_resume.Seek(youtube_upload.CurrentPosition, SeekOrigin.Begin);

youtube_uploading_id = youtube_upload.YouTubeID;        

//Attempt to resume the upload
resumable_uploader.ResumeAsync(youtube_authenticator, youtube_upload.ResumeUri,
  youtube_upload.httpVerb, media_stream_resume, content_type, youtube_upload);

youtube_upload.ErrorText = Constants.YouTubeStatusResuming;
youtube_upload.SaveYouTube();  
Community
  • 1
  • 1
Mick
  • 276
  • 2
  • 9