NodeJS Youtube API v3如何将多个视频上传到youtube?

时间:2016-10-18 09:38:41

标签: node.js youtube-data-api

我是新手NodeJS,只是在v3天前找到了Youtube API。我尝试将3个视频上传到我的频道,但效果不佳。以下是错误图片:

enter image description here

3个视频包括:800mb,600mb和100mb。只有视频100mb可以,其他人都失败了。 但是,如果我上传1个视频有800mb,那没关系,没问题。我不知道原因。这是我的代码:

"use strict";
const Youtube = require("./lib")
, fs = require("fs")
, readJson = require("r-json")
, Lien = require("lien")
, Logger = require("bug-killer")
, opn = require("opn")
, prettyBytes = require("pretty-bytes")
;

// Import list videos
var listVideos = require("./Videos.json");
console.log(typeof listVideos);

// Declare Result
var result;


// I downloaded the file from OAuth2 -> Download JSON
const CREDENTIALS = readJson(`${__dirname}/example/credentials.json`);


// Init lien server
let server = new Lien({
    host: "localhost"
  , port: 5000
});

// Authenticate
// You can access the Youtube resources via OAuth2 only.
// https://developers.google.com/youtube/v3/guides/moving_to_oauth#service_accounts

let oauth = Youtube.authenticate({
    type: "oauth"
  , client_id: CREDENTIALS.web.client_id
  , client_secret: CREDENTIALS.web.client_secret
  , redirect_url: CREDENTIALS.web.redirect_uris[0]
});




opn(oauth.generateAuthUrl({
    access_type: "offline"
  , scope: ["https://www.googleapis.com/auth/youtube.upload"]
}));

// Handle oauth2 callback
server.addPage("/oauth2callback", lien => {
    Logger.log("Trying to get the token using the following code: " + lien.query.code);
    oauth.getToken(lien.query.code, (err, tokens) => {

        if (err) {
            //lien.lien(err, 400);
            return Logger.log(err);
        }

        Logger.log("Got the tokens.");

        oauth.setCredentials(tokens);

        lien.end("The video is being uploaded. Check out the logs in the terminal.");

        for (var video in listVideos) {
            console.log(listVideos[video].title);
            result = UploadYoutube(listVideos[video].title, listVideos[video].description, listVideos[video].tags,
            listVideos[video].fileName);
        }



        setInterval(function () {
            Logger.log(`${prettyBytes(result.req.connection._bytesDispatched)} bytes uploaded.`);
        }, 250);
    });
});

function UploadYoutube (myTitle, myDescription, myTags, myFileLocation) {
    var req = Youtube.videos.insert({
            resource: {
                // Video title and description
                snippet: {
                    title: myTitle
                  , description: myDescription
                  , tags: myTags

                }
                // I don't want to spam my subscribers
              , status: {
                    privacyStatus: "private"

                }
            }


            // This is for the callback function
          , part: "snippet,status"

            // Create the readable stream to upload the video
          , media: {
                body: fs.createReadStream(myFileLocation)
            }
        }, (err, data) => {
            console.log("Done.");
            process.exit();
        });
        return req;
}

这是我的json文件,其中包含nodejs文件中使用的视频信息:

{
    "1" : {
        "title" : "Quang Cao Hai Vinamilk Con Bo Cuoi",
        "description" : "Quang Cao Cho Be, Quang Cao Cho Be An Ngon",
        "tags" : ["Quang Cao Cho Be", "Quang Cao Cho Be An Ngon", "Clip Quang Cao"],
        "fileName" : "Quang Cao Hai Vinamilk Con Bo Cuoi.mp4"

    },
    "2" : {
        "title" : "Clip Ngan Vui Nhon",
        "description" : "Clip Ngan Vui Nhon, Clip Ngan Vui Nhon Minion",
        "tags" : ["Clip Ngan Vui Nhon", "Clip Ngan Vui Nhon Minion"],
        "fileName" : "Clip Ngan  Vui Nhon.mp4"


    },
    "3" : {
        "title" : "Phim Quang Cao Ngan",
        "description" : "Phim Quang Cao Ngan, Phim Quang Cao Ngan Hay Nhat",
        "tags" : ["Phim Quang Cao Ngan", "Phim Quang Cao Ngan Hay Nhat"],
        "fileName" : "Phim Quang Cao Ngan.mp4"

    }

}

请告诉我,问题出在哪里。以及如何解决它?谢谢大家

1 个答案:

答案 0 :(得分:0)

以与调用credetials.json文件的属性相同的方式,可能值得为video.json文件采用类似的方法。

//即您已经使用凭据完成了以下操作... const CREDENTIALS = readJson(${__dirname}/example/credentials.json);

//所以带有您的Video.json信息...。 var videos = readJson(`$ {__ dirname} /videos.json“);

//再次仔细检查文件路径

另一个含义可能是您是否可以在localhost / 5000上上传多个视频