youtube - 视频上传失败 - 无法转换文件 - 视频编码错误?
我正在使用 .NET 创建视频上传应用程序。虽然它是 与 YouTube 通信并上传文件,处理 该文件失败。 YouTube 向我显示错误消息“上传失败 (无法转换视频文件)。”这据称意味着“您的 视频的格式是我们的转换器无法识别的...”
我尝试了两个不同的视频,这两个视频都上传 当我手动执行时,处理得很好。所以我怀疑我的代码是 a.) 未正确编码视频和/或 b.) 未发送我的 API 正确请求。
下面是我如何构建 API PUT 请求并对 视频:
任何有关错误可能是什么的建议将不胜感激。
谢谢
我没有使用客户端库,因为我的应用程序将使用 断点续传功能。因此,我正在手动构建我的 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
I am using .NET to create a video uploading application. Although it's
communicating with YouTube and uploading the file, the processing of
that file fails. YouTube gives me the error message, "Upload failed
(unable to convert video file)." This supposedly means that "your
video is in a format that our converters don't recognize..."
I have made attempts with two different videos, both of which upload
and process fine when I do it manually. So I suspect that my code is
a.) not encoding the video properly and/or b.) not sending my API
request properly.
Below is how I am constructing my API PUT request and encoding the
video:
Any suggestions on what the error could be would be appreciated.
Thanks
P.S. I'm not using the client library because my application will use
the resumable upload feature. Thus, I am manually constructing my API
requests.
Documentation: http://code.google.com/intl/ja/apis/youtube/2.0/developers_guide_protocol_resumable_uploads.html#Uploading_the_Video_File
Code:
// 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道 YouTube 正在寻找什么格式,但如果它是您的 Windows 系统可以识别的格式,我建议您将转换后的视频保存到磁盘上的文件,然后尝试打开它。
I don't know what format YouTube is looking for, but if it's a format that should be recognizable on your Windows system, I suggest you save your converted video to a file on disk, then try to open it.
发送请求分两部分完成...您发送标头,包括视频的大小...YouTube 用一个网址进行响应,然后您将视频发送到该网址。看起来您正在尝试一次发送所有请求。有点像这样。
The send request in done in 2 parts... You send the header, including the size of the video as you have... YouTube responds with a url, and you send the video to that url.. Looks like you are trying to send all in one request. Something a bit like this.