2010-06-08 91 views
5

我使用.NET創建視頻上傳應用程序。雖然 與YouTube進行通信並上傳文件,但是該文件的處理失敗。 YouTube向我發送錯誤訊息「上傳失敗 (無法轉換視頻文件)」。據稱這意味着,「你 視頻採用了我們的轉換器無法識別的格式...」youtube - 視頻上傳失敗 - 無法轉換文件 - 視頻編碼錯誤?

我有兩個不同的視頻,這兩者的上傳 和工藝精細,當我做手工嘗試。所以我懷疑我的代碼是 a。)沒有正確編碼視頻和/或b。)沒有正確發送我的API 請求。

下面是我如何構造我的API PUT請求和編碼 視頻:

任何建議什麼錯誤可能是,將不勝感激。

謝謝

P.S.我沒有使用客戶端庫,因爲我的應用程序將使用 可恢復的上載功能。因此,我手動構建我的API 請求。

文檔:http://code.google.com/intl/ja/apis/youtube/2.0/developers_guide_protocol_resumable_uploads.html#Uploading_the_Video_File

代碼:

  // new PUT request for sending video 
      WebRequest putRequest = WebRequest.Create(uploadURL); 

      // set properties 
      putRequest.Method = "PUT"; 
      putRequest.ContentType = getMIME(file); //the MIME type of the uploaded video file 

      //encode video 
      byte[] videoInBytes = encodeVideo(file); 

    public static byte[] encodeVideo(string video) 
    { 
     try 
     { 
      byte[] fileInBytes = File.ReadAllBytes(video); 
      Console.WriteLine("\nSize of byte array containing " + video + ": " + fileInBytes.Length); 
      return fileInBytes; 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine("\nException: " + e.Message + "\nReturning an empty byte array"); 
      byte [] empty = new byte[0]; 
      return empty; 
     } 
    }//encodeVideo 

      //encode custom headers in a byte array 
      byte[] PUTbytes = encode(putRequest.Headers.ToString()); 

      public static byte[] encode(string headers) 
      {    
       ASCIIEncoding encoding = new ASCIIEncoding(); 
       byte[] bytes = encoding.GetBytes(headers); 
       return bytes; 
      }//encode 

      //entire request contains headers + binary video data 
      putRequest.ContentLength = PUTbytes.Length + videoInBytes.Length; 

      //send request - correct? 
      sendRequest(putRequest, PUTbytes); 
      sendRequest(putRequest, videoInBytes); 

    public static void sendRequest(WebRequest request, byte[] encoding) 
    { 
     Stream stream = request.GetRequestStream(); // The GetRequestStream method returns a stream to use to send data for the HttpWebRequest. 

     try 
     { 
      stream.Write(encoding, 0, encoding.Length); 

     } 
     catch (Exception e) 
     { 
      Console.WriteLine("\nException writing stream: " + e.Message); 
     } 
    }//sendRequest 
+0

請不要重複標題中的「.NET」標籤。將它們留在它們所屬的標籤中。 – 2010-06-13 07:54:34

回答

0

我不知道是什麼格式的YouTube正在尋找,但如果它應該是你的Windows系統上識別的格式,我建議你保存將轉換的視頻轉換爲磁盤上的文件,然後嘗試打開它。

0

發送請求分兩部分完成...您發送標題,包括視頻的大小,因爲您有... YouTube用URL迴應,並將視頻發送到該URL ..看起來像你正試圖發送一個請求中的所有內容。有點像這樣。

Try 
     Try 
      _request = CType(WebRequest.Create(_requestUrl), HttpWebRequest) 

      With _request 
       .ContentType = "application/atom+xml; charset=UTF-8" 
       .ContentLength = _postBytes.Length 
       .Method = "POST" 
       .Headers.Add("Authorization", String.Format("GoogleLogin auth={0}", MasterAccessToken.ClientLoginToken)) 
       .Headers.Add("GData-Version", "2") 
       .Headers.Add("X-GData-Key", String.Format("key={0}", YouTube.Constants.Security.DEVELOPERKEY)) 
       .Headers.Add("Slug", filename) 
      End With 

      _writeStream = _request.GetRequestStream 
      With _writeStream 
       .Write(_postBytes, 0, _postBytes.Length) 
      End With 

      Using _response = CType(_request.GetResponse, HttpWebResponse) 
       With _response 
        If .StatusCode = HttpStatusCode.OK OrElse .StatusCode = HttpStatusCode.Created Then 
         _ans = _response.Headers("Location") 
        Else 
         Throw New WebException("Cannot get ClientLogin upload location", Nothing, WebExceptionStatus.ProtocolError, _response) 
        End If 
       End With 
      End Using 

     Catch ex As Exception 

     Finally 
      If _writeStream IsNot Nothing Then 
       _writeStream.Close() 
      End If 

     End Try 

     _videoUploadLocation = _ans 

     'Got the upload location..... now get the file 
     Dim _file As FileInfo = New FileInfo(filename) 
     Dim _fileLength As Integer 

     Using _fileStream As System.IO.FileStream = _file.OpenRead 
      _fileLength = CType(_fileStream.Length, Integer) 

      If _fileLength = 0 Then 
       Throw New FileLoadException("File appears to be of zero length in UploadVideoFromFileClientLogin:", filename) 
      End If 

      'create the webrequest 
      _request = CType(WebRequest.Create(_videoUploadLocation), HttpWebRequest) 

      'No authentication headers needed.. 
      With _request 
       .Timeout = 6000000  'Timeout for this request changed to 10 minutes 
       .ReadWriteTimeout = 6000000 
       .KeepAlive = True 
       .ContentType = "application/octet-stream" 
       .ContentLength = _fileLength 
       .Method = "POST" 
      End With 

      'and get the stream 
      _writeStream = _request.GetRequestStream 

      'And send it over the net 
      m_StreamUtils.CancelRequest = False 
      m_StreamUtils.SendStreamToStream(_fileStream, _writeStream, AddressOf UploadPogressChanged) 
      m_CancelRequest = m_StreamUtils.CancelRequest 
     End Using 

     If Not (m_CancelRequest) Then 

      Using _response = CType(_request.GetResponse, HttpWebResponse) 
       With _response 
        If .StatusCode = HttpStatusCode.Created Then 
         _ans = _response.ResponseUri.AbsoluteUri 
        Else 
         Throw New WebException("Cannot get ClientLogin upload location", Nothing, WebExceptionStatus.ProtocolError, _response) 
        End If 
       End With 
      End Using 
     Else 
      _ans = String.Empty 

     End If 

     If _writeStream IsNot Nothing Then 
      _writeStream.Close() 
     End If