-
-
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.
Merge pull request #4271 from remotion-dev/media-parser-multiple-clus…
…ters
- Loading branch information
Showing
6 changed files
with
104 additions
and
76 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import {getVariableInt} from '../boxes/webm/ebml'; | ||
import {matroskaToHex} from '../boxes/webm/make-header'; | ||
import {matroskaElements} from '../boxes/webm/segments/all-segments'; | ||
import type {Writer} from '../writers/writer'; | ||
import { | ||
CLUSTER_MIN_VINT_WIDTH, | ||
createClusterSegment, | ||
makeSimpleBlock, | ||
} from './cluster-segment'; | ||
import {CREATE_TIME_SCALE} from './timescale'; | ||
|
||
const maxClusterTimestamp = 2 ** 15; | ||
|
||
const timestampToClusterTimestamp = (timestamp: number) => { | ||
return Math.round((timestamp / CREATE_TIME_SCALE) * 1000); | ||
}; | ||
|
||
export const makeCluster = async (w: Writer, timestamp: number) => { | ||
const cluster = createClusterSegment(timestampToClusterTimestamp(timestamp)); | ||
const clusterVIntPosition = | ||
w.getWrittenByteCount() + | ||
cluster.offsets.offset + | ||
matroskaToHex(matroskaElements.Cluster).byteLength; | ||
|
||
let clusterSize = cluster.bytes.byteLength; | ||
await w.write(cluster.bytes); | ||
|
||
const addSample = async (chunk: EncodedVideoChunk, trackNumber: number) => { | ||
const arr = new Uint8Array(chunk.byteLength); | ||
chunk.copyTo(arr); | ||
const timecodeRelativeToCluster = | ||
timestampToClusterTimestamp(chunk.timestamp) - | ||
timestampToClusterTimestamp(timestamp); | ||
if (timecodeRelativeToCluster > maxClusterTimestamp) { | ||
throw new Error('timecodeRelativeToCluster is too big'); | ||
} | ||
|
||
const keyframe = chunk.type === 'key'; | ||
const simpleBlock = makeSimpleBlock({ | ||
bytes: arr, | ||
invisible: false, | ||
keyframe, | ||
lacing: 0, | ||
trackNumber, | ||
timecodeRelativeToCluster, | ||
}); | ||
|
||
clusterSize += simpleBlock.byteLength; | ||
await w.updateDataAt( | ||
clusterVIntPosition, | ||
getVariableInt(clusterSize, CLUSTER_MIN_VINT_WIDTH), | ||
); | ||
await w.write(simpleBlock); | ||
}; | ||
|
||
const shouldMakeNewCluster = (chunk: EncodedVideoChunk) => { | ||
const newTimestamp = timestampToClusterTimestamp(chunk.timestamp); | ||
const oldTimestamp = timestampToClusterTimestamp(timestamp); | ||
|
||
return newTimestamp - oldTimestamp >= 2000 && chunk.type === 'key'; | ||
}; | ||
|
||
return {addSample, shouldMakeNewCluster}; | ||
}; |
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
15 changes: 15 additions & 0 deletions
15
packages/media-parser/src/create/make-duration-with-padding.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,15 @@ | ||
import {padMatroskaBytes} from '../boxes/webm/make-header'; | ||
|
||
export const makeDurationWithPadding = (newDuration: number) => { | ||
return padMatroskaBytes( | ||
{ | ||
type: 'Duration', | ||
value: { | ||
value: newDuration, | ||
size: '64', | ||
}, | ||
minVintWidth: null, | ||
}, | ||
100, | ||
); | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const CREATE_TIME_SCALE = 1_000_000; |