🎥 Make videos programmatically with React
import {assert, expect, test} from 'vitest';
import {
getMaxVideoCacheSize,
globalMediaCache,
keyframeManager,
} from '../caches';
import {extractFrame} from '../video-extraction/extract-frame';
test('Should render first frame for videos starting after timestamp 0', async () => {
keyframeManager.clearAll('info');
// This video's first video frame is at 0.15 seconds (150ms)
// Requesting frame at 0sec should clamp to first available frame (0.15s)
// Previously would throw: "Timestamp is before start timestamp (requested: 0sec, start: 0.15sec, difference: 0.150sec exceeds tolerance of 0.1sec)"
// Now matches Chrome's behavior: render the first frame rather than showing black
const result = await extractFrame({
src: '/video-start-offset.mp4',
timeInSeconds: 0,
logLevel: 'info',
loop: false,
trimAfter: undefined,
trimBefore: undefined,
playbackRate: 1,
fps: 30,
maxCacheSize: getMaxVideoCacheSize('info'),
credentials: undefined,
mediaCache: globalMediaCache,
});
// Should successfully extract (no error thrown)
expect(result.type).toBe('success');
assert(result.type === 'success');
assert(result.frame, 'Frame should be returned');
// Frame should have valid dimensions
// codedHeight may include codec padding depending on Chrome version (480 or 482)
expect(result.frame.codedWidth).toBe(640);
expect(result.frame.codedHeight).toBeGreaterThanOrEqual(480);
expect(result.frame.codedHeight).toBeLessThanOrEqual(482);
// Frame timestamp should be at video start (0.15s), NOT at requested time (0s)
// This proves we're returning the first actual frame
expect(result.frame.timestamp).toBeCloseTo(0.15 * 1_000_000, 2);
// Verify frame has pixel data (not completely black)
// testsrc generates a test pattern with colors and gradients
const bufferSize = result.frame.codedWidth * result.frame.codedHeight * 4;
const buffer = new Uint8Array(bufferSize);
await result.frame.copyTo(buffer);
const hasNonZeroPixels = buffer.some((byte) => byte > 0);
expect(hasNonZeroPixels).toBe(true);
keyframeManager.clearAll('info');
});