forked from saschagehlich/node-eyed3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
130 lines (105 loc) · 3.38 KB
/
index.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
var spawn = require("child_process").spawn
var path = require("path")
function EyeD3(options) {
if(!options) options = {}
if(!options.eyed3_path) options.eyed3_path = "eyeD3"
this.options = options
}
/**
* Reads the meta data of the given file
*
* @param {String} file
* @param {Function} callback
*/
EyeD3.prototype.readMeta = function(file, callback) {
var args = ['--no-color', file]
, p = spawn(this.options.eyed3_path, args)
, allData = ''
p.stdout.on('data', function (data) {
allData += data
})
p.on('exit', function (exitCode) {
if(exitCode !== 0)
return callback(new Error('eyeD3 exit code: ' + exitCode))
var response = {}
, lines = allData.split('\n')
, line, match
for(var i = 0; i < lines.length; i++) {
line = lines[i]
if(match = line.match(/^(.*): (.*)$/i)) {
response[match[1].toLowerCase()] = match[2]
}
}
callback(null, response)
})
}
/**
* Reads the lyrics of the given file
*
* @param {String} file
* @param {Function} callback
*/
EyeD3.prototype.readLyrics = function(file, callback) {
var args = ['--no-color', '--verbose', file]
, p = spawn(this.options.eyed3_path, args)
, allData = ''
p.stdout.on('data', function (data) {
allData += data
})
p.on('exit', function (exitCode) {
if(exitCode !== 0)
return callback(new Error('eyeD3 exit code: ' + exitCode))
var response = '';
if(match = allData.match(/<.*lyric\/text.*:\s(.*)\s\[Lang:[^\]]*\]\s*\[Desc:[^\]]*\]>/im)) {
response = match[1]
}
callback(null, response)
})
}
/**
* Updates the meta data of the given file
*
* @param {String} file
* @param {Object} meta
* @param {Function} callback
*/
EyeD3.prototype.updateMeta = function(file, meta, callback) {
var args = this.buildArgs(meta).concat([file])
, p = spawn(this.options.eyed3_path, args, { cwd: path.dirname(this.options.eyed3_path) } )
// console.log(this.options.eyed3_path, args);
var errors = '';
p.stderr.on('data', (d) => {
errors += `${d}`;
//console.log(`eyeD3 error: ${d}`);
});
// p.stdout.on('data', (d) => console.log(`eye: ${d}`));
p.on('exit', exitCode => {
if(exitCode !== 0)
return callback(new Error('eyeD3 exit code:' + exitCode + '\nInput: ' + this.options.eyed3_path + ' ' + JSON.stringify(args) + '\n' + errors))
if(callback) callback()
})
}
/**
* Builds an argument error out of the given meta data
*
* @param {Object} meta
* @return {Array} The arguments for our spawn() call
*/
EyeD3.prototype.buildArgs = function(meta) {
var args = []
if(meta.artist) args.push('-a', meta.artist)
if(meta.title) args.push('-t', meta.title)
if(meta.album) args.push('-A', meta.album)
if(meta.track) args.push('-n', meta.track)
if(meta.trackTotal) args.push('-N', meta.trackTotal)
if(meta.disc) args.push('-d', meta.disc)
if(meta.year) args.push('-Y', meta.year, '--release-date', meta.year, '--recording-date', meta.year, '--encoding-date', meta.year)
if(meta.genre) args.push('--genre', meta.genre)
if(meta.image) args.push('--add-image', meta.image+':FRONT_COVER')
if(meta.comment) args.push('-c', meta.comment)
if(meta.lyrics) args.push('--add-lyrics', meta.lyrics)
args.push('--encoding', 'utf8')
args.push('--to-v2.3') // Prefer v2.3 for Windows Explorer and Groove compatibility
return args
}
module.exports = EyeD3