使用node.js将视频上传到youtube

时间:2013-09-14 11:16:43

标签: node.js video youtube

我已经与官方Google API node client取得了一些进展,但我已经达到了一个死胡同,想办法将我的视频文件传输到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);
  }); 
});

我只使用insert方法得到错误(我预期没有传递params),客户端初始化并返回正常。

我不确定如何将视频(与脚本位于同一目录中)传递给YouTube。只需一个指针就会非常感激。

3 个答案:

答案 0 :(得分:10)

这段代码怎么样:

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, '  '));
    });
});

答案 1 :(得分:1)

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

答案 2 :(得分: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();
});