X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;ds=sidebyside;f=server%2Ftools%2Fpeertube-import-videos.ts;h=d04ddfe06dbd69f73cc2caa4b24072b5e4001ce0;hb=30664eb856ac38b26f8cf7b07bdf9fde3f78f037;hp=0efe87810c98f4d2bc39a40bf548b63c67e67b8c;hpb=7cd1b12c19d0589d1d692ed0571ca0800f028aea;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/tools/peertube-import-videos.ts b/server/tools/peertube-import-videos.ts index 0efe87810..d04ddfe06 100644 --- a/server/tools/peertube-import-videos.ts +++ b/server/tools/peertube-import-videos.ts @@ -38,6 +38,7 @@ command .option('--first ', 'Process first n elements of returned playlist') .option('--last ', 'Process last n elements of returned playlist') .option('-T, --tmpdir ', 'Working directory', __dirname) + .usage("[global options] [ -- youtube-dl options]") .parse(process.argv) const log = getLogger(program['verbose']) @@ -71,37 +72,42 @@ async function run (url: string, user: UserInfo) { const youtubeDL = await safeGetYoutubeDL() - const options = [ '-j', '--flat-playlist', '--playlist-reverse' ] - youtubeDL.getInfo(program['targetUrl'], options, processOptions, async (err, info) => { - if (err) { - exitError(err.message) - } + let info = await getYoutubeDLInfo(youtubeDL, program['targetUrl'], command.args) - let infoArray: any[] + if (info?.title === 'Uploads') { + console.log('Fixing URL to %s.', info.url) - // Normalize utf8 fields - infoArray = [].concat(info) - if (program['first']) { - infoArray = infoArray.slice(0, program['first']) - } else if (program['last']) { - infoArray = infoArray.slice(-program['last']) - } - infoArray = infoArray.map(i => normalizeObject(i)) + info = await getYoutubeDLInfo(youtubeDL, info.url, command.args) + } - log.info('Will download and upload %d videos.\n', infoArray.length) + let infoArray: any[] - for (const info of infoArray) { + // Normalize utf8 fields + infoArray = [].concat(info) + if (program['first']) { + infoArray = infoArray.slice(0, program['first']) + } else if (program['last']) { + infoArray = infoArray.slice(-program['last']) + } + infoArray = infoArray.map(i => normalizeObject(i)) + + log.info('Will download and upload %d videos.\n', infoArray.length) + + for (const info of infoArray) { + try { await processVideo({ cwd: program['tmpdir'], url, user, youtubeInfo: info }) + } catch (err) { + console.error('Cannot process video.', { info, url }) } + } - log.info('Video/s for user %s imported: %s', user.username, program['targetUrl']) - process.exit(0) - }) + log.info('Video/s for user %s imported: %s', user.username, program['targetUrl']) + process.exit(0) } function processVideo (parameters: { @@ -146,7 +152,7 @@ function processVideo (parameters: { log.info('Downloading video "%s"...', videoInfo.title) - const options = [ '-f', 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best', '-o', path ] + const options = [ '-f', 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best', ...command.args, '-o', path ] try { const youtubeDL = await safeGetYoutubeDL() youtubeDL.exec(videoInfo.url, options, processOptions, async (err, output) => { @@ -272,7 +278,7 @@ async function getCategory (categories: string[], url: string) { function getLicence (licence: string) { if (!licence) return undefined - if (licence.indexOf('Creative Commons Attribution licence') !== -1) return 1 + if (licence.includes('Creative Commons Attribution licence')) return 1 return undefined } @@ -391,3 +397,15 @@ function exitError (message: string, ...meta: any[]) { console.error(message, ...meta) process.exit(-1) } + +function getYoutubeDLInfo (youtubeDL: any, url: string, args: string[]) { + return new Promise((res, rej) => { + const options = [ '-j', '--flat-playlist', '--playlist-reverse', ...args ] + + youtubeDL.getInfo(url, options, processOptions, async (err, info) => { + if (err) return rej(err) + + return res(info) + }) + }) +}