我使用.NET創建視頻上傳應用程序。雖然 與YouTube進行通信並上傳文件,但是該文件的處理失敗。 YouTube向我發送錯誤訊息「上傳失敗 (無法轉換視頻文件)」。據稱這意味着,「你 視頻採用了我們的轉換器無法識別的格式...」youtube - 視頻上傳失敗 - 無法轉換文件 - 視頻編碼錯誤?
我有兩個不同的視頻,這兩者的上傳 和工藝精細,當我做手工嘗試。所以我懷疑我的代碼是 a。)沒有正確編碼視頻和/或b。)沒有正確發送我的API 請求。
下面是我如何構造我的API PUT請求和編碼 視頻:
任何建議什麼錯誤可能是,將不勝感激。
謝謝
P.S.我沒有使用客戶端庫,因爲我的應用程序將使用 可恢復的上載功能。因此,我手動構建我的API 請求。
代碼:
// 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
請不要重複標題中的「.NET」標籤。將它們留在它們所屬的標籤中。 – 2010-06-13 07:54:34