mirror of
https://github.com/arcodange-org/mediabunny.git
synced 2026-09-27 19:03:46 +02:00
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
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
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,
|
||||
]);
|
||||
});
|
||||
Reference in New Issue
Block a user