Change tag parser for better file support

Should support mp3, ogg, wav, flac and more.
This commit is contained in:
Lynn Smeria 2018-08-17 19:53:14 +03:00
parent 09fa089cfe
commit 15483c0354
7 changed files with 112 additions and 71 deletions

View file

@ -18,17 +18,10 @@ module.exports = {
'track-count': tracks.length,
tracks: tracks.map((track, i) => ({
path: track.path,
size: track.size,
position: i,
...tags.getTags(track, [
'title',
'artist',
'album',
'year',
'comment',
'track',
'genre',
]),
common: track.common,
format: track.format,
...tags.getTags(track, ['foo']),
})),
};
})

View file

@ -5,9 +5,10 @@ const sanitize = require('sanitize-filename');
const debug = require('debug')('publikator:organise');
const tags = require('./tags');
const getFolderName = file => `${file.tags.artist} - ${file.tags.album}`;
const getArtists = file => file.common.artist || file.common.artists.join(', ');
const getFolderName = file => `${getArtists(file)} - ${file.common.album}`;
const getFileName = file =>
`${file.tags.track} - ${file.tags.title}${path.extname(file.path)}`;
`${file.common.track.no} - ${file.common.title}${path.extname(file.path)}`;
module.exports = {
/**
@ -22,7 +23,12 @@ module.exports = {
*/
byAlbum: async (root, taggedFiles) => {
const files = taggedFiles.filter(file =>
tags.hasTags(file, ['artist', 'album', 'track', 'title'])
tags.hasTags(file, [
'common.artists',
'common.album',
'common.track',
'common.title',
])
);
debug(`grouping tracks by album`);

View file

@ -22,7 +22,7 @@ module.exports = {
/**
* Reads ID3 tags from all files and returns an array in the form of:
* [{ path, size, tags }, ...]
* [{ path, common, format, native }, ...]
*/
readTags: files => {
debug(`reading tags from ${files.length} file(s)`);
@ -31,8 +31,7 @@ module.exports = {
const info = await tags.readTags(file);
return {
path: file,
size: info.size,
tags: info.tags,
...info,
};
})
);

View file

@ -1,4 +1,5 @@
const jsmediatags = require('jsmediatags');
const _ = require('lodash');
const mm = require('music-metadata');
const debug = require('debug')('publikator:tags');
module.exports = {
@ -6,24 +7,17 @@ module.exports = {
* Reads tags from a track.
*/
readTags: file =>
new Promise((resolve, reject) => {
jsmediatags.read(file, {
onSuccess: info => {
resolve(info);
},
onError: error => {
debug(error.type);
debug(error.info);
reject(error);
},
});
mm.parseFile(file, {
duration: true,
native: true,
skipCovers: false,
}),
/**
* Returns true if a file has all required tags.
*/
hasTags: (taggedFile, tags) => {
if (tags.some(tag => taggedFile.tags[tag] === undefined)) {
if (tags.some(tag => _.get(taggedFile, tag) === undefined)) {
debug(`track'${taggedFile.path}' is missing one or more required tags`);
return false;
}
@ -35,8 +29,9 @@ module.exports = {
*/
getTags: (taggedFile, tags) =>
tags.reduce((all, tag) => {
if (taggedFile.tags[tag] !== undefined) {
all[tag] = taggedFile.tags[tag]; // eslint-disable-line
const value = _.get(taggedFile, tag);
if (value !== undefined) {
all[tag] = value; // eslint-disable-line
}
return all;
}, {}),