Skip to content

🎥 Make videos programmatically with React

4.8 KB
Raw Blame
import type {
	InputAudioTrack,
	InputFormat,
	InputVideoTrack,
	MetadataTags,
} from 'mediabunny';
import React from 'react';
import {Table, TableBody, TableCell, TableRow} from '@/components/ui/table';
import type {Dimensions} from '~/lib/calculate-new-dimensions-from-dimensions';
import {formatBytes} from '~/lib/format-bytes';
import {formatSeconds} from '~/lib/format-seconds';
import type {VideoFrameRate} from '~/lib/get-video-frame-rate';
import {
	renderHumanReadableAudioCodec,
	renderHumanReadableVideoCodec,
} from '~/lib/render-codec-label';
import {AudioVolumeRow} from './AudioVolumeRow';
import {MetadataDisplay} from './MetadataTable';
import {Skeleton} from './ui/skeleton';

export const ContainerOverview: React.FC<{
	readonly dimensions: Dimensions | null | undefined;
	readonly durationInSeconds: number | null | undefined;
	readonly videoCodec: InputVideoTrack['codec'] | null;
	readonly audioCodec: InputAudioTrack['codec'] | null | undefined;
	readonly size: number | null;
	readonly frameRate: VideoFrameRate | null | undefined;
	readonly container: InputFormat | null;
	readonly isHdr: boolean | undefined;
	readonly metadata: MetadataTags | null;
	readonly isAudioOnly: boolean;
	readonly sampleRate: number | null;
	readonly audioTrack: InputAudioTrack | null;
}> = ({
	container,
	dimensions,
	videoCodec,
	durationInSeconds,
	audioCodec,
	size,
	frameRate,
	isHdr,
	metadata,
	isAudioOnly,
	sampleRate,
	audioTrack,
}) => {
	return (
		<Table className="table-fixed">
			<TableBody>
				<TableRow>
					<TableCell className="font-brand">Container</TableCell>
					<TableCell className="text-right">
						{container ? (
							container.name
						) : (
							<Skeleton className="h-3 w-[100px] inline-block" />
						)}
					</TableCell>
				</TableRow>
				<TableRow>
					<TableCell className="font-brand">Size</TableCell>
					<TableCell className="text-right">
						{size === null ? (
							<Skeleton className="h-3 w-[100px] inline-block" />
						) : (
							<>{formatBytes(size)}</>
						)}
					</TableCell>
				</TableRow>
				<TableRow>
					<TableCell className="font-brand">Duration</TableCell>
					<TableCell className="text-right">
						{durationInSeconds === undefined ? (
							<Skeleton className="h-3 w-[100px] inline-block" />
						) : durationInSeconds === null ? (
							<span>N/A</span>
						) : (
							<>{formatSeconds(durationInSeconds)}</>
						)}
					</TableCell>
				</TableRow>
				{!isAudioOnly && (
					<TableRow>
						<TableCell className="font-brand">Dimensions</TableCell>
						<TableCell className="text-right">
							{dimensions === undefined ? (
								<Skeleton className="h-3 w-[100px] inline-block" />
							) : dimensions === null ? (
								<>N/A</>
							) : (
								<>
									{dimensions.width}x{dimensions.height}
								</>
							)}
						</TableCell>
					</TableRow>
				)}
				{isAudioOnly ? null : (
					<TableRow>
						<TableCell className="font-brand">Frame Rate</TableCell>
						<TableCell className="text-right">
							{frameRate === undefined ? (
								<Skeleton className="h-3 w-[100px] inline-block" />
							) : frameRate?.type === 'variable' ? (
								<>Variable</>
							) : frameRate?.type === 'constant' ? (
								<>{frameRate.rate.toFixed(2)} FPS</>
							) : (
								'N/A'
							)}
						</TableCell>
					</TableRow>
				)}
				{isAudioOnly ? null : (
					<TableRow>
						<TableCell className="font-brand">Video Codec</TableCell>
						<TableCell className="text-right">
							{videoCodec === undefined ? (
								<Skeleton className="h-3 w-[100px] inline-block" />
							) : videoCodec === null ? (
								<>N/A</>
							) : (
								renderHumanReadableVideoCodec(videoCodec)
							)}
						</TableCell>
					</TableRow>
				)}
				<TableRow>
					<TableCell className="font-brand">Audio Codec</TableCell>
					<TableCell className="text-right">
						{audioCodec === undefined ? (
							<Skeleton className="h-3 w-[100px] inline-block" />
						) : audioCodec === null ? (
							'No audio'
						) : (
							renderHumanReadableAudioCodec(audioCodec)
						)}
					</TableCell>
				</TableRow>
				{sampleRate !== null ? (
					<TableRow>
						<TableCell className="font-brand">Sample Rate</TableCell>
						<TableCell className="text-right">{sampleRate} Hz</TableCell>
					</TableRow>
				) : null}
				{isAudioOnly ? null : (
					<TableRow>
						<TableCell className="font-brand">HDR</TableCell>
						<TableCell className="text-right">
							{isHdr === undefined ? (
								<Skeleton className="h-3 w-[100px] inline-block" />
							) : isHdr ? (
								'Yes'
							) : (
								'No'
							)}
						</TableCell>
					</TableRow>
				)}
				{audioTrack ? <AudioVolumeRow track={audioTrack} /> : null}
				{metadata ? <MetadataDisplay metadata={metadata} /> : null}
			</TableBody>
		</Table>
	);
};