Extract cover art

This commit is contained in:
Lynn Smeria 2018-08-20 15:18:47 +02:00
parent 25a3cd56f4
commit 4e2c11aabb
4 changed files with 53 additions and 3 deletions

View file

@ -3,14 +3,35 @@ const path = require('path');
const _ = require('lodash');
const sanitize = require('sanitize-filename');
const debug = require('debug')('publikator:organise');
const mime = require('mime-types');
const tags = require('./tags');
const getFolderName = file => file.common.album.replace(/ /g, '_');
const getFileName = file =>
`${file.common.track.no}-${file.common.title}${path.extname(
file.path
)}`.replace(/ /g, '_');
/**
* Extracts the cover art and saves it to a file with the same name.
*/
const extractCoverArt = async filePath => {
const pictures = await tags.extractCoverArt(filePath);
if (pictures) {
await Promise.all(
pictures.map(async picture => {
const pictureExt = mime.extension(picture.format);
const picturePath = `${filePath.replace(
path.extname(filePath),
''
)}.${pictureExt}`;
await fs.writeFile(picturePath, picture.data);
})
);
}
};
module.exports = {
/**
* Organises tracks into a new folder structure in `root`, as follows:
@ -42,13 +63,14 @@ module.exports = {
folders.map(album => fs.ensureDir(path.resolve(root, sanitize(album))))
);
debug(`copying tracks`);
debug(`copying tracks & extracting covers`);
return Promise.all(
files.map(async file => {
const folderName = getFolderName(file);
const fileName = getFileName(file);
const newPath = path.resolve(root, folderName, fileName);
await fs.copyFile(file.path, newPath);
await extractCoverArt(newPath);
return _.assign(
{},
{

View file

@ -10,9 +10,21 @@ module.exports = {
mm.parseFile(file, {
duration: true,
native: true,
skipCovers: false,
skipCovers: true,
}),
/**
* Extracts the cover art into a file stream.
*/
extractCoverArt: async file => {
const data = await mm.parseFile(file, {
duration: false,
native: false,
skipCovers: false,
});
return data.common.picture;
},
/**
* Returns true if a file has all required tags.
*/