2012-12-13 34 views
6

每當我嘗試通過使用YouTube API直接上傳來上傳大型視頻時。我得到一個OutOfMemory異常。有什麼我可以做的,以擺脫這一點? YouTube API沒有提及使用直接上傳的視頻大小限制。YouTube直接上傳 - OutOfMemory異常

我放棄了直接上傳。現在我嘗試可恢復的上傳方式。我的代碼如下。

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, "[email protected]", "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視頻無法上傳。

+1

你的代碼是什麼?你試過了什麼?什麼是您的錯誤代碼? –

+0

拋出異常在哪裏?也許只是提供堆棧跟蹤 –

+0

行:m_ResumableUploader.InsertAsync(...)錯誤:System.Net.WebException:遠程服務器返回錯誤:(403)禁止。 at System.Net.HttpWebRequest.GetResponse() –

回答

5

我得到一個403 Forbidden錯誤的原因是由於事實上,我並沒有傳遞:

  1. 用戶名密碼&
  2. 開發人員鍵

上述代碼中的請求變量未在上載中使用/發送。所以我正在做一個未經授權的上傳。

4

有可能是你沒有處理你的對象。確保所有一次性的對象是使用語句中..

例如,該代碼將上傳一個大的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; 
}