-
Notifications
You must be signed in to change notification settings - Fork 0
/
m4aFile.js
180 lines (162 loc) · 6.06 KB
/
m4aFile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
const _ = require('lodash');
const fs = require('fs');
const readTags = require('./readTags');
const mapTags = require('./mapTags');
const debug = require('./debug');
const ffmpeg = require('fluent-ffmpeg');
const moveFile = require('./moveFile.js');
const makeDirectory = require('./makeDirectory');
const makePath = require('./makePath');
const pidRegexp = /([bp][a-z0-9]+)_(original|shortened|podcast|editorial|other|default)\.(m(4a|p3))/;
let defaults;
const setDefaults = ({
readDir,
writeDir,
completeDir,
failDir
}) => {
defaults = {
readDir,
writeDir,
completeDir,
failDir
};
};
const processFile = filename => {
let outputTags = {};
const readFilePath = makePath([defaults.readDir, filename]);
const getTags = () => {
debug('get tags');
return readTags(readFilePath).then(
function (tags) {
const genre = mapTags.mapGenre(tags);
const artist = mapTags.mapArtist(genre);
const { programme, seriesNumber, parentSeries } = mapTags.findProgrammeAndSeries(tags);
const trackNumber = mapTags.findTrack(tags);
const title = mapTags.findTitle(tags);
if (!genre) {
throw new Error('NO GENRE FOR ' + filename);
}
outputTags = {
genre,
artist,
album_artist: parentSeries || artist,
album: programme,
track: trackNumber,
disc: seriesNumber,
title
};
});
};
const getAndCreateWriteFilePath = () => {
const pathParts = makePathParts(outputTags);
const writeDir = makePath([defaults.writeDir, ...pathParts]);
const writeFilename = getOutputFilename({ filename, ...outputTags });
const writeFilePath = makePath([writeDir, writeFilename]);
return new Promise(function (resolve, reject) {
debug('Make directory ' + makePath(pathParts));
return makeDirectory(writeDir).then(function () {
resolve(writeFilePath);
});
});
};
const retagFile = (writeFilePath) => {
const flags = metadataForFFmpeg(outputTags);
return new Promise(function (resolve, reject) {
debug('Writing retagged file to ' + writeFilePath);
ffmpeg({ source: readFilePath, logger: { error: debug } })
.outputOptions(flags)
.audioCodec('copy')
.noVideo()
.on('error', function (err, stdout, stderr) {
reject('An error occurred running ffmpeg: ' + err.message + '\nSTDOUT: ' + stdout + '\nSTDERR: ' + stderr);
})
.on('end', function (stdout, stderr) {
// debug(filename, 'Success.', 'STDOUT: ' + stdout, 'STDERR: ' + stderr);
resolve();
})
.saveToFile(writeFilePath);
});
};
const moveSuccesfulFile = () => {
const successPath = makePath([defaults.completeDir, filename]);
debug('Move original to ' + successPath);
// TODO - move directory making to defaults
return new Promise(function (resolve, reject) {
makeDirectory(defaults.completeDir).then(function () {
moveFile(readFilePath, successPath);
resolve();
});
});
}
// TODO - test
const moveFailedFile = () => {
const failPath = makePath([defaults.failDir, filename]);
debug('Move original to ' + failPath);
// TODO - move directory making to defaults
return new Promise(function (resolve, reject) {
makeDirectory(defaults.failDir).then(function () {
moveFile(readFilePath, failPath);
resolve();
});
});
}
const makePathParts = ({ genre, artist, album_artist, album, disc }) => {
const pathParts = [genre, artist];
if (album_artist !== artist) {
pathParts.push(album_artist);
}
pathParts.push(album);
if (genre === 'Comedy' || disc !== '1') {
pathParts.push(`Series ${disc}`);
}
return pathParts.map(sanitisePath);
};
const sanitisePath = (path) => {
const unsafeCharacters = new RegExp(/[:*<>?\\/_]+/, 'g');
const safePath = path.replace(unsafeCharacters, '');
return safePath;
}
const sanitiseParam = (param) => {
return param.includes(' ') ?
`${param} `
: param;
// .replace(/([" ])/g, '\\$1');
}
const metadataForFFmpeg = (tags) => {
return _.flatMap(
_.filter(
_.toPairs(tags),
value => {
return value[1]
}),
(value) => {
return ['-metadata', `${value[0]}=${sanitiseParam(value[1])}`];
});
};
const getOutputFilename = ({ filename, track, title }) => {
const pidResults = pidRegexp.exec(filename);
if (!pidResults) {
throw new Error('Cannot find pid in ' + filename);
}
const pid = pidResults[1];
const fileExtension = pidResults[3];
const sanitisedTitle = sanitisePath(title.substr(0, 30));
const trackNumber = track ? `${track} ` : '';
const outputFilename = `${trackNumber}${sanitisedTitle} ${pid}.${fileExtension}`;
return outputFilename;
}
debug();
debug('Begin ' + readFilePath);
return getTags()
.then(getAndCreateWriteFilePath)
.then(retagFile)
.then(moveSuccesfulFile)
.catch(function (error) {
debug('FAILED', filename, error, '\n');
moveFailedFile();
return error;
}
);
}
module.exports = { setDefaults, processFile };