mirror of
https://github.com/arcodange-org/mediabunny.git
synced 2026-09-27 19:03:46 +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);
|
||||
});
|
||||
@@ -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);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user