1

プログラムでYouTubeにビデオをアップロードしようとしています。このタスクには Node.js を使用することにしました。

XML 応答と HTTP ステータス コード 201 が返され、動画マネージャーに動画が表示されますが、動画には常に「失敗しました (動画ファイルを変換できません)」というメッセージが表示されます。

YouTube のページにある独自のアップローダを介してファイルをアップロードできますが、問題はありません。単一のアカウントにアップロードするだけでよいので、アカウントに OAuth2 を設定し、更新トークンを保存しました。リフレッシュ トークンはハードコードされていますが、以下の変数に置き換えます。

リフレッシュ トークン自体をリフレッシュする必要がありますか?

私のコード:

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();
4

1 に答える 1

2

アップロード中のファイルに問題がありました。この行:var file_path = process.argv[1] || "video.mp4";あるべきだったvar file_path = process.argv[2] || "video.mp4";

注 argv[ 1 ] は実行中のスクリプトへの絶対パスであり、argv[ 2 ] はスクリプトに渡される最初のコマンド ライン引数です。

もちろん、YouTube は「ビデオ」の変換に失敗します。それはビデオではなく、実行中のスクリプトでした。

于 2012-06-02T19:48:01.060 に答える