Node.js YouTube API上传无法转换video

我正尝试以编程方式将video上传到YouTube。 我select使用Node.js来完成任务。

我得到一个XML响应以及一个HTTP状态代码201,我看到video出现在videopipe理器中,但video总是有“失败(无法转换video文件)”信息。

我可以通过他们自己的页面上传自己的上传file upload文件,没有问题。 我只需要上传到一个帐户,所以我设置了帐户的OAuth2并存储了刷新令牌。 刷新标记是硬编码的,尽pipe我将其replace为下面的variables。

刷新标记本身是否需要刷新?

我的代码:

var qs = require('querystring'), https = require('https'), fs = require('fs'); var p_data = qs.stringify({ client_id: myClientID, client_secret: myClientSecret, refresh_token: refreshTokenForAccount, grant_type: 'refresh_token' }); var p_options = { host: 'accounts.google.com', port: '443', method: 'POST', path: '/o/oauth2/token', headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'Content-Length': p_data.length, 'X-GData-Key': myDeveloperKey } }; var file_path = process.argv[1] || "video.mp4"; var json = ""; var p_req = https.request(p_options, function(resp){ resp.setEncoding( 'utf8' ); resp.on('data', function( chunk ){ json += chunk; }); resp.on("end", function(){ debugger; var access_token = JSON.parse(json).access_token; var title="test upload1", description="Second attempt at an API video upload", keywords="", category="Comedy"; var file_reader = fs.createReadStream(file_path, {encoding: 'binary'}); var file_contents = ''; file_reader.on('data', function(data) { file_contents += data; }); file_reader.on('end', function() { var xml = '<?xml version="1.0"?>' + '<entry xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xmlns:yt="http://gdata.youtube.com/schemas/2007">' + ' <media:group>' + ' <media:title type="plain">' + title + '</media:title>' + ' <media:description type="plain">' + description + '</media:description>' + ' <media:category scheme="http://gdata.youtube.com/schemas/2007/categories.cat">' + category + '</media:category>' + ' <media:keywords>' + keywords + '</media:keywords>' + ' </media:group>' + '</entry>'; var boundary = Math.random(); var post_data = []; var part = ''; part = "--" + boundary + "\r\nContent-Type: application/atom+xml; charset=UTF-8\r\n\r\n" + xml + "\r\n"; post_data.push(new Buffer(part, "utf8")); part = "--" + boundary + "\r\nContent-Type: video/mp4\r\nContent-Transfer-Encoding: binary\r\n\r\n"; post_data.push(new Buffer(part, 'utf8')); post_data.push(new Buffer(file_contents, 'binary')); post_data.push(new Buffer("\r\n--" + boundary + "--\r\n\r\n", 'utf8')); var post_length = 0; for(var i = 0; i < post_data.length; i++) { post_length += post_data[i].length; } var options = { host: 'uploads.gdata.youtube.com', port: 443, path: '/feeds/api/users/default/uploads', method: 'POST', headers: { 'Authorization': 'Bearer ' + access_token, 'X-GData-Key': myDeveloperKey, 'Slug': 'video.mp4', 'Content-Type': 'multipart/related; boundary="' + boundary + '"', 'Content-Length': post_length, 'Connection': 'close' } } var req = https.request(options, function(res) { res.setEncoding('utf8'); console.dir(res.statusCode); console.dir(res.headers); var response = ''; res.on('data', function(chunk) { response += chunk; }); res.on('end', function() { console.log( "We got response: " ); console.log(response); }); }); for (var i = 0; i < post_data.length; i++) { req.write(post_data[i]); } req.on('error', function(e) { console.error(e); }); req.end(); }); }); }); p_req.write(p_data); p_req.end(); 

问题出在上传的文件中。 这一行: var file_path = process.argv[1] || "video.mp4"; var file_path = process.argv[1] || "video.mp4"; 应该是var file_path = process.argv[2] || "video.mp4"; var file_path = process.argv[2] || "video.mp4";

注意argv [ 1 ]是正在运行的脚本的绝对path,argv [ 2 ]是传递给脚本的第一个命令行参数。

当然,YouTube将无法转换“video”,这完全不是video,而是脚本的运行。