UNPKG

arib-subtitle-timedmetadater

Version:
154 lines (153 loc) 8.63 kB
#!/usr/bin/env node "use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var stream_1 = require("stream"); var arib_mpeg2ts_parser_1 = require("arib-mpeg2ts-parser"); var arib_mpeg2ts_parser_2 = require("arib-mpeg2ts-parser"); var arib_mpeg2ts_parser_3 = require("arib-mpeg2ts-parser"); var id3_1 = __importDefault(require("./id3")); var MetadataTransform = /** @class */ (function (_super) { __extends(MetadataTransform, _super); function MetadataTransform() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.packetQueue = new arib_mpeg2ts_parser_1.TSPacketQueue(); _this.PAT_TSSectionQueue = new arib_mpeg2ts_parser_2.TSSectionQueue(); _this.PMT_TSSectionQueues = new Map(); _this.PMT_ContinuityCounters = new Map(); _this.Metadata_TSPESQueues = new Map(); _this.Metadata_ContinuityCounters = new Map(); return _this; } MetadataTransform.prototype._transform = function (chunk, encoding, callback) { this.packetQueue.push(chunk); while (!this.packetQueue.isEmpty()) { var packet = this.packetQueue.pop(); var pid = arib_mpeg2ts_parser_1.TSPacket.pid(packet); if (pid == 0x00) { this.PAT_TSSectionQueue.push(packet); while (!this.PAT_TSSectionQueue.isEmpty()) { var PAT = this.PAT_TSSectionQueue.pop(); if (arib_mpeg2ts_parser_2.TSSection.CRC32(PAT) != 0) { continue; } var begin = arib_mpeg2ts_parser_2.TSSection.EXTENDED_HEADER_SIZE; while (begin < arib_mpeg2ts_parser_2.TSSection.BASIC_HEADER_SIZE + arib_mpeg2ts_parser_2.TSSection.section_length(PAT) - arib_mpeg2ts_parser_2.TSSection.CRC_SIZE) { var program_number = (PAT[begin + 0] << 8) | PAT[begin + 1]; var program_map_PID = ((PAT[begin + 2] & 0x1F) << 8) | PAT[begin + 3]; if (program_map_PID === 0x10) { begin += 4; continue; } // NIT if (!this.PMT_TSSectionQueues.has(program_map_PID)) { this.PMT_TSSectionQueues.set(program_map_PID, new arib_mpeg2ts_parser_2.TSSectionQueue()); this.PMT_ContinuityCounters.set(program_map_PID, 0); } begin += 4; } } this.push(packet); } else if (this.PMT_TSSectionQueues.has(pid)) { var PMT_TSSectionQueue = this.PMT_TSSectionQueues.get(pid); PMT_TSSectionQueue.push(packet); while (!PMT_TSSectionQueue.isEmpty()) { var PMT = PMT_TSSectionQueue.pop(); if (arib_mpeg2ts_parser_2.TSSection.CRC32(PMT) != 0) { continue; } var program_info_length = ((PMT[arib_mpeg2ts_parser_2.TSSection.EXTENDED_HEADER_SIZE + 2] & 0x0F) << 8) | PMT[arib_mpeg2ts_parser_2.TSSection.EXTENDED_HEADER_SIZE + 3]; var begin = arib_mpeg2ts_parser_2.TSSection.EXTENDED_HEADER_SIZE + 4 + program_info_length; while (begin < arib_mpeg2ts_parser_2.TSSection.BASIC_HEADER_SIZE + arib_mpeg2ts_parser_2.TSSection.section_length(PMT) - arib_mpeg2ts_parser_2.TSSection.CRC_SIZE) { var stream_type = PMT[begin + 0]; var elementary_PID = ((PMT[begin + 1] & 0x1F) << 8) | PMT[begin + 2]; var ES_info_length = ((PMT[begin + 3] & 0x0F) << 8) | PMT[begin + 4]; if (stream_type !== 0x15) { begin += 5 + ES_info_length; continue; } var descriptor = begin + 5; while (descriptor < begin + 5 + ES_info_length) { var descriptor_tag = PMT[descriptor + 0]; var descriptor_length = PMT[descriptor + 1]; if (descriptor_tag === 0x26) { /* エンコード用なので結構適当な感じ*/ if (!this.Metadata_TSPESQueues.has(elementary_PID)) { this.Metadata_TSPESQueues.set(elementary_PID, new arib_mpeg2ts_parser_3.TSPESQueue()); this.Metadata_ContinuityCounters.set(elementary_PID, 0); } } descriptor += 2 + descriptor_length; } begin += 5 + ES_info_length; } } this.push(packet); } else if (this.Metadata_TSPESQueues.has(pid)) { var Metadata_TSPESQueue = this.Metadata_TSPESQueues.get(pid); Metadata_TSPESQueue.push(packet); while (!Metadata_TSPESQueue.isEmpty()) { var Metadata_PES = Metadata_TSPESQueue.pop(); var pts = 0; pts *= (1 << 3); pts += ((Metadata_PES[arib_mpeg2ts_parser_3.TSPES.PES_HEADER_SIZE + 3 + 0] & 0x0E) >> 1); pts *= (1 << 8); pts += ((Metadata_PES[arib_mpeg2ts_parser_3.TSPES.PES_HEADER_SIZE + 3 + 1] & 0xFF) >> 0); pts *= (1 << 7); pts += ((Metadata_PES[arib_mpeg2ts_parser_3.TSPES.PES_HEADER_SIZE + 3 + 2] & 0xFE) >> 1); pts *= (1 << 8); pts += ((Metadata_PES[arib_mpeg2ts_parser_3.TSPES.PES_HEADER_SIZE + 3 + 3] & 0xFF) >> 0); pts *= (1 << 7); pts += ((Metadata_PES[arib_mpeg2ts_parser_3.TSPES.PES_HEADER_SIZE + 3 + 4] & 0xFE) >> 1); var PES_header_data_length = Metadata_PES[arib_mpeg2ts_parser_3.TSPES.PES_HEADER_SIZE + 2]; var PES_payload_begin = (arib_mpeg2ts_parser_3.TSPES.PES_HEADER_SIZE + 3) + PES_header_data_length; var id3 = Metadata_PES.slice(PES_payload_begin); var new_Metadata_PES = id3_1.default.timedmetadata(pts, id3); var begin = 0; while (begin < new_Metadata_PES.length) { var header = Buffer.from([ packet[0], ((packet[1] & 0xA0) | ((begin === 0 ? 1 : 0) << 6) | ((pid & 0x1F00) >> 8)), (pid & 0x00FF), ((packet[3] & 0xC0) | (1 << 4) /* payload */ | (this.Metadata_ContinuityCounters.get(pid) & 0x0F)), ]); this.Metadata_ContinuityCounters.set(pid, (this.Metadata_ContinuityCounters.get(pid) + 1) & 0x0F); var next = begin + (arib_mpeg2ts_parser_1.TSPacket.PACKET_SIZE - arib_mpeg2ts_parser_1.TSPacket.HEADER_SIZE); this.push(Buffer.concat([ header, new_Metadata_PES.slice(begin, next) ])); begin = next; } } } else { this.push(packet); } } callback(); }; MetadataTransform.prototype._flush = function (callback) { callback(); }; return MetadataTransform; }(stream_1.Transform)); exports.default = MetadataTransform;