mirror of
https://github.com/darksaid98/mediaplex.git
synced 2026-12-01 14:38:45 +00:00
0.0.1
This commit is contained in:
1 parent
64224cb2ae
commit
b48814e7d2
10 files changed
+577
-236
No files matched your search
+2
-2
@@ -1,5 +1,5 @@
|
||||
const fs = require('fs');
|
||||
const { probeStream } = require('../lib/index.js');
|
||||
const { probeStream, readMetadata } = require('../index.js');
|
||||
|
||||
const stream = fs.createReadStream(`${__dirname}/test.mp3`);
|
||||
|
||||
@@ -8,7 +8,7 @@ const stream = fs.createReadStream(`${__dirname}/test.mp3`);
|
||||
*/
|
||||
async function main() {
|
||||
const probeResult = await probeStream(stream);
|
||||
console.log(probeResult.result);
|
||||
console.log(readMetadata(probeResult.result));
|
||||
/*
|
||||
{
|
||||
channels: 2,
|
||||
|
||||
Binary file not shown.
Vendored
+9
-2
@@ -6,8 +6,15 @@ export type StreamProbeResult = {
|
||||
result: ProbeResult;
|
||||
};
|
||||
|
||||
export * from './lib/binding';
|
||||
export interface Metadata {
|
||||
title: string | null;
|
||||
author: string | null;
|
||||
genre: string | null;
|
||||
}
|
||||
|
||||
export * from './js-binding';
|
||||
export function probeStream(
|
||||
stream: Readable,
|
||||
probeSize?: number
|
||||
): Promise<StreamProbeResult>;
|
||||
): Promise<StreamProbeResult>;
|
||||
export function readMetadata(result: ProbeResult): Metadata;
|
||||
@@ -89,10 +89,39 @@ async function probeStream(
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads metadata tags
|
||||
* @param {import('./js-binding').ProbeResult} result The probe result to read metadata from
|
||||
*/
|
||||
function readMetadata(result) {
|
||||
const res = {
|
||||
title: null,
|
||||
author: null,
|
||||
genre: null
|
||||
};
|
||||
|
||||
if (!result.metadata?.length) return res;
|
||||
|
||||
result.metadata.forEach(m => {
|
||||
if (!m.value) return;
|
||||
|
||||
if ((m.name === 'TPE1' || m.name === 'TPUB') && !res['author']) {
|
||||
res['author'] = m.value;
|
||||
} else if (m.name === 'TIT2') {
|
||||
res['title'] = m.value;
|
||||
} else if ((m.name === 'TALB' || m.name === 'TCON') && !res['genre']) {
|
||||
res['genre'] = m.value;
|
||||
}
|
||||
});
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
const { CodecType, probe } = binding;
|
||||
|
||||
module.exports = {
|
||||
CodecType,
|
||||
probe,
|
||||
probeStream,
|
||||
readMetadata
|
||||
};
|
||||
Vendored
+6
@@ -16,6 +16,8 @@ export interface ProbeResult {
|
||||
nFrames: number
|
||||
/** The approximate duration of this media in seconds */
|
||||
duration: number
|
||||
/** The metadata object */
|
||||
metadata: Array<MetadataField>
|
||||
}
|
||||
export const enum CodecType {
|
||||
UNKNOWN = 0,
|
||||
@@ -85,4 +87,8 @@ export const enum CodecType {
|
||||
ALAC = 8195,
|
||||
TTA = 8196
|
||||
}
|
||||
export interface MetadataField {
|
||||
name: string
|
||||
value: string
|
||||
}
|
||||
export function probe(data: Buffer): ProbeResult
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "mediaplex",
|
||||
"version": "0.0.0",
|
||||
"version": "0.0.1",
|
||||
"main": "index.js",
|
||||
"types": "index.d.ts",
|
||||
"napi": {
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
use std::ops::Deref;
|
||||
use thiserror::Error;
|
||||
|
||||
pub mod symphonia_decoder;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum DecoderError {
|
||||
#[error("Symphonia Decoder Error: {0}")]
|
||||
SymphoniaDecoder(String),
|
||||
}
|
||||
|
||||
pub type DecoderResult<T> = Result<T, DecoderError>;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum AudioPacketError {
|
||||
#[error("Decoder Raw Error: Can't return Raw on Samples")]
|
||||
Raw,
|
||||
#[error("Decoder Samples Error: Can't return Samples on Raw")]
|
||||
Samples,
|
||||
}
|
||||
|
||||
pub type AudioPacketResult<T> = Result<T, AudioPacketError>;
|
||||
|
||||
pub enum AudioPacket {
|
||||
Samples(Vec<f64>),
|
||||
Raw(Vec<u8>),
|
||||
}
|
||||
|
||||
impl AudioPacket {
|
||||
pub fn samples(&self) -> AudioPacketResult<&[f64]> {
|
||||
match self {
|
||||
AudioPacket::Samples(s) => Ok(s),
|
||||
AudioPacket::Raw(_) => Err(AudioPacketError::Raw),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn raw(&self) -> AudioPacketResult<&[u8]> {
|
||||
match self {
|
||||
AudioPacket::Raw(d) => Ok(d),
|
||||
AudioPacket::Samples(_) => Err(AudioPacketError::Samples),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
match self {
|
||||
AudioPacket::Samples(s) => s.is_empty(),
|
||||
AudioPacket::Raw(d) => d.is_empty(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct AudioPacketPosition {
|
||||
pub position_ms: u32,
|
||||
pub skipped: bool,
|
||||
}
|
||||
|
||||
impl Deref for AudioPacketPosition {
|
||||
type Target = u32;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.position_ms
|
||||
}
|
||||
}
|
||||
|
||||
pub trait AudioDecoder {
|
||||
fn seek(&mut self, position_ms: u32) -> Result<u32, DecoderError>;
|
||||
fn next_packet(
|
||||
&mut self,
|
||||
) -> DecoderResult<Option<(AudioPacketPosition, AudioPacket, u16, u32)>>;
|
||||
}
|
||||
|
||||
impl From<symphonia::core::errors::Error> for DecoderError {
|
||||
fn from(err: symphonia::core::errors::Error) -> Self {
|
||||
Self::SymphoniaDecoder(err.to_string())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
use std::io;
|
||||
|
||||
use log::warn;
|
||||
use symphonia::core::{
|
||||
audio::SampleBuffer,
|
||||
codecs::{Decoder, DecoderOptions, CODEC_TYPE_NULL},
|
||||
errors::Error,
|
||||
formats::{FormatOptions, FormatReader, SeekMode, SeekTo, Track},
|
||||
io::{MediaSource, MediaSourceStream},
|
||||
meta::{MetadataOptions, Visual},
|
||||
probe::{Hint, ProbeResult},
|
||||
units::{Time, TimeBase},
|
||||
};
|
||||
|
||||
use super::{AudioDecoder, AudioPacket, AudioPacketPosition, DecoderError, DecoderResult};
|
||||
|
||||
use crate::PAGES_PER_MS;
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
struct PlayTrackOptions {
|
||||
track_id: u32,
|
||||
seek_ts: u64,
|
||||
}
|
||||
|
||||
fn first_supported_track(tracks: &[Track]) -> Option<&Track> {
|
||||
tracks
|
||||
.iter()
|
||||
.find(|t| t.codec_params.codec != CODEC_TYPE_NULL)
|
||||
}
|
||||
|
||||
pub struct SymphoniaDecoder {
|
||||
format: Box<dyn FormatReader>,
|
||||
decoder: Box<dyn Decoder>,
|
||||
sample_buffer: Option<SampleBuffer<f64>>,
|
||||
}
|
||||
|
||||
impl SymphoniaDecoder {
|
||||
pub fn new<R>(input: R, hint: Hint) -> DecoderResult<Self>
|
||||
where
|
||||
R: MediaSource + 'static,
|
||||
{
|
||||
let mss = MediaSourceStream::new(Box::new(input), Default::default());
|
||||
|
||||
let format_opts = FormatOptions {
|
||||
enable_gapless: false,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let metadata_opts: MetadataOptions = Default::default();
|
||||
|
||||
let track: Option<usize> = None;
|
||||
|
||||
match symphonia::default::get_probe().format(&hint, mss, &format_opts, &metadata_opts) {
|
||||
Ok(probed) => {
|
||||
let decode_opts = DecoderOptions {
|
||||
verify: false,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let track = track
|
||||
.and_then(|t| probed.format.tracks().get(t))
|
||||
.or_else(|| first_supported_track(probed.format.tracks()));
|
||||
|
||||
let track_id = match track {
|
||||
Some(track) => track.id,
|
||||
_ => {
|
||||
return Err(DecoderError::SymphoniaDecoder(
|
||||
"No supported tracks found".to_string(),
|
||||
))
|
||||
}
|
||||
};
|
||||
|
||||
let seek_ts = 0;
|
||||
|
||||
let track_info = PlayTrackOptions { track_id, seek_ts };
|
||||
|
||||
let track = match probed
|
||||
.format
|
||||
.tracks()
|
||||
.iter()
|
||||
.find(|track| track.id == track_info.track_id)
|
||||
{
|
||||
Some(track) => track,
|
||||
_ => {
|
||||
return Err(DecoderError::SymphoniaDecoder(
|
||||
"No supported tracks found".to_string(),
|
||||
))
|
||||
}
|
||||
};
|
||||
|
||||
let decoder =
|
||||
symphonia::default::get_codecs().make(&track.codec_params, &decode_opts)?;
|
||||
return Ok(SymphoniaDecoder {
|
||||
format: probed.format,
|
||||
decoder,
|
||||
sample_buffer: None,
|
||||
});
|
||||
}
|
||||
Err(err) => {
|
||||
return Err(DecoderError::SymphoniaDecoder(format!(
|
||||
"format not supported. reason? {}",
|
||||
err
|
||||
)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn ts_to_ms(&self, ts: u64) -> u32 {
|
||||
let time_base = self.decoder.codec_params().time_base;
|
||||
let seeked_to_ms = match time_base {
|
||||
Some(time_base) => {
|
||||
let time = time_base.calc_time(ts);
|
||||
(time.seconds as f64 + time.frac) * 1000.
|
||||
}
|
||||
|
||||
None => ts as f64 * PAGES_PER_MS,
|
||||
};
|
||||
seeked_to_ms as u32
|
||||
}
|
||||
}
|
||||
|
||||
impl AudioDecoder for SymphoniaDecoder {
|
||||
fn seek(&mut self, position_ms: u32) -> Result<u32, DecoderError> {
|
||||
let seconds = position_ms as u64 / 1000;
|
||||
let frac = (position_ms as f64 % 1000.) / 1000.;
|
||||
let time = Time::new(seconds, frac);
|
||||
|
||||
let seeked_to_ts = self.format.seek(
|
||||
SeekMode::Accurate,
|
||||
SeekTo::Time {
|
||||
time,
|
||||
track_id: None,
|
||||
},
|
||||
)?;
|
||||
|
||||
self.decoder.reset();
|
||||
|
||||
Ok(self.ts_to_ms(seeked_to_ts.actual_ts))
|
||||
}
|
||||
|
||||
fn next_packet(
|
||||
&mut self,
|
||||
) -> DecoderResult<Option<(AudioPacketPosition, AudioPacket, u16, u32)>> {
|
||||
let mut skipped = false;
|
||||
|
||||
loop {
|
||||
let packet = match self.format.next_packet() {
|
||||
Ok(packet) => packet,
|
||||
Err(Error::IoError(err)) => {
|
||||
if err.kind() == io::ErrorKind::UnexpectedEof {
|
||||
return Ok(None);
|
||||
} else {
|
||||
return Err(DecoderError::SymphoniaDecoder(err.to_string()));
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
return Err(err.into());
|
||||
}
|
||||
};
|
||||
|
||||
let position_ms = self.ts_to_ms(packet.ts());
|
||||
let packet_position = AudioPacketPosition {
|
||||
position_ms,
|
||||
skipped,
|
||||
};
|
||||
|
||||
match self.decoder.decode(&packet) {
|
||||
Ok(decoded) => {
|
||||
let spec = *decoded.spec();
|
||||
let sample_buffer = match self.sample_buffer.as_mut() {
|
||||
Some(buffer) => buffer,
|
||||
None => {
|
||||
let duration = decoded.capacity() as u64;
|
||||
self.sample_buffer.insert(SampleBuffer::new(duration, spec))
|
||||
}
|
||||
};
|
||||
|
||||
sample_buffer.copy_interleaved_ref(decoded);
|
||||
let samples = AudioPacket::Samples(sample_buffer.samples().to_vec());
|
||||
|
||||
return Ok(Some((
|
||||
packet_position,
|
||||
samples,
|
||||
spec.channels.count() as u16,
|
||||
spec.rate,
|
||||
)));
|
||||
}
|
||||
Err(Error::DecodeError(_)) => {
|
||||
skipped = true;
|
||||
continue;
|
||||
}
|
||||
Err(err) => return Err(err.into()),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-231
@@ -4,234 +4,4 @@
|
||||
#[macro_use]
|
||||
extern crate napi_derive;
|
||||
|
||||
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 approximate 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,
|
||||
})
|
||||
}
|
||||
mod probe;
|
||||
@@ -0,0 +1,257 @@
|
||||
use napi::bindgen_prelude::*;
|
||||
use napi::Result;
|
||||
use symphonia::core::meta::MetadataRevision;
|
||||
use symphonia::core::{
|
||||
audio::Channels, codecs::*, io::MediaSourceStream, probe::Hint, 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 approximate duration of this media in seconds
|
||||
pub duration: u32,
|
||||
/// The metadata object
|
||||
pub metadata: Vec<MetadataField>,
|
||||
}
|
||||
|
||||
#[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(object)]
|
||||
pub struct MetadataField {
|
||||
pub name: String,
|
||||
pub value: String,
|
||||
}
|
||||
|
||||
#[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 mut probed = symphonia::default::get_probe()
|
||||
.format(&hint, mss, &fmt_opts, &meta_opts)
|
||||
.expect("unsupported format");
|
||||
|
||||
let mut format = probed.format;
|
||||
|
||||
let metadata: Vec<MetadataField>;
|
||||
|
||||
if let Some(metadata_rev) = format.metadata().current() {
|
||||
metadata = parse_metadata(&metadata_rev);
|
||||
} else if let Some(metadata_rev) = probed.metadata.get().as_ref().and_then(|m| m.current()) {
|
||||
metadata = parse_metadata(&metadata_rev);
|
||||
} else {
|
||||
metadata = Vec::new();
|
||||
}
|
||||
|
||||
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,
|
||||
metadata: metadata,
|
||||
})
|
||||
}
|
||||
|
||||
fn parse_metadata(rev: &MetadataRevision) -> Vec<MetadataField> {
|
||||
let tags = rev.tags();
|
||||
tags.iter().map(|tag| MetadataField {
|
||||
name: tag.key.to_string(),
|
||||
value: tag.value.to_string(),
|
||||
}).collect()
|
||||
}
|
||||
Reference in new issue
Block a user