.NET を使用してビデオ アップロード アプリケーションを作成しています。YouTube と通信してファイルをアップロードしていますが、そのファイルの処理は失敗します。YouTube で「アップロードに失敗しました (動画ファイルを変換できません)」というエラー メッセージが表示されます。これはおそらく、「あなたの動画は当社のコンバーターが認識できない形式です...」という意味です。
私は 2 つの異なるビデオを試してみましたが、どちらも手動でアップロードして正常に処理されました。したがって、私のコードは a.) ビデオを適切にエンコードしていない、および/または b.) API リクエストを適切に送信していないと思われます。
以下は、API PUT リクエストを作成してビデオをエンコードする方法です。
エラーが何であるかについての提案をいただければ幸いです。
ありがとう
PS 私のアプリケーションは再開可能なアップロード機能を使用するため、クライアント ライブラリは使用していません。したがって、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