Improve youtube import script

This commit is contained in:
Chocobozzz 2018-02-12 11:02:14 +01:00
parent 64586951de
commit e78720386f
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
1 changed files with 39 additions and 34 deletions

View File

@ -11,7 +11,7 @@ program
.option('-u, --url <url>', 'Server url') .option('-u, --url <url>', 'Server url')
.option('-U, --username <username>', 'Username') .option('-U, --username <username>', 'Username')
.option('-p, --password <token>', 'Password') .option('-p, --password <token>', 'Password')
.option('-y, --youtube-url <directory>', 'Youtube URL') .option('-y, --youtube-url <youtubeUrl>', 'Youtube URL')
.parse(process.argv) .parse(process.argv)
if ( if (
@ -45,6 +45,9 @@ async function run () {
youtubeDL.getInfo(program['youtubeUrl'], [ '-j', '--flat-playlist' ], async (err, info) => { youtubeDL.getInfo(program['youtubeUrl'], [ '-j', '--flat-playlist' ], async (err, info) => {
if (err) throw err if (err) throw err
// Normalize utf8 fields
info = info.map(i => normalizeObject(i))
const videos = info.map(i => { const videos = info.map(i => {
return { url: 'https://www.youtube.com/watch?v=' + i.id, name: i.title } return { url: 'https://www.youtube.com/watch?v=' + i.id, name: i.title }
}) })
@ -60,54 +63,37 @@ async function run () {
}) })
} }
function processVideo (videoUrlName: { name: string, url: string }) { function processVideo (video: { name: string, url: string }) {
return new Promise(async res => { return new Promise(async res => {
const result = await searchVideo(program['url'], videoUrlName.name) const result = await searchVideo(program['url'], video.name)
console.log('############################################################\n')
if (result.body.total !== 0) { if (result.body.total !== 0) {
console.log('Video "%s" already exist, don\'t reupload it.\n', videoUrlName.name) console.log('Video "%s" already exists, don\'t reupload it.\n', video.name)
return res() return res()
} }
const video = youtubeDL(videoUrlName.url) const path = join(__dirname, new Date().getTime() + '.mp4')
let videoInfo
let videoPath: string
video.on('error', err => console.error(err)) console.log('Downloading video "%s"...', video.name)
let size = 0 youtubeDL.exec(video.url, [ '-f', 'bestvideo[ext=mp4]+bestaudio[ext=m4a]', '-o', path ], {}, async (err, output) => {
video.on('info', info => { if (err) return console.error(err)
videoInfo = info
size = info.size
videoPath = join(__dirname, size + '.mp4') console.log(output.join('\n'))
console.log('Creating "%s" of video "%s".', videoPath, videoInfo.title)
video.pipe(createWriteStream(videoPath)) youtubeDL.getInfo(video.url, async (err, videoInfo) => {
}) if (err) return console.error(err)
let pos = 0 await uploadVideoOnPeerTube(normalizeObject(videoInfo), path)
video.on('data', chunk => {
pos += chunk.length
// `size` should not be 0 here.
if (size) {
const percent = (pos / size * 100).toFixed(2)
writeWaitingPercent(percent)
}
})
video.on('end', async () => { return res()
await uploadVideoOnPeerTube(videoInfo, videoPath) })
return res()
}) })
}) })
} }
function writeWaitingPercent (p: string) {
cursorTo(process.stdout, 0)
process.stdout.write(`waiting ... ${p}%`)
}
async function uploadVideoOnPeerTube (videoInfo: any, videoPath: string) { async function uploadVideoOnPeerTube (videoInfo: any, videoPath: string) {
const category = await getCategory(videoInfo.categories) const category = await getCategory(videoInfo.categories)
const licence = getLicence(videoInfo.license) const licence = getLicence(videoInfo.license)
@ -153,3 +139,22 @@ function getLicence (licence: string) {
return undefined return undefined
} }
function normalizeObject (obj: any) {
const newObj: any = {}
for (const key of Object.keys(obj)) {
// Deprecated key
if (key === 'resolution') continue
const value = obj[key]
if (typeof value === 'string') {
newObj[key] = value.normalize()
} else {
newObj[key] = value
}
}
return newObj
}