Skip to content

🎥 Make videos programmatically with React

5.7 KB
Raw Blame
import {ALL_FORMATS, Input, UrlSource} from 'mediabunny';
import {assert, expect, test} from 'vitest';
import {audioIteratorManager} from '../audio-iterator-manager';
import {
	getMaxVideoCacheSize,
	globalMediaCache,
	keyframeManager,
} from '../caches';
import {makeNonceManager} from '../nonce-manager';
import {extractFrame} from '../video-extraction/extract-frame';
import {videoIteratorManager} from '../video-iterator-manager';

test('in preview, should properly buffer and draw frames', async (t) => {
	if (t.task.file.projectName === 'webkit') {
		t.skip();
		return;
	}

	const input = new Input({
		source: new UrlSource('/no-frames-in-beginning.webm'),
		formats: ALL_FORMATS,
	});

	const videoTrack = await input.getPrimaryVideoTrack();
	if (!videoTrack) {
		throw new Error('No video track found');
	}

	const manager = await videoIteratorManager({
		getIsDisposed: () => false,
		getIsLooping: () => false,
		getLoopSegmentMediaEndTimestamp: () => {
			throw new Error('not implemented');
		},
		getStartTime: () => {
			throw new Error('not implemented');
		},
		delayPlaybackHandleIfNotPremounting: () => ({
			unblock: () => {},
			[Symbol.dispose]: () => {},
		}),
		context: null,
		canvas: null,
		videoTrack,
		drawDebugOverlay: () => {},
		logLevel: 'info',
		getOnVideoFrameCallback: () => null,
		getEffects: () => [],
		getEffectChainState: () => null,
		maxCanvasSinkFrameSize: null,
	});

	const nonceManager = makeNonceManager();

	await manager.startVideoIterator(0, nonceManager.createAsyncOperation());
	await manager.seek({
		newTime: 0.03,
		continuousPlayback: null,
		nonce: nonceManager.createAsyncOperation(),
		fps: 30,
		playbackRate: 1,
		isPlaying: false,
	});
	await manager.seek({
		newTime: 1,
		continuousPlayback: null,
		nonce: nonceManager.createAsyncOperation(),
		fps: 30,
		playbackRate: 1,
		isPlaying: false,
	});
	await manager.seek({
		newTime: 2,
		continuousPlayback: null,
		nonce: nonceManager.createAsyncOperation(),
		fps: 30,
		playbackRate: 1,
		isPlaying: false,
	});

	const iteratorsCreated = manager.getVideoIteratorsCreated();
	expect(iteratorsCreated).toBe(1);

	await manager.seek({
		newTime: 4.5,
		continuousPlayback: null,
		nonce: nonceManager.createAsyncOperation(),
		fps: 30,
		playbackRate: 1,
		isPlaying: false,
	});

	const iteratorsCreated2 = manager.getVideoIteratorsCreated();
	expect(iteratorsCreated2).toBe(2);

	manager.destroy();
});

test('same goes for audio', async () => {
	const input = new Input({
		source: new UrlSource('/no-frames-in-beginning.webm'),
		formats: ALL_FORMATS,
	});

	const audioTrack = await input.getPrimaryAudioTrack();
	if (!audioTrack) {
		throw new Error('No video track found');
	}

	const manager = audioIteratorManager({
		audioTrack,
		delayPlaybackHandleIfNotPremounting: () => ({
			unblock: () => {},
			[Symbol.dispose]: () => {},
		}),
		sharedAudioContext: (() => {
			const audioContext = new AudioContext();
			return {
				audioContext,
				gainNode: audioContext.createGain(),
				audioSyncAnchor: {value: 0},
				scheduleAudioNode: () => ({
					type: 'started',
					scheduledTime: 0,
				}),
				unscheduleAudioNode: () => {},
			};
		})(),
		getMediaEndTimestamp: () => Infinity,
		getSequenceEndTimestamp: () => Infinity,
		getSequenceDurationInSeconds: () => 10,
		getStartTime: () => 0,
		initialMuted: false,
		initialVolume: 1,
		toneFrequency: 1,
		drawDebugOverlay: () => {},
		onError: (error) => {
			throw error;
		},
	});

	const nonceManager = makeNonceManager();

	manager.startAudioIterator({
		nonce: nonceManager.createAsyncOperation(),
		playbackRate: 1,
		startFromSecond: 0.06671494248275864,
		unloopedStartFromSecond: 0.06671494248275864,
		scheduleAudioNode: () => ({
			type: 'started',
			scheduledTime: 0,
		}),
		getTargetTime: (mediaTimestamp: number) => mediaTimestamp,
		logLevel: 'info',
		loop: false,
		unscheduleAudioNode: () => {},
		getAudioContextCurrentTimeMockedInTest: () => 0,
	});

	await manager.waitForNScheduledNodes(2);
	manager.seek({
		newTime: 0.10007241372413796,
		unloopedNewTime: 0.10007241372413796,
		nonce: nonceManager.createAsyncOperation(),
		playbackRate: 1,
		localPlaybackRate: 1,
		scheduleAudioNode: () => ({
			type: 'started',
			scheduledTime: 0,
		}),
		getTargetTime: (mediaTimestamp: number) => mediaTimestamp,
		logLevel: 'info',
		loop: false,
		trimBefore: undefined,
		trimAfter: undefined,
		sequenceOffset: 0,
		sequenceDurationInFrames: 10,
		fps: 30,
		getAudioContextCurrentTimeMockedInTest: () => 0,
	});

	const iterators = manager.getAudioIteratorsCreated();
	expect(iterators).toBe(1);
});

test('in rendering, should also be smart', async (t) => {
	if (t.task.file.projectName === 'webkit') {
		t.skip();
		return;
	}

	let lastFrame;
	for (let i = 0; i < 5; i++) {
		const frame = await extractFrame({
			src: '/no-frames-in-beginning.webm',
			timeInSeconds: i * 0.4,
			logLevel: 'info',
			loop: false,
			trimAfter: undefined,
			trimBefore: undefined,
			playbackRate: 1,
			fps: 30,
			maxCacheSize: getMaxVideoCacheSize('info'),
			credentials: undefined,
			mediaCache: globalMediaCache,
		});
		assert(frame.type === 'success');
		if (lastFrame) {
			// maybe this assertion is useless, since ht e
			expect(frame.frame?.timestamp === lastFrame.timestamp).toBe(true);
			continue;
		}

		expect(frame.frame?.timestamp).toBe(4.045 * 1_000_000);
		lastFrame = frame.frame;
	}

	const firstRealFrame = await extractFrame({
		src: '/no-frames-in-beginning.webm',
		timeInSeconds: 5,
		logLevel: 'info',
		loop: false,
		trimAfter: undefined,
		trimBefore: undefined,
		playbackRate: 1,
		fps: 30,
		maxCacheSize: getMaxVideoCacheSize('info'),
		credentials: undefined,
		mediaCache: globalMediaCache,
	});

	assert(firstRealFrame.type === 'success');
	expect(firstRealFrame.frame?.timestamp).toBe(4.979 * 1_000_000);

	keyframeManager.clearAll('info');
});