Merge pull request #20 from shumaiOne/feat/audio-metadata-fields

feat(metadata): add audio and video codec metadata fields
This commit is contained in:
Yiling-J
2026-05-26 16:16:34 +08:00
committed by GitHub
8 changed files with 265 additions and 18 deletions
+5
View File
@@ -110,6 +110,11 @@ declare global {
duration: number
bitRate: number
frameRate: number
videoCodec?: string
audioCodec?: string
audioChannels?: number
audioSampleRate?: number
audioBitDepth?: number
format: unknown
}
+50
View File
@@ -112,6 +112,56 @@ export const systemFields: Prisma.MetadataFieldCreateInput[] = [
},
},
},
{
key: 'audio_bit_depth',
scope: 'SYSTEM',
readOnly: true,
config: {
name: 'Audio Bit Depth',
type: 'number',
number: { scale: 0 },
},
},
{
key: 'audio_channels',
scope: 'SYSTEM',
readOnly: true,
config: {
name: 'Audio Channels',
type: 'number',
number: { scale: 0 },
},
},
{
key: 'audio_codec',
scope: 'SYSTEM',
readOnly: true,
config: {
name: 'Audio Codec',
type: 'text',
text: {},
},
},
{
key: 'audio_sample_rate',
scope: 'SYSTEM',
readOnly: true,
config: {
name: 'Audio Sample Rate',
type: 'number',
number: { scale: 0 },
},
},
{
key: 'video_codec',
scope: 'SYSTEM',
readOnly: true,
config: {
name: 'Video Codec',
type: 'text',
text: {},
},
},
{
key: 'format',
scope: 'SYSTEM',
@@ -50,6 +50,11 @@ describe('Transcode Activities', () => {
bitRate: 1000,
frameRate: 30,
hasAudio: true,
videoCodec: 'Advanced Video Coding',
audioCodec: 'MPEG-4 Audio',
audioChannels: 2,
audioSampleRate: 48000,
audioBitDepth: 16,
mimeType: 'video/mp4',
})
@@ -62,6 +67,11 @@ describe('Transcode Activities', () => {
expect(transcodeService.getVideoInfo).toHaveBeenCalledWith('/tmp/v.mp4')
expect(result.duration).toBe(10)
expect(result.metadata?.originalWidth).toBe(1920)
expect(result.metadata?.videoCodec).toBe('Advanced Video Coding')
expect(result.metadata?.audioCodec).toBe('MPEG-4 Audio')
expect(result.metadata?.audioChannels).toBe(2)
expect(result.metadata?.audioSampleRate).toBe(48000)
expect(result.metadata?.audioBitDepth).toBe(16)
})
it('should call getImageInfo for image assets', async () => {
+17 -2
View File
@@ -49,15 +49,30 @@ export async function getMediaInfoActivity(params: {
bitRate: info.bitRate,
frameRate: info.frameRate,
hasAudio: info.hasAudio,
videoCodec: info.videoCodec,
audioCodec: info.audioCodec,
audioChannels: info.audioChannels,
audioSampleRate: info.audioSampleRate,
audioBitDepth: info.audioBitDepth,
format: {},
}
await metadataService.updateAssetMetadata(params.assetId, [
const metadataUpdates: { key: string; value: string | number }[] = [
{ key: 'resolution_width', value: info.originalWidth },
{ key: 'resolution_height', value: info.originalHeight },
{ key: 'duration', value: info.duration },
{ key: 'bitRate', value: info.bitRate / 1000 },
{ key: 'frame_rate', value: info.frameRate },
])
]
if (info.videoCodec) metadataUpdates.push({ key: 'video_codec', value: info.videoCodec })
if (info.audioCodec) metadataUpdates.push({ key: 'audio_codec', value: info.audioCodec })
if (info.audioChannels !== undefined)
metadataUpdates.push({ key: 'audio_channels', value: info.audioChannels })
if (info.audioSampleRate !== undefined)
metadataUpdates.push({ key: 'audio_sample_rate', value: info.audioSampleRate })
if (info.audioBitDepth !== undefined)
metadataUpdates.push({ key: 'audio_bit_depth', value: info.audioBitDepth })
await metadataService.updateAssetMetadata(params.assetId, metadataUpdates)
} else if (isImage) {
const info = await transcodeService.getImageInfo(params.filePath)
mediaInfo.metadata = {
+16
View File
@@ -43,12 +43,23 @@ describe('TranscodeService', () => {
streams: [
{
codec_type: 'video',
codec_name: 'h264',
width: 1920,
height: 1080,
r_frame_rate: '30/1',
tags: {
mime_codec_string: 'avc1.4d401e',
},
},
{
codec_type: 'audio',
codec_name: 'aac',
channels: 2,
sample_rate: '48000',
bits_per_raw_sample: '16',
tags: {
mime_codec_string: 'mp4a.40.2',
},
},
],
})
@@ -66,6 +77,11 @@ describe('TranscodeService', () => {
expect(info.duration).toBe(10.5)
expect(info.frameRate).toBe(30)
expect(info.hasAudio).toBe(true)
expect(info.videoCodec).toBe('Advanced Video Coding')
expect(info.audioCodec).toBe('MPEG-4 Audio')
expect(info.audioChannels).toBe(2)
expect(info.audioSampleRate).toBe(48000)
expect(info.audioBitDepth).toBe(16)
expect(child_process.exec).toHaveBeenCalledWith(
expect.stringContaining('ffprobe'),
+151
View File
@@ -10,6 +10,122 @@ import '@/prisma-json-types'
const execAsync = promisify(exec)
const dataFormatNames: Record<string, string> = {
// additional codecs
apch: 'Apple ProRes 422 High Quality',
apcn: 'Apple ProRes 422 Standard Definition',
apcs: 'Apple ProRes 422 LT',
apco: 'Apple ProRes 422 Proxy',
ap4h: 'Apple ProRes 4444',
jpeg: 'JPEG Image',
// codecs from https://mp4ra.org/
'3gvo': '3GPP Video Orientation',
a3d1: 'Multiview Video Coding',
a3d2: 'Multiview Video Coding',
a3d3: 'Multiview Video Coding',
a3d4: 'Multiview Video Coding',
a3ds: 'Auro-Cx 3D audio',
'ac-3': 'AC-3 audio',
'ac-4': 'AC-4 audio',
alac: 'Apple lossless audio codec',
alaw: 'a-Law',
av01: 'AV1 video',
avc1: 'Advanced Video Coding',
avc2: 'Advanced Video Coding',
avc3: 'Advanced Video Coding',
avc4: 'Advanced Video Coding',
avcp: 'Advanced Video Coding Parameters',
dra1: 'DRA Audio',
drac: 'Dirac Video Coder',
'dts-': 'Dependent base layer for DTS layered audio',
'dts+': 'Enhancement layer for DTS layered audio',
dtsc: 'DTS Coherent Acoustics audio',
dtse: 'DTS Express low bit rate audio, also known as DTS LBR',
dtsh: 'DTS-HD High Resolution Audio',
dtsl: 'DTS-HD Master Audio',
dtsx: 'DTS:X',
dvav: 'AVC-based “Dolby Vision”',
dvhe: 'HEVC-based “Dolby Vision”',
'ec-3': 'Enhanced AC-3 audio',
enca: 'Encrypted/Protected audio',
encf: 'Encrypted/Protected font',
encm: 'Encrypted/Protected metadata stream',
encs: 'Encrypted Systems stream',
enct: 'Encrypted Text',
encv: 'Encrypted/protected video',
fdp: 'File delivery hints',
fLaC: 'Fres Lossless Audio Codec',
g719: 'ITU-T Recommendation G.719 (2008)',
g726: 'ITU-T Recommendation G.726 (1990)',
hev1: 'High Efficiency Video Coding',
hvc1: 'High Efficiency Video Coding',
hvt1: 'High Efficiency Video Coding',
ixse: 'DVB Track Level Index Track',
lhe1: 'Layered High Efficiency Video Coding',
lht1: 'Layered High Efficiency Video Coding',
lhv1: 'Layered High Efficiency Video Coding',
m2ts: 'MPEG-2 transport stream for DMB',
m4ae: 'MPEG-4 Audio Enhancement MP4v1/2',
mett: 'Text timed metadata that is not XML',
metx: 'XML timed metadata',
mha1: 'MPEG-H Audio (single stream, uncapsulated)',
mha2: 'MPEG-H Audio (multi-stream, unencapsulated)',
mhm1: 'MPEG-H Audio (single stream, MHAS encapsulated)',
mhm2: 'MPEG-H Audio (multi-stream, MHAS encapsulated)',
mjp2: 'Motion JPEG 2000',
mlix: 'DVB Movie level index track',
mlpa: 'MLP Audio',
mp4a: 'MPEG-4 Audio',
mp4s: 'MPEG-4 Systems',
mp4v: 'MPEG-4 Visual',
mvc1: 'Multiview coding',
mvc2: 'Multiview coding',
mvc3: 'Multiview coding',
mvc4: 'Multiview coding',
mvd1: 'Multiview coding',
mvd2: 'Multiview coding',
mvd3: 'Multiview coding',
mvd4: 'Multiview coding',
oksd: 'OMA Keys',
// eslint-disable-next-line @typescript-eslint/naming-convention
Opus: 'Opus audio coding',
pm2t: 'Protected MPEG-2 Transport',
prtp: 'Protected RTP Reception',
raw: 'Uncompressed audio',
resv: 'Restricted Video',
rm2t: 'MPEG-2 Transport Reception',
rrtp: 'RTP reception',
rsrp: 'SRTP Reception',
rtmd: 'Real Time Metadata Sample Entry(XAVC Format)',
rtp: 'RTP Hints',
s263: 'ITU H.263 video (3GPP format)',
samr: 'Narrowband AMR voice',
sawb: 'Wideband AMR voice',
sawp: 'Extended AMR-WB (AMR-WB+)',
sevc: 'EVRC Voice',
sm2t: 'MPEG-2 Transport Server',
sqcp: '13K Voice',
srtp: 'SRTP Hints',
ssmv: 'SMV Voice',
STGS: 'Subtitle Sample Entry (HMMP)',
stpp: 'Subtitles (Timed Text)',
svc1: 'Scalable Video Coding',
svc2: 'Scalable Video Coding',
svcM: 'SVC Metadata',
tc64: '64 bit timecode samples',
tmcd: '32 bit timecode samples',
twos: 'Uncompressed 16-bit audio',
tx3g: 'Timed Text stream',
ulaw: 'Samples have been compressed using uLaw 2:1.',
unid: 'Dynamic Range Control (DRC) data',
urim: 'Binary timed metadata identified by URI',
'vc-1': 'SMPTE VC-1',
vp08: 'VP8 video',
vp09: 'VP9 video',
wvtt: 'WebVTT',
}
export interface MediaMetadata {
originalWidth: number
originalHeight: number
@@ -17,6 +133,11 @@ export interface MediaMetadata {
bitRate: number
frameRate: number
hasAudio: boolean
videoCodec?: string
audioCodec?: string
audioChannels?: number
audioSampleRate?: number
audioBitDepth?: number
mimeType: string
}
@@ -40,6 +161,29 @@ export interface ExtractVideoFramesParams {
export class TranscodeService {
constructor(private readonly prismaClient: typeof prisma = prisma) {}
private resolveCodecName(stream: {
/* eslint-disable @typescript-eslint/naming-convention */
codec_name?: string
mime_codec_string?: string
tags?: { mime_codec_string?: string }
/* eslint-enable @typescript-eslint/naming-convention */
}): string | undefined {
const mimeCodec = stream.mime_codec_string || stream.tags?.mime_codec_string
if (mimeCodec && typeof mimeCodec === 'string') {
const shortCodec = mimeCodec.split('.')[0]
if (dataFormatNames[shortCodec]) {
return dataFormatNames[shortCodec]
}
}
return stream.codec_name
}
private safeParseInt(value: string | number | undefined): number | undefined {
if (value === undefined || value === null) return undefined
const parsed = typeof value === 'string' ? parseInt(value, 10) : value
return Number.isFinite(parsed) ? (parsed as number) : undefined
}
async getVideoInfo(inputFile: string): Promise<MediaMetadata> {
const { stdout } = await execAsync(
`ffprobe -v quiet -print_format json -show_format -show_streams "${inputFile}"`,
@@ -64,6 +208,13 @@ export class TranscodeService {
bitRate: parseFloat(info.format.bit_rate),
frameRate,
hasAudio: !!audioStream,
videoCodec: this.resolveCodecName(videoStream),
audioCodec: audioStream ? this.resolveCodecName(audioStream) : undefined,
audioChannels: audioStream?.channels,
audioSampleRate: this.safeParseInt(audioStream?.sample_rate),
audioBitDepth:
this.safeParseInt(audioStream?.bits_per_raw_sample) ??
this.safeParseInt(audioStream?.bits_per_sample),
mimeType: '',
}
}
@@ -298,7 +298,7 @@ export function FileViewerRightSidebar({
}
return (
<div className="bg-background h-full">
<div className="bg-background h-full flex flex-col overflow-hidden">
{/* Lightbox */}
{viewingAttachment && viewingAttachment.mediaType?.startsWith('image/') && (
<div
@@ -319,8 +319,8 @@ export function FileViewerRightSidebar({
</div>
</div>
)}
<Tabs defaultValue="comments" className="p-4 h-full flex flex-col px-2">
<TabsList className="w-full">
<Tabs defaultValue="comments" className="p-4 flex-1 flex flex-col px-2 min-h-0">
<TabsList className="w-full shrink-0">
<TabsTrigger value="comments" className="flex-1">
Comments
</TabsTrigger>
@@ -328,7 +328,7 @@ export function FileViewerRightSidebar({
Fields
</TabsTrigger>
</TabsList>
<TabsContent value="comments" className="flex-1 flex flex-col overflow-auto mt-4">
<TabsContent value="comments" className="flex-1 flex flex-col overflow-hidden min-h-0 mt-4">
<ScrollArea className="flex-1 px-4">
<div className="space-y-4">
{comments.map((comment) => (
@@ -360,7 +360,7 @@ export function FileViewerRightSidebar({
</div>
</ScrollArea>
{(!readOnly || isPublic) && (
<div className="mt-4">
<div className="mt-4 shrink-0">
<GuestIdentityPopup
isOpen={isGuestPopupOpen}
onClose={() => setIsGuestPopupOpen(false)}
@@ -380,10 +380,10 @@ export function FileViewerRightSidebar({
</div>
)}
</TabsContent>
<TabsContent value="fields" className="flex-1 overflow-auto">
<div className="mt-4">
<TabsContent value="fields" className="flex-1 flex flex-col overflow-hidden min-h-0 mt-4">
<ScrollArea className="flex-1 px-4">
{sortedFields && sortedFields.length > 0 && (
<div className="space-y-4">
<div className="space-y-4 pb-4">
{sortedFields.map((field) => (
<div key={field.id} className="space-y-1">
<label className="text-xs font-semibold text-muted-foreground uppercase tracking-wide">
@@ -401,7 +401,7 @@ export function FileViewerRightSidebar({
))}
</div>
)}
</div>
</ScrollArea>
</TabsContent>
</Tabs>
</div>
@@ -1,5 +1,3 @@
import { InferRequestType, InferResponseType } from 'hono/client'
import { useMutation } from '@tanstack/react-query'
import { client } from '@/ui/api/client'
import { FileViewer } from '@/ui/components/file-viewer'
import { FileViewerLeftSidebar } from '@/ui/components/file-viewer-left-sidebar'
@@ -7,15 +5,17 @@ import { FileViewerRightSidebar } from '@/ui/components/file-viewer-right-sideba
import { ResizeHandle } from '@/ui/components/resize-handle'
import { useMemberStore } from '@/ui/stores/members'
import { useTeamContextStore } from '@/ui/stores/team-context'
import { useUiStore } from '@/ui/stores/ui'
import { useTopNavStore } from '@/ui/stores/top-nav'
import { useUiStore } from '@/ui/stores/ui'
import { type Annotation } from '@/ui/types'
import { useMutation } from '@tanstack/react-query'
import { InferRequestType, InferResponseType } from 'hono/client'
import type { AssetInfo, CommentInfo } from '@/dtos/asset'
import { keepPreviousData, useQuery, useQueryClient } from '@tanstack/react-query'
import { createFileRoute, useNavigate } from '@tanstack/react-router'
import { Loader2 } from 'lucide-react'
import { useEffect, useState, useRef } from 'react'
import type { CommentInfo, AssetInfo } from '@/dtos/asset'
import { useEffect, useRef, useState } from 'react'
import type Player from 'video.js/dist/types/player'
function FileViewPage() {
@@ -210,8 +210,8 @@ function FileViewPage() {
}
return (
<div className="flex flex-1 flex-col bg-background">
<div className="flex flex-1 overflow-hidden">
<div className="h-full flex flex-1 flex-col bg-background">
<div className="h-full flex flex-1 overflow-hidden">
{!isLeftSidebarCollapsed && (
<>
<div style={{ width: leftSidebarWidth }} className="flex-shrink-0">