mirror of
https://github.com/darksaid98/mediaplex.git
synced 2026-12-01 14:38:45 +00:00
0.0.0
This commit is contained in:
1 parent
8d6839244a
commit
dd706e7f23
14 files changed
+766
-8
No files matched your search
@@ -11,3 +11,4 @@ yarn.lock
|
||||
.yarn
|
||||
__test__
|
||||
renovate.json
|
||||
dev
|
||||
@@ -7,9 +7,11 @@ version = "0.0.0"
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
dasp = "0.11.0"
|
||||
# Default enable napi4 feature, see https://nodejs.org/api/n-api.html#node-api-version-matrix
|
||||
napi = { version = "2.12.2", default-features = false, features = ["napi4"] }
|
||||
napi-derive = "2.12.2"
|
||||
symphonia = { version = "0.5.3", features = ["all"] }
|
||||
|
||||
[build-dependencies]
|
||||
napi-build = "2.0.1"
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
# Mediaplex
|
||||
|
||||
Tiny media transcoding utility for node.
|
||||
|
||||
> 🏗️ This package is a work in progress.
|
||||
|
||||
# Installation
|
||||
|
||||
```sh
|
||||
$ npm install --save mediaplex
|
||||
```
|
||||
|
||||
# Example
|
||||
|
||||
```js
|
||||
const mediaplex = require('mediaplex');
|
||||
|
||||
const stream = createReadStream('./media.mp3');
|
||||
const result = mediaplex.probe(stream);
|
||||
|
||||
console.log(result);
|
||||
|
||||
/*
|
||||
{
|
||||
channels: 2,
|
||||
sampleRate: 44100,
|
||||
framesPerBlock: 0,
|
||||
codec: 4099, // use `CodecType` enum to validate this
|
||||
nFrames: 796032,
|
||||
duration: 18 // seconds
|
||||
}
|
||||
*/
|
||||
```
|
||||
Binary file not shown.
+12
-3
@@ -1,7 +1,16 @@
|
||||
import test from 'ava'
|
||||
import { probe } from '../index.js'
|
||||
import { readFile } from 'fs/promises';
|
||||
|
||||
import { sum } from '../index.js'
|
||||
test('should probe mp3 buffer', async (t) => {
|
||||
const chunk = await readFile('./__test__/data/head.mp3');
|
||||
|
||||
test('sum from native', (t) => {
|
||||
t.is(sum(1, 2), 3)
|
||||
t.deepEqual(probe(chunk), {
|
||||
channels: 2,
|
||||
sampleRate: 44100,
|
||||
framesPerBlock: 0,
|
||||
codec: 4099,
|
||||
nFrames: 796032,
|
||||
duration: 18
|
||||
});
|
||||
})
|
||||
@@ -0,0 +1,24 @@
|
||||
const fs = require('fs');
|
||||
const { probeStream } = require('../lib/index.js');
|
||||
|
||||
const stream = fs.createReadStream(`${__dirname}/test.mp3`);
|
||||
|
||||
/**
|
||||
* @param {Buffer} chunk
|
||||
*/
|
||||
async function main() {
|
||||
const probeResult = await probeStream(stream);
|
||||
console.log(probeResult.result);
|
||||
/*
|
||||
{
|
||||
channels: 2,
|
||||
sampleRate: 44100,
|
||||
framesPerBlock: 0,
|
||||
codec: 4099,
|
||||
nFrames: 796032,
|
||||
duration: 18
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
main();
|
||||
Binary file not shown.
Vendored
+13
@@ -0,0 +1,13 @@
|
||||
import type { ProbeResult } from './lib/binding'
|
||||
import type { Readable } from 'stream'
|
||||
|
||||
export type StreamProbeResult = {
|
||||
stream: Readable;
|
||||
result: ProbeResult;
|
||||
};
|
||||
|
||||
export * from './lib/binding';
|
||||
export function probeStream(
|
||||
stream: Readable,
|
||||
probeSize?: number
|
||||
): Promise<StreamProbeResult>;
|
||||
@@ -0,0 +1,7 @@
|
||||
const { CodecType, probe, probeStream } = require('./lib/index.js');
|
||||
|
||||
module.exports = {
|
||||
CodecType,
|
||||
probe,
|
||||
probeStream,
|
||||
};
|
||||
Vendored
+88
@@ -0,0 +1,88 @@
|
||||
/* tslint:disable */
|
||||
/* eslint-disable */
|
||||
|
||||
/* auto-generated by NAPI-RS */
|
||||
|
||||
export interface ProbeResult {
|
||||
/** The number of channels */
|
||||
channels: number
|
||||
/** The sample rate */
|
||||
sampleRate: number
|
||||
/** The number of frames per block */
|
||||
framesPerBlock: number
|
||||
/** The codec type */
|
||||
codec: JsCodecType
|
||||
/** The number of frames */
|
||||
nFrames: number
|
||||
/** The duration of this media in seconds */
|
||||
duration: number
|
||||
}
|
||||
export const enum CodecType {
|
||||
UNKNOWN = 0,
|
||||
PCM_S32LE = 256,
|
||||
PCM_S32LE_PLANAR = 257,
|
||||
PCM_S32BE = 258,
|
||||
PCM_S32BE_PLANAR = 259,
|
||||
PCM_S24LE = 260,
|
||||
PCM_S24LE_PLANAR = 261,
|
||||
PCM_S24BE = 262,
|
||||
PCM_S24BE_PLANAR = 263,
|
||||
PCM_S16LE = 264,
|
||||
PCM_S16LE_PLANAR = 265,
|
||||
PCM_S16BE = 266,
|
||||
PCM_S16BE_PLANAR = 267,
|
||||
PCM_S8 = 268,
|
||||
PCM_S8_PLANAR = 269,
|
||||
PCM_U32LE = 270,
|
||||
PCM_U32LE_PLANAR = 271,
|
||||
PCM_U32BE = 272,
|
||||
PCM_U32BE_PLANAR = 273,
|
||||
PCM_U24LE = 274,
|
||||
PCM_U24LE_PLANAR = 275,
|
||||
PCM_U24BE = 276,
|
||||
PCM_U24BE_PLANAR = 277,
|
||||
PCM_U16LE = 278,
|
||||
PCM_U16LE_PLANAR = 279,
|
||||
PCM_U16BE = 280,
|
||||
PCM_U16BE_PLANAR = 281,
|
||||
PCM_U8 = 282,
|
||||
PCM_U8_PLANAR = 283,
|
||||
PCM_F32LE = 284,
|
||||
PCM_F32LE_PLANAR = 285,
|
||||
PCM_F32BE = 286,
|
||||
PCM_F32BE_PLANAR = 287,
|
||||
PCM_F64LE = 288,
|
||||
PCM_F64LE_PLANAR = 289,
|
||||
PCM_F64BE = 290,
|
||||
PCM_F64BE_PLANAR = 291,
|
||||
PCM_ALAW = 292,
|
||||
PCM_MULAW = 293,
|
||||
ADPCM_G722 = 512,
|
||||
ADPCM_G726 = 513,
|
||||
ADPCM_G726LE = 514,
|
||||
ADPCM_MS = 515,
|
||||
ADPCM_IMA_WAV = 516,
|
||||
ADPCM_IMA_QT = 517,
|
||||
VORBIS = 4096,
|
||||
MP1 = 4097,
|
||||
MP2 = 4098,
|
||||
MP3 = 4099,
|
||||
AAC = 4100,
|
||||
OPUS = 4101,
|
||||
SPEEX = 4102,
|
||||
MUSEPACK = 4103,
|
||||
ATRAC1 = 4104,
|
||||
ATRAC3 = 4105,
|
||||
ATRAC3PLUS = 4106,
|
||||
ATRAC9 = 4107,
|
||||
EAC3 = 4108,
|
||||
AC4 = 4109,
|
||||
DCA = 4110,
|
||||
WMA = 4111,
|
||||
FLAC = 8192,
|
||||
WAVPACK = 8193,
|
||||
MONKEYS_AUDIO = 8194,
|
||||
ALAC = 8195,
|
||||
TTA = 8196
|
||||
}
|
||||
export function probe(data: Buffer): ProbeResult
|
||||
+258
@@ -0,0 +1,258 @@
|
||||
/* tslint:disable */
|
||||
/* eslint-disable */
|
||||
/* prettier-ignore */
|
||||
|
||||
/* auto-generated by NAPI-RS */
|
||||
|
||||
const { existsSync, readFileSync } = require('fs')
|
||||
const { join } = require('path')
|
||||
|
||||
const { platform, arch } = process
|
||||
|
||||
let nativeBinding = null
|
||||
let localFileExisted = false
|
||||
let loadError = null
|
||||
|
||||
function isMusl() {
|
||||
// For Node 10
|
||||
if (!process.report || typeof process.report.getReport !== 'function') {
|
||||
try {
|
||||
const lddPath = require('child_process').execSync('which ldd').toString().trim()
|
||||
return readFileSync(lddPath, 'utf8').includes('musl')
|
||||
} catch (e) {
|
||||
return true
|
||||
}
|
||||
} else {
|
||||
const { glibcVersionRuntime } = process.report.getReport().header
|
||||
return !glibcVersionRuntime
|
||||
}
|
||||
}
|
||||
|
||||
switch (platform) {
|
||||
case 'android':
|
||||
switch (arch) {
|
||||
case 'arm64':
|
||||
localFileExisted = existsSync(join(__dirname, 'mediaplex.android-arm64.node'))
|
||||
try {
|
||||
if (localFileExisted) {
|
||||
nativeBinding = require('./mediaplex.android-arm64.node')
|
||||
} else {
|
||||
nativeBinding = require('mediaplex-android-arm64')
|
||||
}
|
||||
} catch (e) {
|
||||
loadError = e
|
||||
}
|
||||
break
|
||||
case 'arm':
|
||||
localFileExisted = existsSync(join(__dirname, 'mediaplex.android-arm-eabi.node'))
|
||||
try {
|
||||
if (localFileExisted) {
|
||||
nativeBinding = require('./mediaplex.android-arm-eabi.node')
|
||||
} else {
|
||||
nativeBinding = require('mediaplex-android-arm-eabi')
|
||||
}
|
||||
} catch (e) {
|
||||
loadError = e
|
||||
}
|
||||
break
|
||||
default:
|
||||
throw new Error(`Unsupported architecture on Android ${arch}`)
|
||||
}
|
||||
break
|
||||
case 'win32':
|
||||
switch (arch) {
|
||||
case 'x64':
|
||||
localFileExisted = existsSync(
|
||||
join(__dirname, 'mediaplex.win32-x64-msvc.node')
|
||||
)
|
||||
try {
|
||||
if (localFileExisted) {
|
||||
nativeBinding = require('./mediaplex.win32-x64-msvc.node')
|
||||
} else {
|
||||
nativeBinding = require('mediaplex-win32-x64-msvc')
|
||||
}
|
||||
} catch (e) {
|
||||
loadError = e
|
||||
}
|
||||
break
|
||||
case 'ia32':
|
||||
localFileExisted = existsSync(
|
||||
join(__dirname, 'mediaplex.win32-ia32-msvc.node')
|
||||
)
|
||||
try {
|
||||
if (localFileExisted) {
|
||||
nativeBinding = require('./mediaplex.win32-ia32-msvc.node')
|
||||
} else {
|
||||
nativeBinding = require('mediaplex-win32-ia32-msvc')
|
||||
}
|
||||
} catch (e) {
|
||||
loadError = e
|
||||
}
|
||||
break
|
||||
case 'arm64':
|
||||
localFileExisted = existsSync(
|
||||
join(__dirname, 'mediaplex.win32-arm64-msvc.node')
|
||||
)
|
||||
try {
|
||||
if (localFileExisted) {
|
||||
nativeBinding = require('./mediaplex.win32-arm64-msvc.node')
|
||||
} else {
|
||||
nativeBinding = require('mediaplex-win32-arm64-msvc')
|
||||
}
|
||||
} catch (e) {
|
||||
loadError = e
|
||||
}
|
||||
break
|
||||
default:
|
||||
throw new Error(`Unsupported architecture on Windows: ${arch}`)
|
||||
}
|
||||
break
|
||||
case 'darwin':
|
||||
localFileExisted = existsSync(join(__dirname, 'mediaplex.darwin-universal.node'))
|
||||
try {
|
||||
if (localFileExisted) {
|
||||
nativeBinding = require('./mediaplex.darwin-universal.node')
|
||||
} else {
|
||||
nativeBinding = require('mediaplex-darwin-universal')
|
||||
}
|
||||
break
|
||||
} catch {}
|
||||
switch (arch) {
|
||||
case 'x64':
|
||||
localFileExisted = existsSync(join(__dirname, 'mediaplex.darwin-x64.node'))
|
||||
try {
|
||||
if (localFileExisted) {
|
||||
nativeBinding = require('./mediaplex.darwin-x64.node')
|
||||
} else {
|
||||
nativeBinding = require('mediaplex-darwin-x64')
|
||||
}
|
||||
} catch (e) {
|
||||
loadError = e
|
||||
}
|
||||
break
|
||||
case 'arm64':
|
||||
localFileExisted = existsSync(
|
||||
join(__dirname, 'mediaplex.darwin-arm64.node')
|
||||
)
|
||||
try {
|
||||
if (localFileExisted) {
|
||||
nativeBinding = require('./mediaplex.darwin-arm64.node')
|
||||
} else {
|
||||
nativeBinding = require('mediaplex-darwin-arm64')
|
||||
}
|
||||
} catch (e) {
|
||||
loadError = e
|
||||
}
|
||||
break
|
||||
default:
|
||||
throw new Error(`Unsupported architecture on macOS: ${arch}`)
|
||||
}
|
||||
break
|
||||
case 'freebsd':
|
||||
if (arch !== 'x64') {
|
||||
throw new Error(`Unsupported architecture on FreeBSD: ${arch}`)
|
||||
}
|
||||
localFileExisted = existsSync(join(__dirname, 'mediaplex.freebsd-x64.node'))
|
||||
try {
|
||||
if (localFileExisted) {
|
||||
nativeBinding = require('./mediaplex.freebsd-x64.node')
|
||||
} else {
|
||||
nativeBinding = require('mediaplex-freebsd-x64')
|
||||
}
|
||||
} catch (e) {
|
||||
loadError = e
|
||||
}
|
||||
break
|
||||
case 'linux':
|
||||
switch (arch) {
|
||||
case 'x64':
|
||||
if (isMusl()) {
|
||||
localFileExisted = existsSync(
|
||||
join(__dirname, 'mediaplex.linux-x64-musl.node')
|
||||
)
|
||||
try {
|
||||
if (localFileExisted) {
|
||||
nativeBinding = require('./mediaplex.linux-x64-musl.node')
|
||||
} else {
|
||||
nativeBinding = require('mediaplex-linux-x64-musl')
|
||||
}
|
||||
} catch (e) {
|
||||
loadError = e
|
||||
}
|
||||
} else {
|
||||
localFileExisted = existsSync(
|
||||
join(__dirname, 'mediaplex.linux-x64-gnu.node')
|
||||
)
|
||||
try {
|
||||
if (localFileExisted) {
|
||||
nativeBinding = require('./mediaplex.linux-x64-gnu.node')
|
||||
} else {
|
||||
nativeBinding = require('mediaplex-linux-x64-gnu')
|
||||
}
|
||||
} catch (e) {
|
||||
loadError = e
|
||||
}
|
||||
}
|
||||
break
|
||||
case 'arm64':
|
||||
if (isMusl()) {
|
||||
localFileExisted = existsSync(
|
||||
join(__dirname, 'mediaplex.linux-arm64-musl.node')
|
||||
)
|
||||
try {
|
||||
if (localFileExisted) {
|
||||
nativeBinding = require('./mediaplex.linux-arm64-musl.node')
|
||||
} else {
|
||||
nativeBinding = require('mediaplex-linux-arm64-musl')
|
||||
}
|
||||
} catch (e) {
|
||||
loadError = e
|
||||
}
|
||||
} else {
|
||||
localFileExisted = existsSync(
|
||||
join(__dirname, 'mediaplex.linux-arm64-gnu.node')
|
||||
)
|
||||
try {
|
||||
if (localFileExisted) {
|
||||
nativeBinding = require('./mediaplex.linux-arm64-gnu.node')
|
||||
} else {
|
||||
nativeBinding = require('mediaplex-linux-arm64-gnu')
|
||||
}
|
||||
} catch (e) {
|
||||
loadError = e
|
||||
}
|
||||
}
|
||||
break
|
||||
case 'arm':
|
||||
localFileExisted = existsSync(
|
||||
join(__dirname, 'mediaplex.linux-arm-gnueabihf.node')
|
||||
)
|
||||
try {
|
||||
if (localFileExisted) {
|
||||
nativeBinding = require('./mediaplex.linux-arm-gnueabihf.node')
|
||||
} else {
|
||||
nativeBinding = require('mediaplex-linux-arm-gnueabihf')
|
||||
}
|
||||
} catch (e) {
|
||||
loadError = e
|
||||
}
|
||||
break
|
||||
default:
|
||||
throw new Error(`Unsupported architecture on Linux: ${arch}`)
|
||||
}
|
||||
break
|
||||
default:
|
||||
throw new Error(`Unsupported OS: ${platform}, architecture: ${arch}`)
|
||||
}
|
||||
|
||||
if (!nativeBinding) {
|
||||
if (loadError) {
|
||||
throw loadError
|
||||
}
|
||||
throw new Error(`Failed to load native binding`)
|
||||
}
|
||||
|
||||
const { CodecType, probe } = nativeBinding
|
||||
|
||||
module.exports.CodecType = CodecType
|
||||
module.exports.probe = probe
|
||||
@@ -0,0 +1,95 @@
|
||||
const binding = require('./binding');
|
||||
const { Readable } = require('stream');
|
||||
|
||||
/**
|
||||
* @typedef StreamProbeResult
|
||||
* @property {Readable} stream The source stream
|
||||
* @property {binding.ProbeResult} result The probe result
|
||||
*/
|
||||
|
||||
/**
|
||||
* Attempt to probe a Readable stream
|
||||
* @param {Readable} stream The readable stream to probe
|
||||
* @param {number} [probeSize] The number of bytes to read
|
||||
* @returns {Promise<StreamProbeResult>}
|
||||
*/
|
||||
async function probeStream(
|
||||
stream,
|
||||
probeSize = 1024
|
||||
) {
|
||||
return new Promise((resolve, reject) => {
|
||||
// preconditions
|
||||
if (stream.readableObjectMode) {
|
||||
reject(new Error('Cannot probe a readable stream in object mode'));
|
||||
return;
|
||||
}
|
||||
|
||||
if (stream.readableEnded) {
|
||||
reject(new Error('Cannot probe a stream that has ended'));
|
||||
return;
|
||||
}
|
||||
|
||||
let readBuffer = Buffer.alloc(0);
|
||||
|
||||
/**
|
||||
* @type {binding.ProbeResult}
|
||||
*/
|
||||
let resolved = null;
|
||||
|
||||
const finish = (data) => {
|
||||
stream.off('data', onData);
|
||||
stream.off('close', onClose);
|
||||
stream.off('end', onClose);
|
||||
stream.pause();
|
||||
|
||||
resolved = data;
|
||||
|
||||
if (stream.readableEnded) {
|
||||
resolve({
|
||||
stream: Readable.from(readBuffer),
|
||||
result: data,
|
||||
});
|
||||
} else {
|
||||
if (readBuffer.length > 0) {
|
||||
stream.push(readBuffer);
|
||||
}
|
||||
|
||||
resolve({
|
||||
stream,
|
||||
result: data,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const onClose = () => {
|
||||
if (!resolved) {
|
||||
finish(null);
|
||||
}
|
||||
};
|
||||
|
||||
const onData = (buffer) => {
|
||||
readBuffer = Buffer.concat([readBuffer, buffer]);
|
||||
|
||||
try {
|
||||
const result = binding.probe(readBuffer);
|
||||
return finish(result);
|
||||
} catch {}
|
||||
|
||||
if (readBuffer.length >= probeSize) {
|
||||
stream.off('data', onData);
|
||||
stream.pause();
|
||||
process.nextTick(onClose);
|
||||
}
|
||||
};
|
||||
|
||||
stream.once('error', reject);
|
||||
stream.on('data', onData);
|
||||
stream.once('close', onClose);
|
||||
stream.once('end', onClose);
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
...binding,
|
||||
probeStream,
|
||||
};
|
||||
+2
-2
@@ -34,8 +34,8 @@
|
||||
},
|
||||
"scripts": {
|
||||
"artifacts": "napi artifacts",
|
||||
"build": "napi build --platform --release",
|
||||
"build:debug": "napi build --platform",
|
||||
"build": "yarn build:debug --release",
|
||||
"build:debug": "napi build --platform --dts binding.d.ts --js ./lib/binding.js ./lib",
|
||||
"prepublishOnly": "napi prepublish -t npm",
|
||||
"test": "ava",
|
||||
"universal": "napi universal",
|
||||
|
||||
+231
-3
@@ -1,9 +1,237 @@
|
||||
#![deny(clippy::all)]
|
||||
#![allow(non_camel_case_types, non_snake_case)]
|
||||
|
||||
#[macro_use]
|
||||
extern crate napi_derive;
|
||||
|
||||
#[napi]
|
||||
pub fn sum(a: i32, b: i32) -> i32 {
|
||||
a + b
|
||||
use napi::bindgen_prelude::*;
|
||||
use napi::Result;
|
||||
use symphonia::core::audio::Channels;
|
||||
use symphonia::core::codecs::*;
|
||||
use symphonia::core::io::MediaSourceStream;
|
||||
use symphonia::core::probe::Hint;
|
||||
use symphonia::core::units::TimeBase;
|
||||
|
||||
#[napi(object, js_name = "ProbeResult")]
|
||||
pub struct JsProbeResult {
|
||||
/// The number of channels
|
||||
pub channels: u32,
|
||||
/// The sample rate
|
||||
pub sample_rate: u32,
|
||||
/// The number of frames per block
|
||||
pub frames_per_block: u32,
|
||||
/// The codec type
|
||||
pub codec: JsCodecType,
|
||||
/// The number of frames
|
||||
pub n_frames: u32,
|
||||
/// The duration of this media in seconds
|
||||
pub duration: u32,
|
||||
}
|
||||
|
||||
#[napi(js_name = "CodecType")]
|
||||
pub enum JsCodecType {
|
||||
// Unknown
|
||||
UNKNOWN = 0,
|
||||
// PCM
|
||||
PCM_S32LE = 0x100,
|
||||
PCM_S32LE_PLANAR = 0x101,
|
||||
PCM_S32BE = 0x102,
|
||||
PCM_S32BE_PLANAR = 0x103,
|
||||
PCM_S24LE = 0x104,
|
||||
PCM_S24LE_PLANAR = 0x105,
|
||||
PCM_S24BE = 0x106,
|
||||
PCM_S24BE_PLANAR = 0x107,
|
||||
PCM_S16LE = 0x108,
|
||||
PCM_S16LE_PLANAR = 0x109,
|
||||
PCM_S16BE = 0x10a,
|
||||
PCM_S16BE_PLANAR = 0x10b,
|
||||
PCM_S8 = 0x10c,
|
||||
PCM_S8_PLANAR = 0x10d,
|
||||
PCM_U32LE = 0x10e,
|
||||
PCM_U32LE_PLANAR = 0x10f,
|
||||
PCM_U32BE = 0x110,
|
||||
PCM_U32BE_PLANAR = 0x111,
|
||||
PCM_U24LE = 0x112,
|
||||
PCM_U24LE_PLANAR = 0x113,
|
||||
PCM_U24BE = 0x114,
|
||||
PCM_U24BE_PLANAR = 0x115,
|
||||
PCM_U16LE = 0x116,
|
||||
PCM_U16LE_PLANAR = 0x117,
|
||||
PCM_U16BE = 0x118,
|
||||
PCM_U16BE_PLANAR = 0x119,
|
||||
PCM_U8 = 0x11a,
|
||||
PCM_U8_PLANAR = 0x11b,
|
||||
PCM_F32LE = 0x11c,
|
||||
PCM_F32LE_PLANAR = 0x11d,
|
||||
PCM_F32BE = 0x11e,
|
||||
PCM_F32BE_PLANAR = 0x11f,
|
||||
PCM_F64LE = 0x120,
|
||||
PCM_F64LE_PLANAR = 0x121,
|
||||
PCM_F64BE = 0x122,
|
||||
PCM_F64BE_PLANAR = 0x123,
|
||||
PCM_ALAW = 0x124,
|
||||
PCM_MULAW = 0x125,
|
||||
// ADPCM
|
||||
ADPCM_G722 = 0x200,
|
||||
ADPCM_G726 = 0x201,
|
||||
ADPCM_G726LE = 0x202,
|
||||
ADPCM_MS = 0x203,
|
||||
ADPCM_IMA_WAV = 0x204,
|
||||
ADPCM_IMA_QT = 0x205,
|
||||
// Vorbis
|
||||
VORBIS = 0x1000,
|
||||
// MPEG Layer 1 (MP1)
|
||||
MP1 = 0x1001,
|
||||
// MPEG Layer 2 (MP2)
|
||||
MP2 = 0x1002,
|
||||
// MPEG Layer 3 (MP3)
|
||||
MP3 = 0x1003,
|
||||
// Advanced Audio Coding (AAC)
|
||||
AAC = 0x1004,
|
||||
// Opus
|
||||
OPUS = 0x1005,
|
||||
// Speex
|
||||
SPEEX = 0x1006,
|
||||
// Musepack
|
||||
MUSEPACK = 0x1007,
|
||||
// Adaptive Transform Acoustic Coding (ATRAC1)
|
||||
ATRAC1 = 0x1008,
|
||||
// Adaptive Transform Acoustic Coding 3 (ATRAC3)
|
||||
ATRAC3 = 0x1009,
|
||||
// Adaptive Transform Acoustic Coding 3+ (ATRAC3+)
|
||||
ATRAC3PLUS = 0x100a,
|
||||
// Adaptive Transform Acoustic Coding 9 (ATRAC9)
|
||||
ATRAC9 = 0x100b,
|
||||
// AC-3, E-AC-3, Dolby Digital (ATSC A/52)
|
||||
EAC3 = 0x100c,
|
||||
// Dolby AC-4 (ETSI TS 103 190)
|
||||
AC4 = 0x100d,
|
||||
// DTS Coherent Acoustics (DCA/DTS)
|
||||
DCA = 0x100e,
|
||||
// Windows Media Audio
|
||||
WMA = 0x100f,
|
||||
// FLAC
|
||||
FLAC = 0x2000,
|
||||
// WavPack
|
||||
WAVPACK = 0x2001,
|
||||
// Monkey's Audio (APE)
|
||||
MONKEYS_AUDIO = 0x2002,
|
||||
// Apple Lossless Audio Codec (ALAC)
|
||||
ALAC = 0x2003,
|
||||
// True Audio (TTA)
|
||||
TTA = 0x2004,
|
||||
}
|
||||
|
||||
#[napi(catch_unwind)]
|
||||
pub fn probe(data: Buffer) -> Result<JsProbeResult> {
|
||||
// Create a Cursor over the data.
|
||||
let cursor = std::io::Cursor::new(data.to_vec());
|
||||
|
||||
// Create a MediaSourceStream from the Cursor.
|
||||
let mss = MediaSourceStream::new(Box::new(cursor), Default::default());
|
||||
|
||||
let hint = Hint::new();
|
||||
|
||||
// Use the default options for metadata and format readers.
|
||||
let meta_opts: symphonia::core::meta::MetadataOptions = Default::default();
|
||||
let fmt_opts: symphonia::core::formats::FormatOptions = Default::default();
|
||||
|
||||
let probed = symphonia::default::get_probe()
|
||||
.format(&hint, mss, &fmt_opts, &meta_opts)
|
||||
.expect("unsupported format");
|
||||
|
||||
let format = probed.format;
|
||||
|
||||
let track = format
|
||||
.tracks()
|
||||
.iter()
|
||||
.find(|t| t.codec_params.codec != CODEC_TYPE_NULL)
|
||||
.expect("no supported audio tracks");
|
||||
|
||||
let codec_params = &track.codec_params;
|
||||
|
||||
Ok(JsProbeResult {
|
||||
channels: codec_params
|
||||
.channels
|
||||
.unwrap_or(Channels::FRONT_CENTRE)
|
||||
.count() as u32,
|
||||
sample_rate: codec_params.sample_rate.unwrap_or(44100),
|
||||
frames_per_block: codec_params.frames_per_block.unwrap_or(0) as u32,
|
||||
codec: match codec_params.codec {
|
||||
CODEC_TYPE_PCM_S32LE => JsCodecType::PCM_S32LE,
|
||||
CODEC_TYPE_PCM_S32LE_PLANAR => JsCodecType::PCM_S32LE_PLANAR,
|
||||
CODEC_TYPE_PCM_S32BE => JsCodecType::PCM_S32BE,
|
||||
CODEC_TYPE_PCM_S32BE_PLANAR => JsCodecType::PCM_S32BE_PLANAR,
|
||||
CODEC_TYPE_PCM_S24LE => JsCodecType::PCM_S24LE,
|
||||
CODEC_TYPE_PCM_S24LE_PLANAR => JsCodecType::PCM_S24LE_PLANAR,
|
||||
CODEC_TYPE_PCM_S24BE => JsCodecType::PCM_S24BE,
|
||||
CODEC_TYPE_PCM_S24BE_PLANAR => JsCodecType::PCM_S24BE_PLANAR,
|
||||
CODEC_TYPE_PCM_S16LE => JsCodecType::PCM_S16LE,
|
||||
CODEC_TYPE_PCM_S16LE_PLANAR => JsCodecType::PCM_S16LE_PLANAR,
|
||||
CODEC_TYPE_PCM_S16BE => JsCodecType::PCM_S16BE,
|
||||
CODEC_TYPE_PCM_S16BE_PLANAR => JsCodecType::PCM_S16BE_PLANAR,
|
||||
CODEC_TYPE_PCM_S8 => JsCodecType::PCM_S8,
|
||||
CODEC_TYPE_PCM_S8_PLANAR => JsCodecType::PCM_S8_PLANAR,
|
||||
CODEC_TYPE_PCM_U32LE => JsCodecType::PCM_U32LE,
|
||||
CODEC_TYPE_PCM_U32LE_PLANAR => JsCodecType::PCM_U32LE_PLANAR,
|
||||
CODEC_TYPE_PCM_U32BE => JsCodecType::PCM_U32BE,
|
||||
CODEC_TYPE_PCM_U32BE_PLANAR => JsCodecType::PCM_U32BE_PLANAR,
|
||||
CODEC_TYPE_PCM_U24LE => JsCodecType::PCM_U24LE,
|
||||
CODEC_TYPE_PCM_U24LE_PLANAR => JsCodecType::PCM_U24LE_PLANAR,
|
||||
CODEC_TYPE_PCM_U24BE => JsCodecType::PCM_U24BE,
|
||||
CODEC_TYPE_PCM_U24BE_PLANAR => JsCodecType::PCM_U24BE_PLANAR,
|
||||
CODEC_TYPE_PCM_U16LE => JsCodecType::PCM_U16LE,
|
||||
CODEC_TYPE_PCM_U16LE_PLANAR => JsCodecType::PCM_U16LE_PLANAR,
|
||||
CODEC_TYPE_PCM_U16BE => JsCodecType::PCM_U16BE,
|
||||
CODEC_TYPE_PCM_U16BE_PLANAR => JsCodecType::PCM_U16BE_PLANAR,
|
||||
CODEC_TYPE_PCM_U8 => JsCodecType::PCM_U8,
|
||||
CODEC_TYPE_PCM_U8_PLANAR => JsCodecType::PCM_U8_PLANAR,
|
||||
CODEC_TYPE_PCM_F32LE => JsCodecType::PCM_F32LE,
|
||||
CODEC_TYPE_PCM_F32LE_PLANAR => JsCodecType::PCM_F32LE_PLANAR,
|
||||
CODEC_TYPE_PCM_F32BE => JsCodecType::PCM_F32BE,
|
||||
CODEC_TYPE_PCM_F32BE_PLANAR => JsCodecType::PCM_F32BE_PLANAR,
|
||||
CODEC_TYPE_PCM_F64LE => JsCodecType::PCM_F64LE,
|
||||
CODEC_TYPE_PCM_F64LE_PLANAR => JsCodecType::PCM_F64LE_PLANAR,
|
||||
CODEC_TYPE_PCM_F64BE => JsCodecType::PCM_F64BE,
|
||||
CODEC_TYPE_PCM_F64BE_PLANAR => JsCodecType::PCM_F64BE_PLANAR,
|
||||
CODEC_TYPE_PCM_ALAW => JsCodecType::PCM_ALAW,
|
||||
CODEC_TYPE_PCM_MULAW => JsCodecType::PCM_MULAW,
|
||||
CODEC_TYPE_ADPCM_G722 => JsCodecType::ADPCM_G722,
|
||||
CODEC_TYPE_ADPCM_G726 => JsCodecType::ADPCM_G726,
|
||||
CODEC_TYPE_ADPCM_G726LE => JsCodecType::ADPCM_G726LE,
|
||||
CODEC_TYPE_ADPCM_MS => JsCodecType::ADPCM_MS,
|
||||
CODEC_TYPE_ADPCM_IMA_WAV => JsCodecType::ADPCM_IMA_WAV,
|
||||
CODEC_TYPE_ADPCM_IMA_QT => JsCodecType::ADPCM_IMA_QT,
|
||||
CODEC_TYPE_VORBIS => JsCodecType::VORBIS,
|
||||
CODEC_TYPE_MP1 => JsCodecType::MP1,
|
||||
CODEC_TYPE_MP2 => JsCodecType::MP2,
|
||||
CODEC_TYPE_MP3 => JsCodecType::MP3,
|
||||
CODEC_TYPE_AAC => JsCodecType::AAC,
|
||||
CODEC_TYPE_OPUS => JsCodecType::OPUS,
|
||||
CODEC_TYPE_SPEEX => JsCodecType::SPEEX,
|
||||
CODEC_TYPE_MUSEPACK => JsCodecType::MUSEPACK,
|
||||
CODEC_TYPE_ATRAC1 => JsCodecType::ATRAC1,
|
||||
CODEC_TYPE_ATRAC3 => JsCodecType::ATRAC3,
|
||||
CODEC_TYPE_ATRAC3PLUS => JsCodecType::ATRAC3PLUS,
|
||||
CODEC_TYPE_ATRAC9 => JsCodecType::ATRAC9,
|
||||
CODEC_TYPE_EAC3 => JsCodecType::EAC3,
|
||||
CODEC_TYPE_AC4 => JsCodecType::AC4,
|
||||
CODEC_TYPE_DCA => JsCodecType::DCA,
|
||||
CODEC_TYPE_WMA => JsCodecType::WMA,
|
||||
CODEC_TYPE_FLAC => JsCodecType::FLAC,
|
||||
CODEC_TYPE_WAVPACK => JsCodecType::WAVPACK,
|
||||
CODEC_TYPE_MONKEYS_AUDIO => JsCodecType::MONKEYS_AUDIO,
|
||||
CODEC_TYPE_ALAC => JsCodecType::ALAC,
|
||||
CODEC_TYPE_TTA => JsCodecType::TTA,
|
||||
_ => JsCodecType::UNKNOWN
|
||||
},
|
||||
n_frames: codec_params.n_frames.unwrap_or(0) as u32,
|
||||
duration: TimeBase::calc_time(
|
||||
&codec_params
|
||||
.time_base
|
||||
.unwrap_or(TimeBase { numer: 1, denom: 1 }),
|
||||
codec_params.n_frames.unwrap_or(0),
|
||||
)
|
||||
.seconds as u32,
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user