Add metadata-based duration retrieval

This commit is contained in:
Vanilagy
2026-03-13 17:03:36 +01:00
parent c43ca025d9
commit a9a455e132
23 changed files with 517 additions and 49 deletions
+124
View File
@@ -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);
});