-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
47ba4f4
commit 9fa98b8
Showing
4 changed files
with
270 additions
and
7 deletions.
There are no files selected for viewing
144 changes: 144 additions & 0 deletions
144
packages/media-parser/src/boxes/riff/get-tracks-from-avi.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
import type {AudioTrack, OtherTrack, VideoTrack} from '../../get-tracks'; | ||
import type {RiffStructure} from '../../parse-result'; | ||
import type {StrfBoxAudio, StrfBoxVideo, StrhBox} from './riff-box'; | ||
import { | ||
getAvihBox, | ||
getStrfBox, | ||
getStrhBox, | ||
getStrlBoxes, | ||
isRiffAvi, | ||
} from './traversal'; | ||
|
||
export type AllTracks = { | ||
videoTracks: VideoTrack[]; | ||
audioTracks: AudioTrack[]; | ||
otherTracks: OtherTrack[]; | ||
}; | ||
|
||
export const getNumberOfTracks = (structure: RiffStructure): number => { | ||
const avihBox = getAvihBox(structure); | ||
if (avihBox) { | ||
return avihBox.streams; | ||
} | ||
|
||
throw new Error('No avih box found'); | ||
}; | ||
|
||
const makeAudioTrack = ({ | ||
strh, | ||
strf, | ||
index, | ||
}: { | ||
strh: StrhBox; | ||
strf: StrfBoxAudio; | ||
index: number; | ||
}): AudioTrack => { | ||
// 255 = AAC | ||
if (strf.formatTag !== 255) { | ||
throw new Error(`Unsupported audio format ${strf.formatTag}`); | ||
} | ||
|
||
return { | ||
type: 'audio', | ||
codec: 'mp4a.40.2', // According to Claude 3.5 Sonnet | ||
codecPrivate: null, | ||
codecWithoutConfig: 'aac', | ||
description: undefined, | ||
numberOfChannels: strf.numberOfChannels, | ||
sampleRate: strf.sampleRate, | ||
timescale: strh.rate, | ||
trackId: index, | ||
trakBox: null, | ||
}; | ||
}; | ||
|
||
const makeVideoTrack = ({ | ||
strh, | ||
strf, | ||
index, | ||
}: { | ||
strh: StrhBox; | ||
strf: StrfBoxVideo; | ||
index: number; | ||
}): VideoTrack => { | ||
if (strh.handler !== 'H264') { | ||
throw new Error(`Unsupported video codec ${strh.handler}`); | ||
} | ||
|
||
return { | ||
codecPrivate: null, | ||
// TODO: Which avc1? | ||
codec: 'avc1', | ||
codecWithoutConfig: 'h264', | ||
codedHeight: strf.height, | ||
codedWidth: strf.width, | ||
width: strf.width, | ||
height: strf.height, | ||
type: 'video', | ||
displayAspectHeight: strf.height, | ||
timescale: strh.rate, | ||
description: undefined, | ||
trackId: index, | ||
color: { | ||
fullRange: null, | ||
matrixCoefficients: null, | ||
primaries: null, | ||
transferCharacteristics: null, | ||
}, | ||
displayAspectWidth: strf.width, | ||
trakBox: null, | ||
rotation: 0, | ||
sampleAspectRatio: { | ||
numerator: 1, | ||
denominator: 1, | ||
}, | ||
fps: strh.rate / strh.scale, | ||
}; | ||
}; | ||
|
||
export const getTracksFromAvi = (structure: RiffStructure): AllTracks => { | ||
if (!isRiffAvi(structure)) { | ||
throw new Error('Not an AVI file'); | ||
} | ||
|
||
const videoTracks: VideoTrack[] = []; | ||
const audioTracks: AudioTrack[] = []; | ||
const otherTracks: OtherTrack[] = []; | ||
|
||
const boxes = getStrlBoxes(structure); | ||
|
||
let i = 0; | ||
for (const box of boxes) { | ||
i++; | ||
const strh = getStrhBox(box); | ||
const strf = getStrfBox(box); | ||
if (!strh || !strf) { | ||
continue; | ||
} | ||
|
||
if (strf.type === 'strf-box-video') { | ||
videoTracks.push(makeVideoTrack({strh, strf, index: i})); | ||
} else if (strh.fccType === 'auds') { | ||
audioTracks.push(makeAudioTrack({strh, strf, index: i})); | ||
} else { | ||
throw new Error(`Unsupported track type ${strh.fccType}`); | ||
} | ||
} | ||
|
||
return {audioTracks, otherTracks, videoTracks}; | ||
}; | ||
|
||
export const hasAllTracksFromAvi = (structure: RiffStructure): boolean => { | ||
if (!isRiffAvi(structure)) { | ||
throw new Error('Not an AVI file'); | ||
} | ||
|
||
const numberOfTracks = getNumberOfTracks(structure); | ||
const tracks = getTracksFromAvi(structure); | ||
return ( | ||
tracks.videoTracks.length + | ||
tracks.audioTracks.length + | ||
tracks.otherTracks.length === | ||
numberOfTracks | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import type {RiffStructure} from '../../parse-result'; | ||
import type { | ||
AvihBox, | ||
ListBox, | ||
StrfBoxAudio, | ||
StrfBoxVideo, | ||
StrhBox, | ||
} from './riff-box'; | ||
|
||
export const isRiffAvi = (structure: RiffStructure): boolean => { | ||
return structure.boxes.some( | ||
(box) => box.type === 'riff-header' && box.fileType === 'AVI', | ||
); | ||
}; | ||
|
||
export const getHdlrBox = (structure: RiffStructure): ListBox | null => { | ||
return structure.boxes.find( | ||
(box) => box.type === 'list-box' && box.listType === 'hdrl', | ||
) as ListBox | null; | ||
}; | ||
|
||
export const getAvihBox = (structure: RiffStructure): AvihBox | null => { | ||
const hdlrBox = getHdlrBox(structure); | ||
|
||
if (!hdlrBox) { | ||
return null; | ||
} | ||
|
||
return hdlrBox.children.find( | ||
(box) => box.type === 'avih-box', | ||
) as AvihBox | null; | ||
}; | ||
|
||
export const getStrlBoxes = (structure: RiffStructure): ListBox[] => { | ||
const hdlrBox = getHdlrBox(structure); | ||
|
||
if (!hdlrBox) { | ||
return []; | ||
} | ||
|
||
return hdlrBox.children.filter( | ||
(box) => box.type === 'list-box' && box.listType === 'strl', | ||
) as ListBox[]; | ||
}; | ||
|
||
export const getStrhBox = (strlBox: ListBox): StrhBox | null => { | ||
if (strlBox.listType !== 'strl') { | ||
return null; | ||
} | ||
|
||
return strlBox.children.find( | ||
(box) => box.type === 'strh-box', | ||
) as StrhBox | null; | ||
}; | ||
|
||
export const getStrfBox = ( | ||
strlBox: ListBox, | ||
): StrfBoxAudio | StrfBoxVideo | null => { | ||
if (strlBox.listType !== 'strl') { | ||
return null; | ||
} | ||
|
||
return ( | ||
(strlBox.children.find( | ||
(box) => box.type === 'strf-box-audio' || box.type === 'strf-box-video', | ||
) as StrfBoxAudio | StrfBoxVideo) ?? null | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters