Files
mediabunny/test/read-mp4.test.ts
T
Jonny BurgerandGitHub 0e6cddc941 Add a test (#77)
* Match Prettier style

This way, people who have Prettier as their default formatter in their Editor (probably the majority of people) will not have the code re-formatted to double quotes if they are saving

* Add trailing-comma option

* Instruct .vscode to use ESLint as a default formatter

* Discard changes to package.json

* Add a first test

* Setup CI

* Rename to test

* move tsconfig hacks to local tsconfig.json

* Better test title

* rename file

* Fix ts setup
2025-08-27 09:12:49 +02:00

52 lines
1.3 KiB
TypeScript

import { expect, test } from 'vitest';
import { ALL_FORMATS, MP4, StreamSource, EncodedPacketSink, Input } from '../src/index.js';
import { open } from 'node:fs/promises';
const __dirname = new URL('.', import.meta.url).pathname;
const fileHandle = await open(
`${__dirname}/files/video.mp4`,
'r',
);
const source = new StreamSource({
read: async (start, end) => {
const buffer = Buffer.alloc(end - start);
await fileHandle.read(buffer, 0, end - start, start);
return buffer;
},
getSize: async () => {
const { size } = await fileHandle.stat();
return size;
},
});
test('Should be able to get packets from a .MP4 file', async () => {
const input = new Input({
formats: ALL_FORMATS,
source,
});
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,
]);
});