From e5c6f3a3ff04350b42ecbd526d04784cc0419f48 Mon Sep 17 00:00:00 2001 From: ebelcrom Date: Mon, 25 May 2020 20:00:53 +0200 Subject: [PATCH] fixed not allowed characters --- lib/ytdl-test.js | 16 +++++++++++-- lib/ytdl.js | 62 ++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 72 insertions(+), 6 deletions(-) diff --git a/lib/ytdl-test.js b/lib/ytdl-test.js index f8ff24d..c64d584 100644 --- a/lib/ytdl-test.js +++ b/lib/ytdl-test.js @@ -1,12 +1,24 @@ const ytdl = require('./ytdl'); +const youtubedl = require('youtube-dl'); const qStr = require('querystring'); -console.log('URL escaped:', qStr.escape('https://www.youtube.com/watch?v=B1hxhEfYAlY')); +const url='https://www.youtube.com/watch?v=ImBk7QYHrcM'; -ytdl.download('https://www.youtube.com/watch?v=B1hxhEfYAlY') +console.log('URL escaped:', qStr.escape(url)); + +ytdl.download(url) +.then(data => { + console.log('Data:', data); +}) +.catch(err => { + console.error('Error:', err); +}); +/* +ytdl.downloadMp4(url) .then(data => { console.log('Data:', data); }) .catch(err => { console.error('Error:', err); }); +*/ diff --git a/lib/ytdl.js b/lib/ytdl.js index 195ea2b..b306cd8 100644 --- a/lib/ytdl.js +++ b/lib/ytdl.js @@ -11,6 +11,11 @@ const options = { cwd: dldir }; +function clean(title) { + const regexp = /[\\\/:*"><|?#&=;]/g + return title.replace(regexp, ''); +} + function download(url) { return new Promise((resolve, reject) => { const regex = /[.]mkv$/ @@ -18,7 +23,54 @@ function download(url) { .filter(file => regex.test(file)) .map(file => fs.unlinkSync(dldir + '/' + file)) - const writeStream = fs.createWriteStream(dldir + '/myvideo.mkv'); + ytdl.getInfo(url, (err, info) => { + if (err) { + log.error('An error occured while downloading:', err); + reject(err); + return; + } + + log.info('Got info'); + + const file = dldir + '/myvideo.mkv' + const title = clean(info.title); + const data = { + filename: title + '.mkv', + thumbnail: info.thumbnail, + }; + + ytdl.exec(url, ['--add-metadata', '--merge-output-format', 'mkv', '-o', file], + {}, (err, out) => { + if (err) { + log.error('An error occured while downloading:', err); + reject(err); + return; + } + + log.debug('youtube-dl output:', out.join('\n')); + log.info('Got file'); + + fsPromises.rename(file, dldir + '/' + data.filename) + .then(() => { + resolve(data); + }) + .catch(err => { + log.error('An error occured while file renaming:', err); + reject(err); + }); + }); + }); + }); +} + +function downloadMp4(url) { + return new Promise((resolve, reject) => { + const regex = /[.]mkv$/ + fs.readdirSync(dldir + '/') + .filter(file => regex.test(file)) + .map(file => fs.unlinkSync(dldir + '/' + file)) + + const writeStream = fs.createWriteStream(dldir + '/myvideo.mp4'); writeStream.on('error', err => { log.error('An error occured while creating a file stream:', err); reject(err); @@ -29,8 +81,9 @@ function download(url) { video.on('info', info => { log.info('Got info'); + const title = clean(info.title); data = { - filename: info.title + '.mkv', + filename: title + '.mp4', thumbnail: info.thumbnail, }; }); @@ -42,7 +95,7 @@ function download(url) { video.on('end', () => { log.info('Download finished'); - fsPromises.rename(dldir + '/myvideo.mkv', dldir + '/' + data.filename) + fsPromises.rename(dldir + '/myvideo.mp4', dldir + '/' + data.filename) .then(() => { resolve(data); }) @@ -62,5 +115,6 @@ function download(url) { } module.exports = { - download + download, + downloadMp4 }; -- 2.30.2