mirror of
https://github.com/arcodange-org/mediabunny.git
synced 2026-09-27 10:53:50 +02:00
Add metadata-based duration retrieval
This commit is contained in:
@@ -0,0 +1,124 @@
|
||||
import { expect, test } from 'vitest';
|
||||
import path from 'node:path';
|
||||
import { ALL_FORMATS, Input, FilePathSource } from '../../src/index.js';
|
||||
import { assert } from '../../src/misc.js';
|
||||
|
||||
const __dirname = new URL('.', import.meta.url).pathname;
|
||||
const publicPath = (file: string) => path.join(__dirname, '../public', file);
|
||||
|
||||
test('MP4 ISOBMFF duration metadata', async () => {
|
||||
using input = new Input({
|
||||
source: new FilePathSource(publicPath('video.mp4')),
|
||||
formats: ALL_FORMATS,
|
||||
});
|
||||
|
||||
expect(await input.getDurationFromMetadata()).toBe(5.056);
|
||||
expect(await input.computeDuration()).toBe(5.056);
|
||||
|
||||
const videoTrack = await input.getPrimaryVideoTrack();
|
||||
assert(videoTrack);
|
||||
expect(await videoTrack.getDurationFromMetadata()).toBe(5);
|
||||
|
||||
const audioTrack = await input.getPrimaryAudioTrack();
|
||||
assert(audioTrack);
|
||||
expect(await audioTrack.getDurationFromMetadata()).toBe(5.056);
|
||||
});
|
||||
|
||||
test('Matroska MKV duration metadata', async () => {
|
||||
using input = new Input({
|
||||
source: new FilePathSource(publicPath('ac3.mkv')),
|
||||
formats: ALL_FORMATS,
|
||||
});
|
||||
|
||||
expect(await input.getDurationFromMetadata()).toBe(2);
|
||||
expect(await input.computeDuration()).toBe(2);
|
||||
|
||||
const audioTrack = await input.getPrimaryAudioTrack();
|
||||
assert(audioTrack);
|
||||
expect(await audioTrack.getDurationFromMetadata()).toBe(2);
|
||||
});
|
||||
|
||||
test('MPEG-TS duration metadata', async () => {
|
||||
using input = new Input({
|
||||
source: new FilePathSource(publicPath('0.ts')),
|
||||
formats: ALL_FORMATS,
|
||||
});
|
||||
|
||||
expect(await input.getDurationFromMetadata()).toBe(null);
|
||||
expect(await input.computeDuration()).not.toBe(null);
|
||||
|
||||
const tracks = await input.getTracks();
|
||||
for (const track of tracks) {
|
||||
expect(await track.getDurationFromMetadata()).toBe(null);
|
||||
}
|
||||
});
|
||||
|
||||
test('FLAC duration metadata', async () => {
|
||||
using input = new Input({
|
||||
source: new FilePathSource(publicPath('sample.flac')),
|
||||
formats: ALL_FORMATS,
|
||||
});
|
||||
|
||||
expect(await input.getDurationFromMetadata()).toBe(19.714285714285715);
|
||||
expect(await input.computeDuration()).toBe(19.714285714285715);
|
||||
|
||||
const audioTrack = await input.getPrimaryAudioTrack();
|
||||
assert(audioTrack);
|
||||
expect(await audioTrack.getDurationFromMetadata()).toBe(19.714285714285715);
|
||||
});
|
||||
|
||||
test('WAV duration metadata', async () => {
|
||||
using input = new Input({
|
||||
source: new FilePathSource(publicPath('glitch-hop-is-dead.wav')),
|
||||
formats: ALL_FORMATS,
|
||||
});
|
||||
|
||||
expect(await input.getDurationFromMetadata()).toBe(9.63718820861678);
|
||||
expect(await input.computeDuration()).toBe(9.63718820861678);
|
||||
|
||||
const audioTrack = await input.getPrimaryAudioTrack();
|
||||
assert(audioTrack);
|
||||
expect(await audioTrack.getDurationFromMetadata()).toBe(9.63718820861678);
|
||||
});
|
||||
|
||||
test('OGG Vorbis duration metadata', async () => {
|
||||
using input = new Input({
|
||||
source: new FilePathSource(publicPath('vorbis-eos.ogg')),
|
||||
formats: ALL_FORMATS,
|
||||
});
|
||||
|
||||
expect(await input.getDurationFromMetadata()).toBe(null);
|
||||
|
||||
const tracks = await input.getTracks();
|
||||
for (const track of tracks) {
|
||||
expect(await track.getDurationFromMetadata()).toBe(null);
|
||||
}
|
||||
});
|
||||
|
||||
test('ADTS AAC duration metadata', async () => {
|
||||
using input = new Input({
|
||||
source: new FilePathSource(publicPath('sample3.aac')),
|
||||
formats: ALL_FORMATS,
|
||||
});
|
||||
|
||||
expect(await input.getDurationFromMetadata()).toBe(null);
|
||||
|
||||
const tracks = await input.getTracks();
|
||||
for (const track of tracks) {
|
||||
expect(await track.getDurationFromMetadata()).toBe(null);
|
||||
}
|
||||
});
|
||||
|
||||
test('MP3 duration metadata', async () => {
|
||||
using input = new Input({
|
||||
source: new FilePathSource(publicPath('Toothsome-Meme.VBRv2.mp3')),
|
||||
formats: ALL_FORMATS,
|
||||
});
|
||||
|
||||
expect(await input.getDurationFromMetadata()).toBe(38.568);
|
||||
expect(await input.computeDuration()).toBe(38.568000000000005);
|
||||
|
||||
const audioTrack = await input.getPrimaryAudioTrack();
|
||||
assert(audioTrack);
|
||||
expect(await audioTrack.getDurationFromMetadata()).toBe(38.568);
|
||||
});
|
||||
Reference in New Issue
Block a user