Files
mediabunny/test/node/read-mp4.test.ts
T
Jonny BurgerandGitHub f13a2e6732 Merge pull request #86 from JonnyBurger/url-source-allow-reading-small-files
Do not make UrlSource fail if the size of the file is smaller than 512kb (+ add a Vitest browser mode test)
2025-09-03 18:13:42 +02:00

36 lines
1.1 KiB
TypeScript

import { expect, test } from 'vitest';
import path from 'node:path';
import { ALL_FORMATS, MP4, EncodedPacketSink, Input, FilePathSource } from '../../src/index.js';
const __dirname = new URL('.', import.meta.url).pathname;
test('Should be able to get packets from a .MP4 file', async () => {
const filePath = path.join(__dirname, '..', 'public/video.mp4');
const input = new Input({
source: new FilePathSource(filePath),
formats: ALL_FORMATS,
});
expect(await input.getFormat()).toBe(MP4);
expect(await input.getMimeType()).toBe('video/mp4; codecs="avc1.640028, mp4a.40.2"');
expect(await input.computeDuration()).toBe(5.056);
const track = await input.getPrimaryVideoTrack();
if (!track) throw new Error('No video track found');
const sink = new EncodedPacketSink(track);
let samples = 0;
const timestamps: number[] = [];
for await (const packet of sink.packets()) {
timestamps.push(packet.timestamp);
samples++;
}
expect(samples).toBe(125);
expect(timestamps.slice(0, 10)).toEqual([
0, 0.16, 0.08, 0.04, 0.12, 0.32, 0.24, 0.2, 0.28, 0.48,
]);
});