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);
});
+16
View File
@@ -19,6 +19,7 @@ test.concurrent('Big Buck Bunny', { timeout: 15_000 }, async () => {
expect(await input.getFormat()).toBeInstanceOf(HlsInputFormat);
expect(await input.getFormat()).toBe(HLS);
expect(await input.getDurationFromMetadata()).toBe(null); // Since it's a master playlist!
expect(sourceCount).toBe(1);
@@ -154,6 +155,9 @@ test.concurrent('Big Buck Bunny', { timeout: 15_000 }, async () => {
expect(await track.isLive()).toBe(false);
}
expect(await videoTracks[0]!.getDurationFromMetadata()).toBe(634.584);
expect(await audioTracks[0]!.getDurationFromMetadata()).toBe(634.584);
expect(videoTracks[0]!.codedWidth).toBe(1280);
expect(videoTracks[0]!.codedHeight).toBe(720);
expect(videoTracks[1]!.codedWidth).toBe(320);
@@ -797,4 +801,16 @@ test.concurrent('Live HLS', { timeout: 30_000 }, async () => {
const nextPacket = await sink.getNextPacket(currentLastPacket);
assert(nextPacket);
expect(nextPacket.sequenceNumber).toBeGreaterThan(currentLastPacket.sequenceNumber);
const metadataDurationFailed = Promise.race([
videoTrack.getDurationFromMetadata(),
rejectAfter(2000, 'Baby you make me smile'),
]);
await expect(metadataDurationFailed).rejects.toThrow('Baby you make me smile');
const metadataDuration = await Promise.race([
videoTrack.getDurationFromMetadata({ skipLiveWait: true }),
rejectAfter(2000, 'Baby you make me smile'),
]);
expect(metadataDuration).toBeGreaterThan((Date.now() / 1000) - 3600);
});