forked from parshap/node-ffmetadata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
220 lines (175 loc) · 4.25 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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/* jshint node:true */
"use strict";
var spawn = require("child_process").spawn,
ffmpeg = spawn.bind(null, "ffmpeg"),
fs = require("fs"),
through = require("through"),
concat = require("concat-stream");
module.exports.read = function(src, options, callback) {
if (typeof options === "function") {
callback = options;
options = {};
}
var args = getReadArgs(src);
if (options.dryRun) {
return args;
}
var proc = spawnRead(args),
stream = through(),
output = parseini(),
error = concat();
// Proxy any child process error events along
proc.on("error", stream.emit.bind(stream, "error"));
// Parse ffmetadata "ini" output
proc.stdout.pipe(output);
// Capture stderr
proc.stderr.pipe(error);
proc.on("close", function(code) {
if (code === 0) {
stream.emit("metadata", output.data);
}
else {
stream.emit("error", new Error(error.getBody().toString()));
}
});
if (callback) {
stream.on("metadata", callback.bind(null, null));
stream.on("error", callback);
}
return stream;
};
module.exports.write = function(src, data, options, callback) {
if (typeof options === "function") {
callback = options;
options = {};
}
var dst = getTempPath(src),
args = getWriteArgs(src, dst, data, options);
if (options.dryRun) {
return args;
}
var proc = ffmpeg(args),
stream = through(),
error = concat();
// Proxy any child process error events
proc.on("error", stream.emit.bind(stream, "error"));
// Proxy child process stdout but don't end the stream until we know
// the process exits with a zero exit code
proc.stdout.on("data", stream.emit.bind(stream, "data"));
// Capture stderr (to use in case of non-zero exit code)
proc.stderr.pipe(error);
proc.on("close", function(code) {
if (code === 0) {
finish();
}
else {
handleError(new Error(error.getBody().toString()));
}
});
if (callback) {
stream.on("end", callback);
stream.on("error", callback);
}
function handleError(err) {
fs.unlink(dst, function() {
stream.emit("error", err);
});
}
function finish() {
fs.rename(dst, src, function(err) {
if (err) {
handleError(err);
}
else {
stream.emit("end");
}
});
}
return stream;
};
var path = require("path");
function getTempPath(src) {
var ext = path.extname(src),
basename = path.basename(src).slice(0, -ext.length),
newName = basename + ".ffmetadata" + ext,
dirname = path.dirname(src),
newPath = path.join(dirname, newName);
return newPath;
}
// -- Child process helpers
function getReadArgs(src) {
return [
"-i",
src,
"-f",
"ffmetadata",
"pipe:1", // output to stdout
];
}
function spawnRead(args) {
return ffmpeg(args, { detached: true, encoding: "binary" });
}
function getWriteArgs(src, dst, data, options) {
// ffmpeg options
var inputs = ["-i", src], // src input
maps = ['-map', '0:0'], // set as the first
args = ["-y"]; // overwrite file
// Attach additional input files if included
getAttachments(options).forEach(function(el) {
var inputIndex = inputs.length / 2;
inputs.push('-i', el);
maps.push("-map", inputIndex + ":0");
});
// Copy flag in order to not transcode
args = args.concat(inputs, maps, ["-codec", "copy"]);
if (options["id3v2.3"]) {
args.push("-id3v2_version", "3");
}
// append metadata
Object.keys(data).forEach(function(name) {
args.push("-metadata");
args.push(escapeini(name) + "=" + escapeini(data[name]));
});
args.push(dst); // output to src path
return args;
}
function getAttachments(options) {
if (Array.isArray(options)) {
return options;
}
return options.attachments || [];
}
// -- Parse ini
var combine = require("stream-combiner"),
filter = require("stream-filter"),
split = require("split");
function parseini(callback) {
var stream = combine(
split(),
filter(Boolean),
filter(isNotComment),
through(parseLine)
);
// Object to store INI data in
stream.data = {};
if (callback) {
stream.on("end", callback.bind(null, stream.data));
}
return stream;
function parseLine(data) {
data = unescapeini(data);
var index = data.indexOf("=");
stream.data[data.slice(0, index)] = data.slice(index + 1);
}
}
function isNotComment(data) {
return data.slice(0, 1) !== ";";
}
function escapeini(data) {
// @TODO
return data;
}
function unescapeini(data) {
// @TODO
return data;
}