9

我在官方Google API 节点客户端方面取得了一些进展,但在弄清楚如何将我的视频文件上传到 youtube 上时,我遇到了一些死胡同。

我有一个视频:example.mp4

我有这个代码:

OAuth2Client = googleapis.OAuth2Client;               
var oauth2Client = new OAuth2Client('XXXXXX', 'XXXXXXX', 'http://callback_url');

googleapis.discover('youtube', 'v3').execute(function(err, client) {
  if (err) console.log(err);
  // I think the following is the bones of what I want to do
  client.youtube.videos.insert({}).execute(function(err, client) {
    if (err) console.log(err);
  }); 
});

我只使用插入方法(我期望没有传递参数)得到一个错误,客户端初始化并返回正常。

我不确定如何将视频(与脚本在同一目录中)传递给 YouTube。只是一个指针将不胜感激。

4

3 回答 3

13

这段代码怎么样:

var googleapis = require('googleapis');

googleapis.discover('youtube', 'v3').execute(function(err, client) {

var metadata = {
    snippet: { title:'title', description: 'description'}, 
    status: { privacyStatus: 'private' }
};

client
    .youtube.videos.insert({ part: 'snippet,status'}, metadata)
    .withMedia('video/mp4', fs.readFileSync('user.flv'))
    .withAuthClient(auth)
    .execute(function(err, result) {
        if (err) console.log(err);
        else console.log(JSON.stringify(result, null, '  '));
    });
});
于 2014-01-26T04:20:54.613 回答
1

你可以使用这个包来做到这一点: https ://github.com/h2non/youtube-video-api

于 2015-03-03T11:06:07.633 回答
0

这段代码对我有用:

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, 'ascii'));
    post_data.push(new Buffer(file_contents, 'binary'));
    post_data.push(new Buffer("\r\n--" + boundary + "--"), 'ascii');

    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: 80,
      path: '/feeds/api/users/default/uploads?alt=json',
      method: 'POST',
        headers: {
            'Authorization': 'GoogleLogin auth=' + auth_key,
            'GData-Version': '2',
            'X-GData-Key': 'key=' + exports.developer_key,
            'Slug': 'example.mp4',
            'Content-Type': 'multipart/related; boundary="' + boundary + '"',
            'Content-Length': post_length,
            'Connection': 'close'
        }
    }

    var req = http.request(options, function(res)
    {
        res.setEncoding('utf8');

        var response = '';
        res.on('data', function(chunk)
        {
            response += chunk;
        });
        res.on('end', function()
        {
            console.log(response);
            response = JSON.parse(response);

            callback(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();
});
于 2014-01-26T21:54:23.717 回答