Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload a video to youtube with node.js

I've made some headway with the official Google API node client but I've reached a bit of a dead end figuring out how to get my video file up to youtube.

I have a video: example.mp4

I have this code:

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

I only get an error using the insert method (which I expected with no params passed), the client initialises and returns fine.

I'm not sure how to pass the video (in the same directory as the script) to YouTube. Just a pointer would be greatly appreciated.

like image 912
timstermatic Avatar asked Sep 14 '13 11:09

timstermatic


People also ask

Can you upload videos with YouTube API?

Stay organized with collections Save and categorize content based on your preferences. This guide provides and explains a Python script that uploads a YouTube video using the YouTube Data API. The code uses the Google APIs Client Library for Python.

Is YouTube use node JS?

There is the YouTube Live Streaming API. It is an HTTP-based API, so you will be able to access it from Node. js as well as basically any other programming language capable of making HTTP-requests.


2 Answers

How about this code:

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, '  '));
    });
});
like image 98
Gaurav Joseph Avatar answered Nov 15 '22 19:11

Gaurav Joseph


You could use this package to do that: https://github.com/h2non/youtube-video-api

like image 24
Tomas Avatar answered Nov 15 '22 21:11

Tomas