Browse Source

deps for server-side transcoding

master
Ambrose Chua 7 years ago
parent
commit
fa4b1834fe
  1. 146
      index.js.pipesha
  2. 3
      package.json

146
index.js.pipesha

@ -1,146 +0,0 @@
var fs = require("fs");
var path = require("path");
var mm = require("musicmetadata");
var express = require("express");
var tracks = {};
var art = {};
var music = {
artists: {}
};
var promise = new Promise(function (resolve, reject) {
fs.readdir(path.join(__dirname, "music"), function (err, files) {
var promises = [];
files.forEach(function (artist) {
art.artists[artist] = {
albums: {},
picture: []
};
music.artists[artist] = {
albums: {}
};
var promise = new Promise(function (resolve, reject) {
fs.readdir(path.join(__dirname, "music", artist), function (err, files) {
var promises = [];
files.forEach(function (album) {
art.artists[artist].albums[album] = {
tracks: [],
picture: []
};
music.artists[artist].albums[album] = {
tracks: []
};
var promise = new Promise(function (resolve, reject) {
fs.readdir(path.join(__dirname, "music", artist, album), function (err, files) {
var promises = [];
files.forEach(function (title) {
var fn = path.join(__dirname, "music", artist, album, title);
var promise = new Promise(function (resolve, reject) {
fs.stat(fn, function (err, stat) {
if (stat.isFile()) {
if (fn.match(/(.mp3|.wav)/) && fn.charAt(0) != ".") {
mm(fs.createReadStream(fn), function (err, metadata) {
if (err) {
console.log(err);
resolve();
}
else {
var number = parseInt(title.split(" ")[0], 10);
if (isNaN(number)) {
number = music.artists[artist].albums[album].tracks.length;
}
// Art
/*art.artists[artist].albums[album].tracks[number - 1] = {
picture: []
};
art.artists[artist].albums[album].tracks[number - 1].picture = metadata.picture;
if (!art.artists[artist].albums[album].picture[0] && metadata.picture[0]) {
art.artists[artist].albums[album].picture = metadata.picture;
}
if (!art.artists[artist].picture[0] && metadata.picture[0]) {
art.artists[artist].picture = metadata.picture;
}*/
var sha = crypto.createHash("sha1");
fs.createReadStream(fn).pipe(sha).on("end", function () {
metadata.number = number;
metadata.file = "/tracks/" + sha.read();
tracks[sha.read()] = new Buffer();
fs.createReadStream(fn).pipe(tracks[sha.read()]);
music.artists[artist].albums[album].tracks[number - 1] = metadata;
resolve();
});
}
});
}
else if (fn.match(/(.jpg|.png)/)) {
fs.readFile(fn, function (err, data) {
if (!err) {
var fns = fn.split(".");
art.artists[artist].albums[album].picture = [{
format: fns[fns.length - 1],
data: data
}];
art.artists[artist].picture = art.artists[artist].albums[album].picture;
}
resolve();
});
}
else {
resolve();
}
}
else {
resolve();
}
});
});
promises.push(promise);
});
Promise.all(promises).then(function () {
music.artists[artist].albums[album].tracks.forEach(function (track, i) {
if ((!art.artists[artist].albums[album].tracks[i].picture[0]) && art.artists[artist].albums[album].picture[0]) {
art.artists[artist].albums[album].tracks[i].picture = art.artists[artist].albums[album].picture;
}
});
if (music.artists[artist].albums[album].tracks.length < 1) {
music.artists[artist].albums[album] = undefined;
}
resolve();
});
});
});
promises.push(promise);
});
Promise.all(promises).then(resolve);
});
});
promises.push(promise);
});
Promise.all(promises).then(resolve);
});
});
promise.then(function () {
console.log("done");
for (var artist in music.artists) {
for (var album in music.artists[artist].albums) {
for (var track in music.artists[artist].albums[album].tracks) {
}
//music.artists[artist].albums[album].picture = ""
}
}
});
var app = express();
app.get("/", function (req, res) {
res.end(JSON.stringify(music));
});

3
package.json

@ -13,7 +13,8 @@
"bluebird": "^2.10.2",
"body-parser": "^1.12.4",
"express": "^4.12.4",
"musicmetadata": "^2.0.1"
"musicmetadata": "^2.0.1",
"stream-transcoder": "0.0.5"
},
"private": true
}
Loading…
Cancel
Save