Javascript NodeJS要上传带有Youtube API的视频列表,挂起/崩溃挂起?
所有这些视频都可以上传到youtube。 这个程序在上传一个视频时运行良好。 它第一次上传了6条,之后每次上传1条,三次 在这四个视频中,没有一个视频是“上传”或“处理”的,它只是在所有视频上显示“待处理”,但标题/标签/描述加载良好。它们永远不会退出挂起状态或有任何状态更改,程序挂起在我的端,尽管循环一直运行。但是没有来自youtuber.js的消息 我最困惑的是,为什么它要运行18次循环,而不是先运行循环,然后再运行uploadVideos功能,然后返回循环,然后重复 功能文件:Javascript NodeJS要上传带有Youtube API的视频列表,挂起/崩溃挂起?,javascript,node.js,youtube-api,Javascript,Node.js,Youtube Api,所有这些视频都可以上传到youtube。 这个程序在上传一个视频时运行良好。 它第一次上传了6条,之后每次上传1条,三次 在这四个视频中,没有一个视频是“上传”或“处理”的,它只是在所有视频上显示“待处理”,但标题/标签/描述加载良好。它们永远不会退出挂起状态或有任何状态更改,程序挂起在我的端,尽管循环一直运行。但是没有来自youtuber.js的消息 我最困惑的是,为什么它要运行18次循环,而不是先运行循环,然后再运行uploadVideos功能,然后返回循环,然后重复 功能文件: const
const fs = require('fs');
const readline = require('readline');
const assert = require('assert')
const {google} = require('googleapis');
const OAuth2 = google.auth.OAuth2;
/*
// video category IDs for YouTube:
const categoryIds = {
Gaming: 20
}
*/
const SCOPES = ['https://www.googleapis.com/auth/youtube.upload'];
const TOKEN_PATH = './' + 'client_oauth_token.json';
exports.uploadVideo = (vid, thumb, title, description, tags) => {
videoFilePath = "../gamevids/" + vid
thumbFilePath = "../gamevids/" + thumb
assert(fs.existsSync(videoFilePath))
assert(fs.existsSync(thumbFilePath))
fs.readFile('./client_secret.json', function processClientSecrets(err, content) {
if (err) {
console.log('Error loading client secret file: ' + err);
return;
}
authorize(JSON.parse(content), (auth) => uploadVideo(auth, videoFilePath, thumbFilePath, title, description, tags));
});
}
function uploadVideo(auth, videoFilePath, thumbFilePath, title, description, tags) {
const service = google.youtube('v3')
service.videos.insert({
auth: auth,
part: 'snippet,status',
requestBody: {
snippet: {
title,
description,
tags,
categoryId: categoryIds.ScienceTechnology,
defaultLanguage: 'en',
defaultAudioLanguage: 'en'
},
status: {
privacyStatus: "private"
},
},
media: {
body: fs.createReadStream(videoFilePath),
},
}, function(err, response) {
if (err) {
console.log('The API returned an error: ' + err);
return;
}
console.log(response.data)
console.log('Video uploaded. Uploading the thumbnail now.')
service.thumbnails.set({
auth: auth,
videoId: response.data.id,
media: {
body: fs.createReadStream(thumbFilePath)
},
}, function(err, response) {
if (err) {
console.log('The API returned an error: ' + err);
return;
}
console.log(response.data)
})
});
}
/**
* Create an OAuth2 client with the given credentials, and then execute the
* given callback function.
*
* @param {Object} credentials The authorization client credentials.
* @param {function} callback The callback to call with the authorized client.
*/
function authorize(credentials, callback) {
const clientSecret = credentials.installed.client_secret;
const clientId = credentials.installed.client_id;
const redirectUrl = credentials.installed.redirect_uris[0];
const oauth2Client = new OAuth2(clientId, clientSecret, redirectUrl);
// Check if we have previously stored a token.
fs.readFile(TOKEN_PATH, function(err, token) {
if (err) {
getNewToken(oauth2Client, callback);
} else {
oauth2Client.credentials = JSON.parse(token);
callback(oauth2Client);
}
});
}
/**
* Get and store new token after prompting for user authorization, and then
* execute the given callback with the authorized OAuth2 client.
*
* @param {google.auth.OAuth2} oauth2Client The OAuth2 client to get token for.
* @param {getEventsCallback} callback The callback to call with the authorized
* client.
*/
function getNewToken(oauth2Client, callback) {
const authUrl = oauth2Client.generateAuthUrl({
access_type: 'offline',
scope: SCOPES
});
console.log('Authorize this app by visiting this url: ', authUrl);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('Enter the code from that page here: ', function(code) {
rl.close();
oauth2Client.getToken(code, function(err, token) {
if (err) {
console.log('Error while trying to retrieve access token', err);
return;
}
oauth2Client.credentials = token;
storeToken(token);
callback(oauth2Client);
});
});
}
/**
* Store token to disk be used in later program executions.
*
* @param {Object} token The token to store to disk.
*/
function storeToken(token) {
fs.writeFile(TOKEN_PATH, JSON.stringify(token), (err) => {
if (err) throw err;
console.log('Token stored to ' + TOKEN_PATH);
});
}
主节点文件:
const myModule = require('./youtuber')
const fs = require('fs')
const videos = fs.readdirSync('../gamevids/').filter(file => file.endsWith('.mp4'))
for (let i = 0; i < videos.length; i++) {
vid = videos[i]
thumb = vid.slice(0,-3) + "png"
title = "Thursday: Game " + vid.slice(15,-3)
description = "Thursday Night, May 20th, 2021"
tags = "tags"
myModule.uploadVideo(vid, thumb, title, description, tags)
console.log(`OK Video ${i} has been functioned`)
}
const myModule=require('./youtuber')
常量fs=require('fs')
const videos=fs.readdirSync('../gamevids/').filter(file=>file.endsWith('.mp4'))
for(设i=0;i