Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //***********************************
- // Better WebM title parsing
- //***********************************
- // differences with the 2ch version:
- // - finds title text in more webms
- // - can handle cp1251 encoded text
- // Fails when bytes are made from text.encode('utf-8').decode('cp1251').encode('utf-8')
- const decodeText = (function() {
- if (!window.TextDecoder) {
- // "fancy" utf-8 decoder, see:
- // https://stackoverflow.com/a/13691499
- return function decodeUTF8(bytes) {
- try {
- return decodeURIComponent(escape(String.fromCharCode.apply(0, bytes)));
- } catch(e) {
- console.warn('failed to decode webm title', bytes, e);
- return null;
- }
- }
- }
- const encodings = [ 'utf-8', 'cp1251' ];
- const options = { fatal: true };
- const decoders = encodings.map(e => new TextDecoder(e, options));
- return function decodeText(bytes) {
- for (const decoder of decoders) {
- try {
- return decoder.decode(bytes);
- } catch(e) {
- continue;
- }
- }
- console.warn('failed to decode webm title', bytes);
- return null;
- }
- })();
- // const MATROSKA_ID_SEGMENT = 0x18538067;
- const MATROSKA_ID_INFO = 0x1549A966;
- const MATROSKA_ID_TITLE = 0x7BA9;
- const MATROSKA_ID_MUXINGAPP = 0x4D80;
- const MATROSKA_ID_SEEK_POS = 0x53AC;
- MediaDataParser.prototype.getWebmTitle = function getWebmTitle() {
- const data = new DataView(this.data);
- const dataEnd = data.byteLength - 4;
- for (let i = 0; i < dataEnd; i++) {
- if (data.getUint32(i) === MATROSKA_ID_INFO) {
- if (data.getUint16(i + 4) === MATROSKA_ID_SEEK_POS) {
- continue;
- }
- for (i += 4; i < dataEnd; i++) {
- let tag = data.getUint16(i);
- if (tag === MATROSKA_ID_TITLE) {
- // |i:id_tag|i + 2:length|i + 3:bytes|
- const bytes = new Uint8Array(this.data, i + 3, data.getUint8(i + 2) & 0x7F);
- return decodeText(bytes) ?? ':: non-utf8 garbage ::';
- }
- if (tag === MATROSKA_ID_MUXINGAPP) {
- // title not found, search the next info segment
- break;
- }
- }
- }
- }
- return '';
- }
Add Comment
Please, Sign In to add comment