我正在尝试使用ResumableUploader
通过C#API将大型视频文件上传到youtube代码如下所示:
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());
我尝试在async opertion完成时获取视频ID,如下所示:
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.
}
}
问题是。有时上传,抖动连接或类似的东西有问题。当发生错误时,AsyncOperationCompleted被调用。
现在的问题是。如何恢复上传?我可以通过AsyncOperationProgress事件获取当前位置。但是,我该如何继续上传?
答案 0 :(得分:0)
我知道这是一个老问题,但我在尝试使用YouTube API的V3做同样的事情时发现了这个问题。这看起来像早期版本的API中的代码,因此my question在升级时可能会对API的V3提出相同的建议。
如果有人在努力解决这个问题,我已经粘贴了我的一些旧代码,但我正在切换到新版本并替换所有这些代码。 youtube_upload对象是我自己的滚动类,不是api的一部分。我现在也在使用ReSharper并遵循他们的命名惯例。
//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();