Created
September 28, 2023 14:06
-
-
Save pyldin601/6662bc11cbba8e059787f11e0f176e9d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import makeDebug from 'debug' | |
const debug = makeDebug('webm') | |
// Parse EBML variable-length integer | |
function parseVInt(buffer: Uint8Array, start: number): { value: number; length: number } { | |
let firstByte = buffer[start] | |
let numBytes = 0 | |
for (let mask = 0x80; mask >= 0x08; mask >>= 1) { | |
if ((firstByte & mask) !== 0) { | |
numBytes = 8 - Math.log2(mask) | |
break | |
} | |
} | |
if (numBytes === 0) { | |
throw new TypeError('Invalid VInt') | |
} | |
let value = firstByte & (0xff >>> numBytes) | |
for (let i = 1; i < numBytes; i++) { | |
value = (value << 8) | buffer[start + i] | |
} | |
return { value, length: numBytes } | |
} | |
// Remove WebM header from the beginning of the chunk | |
export function removeWebmHeader(webmData: Uint8Array): Uint8Array { | |
if ( | |
webmData[0] === 0x1a && | |
webmData[1] === 0x45 && | |
webmData[2] === 0xdf && | |
webmData[3] === 0xa3 | |
) { | |
let { value, length } = parseVInt(webmData, 4) | |
const totalHeaderSize = 4 + length + value | |
debug('Skipping header with size %d', value) | |
return webmData.slice(totalHeaderSize) | |
} | |
return webmData | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment