2012-12-13 13 views
6

Ogni volta che provo a caricare un video di grandi dimensioni tramite il caricamento diretto utilizzando l'API di YouTube. Ottengo un'eccezione OutOfMemory. C'è qualcosa che posso fare per sbarazzarmi di questo? L'API di YouTube non dice nulla riguardo al limite delle dimensioni dei video utilizzando il caricamento diretto.Upload diretto YouTube - Eccezione OutOfMemory

Ho rinunciato al caricamento diretto. Ora provo il modo di caricare la batteria. Il mio codice è sotto

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; 
    } 
} 

Qualcuno può dirmi cosa c'è che non va? Il mio video da 2 GB non viene caricato.

+1

Qual è il tuo codice? Cosa hai provato? Qual è il tuo codice di errore? –

+0

Dov'è l'eccezione? Forse basta fornire lo stacktrace –

+0

Riga: m_ResumableUploader.InsertAsync (...) Errore: System.Net.WebException: il server remoto ha restituito un errore: (403) Proibito. a System.Net.HttpWebRequest.GetResponse() –

risposta

5

La ragione per cui mi è stato sempre un errore 403 Forbidden è dovuta al fatto che io non passavo in:

  1. Nome utente & password
  2. Uno sviluppatore chiave

La variabile richiesta nel codice sopra non viene utilizzata/inviata nel caricamento. Quindi stavo facendo un upload non autorizzato.

4

È probabile che non si disponga degli oggetti. Assicurarsi che tutti gli oggetti usa e getta sono all'interno di un'istruzione using ..

Per esempio, questo codice sarà caricare un file di grandi dimensioni zip a un server:

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; 
} 
Problemi correlati