【问题标题】:YouTube Direct Upload - OutOfMemory ExceptionYouTube 直接上传 - 内存不足异常
【发布时间】:2012-12-01 10:59:58
【问题描述】:

每当我尝试使用 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 视频没有上传。

【问题讨论】:

  • 你的代码是什么?你试过什么?你的错误代码是什么?
  • 异常抛出在哪里?也许只是提供堆栈跟踪
  • 行:m_ResumableUploader.InsertAsync(...) 错误:System.Net.WebException:远程服务器返回错误:(403) 禁止。在 System.Net.HttpWebRequest.GetResponse()

标签: c# windows-services youtube-api


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    您可能没有处理您的对象。确保所有一次性对象都在 using 语句中..

    例如,这段代码会将一个大的 zip 文件上传到服务器:

    try
    {
        using (Stream ftpStream = FTPRequest.GetRequestStream())
        {
            using (FileStream file = File.OpenRead(ImagesZipFile))
            {
                // set up variables we'll use to read the file
                int length = 1024;
                byte[] buffer = new byte[length];
                int bytesRead = 0;
    
                // write the file to the request stream
                do
                {
                    bytesRead = file.Read(buffer, 0, length);
                    ftpStream.Write(buffer, 0, bytesRead);
                }
                while (bytesRead != 0);
            }
        }
    }
    catch (Exception e)
    {
        // throw the exception
        throw e;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-09
      • 2015-09-28
      • 2010-10-05
      相关资源
      最近更新 更多