Skip to content

Commit df590d8

Browse files
authored
Merge pull request #8 from futpib/stream
Add stream support
2 parents afed416 + 4f80323 commit df590d8

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ fpcalc("./audio.mp3", function(err, result) {
4040

4141
Calculates the fingerprint of the given audio file.
4242

43-
*File* must be the path to an audio file.
43+
*File* must be the path to an audio file or a readable stream.
44+
45+
If using a stream, note that you will not get `duration` out due to an [fpcalc issue](https://github.com/acoustid/chromaprint/issues/53).
4446

4547
*Options* may be an object with any of the following keys:
4648

index.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@ module.exports = function(file, options, callback) {
2626
args.push("-raw");
2727
}
2828

29-
args.push(file);
29+
if (file && typeof file.pipe === "function") {
30+
args.push("-");
31+
options.stdin = file;
32+
} else {
33+
args.push(file);
34+
}
3035

3136
run(args, options)
3237
.on("error", callback)
@@ -71,6 +76,11 @@ function run(args, options) {
7176
// event so that we can make sure the process exited without error.
7277
stream = es.through(null, function() {});
7378

79+
// If passed stdin stream, pipe it to the child process
80+
if (options.stdin) {
81+
options.stdin.pipe(cp.stdin);
82+
}
83+
7484
// Pass fpcalc stdout through the stream
7585
cp.stdout.pipe(stream);
7686

test/test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"use strict";
33

44
var path = require("path"),
5+
fs = require("fs"),
56
test = require("tape"),
67
fpcalc = require("../");
78

@@ -52,3 +53,28 @@ test("fingerprint output", function(t) {
5253
t.ok(/^[-_a-zA-Z0-9]+$/.test(result.fingerprint));
5354
});
5455
});
56+
57+
test("stream input", function(t) {
58+
t.plan(5);
59+
60+
fpcalc(fs.createReadStream(TEST_FILE), {raw: true}, function(err, result) {
61+
t.ok(result.fingerprint);
62+
t.ok(Buffer.isBuffer(result.fingerprint));
63+
});
64+
65+
fpcalc(fs.createReadStream(TEST_FILE), function(err, result) {
66+
t.ok(result.fingerprint);
67+
t.equal(typeof result.fingerprint, "string");
68+
t.ok(/^[-_a-zA-Z0-9]+$/.test(result.fingerprint));
69+
});
70+
});
71+
72+
test("stream fignerprint is the same as file fingerprint", function(t) {
73+
t.plan(1);
74+
75+
fpcalc(fs.createReadStream(TEST_FILE), function(err, streamResult) {
76+
fpcalc(TEST_FILE, function(err, fileResult) {
77+
t.equal(fileResult.fingerprint, streamResult.fingerprint);
78+
});
79+
});
80+
});

0 commit comments

Comments
 (0)