Skip to content

Commit ee23c05

Browse files
committed
strh header
1 parent 875e3f3 commit ee23c05

File tree

5 files changed

+114
-7
lines changed

5 files changed

+114
-7
lines changed

packages/media-parser/src/boxes/riff/parse-avih.ts

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export const parseAvih = ({
99
size: number;
1010
}): RiffBox => {
1111
const {expectNoMoreBytes} = iterator.startBox(size);
12+
1213
const dwMicroSecPerFrame = iterator.getUint32Le();
1314
const dwMaxBytesPerSec = iterator.getUint32Le();
1415
const paddingGranularity = iterator.getUint32Le();

packages/media-parser/src/boxes/riff/parse-riff-box.ts

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type {BufferIterator} from '../../buffer-iterator';
22
import {parseAvih} from './parse-avih';
33
import {parseFmtBox} from './parse-fmt-box';
44
import {parseListBox} from './parse-list-box';
5+
import {parseStrh} from './parse-strh';
56
import type {RiffBox, RiffRegularBox} from './riff-box';
67

78
export const parseRiffBox = ({
@@ -27,6 +28,10 @@ export const parseRiffBox = ({
2728
return parseAvih({iterator, size});
2829
}
2930

31+
if (id === 'strh') {
32+
return parseStrh({iterator, size});
33+
}
34+
3035
iterator.discard(size);
3136

3237
const box: RiffRegularBox = {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import type {BufferIterator} from '../../buffer-iterator';
2+
import type {RiffBox} from './riff-box';
3+
4+
export const parseStrh = ({
5+
iterator,
6+
size,
7+
}: {
8+
iterator: BufferIterator;
9+
size: number;
10+
}): RiffBox => {
11+
const box = iterator.startBox(size);
12+
const fccType = iterator.getByteString(4);
13+
if (fccType !== 'vids' && fccType !== 'auds') {
14+
throw new Error('Expected AVI handler to be vids / auds');
15+
}
16+
17+
const handler =
18+
fccType === 'vids' ? iterator.getByteString(4) : iterator.getUint32Le();
19+
if (typeof handler === 'string' && handler !== 'H264') {
20+
throw new Error(
21+
`Only H264 is supported as a stream type in .avi, got ${handler}`,
22+
);
23+
}
24+
25+
if (fccType === 'auds' && handler !== 1) {
26+
throw new Error(
27+
`Only "1" is supported as a stream type in .avi, got ${handler}`,
28+
);
29+
}
30+
31+
const flags = iterator.getUint32Le();
32+
const priority = iterator.getUint16Le();
33+
const language = iterator.getUint16Le();
34+
const initialFrames = iterator.getUint32Le();
35+
const scale = iterator.getUint32Le();
36+
const rate = iterator.getUint32Le();
37+
const start = iterator.getUint32Le();
38+
const length = iterator.getUint32Le();
39+
const suggestedBufferSize = iterator.getUint32Le();
40+
const quality = iterator.getUint32Le();
41+
const sampleSize = iterator.getUint32Le();
42+
43+
box.discardRest();
44+
45+
return {
46+
type: 'strh-box',
47+
fccType,
48+
handler,
49+
flags,
50+
priority,
51+
initialFrames,
52+
length,
53+
quality,
54+
rate,
55+
sampleSize,
56+
scale,
57+
start,
58+
suggestedBufferSize,
59+
language,
60+
};
61+
};

packages/media-parser/src/boxes/riff/riff-box.ts

+19-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,23 @@ export type AvihBox = {
3434
height: number;
3535
};
3636

37+
export type StrhBox = {
38+
type: 'strh-box';
39+
fccType: 'vids' | 'auds';
40+
handler: 'H264' | number;
41+
flags: number;
42+
priority: number;
43+
initialFrames: number;
44+
scale: number;
45+
rate: number;
46+
start: number;
47+
length: number;
48+
suggestedBufferSize: number;
49+
quality: number;
50+
sampleSize: number;
51+
language: number;
52+
};
53+
3754
export type RiffHeader = {
3855
type: 'riff-header';
3956
fileSize: number;
@@ -45,4 +62,5 @@ export type RiffBox =
4562
| WaveFormatBox
4663
| RiffHeader
4764
| ListBox
48-
| AvihBox;
65+
| AvihBox
66+
| StrhBox;

packages/media-parser/src/test/parse-avi-file.test.ts

+28-6
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,20 @@ test('AVI file', async () => {
3535
{
3636
children: [
3737
{
38-
id: 'strh',
39-
size: 56,
40-
type: 'riff-box',
38+
type: 'strh-box',
39+
fccType: 'vids',
40+
handler: 'H264',
41+
flags: 0,
42+
initialFrames: 0,
43+
scale: 1,
44+
length: 901,
45+
priority: 0,
46+
quality: 4294967295,
47+
rate: 30,
48+
sampleSize: 0,
49+
start: 0,
50+
suggestedBufferSize: 4796,
51+
language: 0,
4152
},
4253
{
4354
id: 'strf',
@@ -61,9 +72,20 @@ test('AVI file', async () => {
6172
{
6273
children: [
6374
{
64-
id: 'strh',
65-
size: 56,
66-
type: 'riff-box',
75+
type: 'strh-box',
76+
fccType: 'auds',
77+
handler: 1,
78+
flags: 0,
79+
priority: 0,
80+
initialFrames: 0,
81+
scale: 8,
82+
rate: 375,
83+
length: 1435,
84+
quality: 4294967295,
85+
sampleSize: 0,
86+
start: 0,
87+
suggestedBufferSize: 373,
88+
language: 0,
6789
},
6890
{
6991
id: 'strf',

0 commit comments

Comments
 (0)