我使用.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