improve probe accuracy

This commit is contained in:
skdhg committed 2023-08-12 14:09:29 +05:45
1 parent 888e075376
commit 3f0a20b3a2
3 files changed
+19 -10

No files matched your search

+6 -2
View File
@@ -1,13 +1,17 @@
const fs = require('fs');
const { probeStream, readMetadata } = require('../index.js');
const stream = fs.createReadStream(`${__dirname}/demo.pcm`);
const url = "https://cdn.discordapp.com/attachments/1128739211701395518/1134561530885705738/16905709686973736.mp3";
// const stream = fs.createReadStream(`${__dirname}/test.pcm`);
const stream = new Promise((resolve, reject) => {
require('https').get(url, resolve);
});
/**
* @param {Buffer} chunk
*/
async function main() {
const probeResult = await probeStream(stream);
const probeResult = await probeStream(await stream);
console.log(probeResult.result);
// console.log(readMetadata(probeResult.result));
/*
+12 -7
View File
@@ -10,12 +10,12 @@ const { Readable } = require('stream');
/**
* Attempt to probe a Readable stream
* @param {Readable} stream The readable stream to probe
* @param {number} [probeSize] The number of bytes to read
* @param {number} [probeSize] The number of bytes to read. Defaults to 10 MB
* @returns {Promise<StreamProbeResult>}
*/
async function probeStream(
stream,
probeSize = 1024
probeSize = 1024 * 1024 * 10
) {
return new Promise((resolve, reject) => {
// preconditions
@@ -33,7 +33,7 @@ async function probeStream(
/**
* @type {binding.ProbeResult}
*/
*/
let resolved = null;
const finish = (data) => {
@@ -62,17 +62,22 @@ async function probeStream(
};
const onClose = () => {
if (!resolved) {
finish(null);
}
finish(resolved || null);
};
const onData = (buffer) => {
readBuffer = Buffer.concat([readBuffer, buffer]);
try {
const result = binding.probe(readBuffer);
return finish(result);
if (result != null) resolved = result;
if (result && ![
CodecType.MP3,
CodecType.MP1,
CodecType.MP2,
].some(c => resolved.codec === c)) return finish(result);
} catch { }
if (readBuffer.length >= probeSize) {
+1 -1
View File
@@ -14,7 +14,7 @@ pub struct JsProbeResult {
/// The number of frames per block
pub frames_per_block: u32,
/// The codec type
#[napi(ts_type="CodecType")]
#[napi(ts_type = "CodecType")]
pub codec: JsCodecType,
/// The number of frames
pub n_frames: u32,