youtube downloader
This commit is contained in:
40
JavaScript/youtube-dowloader/index.js
Normal file
40
JavaScript/youtube-dowloader/index.js
Normal file
@@ -0,0 +1,40 @@
|
||||
const fs = require('fs');
|
||||
const ytdl = require('ytdl-core');
|
||||
|
||||
let videos = [];
|
||||
|
||||
if (!fs.existsSync('./videos.txt')) {
|
||||
console.log('Could not find videos.txt, make sure it exists and has youtube links, each on new lines');
|
||||
process.exit();
|
||||
}
|
||||
|
||||
let text = fs.readFileSync('./videos.txt').toString();
|
||||
text.split('\n').forEach((line) => {
|
||||
videos.push(line.replace(/\r/, ''));
|
||||
});
|
||||
|
||||
videos.forEach((url) => {
|
||||
if (ytdl.validateURL(url)) {
|
||||
ytdl.getInfo(url, (err, info) => {
|
||||
if (err) {
|
||||
console.log(`An error occured while downloading '${url}'`)
|
||||
} else {
|
||||
let title = info.title;
|
||||
|
||||
try {
|
||||
let stream = ytdl(url, {quality: 'highest'}).pipe(fs.createWriteStream(`${title}.mp4`));
|
||||
stream.on('finish', () => console.log(`Finish downloading ${title}`));
|
||||
} catch (e) {
|
||||
console.log(`An error occured while downloaing '${title}' retrying`)
|
||||
try {
|
||||
let stream = ytdl(url, {quality: 'highest'}).pipe(fs.createWriteStream(`${title}.mp4`));
|
||||
stream.on('finish', () => console.log(`Finish downloading '${title}'`));
|
||||
} catch (e) {
|
||||
console.log(`Unable to download '${title}'`)
|
||||
}
|
||||
}
|
||||
console.log(`Downloading '${title}'`);
|
||||
}
|
||||
})
|
||||
} else console.log(`Video ${url} was not found`);
|
||||
});
|
||||
Reference in New Issue
Block a user