forked from parshap/node-ffmetadata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
182 lines (147 loc) · 3.42 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
/* 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, callback) {
var stream = through(),
proc = spawnRead(src),
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, callback) {
var stream = through(),
dst = getTempPath(src),
proc = spawnWrite(src, dst, data),
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 spawnRead(src) {
var args = [
"-i",
src,
"-f",
"ffmetadata",
"pipe:1", // output to stdout
];
return ffmpeg(args, { detached: true, encoding: "binary" });
}
function spawnWrite(src, dst, data) {
var args = [
"-y",
"-i",
src, // input from src path
"-map",
"0",
"-id3v2_version",
"3",
"-codec",
"copy",
];
// Write 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 ffmpeg(args);
}
// -- 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;
}