From a8acd04d04dba977e12bf9574b903322aee87727 Mon Sep 17 00:00:00 2001 From: skdhg <46562212+skdhg@users.noreply.github.com> Date: Sat, 12 Aug 2023 21:52:56 +0545 Subject: [PATCH] 0.0.4 --- Cargo.toml | 6 +++++- README.md | 15 ++++++++++++--- dev/index.js | 9 ++++++--- index.js | 19 ++++++------------- package.json | 2 +- 5 files changed, 30 insertions(+), 21 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5bb72df..91618d2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,9 +10,13 @@ crate-type = ["cdylib"] dasp = { version = "0.11.0" } napi = { git = "https://github.com/napi-rs/napi-rs", default-features = false, features = [ "napi4", + "tokio_rt", +] } +napi-derive = { git = "https://github.com/napi-rs/napi-rs", features = [ + "compat-mode", ] } -napi-derive = { git = "https://github.com/napi-rs/napi-rs" } symphonia = { version = "0.5.3", features = ["all"] } +tokio = { version = "1", features = ["default"] } [build-dependencies] napi-build = { git = "https://github.com/napi-rs/napi-rs" } diff --git a/README.md b/README.md index 3e12b69..9b2ba95 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ You can use Mediaplex to probe media files for metadata. Here's an example: const mediaplex = require('mediaplex'); const stream = createReadStream('./media.mp3'); -const { result } = mediaplex.probeStream(stream); +const { result } = await mediaplex.probeStream(stream); console.log(result); @@ -58,6 +58,15 @@ console.log(result); ``` This will output an object containing information about the media file, including the number of channels, sample rate, codec, duration, and metadata. +The default probe size is `10MB`, but you can adjust this as needed by passing the second argument to `probeStream`: + +```js +// probe only 1024 bytes +const { result } = await mediaplex.probeStream(stream, 1024); + +// probe 5 MB +const { result } = await mediaplex.probeStream(stream, 5 * 1024 * 1024); +``` ## Opus Encoder @@ -74,7 +83,7 @@ const encoded = encoder.encode(buffer); const decoded = encoder.decode(encoded); ``` -You can use `OpusEncoder` to encode pcm data to opus and decode opus data to pcm format. +You can use `OpusEncoder` to encode pcm data to opus and decode opus data to pcm format. Stream interface is provided by [@discord-player/opus](https://npm.im/@discord-player/opus) package. #### Opus Benchmarks @@ -122,4 +131,4 @@ Finished 4 cases! Slowest: opusscript (no wasm) ``` -These benchmarks compare the performance of Mediaplex's Opus encoder/decoder to other popular Opus libraries. \ No newline at end of file +These benchmarks compare the performance of Mediaplex's Opus encoder/decoder to other popular Opus libraries. diff --git a/dev/index.js b/dev/index.js index 7afd3f2..1468ec2 100644 --- a/dev/index.js +++ b/dev/index.js @@ -2,7 +2,7 @@ const fs = require('fs'); const { probeStream, readMetadata } = require('../index.js'); const url = "https://cdn.discordapp.com/attachments/1128739211701395518/1134561530885705738/16905709686973736.mp3"; -// const stream = fs.createReadStream(`${__dirname}/test.pcm`); +// const stream = fs.createReadStream(`${__dirname}/test.mp3`); const stream = new Promise((resolve, reject) => { require('https').get(url, resolve); }); @@ -11,8 +11,11 @@ const stream = new Promise((resolve, reject) => { * @param {Buffer} chunk */ async function main() { - const probeResult = await probeStream(await stream); - console.log(probeResult.result); + const s = await stream; + const start = performance.now(); + const probeResult = await probeStream(s); + const end = performance.now() - start; + console.log(probeResult.result, readMetadata(probeResult.result), `Took ${end.toFixed(2)}ms`); // console.log(readMetadata(probeResult.result)); /* { diff --git a/index.js b/index.js index 83ea742..cf044d2 100644 --- a/index.js +++ b/index.js @@ -62,24 +62,17 @@ async function probeStream( }; const onClose = () => { - finish(resolved || null); - }; - - - const onData = (buffer) => { - readBuffer = Buffer.concat([readBuffer, buffer]); - try { const result = binding.probe(readBuffer); if (result != null) resolved = result; - - if (result && ![ - CodecType.MP3, - CodecType.MP1, - CodecType.MP2, - ].some(c => resolved.codec === c)) return finish(result); } catch { } + finish(resolved || null); + }; + + const onData = (buffer) => { + readBuffer = Buffer.concat([readBuffer, buffer]); + if (readBuffer.length >= probeSize) { stream.off('data', onData); stream.pause(); diff --git a/package.json b/package.json index 447ee26..76345c0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mediaplex", - "version": "0.0.3", + "version": "0.0.4", "main": "index.js", "types": "index.d.ts", "napi": {