处理结束时以编程方式通知

时间:2016-06-08 15:05:51

标签: youtube google-api youtube-api

将视频上传到YouTube时,生成的视频链接立即可用,但视频尚未准备好播放,因为Youtube正在处理它。如何在处理完成后以编程方式通知?我正在使用node.js googleapis。

1 个答案:

答案 0 :(得分:0)

/youtube/videos/list提供了查询视频上传状态的API。

var videoId = 'aBcdEfjhkLm'

const Youtube = require("youtube-api"), fs = require("fs"),
    readJson = require("r-json"), Lien = require("lien"),
    Logger = require("bug-killer"), opn = require("opn"),
    prettyBytes = require("pretty-bytes");

const CREDENTIALS = readJson(`${__dirname}/credentials.json`);
let server = new Lien({ host: "localhost" , port: 5000 });
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"]
}));

server.addPage("/oauth2callback", lien => {
    Logger.log("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(
        '<script>window.close()</script>'

        );
        var req = Youtube.videos.list({
            id: 'fEJc2CwddgU',
            part: "status",
        },
        (err, data) => {
            if (err) {
                console.log('err:', err)
                process.exit()
            }
            var s = data.items[0].status.uploadStatus
            if (s == 'uploaded') console.log('Video is being processed.')
            if (s == 'processed') console.log('Video processing completed.')
            process.exit()
        });
    });
});