Skip to content

🎥 Make videos programmatically with React

fix timecode

Jonny Burgerb649887fab54
diff --git a/packages/media-parser/src/boxes/webm/segments/track-entry.ts b/packages/media-parser/src/boxes/webm/segments/track-entry.ts
index d2e0a6ccb7..b774a95641 100644
--- a/packages/media-parser/src/boxes/webm/segments/track-entry.ts
+++ b/packages/media-parser/src/boxes/webm/segments/track-entry.ts
@@ -600,7 +600,7 @@ export type SimpleBlockOrBlockSegment = {
type: 'simple-block-or-block-segment';
length: number;
trackNumber: number;
- timecode: number;
+ timecodeInMicroseconds: number;
keyframe: boolean | null;
lacing: number;
invisible: boolean;
@@ -640,14 +640,23 @@ export const parseSimpleBlockOrBlockSegment = async ({
iterator.counter.getOffset(),
);
+ const timescale = parserContext.parserState.getTimescale();
+
if (clusterOffset === undefined) {
throw new Error(
'Could not find offset for byte offset ' + iterator.counter.getOffset(),
);
}
- const timecode =
- (timecodeRelativeToCluster + clusterOffset) * (trackTimescale ?? 1);
+ // https://github.com/hubblec4/Matroska-Chapters-Specs/blob/master/notes.md/#timestampscale
+ // The TimestampScale Element is used to calculate the Raw Timestamp of a Block. The timestamp is obtained by adding the Block's timestamp to the Cluster's Timestamp Element, and then multiplying that result by the TimestampScale. The result will be the Block's Raw Timestamp in nanoseconds.
+ const timecodeInNanoSeconds =
+ (timecodeRelativeToCluster + clusterOffset) *
+ timescale *
+ (trackTimescale ?? 1);
+
+ // Timecode should be in microseconds
+ const timecodeInMicroseconds = timecodeInNanoSeconds / 1000;
if (!codec) {
throw new Error('Could not find codec for track ' + trackNumber);
@@ -664,7 +673,7 @@ export const parseSimpleBlockOrBlockSegment = async ({
dts: null,
duration: undefined,
trackId: trackNumber,
- timestamp: timecode,
+ timestamp: timecodeInMicroseconds,
};
if (keyframe === null) {
@@ -684,7 +693,7 @@ export const parseSimpleBlockOrBlockSegment = async ({
await parserContext.parserState.onAudioSample(trackNumber, {
data: iterator.getSlice(remainingNow),
trackId: trackNumber,
- timestamp: timecode,
+ timestamp: timecodeInMicroseconds,
type: 'key',
});
}
@@ -698,7 +707,7 @@ export const parseSimpleBlockOrBlockSegment = async ({
type: 'simple-block-or-block-segment',
length,
trackNumber,
- timecode,
+ timecodeInMicroseconds,
keyframe,
lacing,
invisible,
diff --git a/packages/media-parser/src/test/matroska.test.ts b/packages/media-parser/src/test/matroska.test.ts
index 4be42fe01c..6fa9638bbb 100644
--- a/packages/media-parser/src/test/matroska.test.ts
+++ b/packages/media-parser/src/test/matroska.test.ts
@@ -237,7 +237,7 @@ test('Should get duration of AV1 video', async () => {
type: 'simple-block-or-block-segment',
length: 279307,
trackNumber: 1,
- timecode: 0,
+ timecodeInMicroseconds: 0,
keyframe: true,
lacing: 0,
invisible: false,
@@ -247,7 +247,7 @@ test('Should get duration of AV1 video', async () => {
type: 'simple-block-or-block-segment',
length: 96,
trackNumber: 1,
- timecode: 40,
+ timecodeInMicroseconds: 40000,
keyframe: false,
lacing: 0,
invisible: false,
@@ -257,7 +257,7 @@ test('Should get duration of AV1 video', async () => {
type: 'simple-block-or-block-segment',
length: 556,
trackNumber: 1,
- timecode: 80,
+ timecodeInMicroseconds: 80000,
keyframe: false,
lacing: 0,
invisible: false,
@@ -267,7 +267,7 @@ test('Should get duration of AV1 video', async () => {
type: 'simple-block-or-block-segment',
length: 948,
trackNumber: 1,
- timecode: 120,
+ timecodeInMicroseconds: 120000,
keyframe: false,
lacing: 0,
invisible: false,
@@ -277,7 +277,7 @@ test('Should get duration of AV1 video', async () => {
type: 'simple-block-or-block-segment',
length: 577,
trackNumber: 1,
- timecode: 160,
+ timecodeInMicroseconds: 160000,
keyframe: false,
lacing: 0,
invisible: false,
@@ -287,7 +287,7 @@ test('Should get duration of AV1 video', async () => {
type: 'simple-block-or-block-segment',
length: 779,
trackNumber: 1,
- timecode: 200,
+ timecodeInMicroseconds: 200000,
keyframe: false,
lacing: 0,
invisible: false,
@@ -297,7 +297,7 @@ test('Should get duration of AV1 video', async () => {
type: 'simple-block-or-block-segment',
length: 793,
trackNumber: 1,
- timecode: 240,
+ timecodeInMicroseconds: 240000,
keyframe: false,
lacing: 0,
invisible: false,
@@ -307,7 +307,7 @@ test('Should get duration of AV1 video', async () => {
type: 'simple-block-or-block-segment',
length: 740,
trackNumber: 1,
- timecode: 280,
+ timecodeInMicroseconds: 280000,
keyframe: false,
lacing: 0,
invisible: false,
@@ -317,7 +317,7 @@ test('Should get duration of AV1 video', async () => {
type: 'simple-block-or-block-segment',
length: 1095,
trackNumber: 1,
- timecode: 320,
+ timecodeInMicroseconds: 320000,
keyframe: false,
lacing: 0,
invisible: false,
@@ -327,7 +327,7 @@ test('Should get duration of AV1 video', async () => {
type: 'simple-block-or-block-segment',
length: 1097,
trackNumber: 1,
- timecode: 360,
+ timecodeInMicroseconds: 360000,
keyframe: false,
lacing: 0,
invisible: false,
@@ -337,7 +337,7 @@ test('Should get duration of AV1 video', async () => {
type: 'simple-block-or-block-segment',
length: 1155,
trackNumber: 1,
- timecode: 400,
+ timecodeInMicroseconds: 400000,
keyframe: false,
lacing: 0,
invisible: false,
@@ -347,7 +347,7 @@ test('Should get duration of AV1 video', async () => {
type: 'simple-block-or-block-segment',
length: 1526,
trackNumber: 1,
- timecode: 440,
+ timecodeInMicroseconds: 440000,
keyframe: false,
lacing: 0,
invisible: false,
@@ -357,7 +357,7 @@ test('Should get duration of AV1 video', async () => {
type: 'simple-block-or-block-segment',
length: 1487,
trackNumber: 1,
- timecode: 480,
+ timecodeInMicroseconds: 480000,
keyframe: false,
lacing: 0,
invisible: false,
@@ -367,7 +367,7 @@ test('Should get duration of AV1 video', async () => {
type: 'simple-block-or-block-segment',
length: 2046,
trackNumber: 1,
- timecode: 520,
+ timecodeInMicroseconds: 520000,
keyframe: false,
lacing: 0,
invisible: false,
@@ -377,7 +377,7 @@ test('Should get duration of AV1 video', async () => {
type: 'simple-block-or-block-segment',
length: 1372,
trackNumber: 1,
- timecode: 560,
+ timecodeInMicroseconds: 560000,
keyframe: false,
lacing: 0,
invisible: false,
@@ -387,7 +387,7 @@ test('Should get duration of AV1 video', async () => {
type: 'simple-block-or-block-segment',
length: 1441,
trackNumber: 1,
- timecode: 600,
+ timecodeInMicroseconds: 600000,
keyframe: false,
lacing: 0,
invisible: false,
@@ -397,7 +397,7 @@ test('Should get duration of AV1 video', async () => {
type: 'simple-block-or-block-segment',
length: 2947,
trackNumber: 1,
- timecode: 640,
+ timecodeInMicroseconds: 640000,
keyframe: false,
lacing: 0,
invisible: false,
@@ -407,7 +407,7 @@ test('Should get duration of AV1 video', async () => {
type: 'simple-block-or-block-segment',
length: 2652,
trackNumber: 1,
- timecode: 680,
+ timecodeInMicroseconds: 680000,
keyframe: false,
lacing: 0,
invisible: false,
@@ -417,7 +417,7 @@ test('Should get duration of AV1 video', async () => {
type: 'simple-block-or-block-segment',
length: 4199,
trackNumber: 1,
- timecode: 720,
+ timecodeInMicroseconds: 720000,
keyframe: false,
lacing: 0,
invisible: false,
@@ -427,7 +427,7 @@ test('Should get duration of AV1 video', async () => {
type: 'simple-block-or-block-segment',
length: 3998,
trackNumber: 1,
- timecode: 760,
+ timecodeInMicroseconds: 760000,
keyframe: false,
lacing: 0,
invisible: false,
@@ -437,7 +437,7 @@ test('Should get duration of AV1 video', async () => {
type: 'simple-block-or-block-segment',
length: 6373,
trackNumber: 1,
- timecode: 800,
+ timecodeInMicroseconds: 800000,
keyframe: false,
lacing: 0,
invisible: false,
@@ -447,7 +447,7 @@ test('Should get duration of AV1 video', async () => {
type: 'simple-block-or-block-segment',
length: 5955,
trackNumber: 1,
- timecode: 840,
+ timecodeInMicroseconds: 840000,
keyframe: false,
lacing: 0,
invisible: false,
@@ -457,7 +457,7 @@ test('Should get duration of AV1 video', async () => {
type: 'simple-block-or-block-segment',
length: 7943,
trackNumber: 1,
- timecode: 880,
+ timecodeInMicroseconds: 880000,
keyframe: false,
lacing: 0,
invisible: false,
@@ -467,7 +467,7 @@ test('Should get duration of AV1 video', async () => {
type: 'simple-block-or-block-segment',
length: 8241,
trackNumber: 1,
- timecode: 920,
+ timecodeInMicroseconds: 920000,
keyframe: false,
lacing: 0,
invisible: false,
@@ -477,7 +477,7 @@ test('Should get duration of AV1 video', async () => {
type: 'simple-block-or-block-segment',
length: 9506,
trackNumber: 1,
- timecode: 960,
+ timecodeInMicroseconds: 960000,
keyframe: false,
lacing: 0,
invisible: false,