c# - YouTube 直接上传 - OutOfMemory 异常

标签 c# windows-services youtube-api

每当我尝试使用 YouTube API 通过直接上传上传大型视频时。我收到 OutOfMemory 异常。我能做些什么来摆脱这个吗? YouTube API 没有说明使用直接上传的视频大小限制。

我放弃了直接上传。现在我尝试断点续传的方式。我的代码如下。

YouTubeRequest request;
YouTubeRequestSettings settings = new YouTubeRequestSettings("YouTube Upload", Client Key, "Username", "Password");
request = new YouTubeRequest(settings);
Video newVideo = new Video();

ResumableUploader m_ResumableUploader = null;
Authenticator YouTubeAuthenticator;

m_ResumableUploader = new ResumableUploader(256); //chunksize 256 kilobyte
m_ResumableUploader.AsyncOperationCompleted += new AsyncOperationCompletedEventHandler(m_ResumableUploader_AsyncOperationCompleted);
m_ResumableUploader.AsyncOperationProgress += new AsyncOperationProgressEventHandler(m_ResumableUploader_AsyncOperationProgress);

YouTubeAuthenticator = new ClientLoginAuthenticator("YouTubeUploader", ServiceNames.YouTube, "kjohnson@resoluteinnovations.com", "password");

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

System.IO.FileStream stream = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);

byte[] chunk = new byte[256000];int count = 1;
while (true) {
    int index = 0;
    while (index < chunk.Length) {
        int bytesRead = stream.Read(chunk, index, chunk.Length - index);
        if (bytesRead == 0) {
            break;
        }
        index += bytesRead;
    }
    if (index != 0) { // Our previous chunk may have been the last one
        newVideo.MediaSource = new MediaFileSource(new MemoryStream(chunk), filePath, "video/quicktime");
        if (count == 1) {
            m_ResumableUploader.InsertAsync(YouTubeAuthenticator, newVideo.YouTubeEntry, new MemoryStream(chunk));
            count++;
        }
        else
            m_ResumableUploader.ResumeAsync(YouTubeAuthenticator, new Uri("http://uploads.gdata.youtube.com/resumable/feeds/api/users/uploads"), "POST", new MemoryStream(chunk), "video/quicktime", new object());
    }
    if (index != chunk.Length) { // We didn't read a full chunk: we're done
        break;
    }
}

谁能告诉我哪里出了问题?我的 2 GB 视频无法上传。

最佳答案

我收到 403 Forbidden 错误的原因是我没有传入:

  1. 用户名和密码
  2. 开发者 key

以上代码中的请求变量未在上传中使用/发送。因此我进行了未经授权的上传。

关于c# - YouTube 直接上传 - OutOfMemory 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13861802/

相关文章:

c# - 如何使用 Bootstrap 添加事件样式

c# - 针对匿名对象的 System.Text.Json 序列化

c# - 尝试调用 COM 库时服务引发异常

Windows 服务无法访问共享位置

c# - 获取特定语言的区域信息

c# - 将数据从字符串值发布到 asp net core web api Controller 方法

c# - 如何允许用户编辑 Program Files 下的 App.config 是记事本或其他编辑器?

css - 视频嵌入和图像在一行中的高度相等

ffmpeg - YouTube 直播静音检测

android - Android 的 Youtube Player API 控件隐藏