Last active
December 14, 2018 10:27
-
-
Save peaBerberian/90ea073a51298b27eb31d13a05f7ea86 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
| /** | |
| * Translate groups of 4 big-endian bytes to Integer. | |
| * @param {Uint8Array} bytes | |
| * @param {Number} offset - The offset (from the start of the given array) | |
| * @returns {Number} | |
| */ | |
| function be4toi(bytes, offset) { | |
| return ( | |
| (bytes[offset + 0] * 0x1000000) + | |
| (bytes[offset + 1] * 0x0010000) + | |
| (bytes[offset + 2] * 0x0000100) + | |
| (bytes[offset + 3])); | |
| } | |
| /** | |
| * Find the right box in an isobmff. | |
| * @param {Uint8Array} buf - the isobmff | |
| * @param {Number} wantedName | |
| * @returns {Number} - Offset where the box begins. -1 if not found. | |
| */ | |
| function findBox(buf, wantedName) { | |
| const len = buf.length; | |
| let i = 0; | |
| while (i + 8 < len) { | |
| const size = be4toi(buf, i); | |
| if (size <= 0) { | |
| console.error("size out of range"); | |
| return - 1; | |
| } | |
| const name = be4toi(buf, i + 4); | |
| if (name === wantedName) { | |
| if (i + size <= len) { | |
| return i; | |
| } | |
| console.error("box out of range"); | |
| return -1; | |
| } | |
| i += size; | |
| } | |
| return -1; | |
| } | |
| /** | |
| * @param {Uint8Array} segment - the isobmff | |
| * @returns {Object} | |
| */ | |
| function findSidx(segment) { | |
| const offset = findBox(segment, 0x73696478 /* "sidx" */); | |
| if (offset === -1) { | |
| console.log("sidx not found"); | |
| return null; | |
| } | |
| // do things with the sidx: | |
| // offset -> (offset + 4) = length | |
| // (offset + 4) -> (offset + 8) = 0x73696478 ("sidx") | |
| // (offset + 8) -> (offset + length) = box content | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment