New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

youtube-mp3-downloader

Package Overview
Dependencies
Maintainers
1
Versions
32
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

youtube-mp3-downloader - npm Package Compare versions

Comparing version 0.1.0 to 0.2.0

146

lib/YoutubeMp3Downloader.js
"use strict";
var os = require('os');
var util = require('util');
var EventEmitter = require("events").EventEmitter;
var ffmpeg = require('fluent-ffmpeg');
var ytdl = require('ytdl-core');
var async = require('async');
var progress = require('progress-stream');

@@ -14,3 +17,4 @@ function YoutubeMp3Downloader(options) {

self.outputPath = (options && options.outputPath ? options.outputPath : (os.platform() === 'win32' ? 'D:/temp' : '/tmp'));
self.parallelismFactor = (options && options.parallelismFactor ? options.parallelismFactor : 1);
self.queueParallelism = (options && options.queueParallelism ? options.queueParallelism : 1);
self.progressTimeout = (options && options.progressTimeout ? options.progressTimeout : 1000);

@@ -21,73 +25,107 @@ if (options && options.ffmpegPath) {

//Async download/transcode queue
self.downloadQueue = async.queue(function (videoId, callback) {
self.performDownload(videoId, function(err, result) {
callback(err, result);
});
}, self.queueParallelism);
}
YoutubeMp3Downloader.prototype.download = function(videoInfo, cb) {
util.inherits(YoutubeMp3Downloader, EventEmitter);
YoutubeMp3Downloader.prototype.download = function(videoId) {
var self = this;
var videoArray = [];
if (Array.isArray(videoInfo)) {
videoArray = videoInfo;
} else if (typeof videoInfo === "string") {
videoArray.push(videoInfo);
}
self.downloadQueue.push(videoId, function (err, data) {
if (err) {
self.emit("error", err);
} else {
self.emit('finished', data);
}
});
function getVideo(videoId, callback) {
};
var videoUrl = self.youtubeBaseUrl+videoId;
YoutubeMp3Downloader.prototype.performDownload = function(videoId, cb) {
ytdl.getInfo(videoUrl, function(err, info){
var self = this;
var videoUrl = self.youtubeBaseUrl+videoId;
var videoTitle = info.title.replace(/"/g, '').replace(/'/g, '').replace(/\//g, '').replace(/\?/g, '');
var artist = "Unknown";
var title = "Unknown";
ytdl.getInfo(videoUrl, function(err, info){
if (videoTitle.indexOf("-") > -1) {
var temp = videoTitle.split("-");
if (temp.length >= 2) {
artist = temp[0].trim();
title = temp[1].trim();
}
} else {
title = videoTitle;
var videoTitle = info.title.replace(/"/g, '').replace(/'/g, '').replace(/\//g, '').replace(/\?/g, '');
var artist = "Unknown";
var title = "Unknown";
if (videoTitle.indexOf("-") > -1) {
var temp = videoTitle.split("-");
if (temp.length >= 2) {
artist = temp[0].trim();
title = temp[1].trim();
}
} else {
title = videoTitle;
}
var stream = ytdl(videoUrl, {
quality: self.youtubeVideoQuality
var fileName = self.outputPath + '/' + videoTitle + '.mp3';
var stream = ytdl(videoUrl, {
quality: self.youtubeVideoQuality
});
stream.on("info", function(info, format) {
var resultObj = {};
//Setup of progress module
var str = progress({
length: format.size,
time: self.progressTimeout
});
var fileName = self.outputPath + '/' + videoTitle + '.mp3';
var startTimestamp = new Date().getTime();
//Add progress event listener
str.on('progress', function(progress) {
if (progress.percentage === 100) {
resultObj.stats= {
transferredBytes: progress.transferred,
runtime: progress.runtime,
averageSpeed: parseFloat(progress.speed.toFixed(2))
}
}
self.emit("progress", {videoId: videoId, progress: progress})
});
//Pipe through progress module
var videoStream = stream.pipe(str);
//Start encoding
var proc = new ffmpeg({
source: stream
source: videoStream
})
.audioBitrate(info.formats[0].audioBitrate)
.withAudioCodec('libmp3lame')
.toFormat('mp3')
.outputOptions('-id3v2_version', '4')
.outputOptions('-metadata', 'title=' + title)
.outputOptions('-metadata', 'artist=' + artist)
.on('error', function(err) {
console.log('An error occurred: ' + err.message);
callback(err.message, null);
})
.on('end', function() {
//console.log('Processing finished !');
callback(null, {
"file": fileName,
"videoId": videoId,
"youtubeUrl": videoUrl,
"videoTitle": videoTitle,
"artist": artist,
"title": title,
"took": (new Date().getTime()-startTimestamp)
});
})
.saveToFile(fileName);
.audioBitrate(info.formats[0].audioBitrate)
.withAudioCodec('libmp3lame')
.toFormat('mp3')
.outputOptions('-id3v2_version', '4')
.outputOptions('-metadata', 'title=' + title)
.outputOptions('-metadata', 'artist=' + artist)
.on('error', function(err) {
cb(err.message, null);
})
.on('end', function() {
resultObj.file = fileName;
resultObj.videoId = videoId;
resultObj.youtubeUrl = videoUrl;
resultObj.videoTitle = videoTitle;
resultObj.artist = artist;
resultObj.title = title;
cb(null, resultObj);
})
.saveToFile(fileName);
});
}
async.mapLimit(videoArray, self.parallelismFactor, getVideo, function(err, results){
cb(results);
});

@@ -97,2 +135,2 @@

module.exports = YoutubeMp3Downloader;
module.exports = YoutubeMp3Downloader;
{
"name": "youtube-mp3-downloader",
"version": "0.1.0",
"version": "0.2.0",
"description": "Downloads Youtube videos (in parallel, as streams), encodes the audio data as mp3 and stores them in a defineable location",

@@ -21,5 +21,6 @@ "keywords": [

"dependencies": {
"ytdl-core": "0.4.1",
"fluent-ffmpeg": "2.0.0-rc3",
"async": "0.9.0"
"async": "1.2.1",
"fluent-ffmpeg": "2.0.1",
"progress-stream": "^1.1.1",
"ytdl-core": "0.5.1"
},

@@ -26,0 +27,0 @@ "author": {

# Youtube MP3 Downloader
Youtube MP3 Downloader is a module which allows to specify one or multiple YouTube videos from whom the audio data should be extracted, converted to MP3, and stored on disk.
Youtube MP3 Downloader is a module which allows to specify YouTube videos from which the audio data should be extracted, converted to MP3, and stored on disk.

@@ -28,12 +28,23 @@ ## Installation

var YD = new YoutubeMp3Downloader({
"ffmpegPath": "/path/to/ffmpeg", //Where is the FFmpeg binary located?
"outputPath": "/path/to/mp3/folder", //Where should the downloaded and encoded files be stored?
"youtubeVideoQuality": "highest", //What video quality should be used?
"parallelismFactor": 2 //How many parallel downloads/encodes should be started?
"ffmpegPath": "/path/to/ffmpeg", // Where is the FFmpeg binary located?
"outputPath": "/path/to/mp3/folder", // Where should the downloaded and encoded files be stored?
"youtubeVideoQuality": "highest", // What video quality should be used?
"queueParallelism": 2, // How many parallel downloads/encodes should be started?
"progressTimeout": 2000 // How long should be the interval of the progress reports
});
//Download video and save as MP3 file
YD.download(["rnkuRQ8tjIE"], function(result) {
console.log(result);
YD.download("rnkuRQ8tjIE");
YD.on("finished", function(data) {
console.log(data);
});
YD.on("error", function(error) {
console.log(error);
});
YD.on("progress", function(progress) {
console.log(progress);
});
```

@@ -44,13 +55,15 @@

```javascript
[
{
"file": "/path/to/mp3/folder/Nancy Sinatra & Lee Hazlewood - Jackson.mp3",
"videoId": "rnkuRQ8tjIE",
"youtubeUrl": "http: //www.youtube.com/watch?v=rnkuRQ8tjIE",
"videoTitle": "Nancy Sinatra & Lee Hazlewood - Jackson",
"artist": "Nancy Sinatra & Lee Hazlewood",
"title": "Jackson",
"took": 17708
{
"videoId": "rnkuRQ8tjIE",
"file": "/path/to/mp3/folder/Nancy Sinatra & Lee Hazlewood - Jackson.mp3",
"youtubeUrl": "http://www.youtube.com/watch?v=rnkuRQ8tjIE",
"videoTitle": "Nancy Sinatra & Lee Hazlewood - Jackson",
"artist": "Nancy Sinatra & Lee Hazlewood",
"title": "Jackson",
"stats": {
"transferred": 7315910,
"runtime": 9,
"averageSpeed": 713747.31
}
]
}
```

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc