SnowInferno
SnowInferno

Reputation: 449

Node.js YouTube API Upload unable to convert video

I'm trying to upload video to youtube programatically. I chose to use Node.js for the task.

I get an XML response as well as an HTTP Status Code of 201 and I see the video appear in video manager, however the video always has the message "Failed (unable to convert video file)".

I can upload the file through YouTube's own uploader on their page and there are no problems. I only have to upload to a single account, so I set up the OAuth2 for the account and stored the refresh token. The refresh token is hard-coded, though I replace it with a variable below.

Does the refresh token need to, itself, be refreshed?

My code:

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

Upvotes: 1

Views: 925

Answers (1)

SnowInferno
SnowInferno

Reputation: 449

The problem was in the file being uploaded. This line: var file_path = process.argv[1] || "video.mp4"; should have been var file_path = process.argv[2] || "video.mp4";

Note argv[1] is the absolute path to the script being run, argv[2] is the first command line argument passed to the script.

Of course YouTube would fail to convert the "video", it wasn't video at all it was the script being run.

Upvotes: 2

Related Questions