Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nizulzaim committed Jun 24, 2017
0 parents commit be19557
Show file tree
Hide file tree
Showing 7 changed files with 1,187 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
49 changes: 49 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Logs
logs
*.log
npm-debug.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules
jspm_packages

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

*.mp4
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# youtube-downloader
116 changes: 116 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
const fs = require('fs');
const youtubedl = require('youtube-dl');
const path = require('path');
const ffmpegPath = require('@ffmpeg-installer/ffmpeg').path;
const ffmpeg = require('fluent-ffmpeg');

let youtubeDownloader = {
url: null,
quality: 0,
finalOutput: null,
videoFileName: null,
audioFileName: null,
videoTag: null,
audioTag: null,
downloader(tag) {
let download = youtubedl(this.url,
['-f', tag], { cwd: __dirname });

return download
},
downloadVideo () {
let size = 0
let pos = 0

let d = this.downloader(this.videoTag)
d.on('info', (info) => {
console.log('Video Download started');
console.log('size: ' + info.size);
size = info.size;

this.finalOutput = path.join(__dirname, info._filename)
this.videoFileName = path.join(__dirname, info.size.toString() + "." + info.ext)
d.pipe(fs.createWriteStream(this.videoFileName));
});

d.on('data', function data(chunk) {
pos += chunk.length;

if (size) {
var percent = (pos / size * 100).toFixed(2);
process.stdout.cursorTo(0);
process.stdout.clearLine(1);
process.stdout.write(percent + '%');
}
});

return d;
},
downloadAudio() {
let size = 0
let pos = 0

let d = this.downloader(this.audioTag)
d.on('info', (info) => {
console.log('Audio Download started');
console.log('size: ' + info.size);
size = info.size;

this.audioFileName = path.join(__dirname, info.size.toString() + "." + info.ext)
d.pipe(fs.createWriteStream(this.audioFileName));
});

d.on('data', function data(chunk) {
pos += chunk.length;

if (size) {
var percent = (pos / size * 100).toFixed(2);
process.stdout.cursorTo(0);
process.stdout.clearLine(1);
process.stdout.write(percent + '%');
}
});

return d;
},
checkFileTag(callback) {
youtubedl.getInfo(this.url, (err, info) => {
if (err) { throw err; }
for (let index in info.formats) {
((index, format) => {
if (format.ext === "mp4" && !format.resolution) {
if (format.height.toString() === this.quality) {
this.videoTag = format.format_id
}
}
if (format.ext === "m4a" && !format.resolution) {
this.audioTag = format.format_id
}
})(index, info.formats[index])
}
callback()
});
},
start(u, q) {
this.url = u
this.quality = q
this.checkFileTag(() => {
this.downloadVideo().on("end", () => {
this.downloadAudio().on("end", () => {
console.log('\nFinish Dowloading, processing...');
ffmpeg.setFfmpegPath(ffmpegPath);
let command = ffmpeg(this.videoFileName).videoCodec('copy').input(this.audioFileName).audioCodec('copy').saveToFile(this.finalOutput)

command.on('end', () => {
fs.unlink(this.videoFileName, () => {});
fs.unlink(this.audioFileName, () => {});
console.log('Finished processing');
})
})
})
})

}
}

youtubeDownloader.start('https://www.youtube.com/watch?v=KpiQ9lI5aWc', '1080')
Loading

0 comments on commit be19557

Please sign in to comment.