🏠 Home 

Greasy Fork is available in English.

bilibili merged flv+mp4+ass+enhance

bilibili/哔哩哔哩:超清FLV下载,FLV合并,原生MP4下载,弹幕ASS下载,MKV打包,播放体验增强,原生appsecret,不借助其他网站


安装此脚本?
  1. // ==UserScript==
  2. // @name bilibili merged flv+mp4+ass+enhance
  3. // @namespace http://qli5.tk/
  4. // @homepageURL https://github.com/liqi0816/bilitwin/
  5. // @description bilibili/哔哩哔哩:超清FLV下载,FLV合并,原生MP4下载,弹幕ASS下载,MKV打包,播放体验增强,原生appsecret,不借助其他网站
  6. // @match *://www.bilibili.com/video/av*
  7. // @match *://bangumi.bilibili.com/anime/*/play*
  8. // @match *://www.bilibili.com/bangumi/play/ep*
  9. // @match *://www.bilibili.com/bangumi/play/ss*
  10. // @match *://www.bilibili.com/watchlater/
  11. // @version 1.14
  12. // @author qli5
  13. // @copyright qli5, 2014+, 田生, grepmusic, zheng qian, ryiwamoto
  14. // @license Mozilla Public License 2.0; http://www.mozilla.org/MPL/2.0/
  15. // @grant none
  16. // @run-at document-start
  17. // ==/UserScript==
  18. /***
  19. *
  20. * @author qli5 <goodlq11[at](163|gmail).com>
  21. *
  22. * BiliTwin consists of two parts - BiliMonkey and BiliPolyfill.
  23. * They are bundled because I am too lazy to write two user interfaces.
  24. *
  25. * So what is the difference between BiliMonkey and BiliPolyfill?
  26. *
  27. * BiliMonkey deals with network. It is a (naIve) Service Worker.
  28. * This is also why it uses IndexedDB instead of localStorage.
  29. * BiliPolyfill deals with experience. It is more a "user script".
  30. * Everything it can do can be done by hand.
  31. *
  32. * BiliPolyfill will be pointless in the long run - I believe bilibili
  33. * will finally provide these functions themselves.
  34. *
  35. * This Source Code Form is subject to the terms of the Mozilla Public
  36. * License, v. 2.0. If a copy of the MPL was not distributed with this
  37. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  38. *
  39. * Covered Software is provided under this License on an “as is” basis,
  40. * without warranty of any kind, either expressed, implied, or statutory,
  41. * including, without limitation, warranties that the Covered Software
  42. * is free of defects, merchantable, fit for a particular purpose or
  43. * non-infringing. The entire risk as to the quality and performance of
  44. * the Covered Software is with You. Should any Covered Software prove
  45. * defective in any respect, You (not any Contributor) assume the cost
  46. * of any necessary servicing, repair, or correction. This disclaimer
  47. * of warranty constitutes an essential part of this License. No use of
  48. * any Covered Software is authorized under this License except under
  49. * this disclaimer.
  50. *
  51. * Under no circumstances and under no legal theory, whether tort
  52. * (including negligence), contract, or otherwise, shall any Contributor,
  53. * or anyone who distributes Covered Software as permitted above, be
  54. * liable to You for any direct, indirect, special, incidental, or
  55. * consequential damages of any character including, without limitation,
  56. * damages for lost profits, loss of goodwill, work stoppage, computer
  57. * failure or malfunction, or any and all other commercial damages or
  58. * losses, even if such party shall have been informed of the possibility
  59. * of such damages. This limitation of liability shall not apply to
  60. * liability for death or personal injury r###lting from such party’s
  61. * negligence to the extent applicable law prohibits such limitation.
  62. * Some jurisdictions do not allow the exclusion or limitation of
  63. * incidental or consequential damages, so this exclusion and limitation
  64. * may not apply to You.
  65. */
  66. /***
  67. * This is a bundled code. While it is not uglified, it may still be too
  68. * complex for reviewing. Please refer to
  69. * https://github.com/liqi0816/bilitwin/
  70. * for source code.
  71. */
  72. /***
  73. * Copyright (C) 2018 Qli5. All Rights Reserved.
  74. *
  75. * @author qli5 <goodlq11[at](163|gmail).com>
  76. *
  77. * This Source Code Form is subject to the terms of the Mozilla Public
  78. * License, v. 2.0. If a copy of the MPL was not distributed with this
  79. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  80. */
  81. /**
  82. * Basically a Promise that exposes its resolve and reject callbacks
  83. */
  84. class AsyncContainer {
  85. /***
  86. * The thing is, if we cannot cancel a promise, we should at least be able to
  87. * explicitly mark a promise as garbage collectible.
  88. *
  89. * Yes, this is something like cancelable Promise. But I insist they are different.
  90. */
  91. constructor(callback) {
  92. // 1. primary promise
  93. this.primaryPromise = new Promise((s, j) => {
  94. this.resolve = arg => { s(arg); return arg; };
  95. this.reject = arg => { j(arg); return arg; };
  96. });
  97. // 2. hang promise
  98. this.hangReturn = Symbol();
  99. this.hangPromise = new Promise(s => this.hang = () => s(this.hangReturn));
  100. this.destroiedThen = this.hangPromise.then.bind(this.hangPromise);
  101. this.primaryPromise.then(() => this.state = 'fulfilled');
  102. this.primaryPromise.catch(() => this.state = 'rejected');
  103. this.hangPromise.then(() => this.state = 'hanged');
  104. // 4. race
  105. this.promise = Promise
  106. .race([this.primaryPromise, this.hangPromise])
  107. .then(s => s == this.hangReturn ? new Promise(() => { }) : s);
  108. // 5. inherit
  109. this.then = this.promise.then.bind(this.promise);
  110. this.catch = this.promise.catch.bind(this.promise);
  111. this.finally = this.promise.finally.bind(this.promise);
  112. // 6. optional callback
  113. if (typeof callback == 'function') callback(this.resolve, this.reject);
  114. }
  115. /***
  116. * Memory leak notice:
  117. *
  118. * The V8 implementation of Promise requires
  119. * 1. the resolve handler of a Promise
  120. * 2. the reject handler of a Promise
  121. * 3. !! the Promise object itself !!
  122. * to be garbage collectible to correctly free Promise runtime contextes
  123. *
  124. * This piece of code will work
  125. * void (async () => {
  126. * const buf = new Uint8Array(#### * #### * ####);
  127. * for (let i = 0; i < buf.length; i++) buf[i] = i;
  128. * await new Promise(() => { });
  129. * return buf;
  130. * })();
  131. * if (typeof gc == 'function') gc();
  132. *
  133. * This piece of code will cause a Promise context mem leak
  134. * const deadPromise = new Promise(() => { });
  135. * void (async () => {
  136. * const buf = new Uint8Array(#### * #### * ####);
  137. * for (let i = 0; i < buf.length; i++) buf[i] = i;
  138. * await deadPromise;
  139. * return buf;
  140. * })();
  141. * if (typeof gc == 'function') gc();
  142. *
  143. * In other words, do NOT directly inherit from promise. You will need to
  144. * dereference it on destroying.
  145. */
  146. destroy() {
  147. this.hang();
  148. this.resolve = () => { };
  149. this.reject = this.resolve;
  150. this.hang = this.resolve;
  151. this.primaryPromise = null;
  152. this.hangPromise = null;
  153. this.promise = null;
  154. this.then = this.resolve;
  155. this.catch = this.resolve;
  156. this.finally = this.resolve;
  157. this.destroiedThen = f => f();
  158. /***
  159. * For ease of debug, do not dereference hangReturn
  160. *
  161. * If run from console, mysteriously this tiny symbol will help correct gc
  162. * before a console.clear
  163. */
  164. //this.hangReturn = null;
  165. }
  166. static _UNIT_TEST() {
  167. const containers = [];
  168. async function foo() {
  169. const buf = new Uint8Array(600 * #### * ####);
  170. for (let i = 0; i < buf.length; i++) buf[i] = i;
  171. const ac = new AsyncContainer();
  172. ac.destroiedThen(() => console.log('asyncContainer destroied'));
  173. containers.push(ac);
  174. await ac;
  175. return buf;
  176. }
  177. const foos = [foo(), foo(), foo()];
  178. containers.forEach(e => e.destroy());
  179. console.warn('Check your RAM usage. I allocated 1.8GB in three dead-end promises.');
  180. return [foos, containers];
  181. }
  182. }
  183. /***
  184. * Copyright (C) 2018 Qli5. All Rights Reserved.
  185. *
  186. * @author qli5 <goodlq11[at](163|gmail).com>
  187. *
  188. * This Source Code Form is subject to the terms of the Mozilla Public
  189. * License, v. 2.0. If a copy of the MPL was not distributed with this
  190. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  191. */
  192. /**
  193. * Provides common util for all bilibili user scripts
  194. */
  195. class BiliUserJS {
  196. static async getIframeWin() {
  197. if (document.querySelector('#bofqi > iframe').contentDocument.getElementById('bilibiliPlayer')) {
  198. return document.querySelector('#bofqi > iframe').contentWindow;
  199. }
  200. else {
  201. return new Promise(resolve => {
  202. document.querySelector('#bofqi > iframe').addEventListener('load', () => {
  203. resolve(document.querySelector('#bofqi > iframe').contentWindow);
  204. }, { once: true });
  205. });
  206. }
  207. }
  208. static async getPlayerWin() {
  209. if (location.href.includes('/watchlater/#/list')) {
  210. await new Promise(resolve => {
  211. window.addEventListener('hashchange', () => resolve(location.href), { once: true });
  212. });
  213. }
  214. if (location.href.includes('/watchlater/#/')) {
  215. if (!document.getElementById('bofqi')) {
  216. await new Promise(resolve => {
  217. const observer = new MutationObserver(() => {
  218. if (document.getElementById('bofqi')) {
  219. resolve(document.getElementById('bofqi'));
  220. observer.disconnect();
  221. }
  222. });
  223. observer.observe(document, { childList: true, subtree: true });
  224. });
  225. }
  226. }
  227. if (document.getElementById('bilibiliPlayer')) {
  228. return window;
  229. }
  230. else if (document.querySelector('#bofqi > iframe')) {
  231. return BiliUserJS.getIframeWin();
  232. }
  233. else if (document.querySelector('#bofqi > object')) {
  234. throw 'Need H5 Player';
  235. }
  236. else {
  237. return new Promise(resolve => {
  238. const observer = new MutationObserver(() => {
  239. if (document.getElementById('bilibiliPlayer')) {
  240. observer.disconnect();
  241. resolve(window);
  242. }
  243. else if (document.querySelector('#bofqi > iframe')) {
  244. observer.disconnect();
  245. resolve(BiliUserJS.getIframeWin());
  246. }
  247. else if (document.querySelector('#bofqi > object')) {
  248. observer.disconnect();
  249. throw 'Need H5 Player';
  250. }
  251. });
  252. observer.observe(document.getElementById('bofqi'), { childList: true });
  253. });
  254. }
  255. }
  256. static tryGetPlayerWinSync() {
  257. if (document.getElementById('bilibiliPlayer')) {
  258. return window;
  259. }
  260. else if (document.querySelector('#bofqi > object')) {
  261. throw 'Need H5 Player';
  262. }
  263. }
  264. static getCidRefreshPromise(playerWin) {
  265. /***********
  266. * !!!Race condition!!!
  267. * We must finish everything within one microtask queue!
  268. *
  269. * bilibili script:
  270. * videoElement.remove() -> setTimeout(0) -> [[microtask]] -> load playurl
  271. * \- synchronous macrotask -/ || \- synchronous
  272. * ||
  273. * the only position to inject monkey.sniffDefaultFormat
  274. */
  275. const cidRefresh = new AsyncContainer();
  276. // 1. no active video element in document => cid refresh
  277. const observer = new MutationObserver(() => {
  278. if (!playerWin.document.getElementsByTagName('video')[0]) {
  279. observer.disconnect();
  280. cidRefresh.resolve();
  281. }
  282. });
  283. observer.observe(playerWin.document.getElementsByClassName('bilibili-player-video')[0], { childList: true });
  284. // 2. playerWin unload => cid refresh
  285. playerWin.addEventListener('unload', () => Promise.resolve().then(() => cidRefresh.resolve()));
  286. return cidRefresh;
  287. }
  288. static async domContentLoadedThen(func) {
  289. if (document.readyState == 'loading') {
  290. return new Promise(resolve => {
  291. document.addEventListener('DOMContentLoaded', () => resolve(func()), { once: true });
  292. })
  293. }
  294. else {
  295. return func();
  296. }
  297. }
  298. }
  299. /***
  300. * Copyright (C) 2018 Qli5. All Rights Reserved.
  301. *
  302. * @author qli5 <goodlq11[at](163|gmail).com>
  303. *
  304. * This Source Code Form is subject to the terms of the Mozilla Public
  305. * License, v. 2.0. If a copy of the MPL was not distributed with this
  306. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  307. */
  308. /**
  309. * A promisified indexedDB with large file(>100MB) support
  310. */
  311. class CacheDB {
  312. constructor(dbName = 'biliMonkey', osName = 'flv', keyPath = 'name', maxItemSize = 100 * #### * ####) {
  313. // Neither Chrome or Firefox can handle item size > 100M
  314. this.dbName = dbName;
  315. this.osName = osName;
  316. this.keyPath = keyPath;
  317. this.maxItemSize = maxItemSize;
  318. this.db = null;
  319. }
  320. async getDB() {
  321. if (this.db) return this.db;
  322. this.db = new Promise((resolve, reject) => {
  323. const openRequest = indexedDB.open(this.dbName);
  324. openRequest.onupgradeneeded = e => {
  325. const db = e.target.r###lt;
  326. if (!db.objectStoreNames.contains(this.osName)) {
  327. db.createObjectStore(this.osName, { keyPath: this.keyPath });
  328. }
  329. };
  330. openRequest.onsuccess = e => {
  331. return resolve(this.db = e.target.r###lt);
  332. };
  333. openRequest.onerror = reject;
  334. });
  335. return this.db;
  336. }
  337. async addData(item, name = item.name, data = item.data || item) {
  338. if (!data instanceof Blob) throw 'CacheDB: data must be a Blob';
  339. const itemChunks = [];
  340. const numChunks = Math.ceil(data.size / this.maxItemSize);
  341. for (let i = 0; i < numChunks; i++) {
  342. itemChunks.push({
  343. name: `${name}/part_${i}`,
  344. numChunks,
  345. data: data.slice(i * this.maxItemSize, (i + 1) * this.maxItemSize)
  346. });
  347. }
  348. const reqCascade = new Promise(async (resolve, reject) => {
  349. const db = await this.getDB();
  350. const objectStore = db.transaction([this.osName], 'readwrite').objectStore(this.osName);
  351. const onsuccess = e => {
  352. const chunk = itemChunks.pop();
  353. if (!chunk) return resolve(e);
  354. const req = objectStore.add(chunk);
  355. req.onerror = reject;
  356. req.onsuccess = onsuccess;
  357. };
  358. onsuccess();
  359. });
  360. return reqCascade;
  361. }
  362. async putData(item, name = item.name, data = item.data || item) {
  363. if (!data instanceof Blob) throw 'CacheDB: data must be a Blob';
  364. const itemChunks = [];
  365. const numChunks = Math.ceil(data.size / this.maxItemSize);
  366. for (let i = 0; i < numChunks; i++) {
  367. itemChunks.push({
  368. name: `${name}/part_${i}`,
  369. numChunks,
  370. data: data.slice(i * this.maxItemSize, (i + 1) * this.maxItemSize)
  371. });
  372. }
  373. const reqCascade = new Promise(async (resolve, reject) => {
  374. const db = await this.getDB();
  375. const objectStore = db.transaction([this.osName], 'readwrite').objectStore(this.osName);
  376. const onsuccess = e => {
  377. const chunk = itemChunks.pop();
  378. if (!chunk) return resolve(e);
  379. const req = objectStore.put(chunk);
  380. req.onerror = reject;
  381. req.onsuccess = onsuccess;
  382. };
  383. onsuccess();
  384. });
  385. return reqCascade;
  386. }
  387. async getData(name) {
  388. const reqCascade = new Promise(async (resolve, reject) => {
  389. const dataChunks = [];
  390. const db = await this.getDB();
  391. const objectStore = db.transaction([this.osName], 'readwrite').objectStore(this.osName);
  392. const probe = objectStore.get(`${name}/part_0`);
  393. probe.onerror = reject;
  394. probe.onsuccess = e => {
  395. // 1. Probe fails => key does not exist
  396. if (!probe.r###lt) return resolve(null);
  397. // 2. How many chunks to retrieve?
  398. const { numChunks } = probe.r###lt;
  399. // 3. Cascade on the remaining chunks
  400. const onsuccess = e => {
  401. dataChunks.push(e.target.r###lt.data);
  402. if (dataChunks.length == numChunks) return resolve(dataChunks);
  403. const req = objectStore.get(`${name}/part_${dataChunks.length}`);
  404. req.onerror = reject;
  405. req.onsuccess = onsuccess;
  406. };
  407. onsuccess(e);
  408. };
  409. });
  410. const dataChunks = await reqCascade;
  411. return dataChunks ? { name, data: new Blob(dataChunks) } : null;
  412. }
  413. async deleteData(name) {
  414. const reqCascade = new Promise(async (resolve, reject) => {
  415. let currentChunkNum = 0;
  416. const db = await this.getDB();
  417. const objectStore = db.transaction([this.osName], 'readwrite').objectStore(this.osName);
  418. const probe = objectStore.get(`${name}/part_0`);
  419. probe.onerror = reject;
  420. probe.onsuccess = e => {
  421. // 1. Probe fails => key does not exist
  422. if (!probe.r###lt) return resolve(null);
  423. // 2. How many chunks to delete?
  424. const { numChunks } = probe.r###lt;
  425. // 3. Cascade on the remaining chunks
  426. const onsuccess = e => {
  427. const req = objectStore.delete(`${name}/part_${currentChunkNum}`);
  428. req.onerror = reject;
  429. req.onsuccess = onsuccess;
  430. currentChunkNum++;
  431. if (currentChunkNum == numChunks) return resolve(e);
  432. };
  433. onsuccess();
  434. };
  435. });
  436. return reqCascade;
  437. }
  438. async deleteEntireDB() {
  439. const req = indexedDB.deleteDatabase(this.dbName);
  440. return new Promise((resolve, reject) => {
  441. req.onsuccess = () => resolve(this.db = null);
  442. req.onerror = reject;
  443. });
  444. }
  445. static async _UNIT_TEST() {
  446. let db = new CacheDB();
  447. console.warn('Storing 201MB...');
  448. console.log(await db.putData(new Blob([new ArrayBuffer(201 * #### * ####)]), 'test'));
  449. console.warn('Deleting 201MB...');
  450. console.log(await db.deleteData('test'));
  451. }
  452. }
  453. /***
  454. * Copyright (C) 2018 Qli5. All Rights Reserved.
  455. *
  456. * @author qli5 <goodlq11[at](163|gmail).com>
  457. *
  458. * This Source Code Form is subject to the terms of the Mozilla Public
  459. * License, v. 2.0. If a copy of the MPL was not distributed with this
  460. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  461. */
  462. /**
  463. * A more powerful fetch with
  464. * 1. onprogress handler
  465. * 2. partial response getter
  466. */
  467. class DetailedFetchBlob {
  468. constructor(input, init = {}, onprogress = init.onprogress, onabort = init.onabort, onerror = init.onerror, fetch = init.fetch || top.fetch) {
  469. // Fire in the Fox fix
  470. if (this.firefoxConstructor(input, init, onprogress, onabort, onerror)) return;
  471. // Now I know why standardizing cancelable Promise is that difficult
  472. // PLEASE refactor me!
  473. this.onprogress = onprogress;
  474. this.onabort = onabort;
  475. this.onerror = onerror;
  476. this.abort = null;
  477. this.loaded = init.cacheLoaded || 0;
  478. this.total = init.cacheLoaded || 0;
  479. this.lengthComputable = false;
  480. this.buffer = [];
  481. this.blob = null;
  482. this.reader = null;
  483. this.blobPromise = fetch(input, init).then(res => {
  484. if (this.reader == 'abort') return res.body.getReader().cancel().then(() => null);
  485. if (!res.ok) throw `HTTP Error ${res.status}: ${res.statusText}`;
  486. this.lengthComputable = res.headers.has('Content-Length');
  487. this.total += parseInt(res.headers.get('Content-Length')) || Infinity;
  488. if (this.lengthComputable) {
  489. this.reader = res.body.getReader();
  490. return this.blob = this.consume();
  491. }
  492. else {
  493. if (this.onprogress) this.onprogress(this.loaded, this.total, this.lengthComputable);
  494. return this.blob = res.blob();
  495. }
  496. });
  497. this.blobPromise.then(() => this.abort = () => { });
  498. this.blobPromise.catch(e => this.onerror({ target: this, type: e }));
  499. this.promise = Promise.race([
  500. this.blobPromise,
  501. new Promise(resolve => this.abort = () => {
  502. this.onabort({ target: this, type: 'abort' });
  503. resolve('abort');
  504. this.buffer = [];
  505. this.blob = null;
  506. if (this.reader) this.reader.cancel();
  507. else this.reader = 'abort';
  508. })
  509. ]).then(s => s == 'abort' ? new Promise(() => { }) : s);
  510. this.then = this.promise.then.bind(this.promise);
  511. this.catch = this.promise.catch.bind(this.promise);
  512. }
  513. getPartialBlob() {
  514. return new Blob(this.buffer);
  515. }
  516. async getBlob() {
  517. return this.promise;
  518. }
  519. async pump() {
  520. while (true) {
  521. let { done, value } = await this.reader.read();
  522. if (done) return this.loaded;
  523. this.loaded += value.byteLength;
  524. this.buffer.push(new Blob([value]));
  525. if (this.onprogress) this.onprogress(this.loaded, this.total, this.lengthComputable);
  526. }
  527. }
  528. async consume() {
  529. await this.pump();
  530. this.blob = new Blob(this.buffer);
  531. this.buffer = null;
  532. return this.blob;
  533. }
  534. firefoxConstructor(input, init = {}, onprogress = init.onprogress, onabort = init.onabort, onerror = init.onerror) {
  535. if (!top.navigator.userAgent.includes('Firefox')) return false;
  536. this.onprogress = onprogress;
  537. this.onabort = onabort;
  538. this.onerror = onerror;
  539. this.abort = null;
  540. this.loaded = init.cacheLoaded || 0;
  541. this.total = init.cacheLoaded || 0;
  542. this.lengthComputable = false;
  543. this.buffer = [];
  544. this.blob = null;
  545. this.reader = undefined;
  546. this.blobPromise = new Promise((resolve, reject) => {
  547. let xhr = new XMLHttpRequest();
  548. xhr.responseType = 'moz-chunked-arraybuffer';
  549. xhr.onload = () => { resolve(this.blob = new Blob(this.buffer)); this.buffer = null; };
  550. let cacheLoaded = this.loaded;
  551. xhr.onprogress = e => {
  552. this.loaded = e.loaded + cacheLoaded;
  553. this.total = e.total + cacheLoaded;
  554. this.lengthComputable = e.lengthComputable;
  555. this.buffer.push(new Blob([xhr.response]));
  556. if (this.onprogress) this.onprogress(this.loaded, this.total, this.lengthComputable);
  557. };
  558. xhr.onabort = e => this.onabort({ target: this, type: 'abort' });
  559. xhr.onerror = e => { this.onerror({ target: this, type: e.type }); reject(e); };
  560. this.abort = xhr.abort.bind(xhr);
  561. xhr.open('get', input);
  562. xhr.send();
  563. });
  564. this.promise = this.blobPromise;
  565. this.then = this.promise.then.bind(this.promise);
  566. this.catch = this.promise.catch.bind(this.promise);
  567. return true;
  568. }
  569. }
  570. /***
  571. * Copyright (C) 2018 Qli5. All Rights Reserved.
  572. *
  573. * @author qli5 <goodlq11[at](163|gmail).com>
  574. *
  575. * This Source Code Form is subject to the terms of the Mozilla Public
  576. * License, v. 2.0. If a copy of the MPL was not distributed with this
  577. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  578. */
  579. /**
  580. * A simple emulation of pthread_mutex
  581. */
  582. class Mutex {
  583. constructor() {
  584. this.queueTail = Promise.resolve();
  585. this.resolveHead = null;
  586. }
  587. /**
  588. * await mutex.lock = pthread_mutex_lock
  589. * @returns a promise to be resolved when the mutex is available
  590. */
  591. async lock() {
  592. let myResolve;
  593. let _queueTail = this.queueTail;
  594. this.queueTail = new Promise(resolve => myResolve = resolve);
  595. await _queueTail;
  596. this.resolveHead = myResolve;
  597. return;
  598. }
  599. /**
  600. * mutex.unlock = pthread_mutex_unlock
  601. */
  602. unlock() {
  603. this.resolveHead();
  604. return;
  605. }
  606. /**
  607. * lock, ret = await async, unlock, return ret
  608. * @param {(Function|Promise)} promise async thing to wait for
  609. */
  610. async lockAndAwait(promise) {
  611. await this.lock();
  612. let ret;
  613. try {
  614. if (typeof promise == 'function') promise = promise();
  615. ret = await promise;
  616. }
  617. finally {
  618. this.unlock();
  619. }
  620. return ret;
  621. }
  622. static _UNIT_TEST() {
  623. let m = new Mutex();
  624. function sleep(time) {
  625. return new Promise(r => setTimeout(r, time));
  626. }
  627. m.lockAndAwait(() => {
  628. console.warn('Check message timestamps.');
  629. console.warn('Bad:');
  630. console.warn('1 1 1 1 1:5s');
  631. console.warn(' 1 1 1 1 1:10s');
  632. console.warn('Good:');
  633. console.warn('1 1 1 1 1:5s');
  634. console.warn(' 1 1 1 1 1:10s');
  635. });
  636. m.lockAndAwait(async () => {
  637. await sleep(1000);
  638. await sleep(1000);
  639. await sleep(1000);
  640. await sleep(1000);
  641. await sleep(1000);
  642. });
  643. m.lockAndAwait(async () => console.log('5s!'));
  644. m.lockAndAwait(async () => {
  645. await sleep(1000);
  646. await sleep(1000);
  647. await sleep(1000);
  648. await sleep(1000);
  649. await sleep(1000);
  650. });
  651. m.lockAndAwait(async () => console.log('10s!'));
  652. }
  653. }
  654. /**
  655. * @typedef DanmakuColor
  656. * @property {number} r
  657. * @property {number} g
  658. * @property {number} b
  659. */
  660. /**
  661. * @typedef Danmaku
  662. * @property {string} text
  663. * @property {number} time
  664. * @property {string} mode
  665. * @property {number} size
  666. * @property {DanmakuColor} color
  667. * @property {boolean} bottom
  668. */
  669. const parser = {};
  670. /**
  671. * @param {Danmaku} danmaku
  672. * @returns {boolean}
  673. */
  674. const danmakuFilter = danmaku => {
  675. if (!danmaku) return false;
  676. if (!danmaku.text) return false;
  677. if (!danmaku.mode) return false;
  678. if (!danmaku.size) return false;
  679. if (danmaku.time < 0 || danmaku.time >= 360000) return false;
  680. return true;
  681. };
  682. const parseRgb256IntegerColor = color => {
  683. const rgb = parseInt(color, 10);
  684. const r = (rgb >>> 4) & 0xff;
  685. const g = (rgb >>> 2) & 0xff;
  686. const b = (rgb >>> 0) & 0xff;
  687. return { r, g, b };
  688. };
  689. const parseNiconicoColor = mail => {
  690. const colorTable = {
  691. red: { r: 255, g: 0, b: 0 },
  692. pink: { r: 255, g: 128, b: 128 },
  693. orange: { r: 255, g: 184, b: 0 },
  694. yellow: { r: 255, g: 255, b: 0 },
  695. green: { r: 0, g: 255, b: 0 },
  696. cyan: { r: 0, g: 255, b: 255 },
  697. blue: { r: 0, g: 0, b: 255 },
  698. purple: { r: 184, g: 0, b: 255 },
  699. black: { r: 0, g: 0, b: 0 },
  700. };
  701. const defaultColor = { r: 255, g: 255, b: 255 };
  702. const line = mail.toLowerCase().split(/\s+/);
  703. const color = Object.keys(colorTable).find(color => line.includes(color));
  704. return color ? colorTable[color] : defaultColor;
  705. };
  706. const parseNiconicoMode = mail => {
  707. const line = mail.toLowerCase().split(/\s+/);
  708. if (line.includes('ue')) return 'TOP';
  709. if (line.includes('shita')) return 'BOTTOM';
  710. return 'RTL';
  711. };
  712. const parseNiconicoSize = mail => {
  713. const line = mail.toLowerCase().split(/\s+/);
  714. if (line.includes('big')) return 36;
  715. if (line.includes('small')) return 16;
  716. return 25;
  717. };
  718. /**
  719. * @param {string|ArrayBuffer} content
  720. * @return {{ cid: number, danmaku: Array<Danmaku> }}
  721. */
  722. parser.bilibili = function (content) {
  723. const text = typeof content === 'string' ? content : new TextDecoder('utf-8').decode(content);
  724. const clean = text.replace(/(?:[\0-\x08\x0B\f\x0E-\x1F\uFFFE\uFFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF])/g, '');
  725. const data = (new DOMParser()).parseFromString(clean, 'text/xml');
  726. const cid = +data.querySelector('chatid').textContent;
  727. /** @type {Array<Danmaku>} */
  728. const danmaku = Array.from(data.querySelectorAll('d')).map(d => {
  729. const p = d.getAttribute('p');
  730. const [time, mode, size, color, create, bottom, sender, id] = p.split(',');
  731. return {
  732. text: d.textContent,
  733. time: +time,
  734. // We do not support ltr mode
  735. mode: [null, 'RTL', 'RTL', 'RTL', 'BOTTOM', 'TOP'][+mode],
  736. size: +size,
  737. color: parseRgb256IntegerColor(color),
  738. bottom: bottom > 0,
  739. };
  740. }).filter(danmakuFilter);
  741. return { cid, danmaku };
  742. };
  743. /**
  744. * @param {string|ArrayBuffer} content
  745. * @return {{ cid: number, danmaku: Array<Danmaku> }}
  746. */
  747. parser.acfun = function (content) {
  748. const text = typeof content === 'string' ? content : new TextDecoder('utf-8').decode(content);
  749. const data = JSON.parse(text);
  750. const list = data.reduce((x, y) => x.concat(y), []);
  751. const danmaku = list.map(line => {
  752. const [time, color, mode, size, sender, create, uuid] = line.c.split(','), text = line.m;
  753. return {
  754. text,
  755. time: +time,
  756. color: parseRgb256IntegerColor(+color),
  757. mode: [null, 'RTL', null, null, 'BOTTOM', 'TOP'][mode],
  758. size: +size,
  759. bottom: false,
  760. uuid,
  761. };
  762. }).filter(danmakuFilter);
  763. return { danmaku };
  764. };
  765. /**
  766. * @param {string|ArrayBuffer} content
  767. * @return {{ cid: number, danmaku: Array<Danmaku> }}
  768. */
  769. parser.niconico = function (content) {
  770. const text = typeof content === 'string' ? content : new TextDecoder('utf-8').decode(content);
  771. const data = JSON.parse(text);
  772. const list = data.map(item => item.chat).filter(x => x);
  773. const { thread } = list.find(comment => comment.thread);
  774. const danmaku = list.map(comment => {
  775. if (!comment.content || !(comment.vpos >= 0) || !comment.no) return null;
  776. const { vpos, mail = '', content, no } = comment;
  777. return {
  778. text: content,
  779. time: vpos / 100,
  780. color: parseNiconicoColor(mail),
  781. mode: parseNiconicoMode(mail),
  782. size: parseNiconicoSize(mail),
  783. bottom: false,
  784. id: no,
  785. };
  786. }).filter(danmakuFilter);
  787. return { thread, danmaku };
  788. };
  789. const font = {};
  790. // Meansure using canvas
  791. font.textByCanvas = function () {
  792. const canvas = document.createElement('canvas');
  793. const context = canvas.getContext('2d');
  794. return function (fontname, text, fontsize) {
  795. context.font = `bold ${fontsize}px ${fontname}`;
  796. return Math.ceil(context.measureText(text).width);
  797. };
  798. };
  799. // Meansure using <div>
  800. font.textByDom = function () {
  801. const container = document.createElement('div');
  802. container.setAttribute('style', 'all: initial !important');
  803. const content = document.createElement('div');
  804. content.setAttribute('style', [
  805. 'top: -10000px', 'left: -10000px',
  806. 'width: auto', 'height: auto', 'position: absolute',
  807. ].map(item => item + ' !important;').join(' '));
  808. const active = () => { document.body.parentNode.appendChild(content); };
  809. if (!document.body) document.addEventListener('DOMContentLoaded', active);
  810. else active();
  811. return (fontname, text, fontsize) => {
  812. content.textContent = text;
  813. content.style.font = `bold ${fontsize}px ${fontname}`;
  814. return content.clientWidth;
  815. };
  816. };
  817. font.text = (function () {
  818. // https://bugzilla.mozilla.org/show_bug.cgi?id=561361
  819. if (/linux/i.test(navigator.platform)) {
  820. return font.textByDom();
  821. } else {
  822. return font.textByCanvas();
  823. }
  824. }());
  825. font.valid = (function () {
  826. const cache = new Map();
  827. const textWidth = font.text;
  828. // Use following texts for checking
  829. const sampleText = [
  830. 'The quick brown fox jumps over the lazy dog',
  831. '7531902468', ',.!-', ',。:!',
  832. '天地玄黄', '則近道矣',
  833. 'あいうえお', 'アイウエオガパ', 'アイウエオガパ',
  834. ].join('');
  835. // Some given font family is avaliable iff we can meansure different width compared to other fonts
  836. const sampleFont = [
  837. 'monospace', 'sans-serif', 'sans',
  838. 'Symbol', 'Arial', 'Comic Sans MS', 'Fixed', 'Terminal',
  839. 'Times', 'Times New Roman',
  840. 'SimSum', 'Microsoft YaHei', 'PingFang SC', 'Heiti SC', 'WenQuanYi Micro Hei',
  841. 'Pmingliu', 'Microsoft JhengHei', 'PingFang TC', 'Heiti TC',
  842. 'MS Gothic', 'Meiryo', 'Hiragino Kaku Gothic Pro', 'Hiragino Mincho Pro',
  843. ];
  844. const diffFont = function (base, test) {
  845. const baseSize = textWidth(base, sampleText, 72);
  846. const testSize = textWidth(test + ',' + base, sampleText, 72);
  847. return baseSize !== testSize;
  848. };
  849. const validFont = function (test) {
  850. if (cache.has(test)) return cache.get(test);
  851. const r###lt = sampleFont.some(base => diffFont(base, test));
  852. cache.set(test, r###lt);
  853. return r###lt;
  854. };
  855. return validFont;
  856. }());
  857. const rtlCanvas = function (options) {
  858. const {
  859. resolutionX: wc, // width of canvas
  860. resolutionY: hc, // height of canvas
  861. bottomReserved: b, // reserved bottom height for subtitle
  862. rtlDuration: u, // duration appeared on screen
  863. maxDelay: maxr, // max allowed delay
  864. } = options;
  865. // Initial canvas border
  866. let used = [
  867. // p: top
  868. // m: bottom
  869. // tf: time completely enter screen
  870. // td: time completely leave screen
  871. // b: allow conflict with subtitle
  872. // add a fake danmaku for describe top of screen
  873. { p: -Infinity, m: 0, tf: Infinity, td: Infinity, b: false },
  874. // add a fake danmaku for describe bottom of screen
  875. { p: hc, m: Infinity, tf: Infinity, td: Infinity, b: false },
  876. // add a fake danmaku for placeholder of subtitle
  877. { p: hc - b, m: hc, tf: Infinity, td: Infinity, b: true },
  878. ];
  879. // Find out some position is available
  880. const available = (hv, t0s, t0l, b) => {
  881. const suggestion = [];
  882. // Upper edge of candidate position should always be bottom of other danmaku (or top of screen)
  883. used.forEach(i => {
  884. if (i.m + hv >= hc) return;
  885. const p = i.m;
  886. const m = p + hv;
  887. let tas = t0s;
  888. let tal = t0l;
  889. // and left border should be right edge of others
  890. used.forEach(j => {
  891. if (j.p >= m) return;
  892. if (j.m <= p) return;
  893. if (j.b && b) return;
  894. tas = Math.max(tas, j.tf);
  895. tal = Math.max(tal, j.td);
  896. });
  897. const r = Math.max(tas - t0s, tal - t0l);
  898. if (r > maxr) return;
  899. // save a candidate position
  900. suggestion.push({ p, r });
  901. });
  902. // sorted by its vertical position
  903. suggestion.sort((x, y) => x.p - y.p);
  904. let mr = maxr;
  905. // the bottom and later choice should be ignored
  906. const filtered = suggestion.filter(i => {
  907. if (i.r >= mr) return false;
  908. mr = i.r;
  909. return true;
  910. });
  911. return filtered;
  912. };
  913. // mark some area as used
  914. let use = (p, m, tf, td) => {
  915. used.push({ p, m, tf, td, b: false });
  916. };
  917. // remove danmaku not needed anymore by its time
  918. const syn = (t0s, t0l) => {
  919. used = used.filter(i => i.tf > t0s || i.td > t0l);
  920. };
  921. // give a score in range [0, 1) for some position
  922. const score = i => {
  923. if (i.r > maxr) return -Infinity;
  924. return 1 - Math.hypot(i.r / maxr, i.p / hc) * Math.SQRT1_2;
  925. };
  926. // add some danmaku
  927. return line => {
  928. const {
  929. time: t0s, // time sent (start to appear if no delay)
  930. width: wv, // width of danmaku
  931. height: hv, // height of danmaku
  932. bottom: b, // is subtitle
  933. } = line;
  934. const t0l = wc / (wv + wc) * u + t0s; // time start to leave
  935. syn(t0s, t0l);
  936. const al = available(hv, t0s, t0l, b);
  937. if (!al.length) return null;
  938. const scored = al.map(i => [score(i), i]);
  939. const best = scored.reduce((x, y) => {
  940. return x[0] > y[0] ? x : y;
  941. })[1];
  942. const ts = t0s + best.r; // time start to enter
  943. const tf = wv / (wv + wc) * u + ts; // time complete enter
  944. const td = u + ts; // time complete leave
  945. use(best.p, best.p + hv, tf, td);
  946. return {
  947. top: best.p,
  948. time: ts,
  949. };
  950. };
  951. };
  952. const fixedCanvas = function (options) {
  953. const {
  954. resolutionY: hc,
  955. bottomReserved: b,
  956. fixDuration: u,
  957. maxDelay: maxr,
  958. } = options;
  959. let used = [
  960. { p: -Infinity, m: 0, td: Infinity, b: false },
  961. { p: hc, m: Infinity, td: Infinity, b: false },
  962. { p: hc - b, m: hc, td: Infinity, b: true },
  963. ];
  964. // Find out some available position
  965. const fr = (p, m, t0s, b) => {
  966. let tas = t0s;
  967. used.forEach(j => {
  968. if (j.p >= m) return;
  969. if (j.m <= p) return;
  970. if (j.b && b) return;
  971. tas = Math.max(tas, j.td);
  972. });
  973. const r = tas - t0s;
  974. if (r > maxr) return null;
  975. return { r, p, m };
  976. };
  977. // layout for danmaku at top
  978. const top = (hv, t0s, b) => {
  979. const suggestion = [];
  980. used.forEach(i => {
  981. if (i.m + hv >= hc) return;
  982. suggestion.push(fr(i.m, i.m + hv, t0s, b));
  983. });
  984. return suggestion.filter(x => x);
  985. };
  986. // layout for danmaku at bottom
  987. const bottom = (hv, t0s, b) => {
  988. const suggestion = [];
  989. used.forEach(i => {
  990. if (i.p - hv <= 0) return;
  991. suggestion.push(fr(i.p - hv, i.p, t0s, b));
  992. });
  993. return suggestion.filter(x => x);
  994. };
  995. const use = (p, m, td) => {
  996. used.push({ p, m, td, b: false });
  997. };
  998. const syn = t0s => {
  999. used = used.filter(i => i.td > t0s);
  1000. };
  1001. // Score every position
  1002. const score = (i, is_top) => {
  1003. if (i.r > maxr) return -Infinity;
  1004. const f = p => is_top ? p : (hc - p);
  1005. return 1 - (i.r / maxr * (31 / 32) + f(i.p) / hc * (1 / 32));
  1006. };
  1007. return function (line) {
  1008. const { time: t0s, height: hv, bottom: b } = line;
  1009. const is_top = line.mode === 'TOP';
  1010. syn(t0s);
  1011. const al = (is_top ? top : bottom)(hv, t0s, b);
  1012. if (!al.length) return null;
  1013. const scored = al.map(function (i) { return [score(i, is_top), i]; });
  1014. const best = scored.reduce(function (x, y) {
  1015. return x[0] > y[0] ? x : y;
  1016. }, [-Infinity, null])[1];
  1017. if (!best) return null;
  1018. use(best.p, best.m, best.r + t0s + u);
  1019. return { top: best.p, time: best.r + t0s };
  1020. };
  1021. };
  1022. const placeDanmaku = function (options) {
  1023. const layers = options.maxOverlap;
  1024. const normal = Array(layers).fill(null).map(x => rtlCanvas(options));
  1025. const fixed = Array(layers).fill(null).map(x => fixedCanvas(options));
  1026. return function (line) {
  1027. line.fontSize = Math.round(line.size * options.fontSize);
  1028. line.height = line.fontSize;
  1029. line.width = line.width || font.text(options.fontFamily, line.text, line.fontSize) || 1;
  1030. if (line.mode === 'RTL') {
  1031. const pos = normal.reduce((pos, layer) => pos || layer(line), null);
  1032. if (!pos) return null;
  1033. const { top, time } = pos;
  1034. line.layout = {
  1035. type: 'Rtl',
  1036. start: {
  1037. x: options.resolutionX + line.width / 2,
  1038. y: top + line.height,
  1039. time,
  1040. },
  1041. end: {
  1042. x: -line.width / 2,
  1043. y: top + line.height,
  1044. time: options.rtlDuration + time,
  1045. },
  1046. };
  1047. } else if (['TOP', 'BOTTOM'].includes(line.mode)) {
  1048. const pos = fixed.reduce((pos, layer) => pos || layer(line), null);
  1049. if (!pos) return null;
  1050. const { top, time } = pos;
  1051. line.layout = {
  1052. type: 'Fix',
  1053. start: {
  1054. x: Math.round(options.resolutionX / 2),
  1055. y: top + line.height,
  1056. time,
  1057. },
  1058. end: {
  1059. time: options.fixDuration + time,
  1060. },
  1061. };
  1062. }
  1063. return line;
  1064. };
  1065. };
  1066. // main layout algorithm
  1067. const layout = async function (danmaku, optionGetter) {
  1068. const options = JSON.parse(JSON.stringify(optionGetter));
  1069. const sorted = danmaku.slice(0).sort(({ time: x }, { time: y }) => x - y);
  1070. const place = placeDanmaku(options);
  1071. const r###lt = Array(sorted.length);
  1072. let length = 0;
  1073. for (let i = 0, l = sorted.length; i < l; i++) {
  1074. let placed = place(sorted[i]);
  1075. if (placed) r###lt[length++] = placed;
  1076. if ((i + 1) % 1000 === 0) {
  1077. await new Promise(resolve => setTimeout(resolve, 0));
  1078. }
  1079. }
  1080. r###lt.length = length;
  1081. r###lt.sort((x, y) => x.layout.start.time - y.layout.start.time);
  1082. return r###lt;
  1083. };
  1084. // escape string for ass
  1085. const textEscape = s => (
  1086. // VSFilter do not support escaped "{" or "}"; we use full-width version instead
  1087. s.replace(/{/g, '{').replace(/}/g, '}').replace(/\s/g, ' ')
  1088. );
  1089. const formatColorChannel = v => (v & 255).toString(16).toUpperCase().padStart(2, '0');
  1090. // format color
  1091. const formatColor = color => '&H' + (
  1092. [color.b, color.g, color.r].map(formatColorChannel).join('')
  1093. );
  1094. // format timestamp
  1095. const formatTimestamp = time => {
  1096. const value = Math.round(time * 100) * 10;
  1097. const rem = value % 3600000;
  1098. const hour = (value - rem) / 3600000;
  1099. const fHour = hour.toFixed(0).padStart(2, '0');
  1100. const fRem = new Date(rem).toISOString().slice(-11, -2);
  1101. return fHour + fRem;
  1102. };
  1103. // test is default color
  1104. const isDefaultColor = ({ r, g, b }) => r === 255 && g === 255 && b === 255;
  1105. // test is dark color
  1106. const isDarkColor = ({ r, g, b }) => r * 0.299 + g * 0.587 + b * 0.114 < 0x30;
  1107. // Ass header
  1108. const header = info => [
  1109. '[Script Info]',
  1110. `Title: ${info.title}`,
  1111. `Original Script: ${info.original}`,
  1112. 'ScriptType: v4.00+',
  1113. 'Collisions: Normal',
  1114. `PlayResX: ${info.playResX}`,
  1115. `PlayResY: ${info.playResY}`,
  1116. 'Timer: 100.0000',
  1117. '',
  1118. '[V4+ Styles]',
  1119. 'Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding',
  1120. `Style: Fix,${info.fontFamily},${info.fontSize},&H${info.alpha}FFFFFF,&H${info.alpha}FFFFFF,&H${info.alpha}000000,&H${info.alpha}000000,${info.bold},0,0,0,100,100,0,0,1,2,0,2,20,20,2,0`,
  1121. `Style: Rtl,${info.fontFamily},${info.fontSize},&H${info.alpha}FFFFFF,&H${info.alpha}FFFFFF,&H${info.alpha}000000,&H${info.alpha}000000,${info.bold},0,0,0,100,100,0,0,1,2,0,2,20,20,2,0`,
  1122. '',
  1123. '[Events]',
  1124. 'Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text',
  1125. ];
  1126. // Set color of text
  1127. const lineColor = ({ color }) => {
  1128. let output = [];
  1129. if (!isDefaultColor(color)) output.push(`\\c${formatColor(color)}`);
  1130. if (isDarkColor(color)) output.push(`\\3c&HFFFFFF`);
  1131. return output.join('');
  1132. };
  1133. // Set fontsize
  1134. let defaultFontSize;
  1135. const lineFontSize = ({ size }) => {
  1136. if (size === defaultFontSize) return '';
  1137. return `\\fs${size}`;
  1138. };
  1139. const getCommonFontSize = list => {
  1140. const count = new Map();
  1141. let commonCount = 0, common = 1;
  1142. list.forEach(({ size }) => {
  1143. let value = 1;
  1144. if (count.has(size)) value = count.get(size) + 1;
  1145. count.set(size, value);
  1146. if (value > commonCount) {
  1147. commonCount = value;
  1148. common = size;
  1149. }
  1150. });
  1151. defaultFontSize = common;
  1152. return common;
  1153. };
  1154. // Add animation of danmaku
  1155. const lineMove = ({ layout: { type, start = null, end = null } }) => {
  1156. if (type === 'Rtl' && start && end) return `\\move(${start.x},${start.y},${end.x},${end.y})`;
  1157. if (type === 'Fix' && start) return `\\pos(${start.x},${start.y})`;
  1158. return '';
  1159. };
  1160. // format one line
  1161. const formatLine = line => {
  1162. const start = formatTimestamp(line.layout.start.time);
  1163. const end = formatTimestamp(line.layout.end.time);
  1164. const type = line.layout.type;
  1165. const color = lineColor(line);
  1166. const fontSize = lineFontSize(line);
  1167. const move = lineMove(line);
  1168. const format = `${color}${fontSize}${move}`;
  1169. const text = textEscape(line.text);
  1170. return `Dialogue: 0,${start},${end},${type},,20,20,2,,{${format}}${text}`;
  1171. };
  1172. const ass = (danmaku, options) => {
  1173. const info = {
  1174. title: danmaku.meta.name,
  1175. original: `Generated by tiansh/ass-danmaku (embedded in liqi0816/bilitwin) based on ${danmaku.meta.url}`,
  1176. playResX: options.resolutionX,
  1177. playResY: options.resolutionY,
  1178. fontFamily: options.fontFamily,
  1179. fontSize: getCommonFontSize(danmaku.layout),
  1180. alpha: formatColorChannel(0xFF * (100 - options.textOpacity) / 100),
  1181. bold: options.bold? -1 : 0,
  1182. };
  1183. return [
  1184. ...header(info),
  1185. ...danmaku.layout.map(formatLine).filter(x => x),
  1186. ].join('\r\n');
  1187. };
  1188. /**
  1189. * @file Common works for reading / writing optinos
  1190. */
  1191. /**
  1192. * @returns {string}
  1193. */
  1194. const predefFontFamily = () => {
  1195. // const sc = ['Microsoft YaHei', 'PingFang SC', 'Noto Sans CJK SC'];
  1196. // replaced with bilibili defaults
  1197. const sc = ["SimHei", "'Microsoft JhengHei'", "SimSun", "NSimSun", "FangSong", "'Microsoft YaHei'", "'Microsoft Yahei UI Light'", "'Noto Sans CJK SC Bold'", "'Noto Sans CJK SC DemiLight'", "'Noto Sans CJK SC Regular'"];
  1198. const tc = ['Microsoft JhengHei', 'PingFang TC', 'Noto Sans CJK TC'];
  1199. const ja = ['MS PGothic', 'Hiragino Kaku Gothic Pro', 'Noto Sans CJK JP'];
  1200. const lang = navigator.language;
  1201. const fonts = /^ja/.test(lang) ? ja : /^zh(?!.*Hans).*(?:TW|HK|MO)/.test(lang) ? tc : sc;
  1202. const chosed = fonts.find(font$$1 => font.valid(font$$1)) || fonts[0];
  1203. return chosed;
  1204. };
  1205. const attributes = [
  1206. { name: 'resolutionX', type: 'number', min: 480, predef: 560 },
  1207. { name: 'resolutionY', type: 'number', min: 360, predef: 420 },
  1208. { name: 'bottomReserved', type: 'number', min: 0, predef: 60 },
  1209. { name: 'fontFamily', type: 'string', predef: predefFontFamily(), valid: font$$1 => font.valid(font$$1) },
  1210. { name: 'fontSize', type: 'number', min: 0, predef: 1, step: 0.01 },
  1211. { name: 'textSpace', type: 'number', min: 0, predef: 0 },
  1212. { name: 'rtlDuration', type: 'number', min: 0.1, predef: 8, step: 0.1 },
  1213. { name: 'fixDuration', type: 'number', min: 0.1, predef: 4, step: 0.1 },
  1214. { name: 'maxDelay', type: 'number', min: 0, predef: 6, step: 0.1 },
  1215. { name: 'textOpacity', type: 'number', min: 10, max: 100, predef: 60 },
  1216. { name: 'maxOverlap', type: 'number', min: 1, max: 20, predef: 1 },
  1217. { name: 'bold', type: 'boolean', predef: true },
  1218. ];
  1219. const attrNormalize = (option, { name, type, min = -Infinity, max = Infinity, step = 1, predef, valid }) => {
  1220. let value = option;
  1221. if (type === 'number') value = +value;
  1222. else if (type === 'string') value = '' + value;
  1223. else if (type === 'boolean') value = !!value;
  1224. if (valid && !valid(value)) value = predef;
  1225. if (type === 'number') {
  1226. if (Number.isNaN(value)) value = predef;
  1227. if (value < min) value = min;
  1228. if (value > max) value = max;
  1229. value = Math.round((value - min) / step) * step + min;
  1230. }
  1231. return value;
  1232. };
  1233. /**
  1234. * @param {ExtOption} option
  1235. * @returns {ExtOption}
  1236. */
  1237. const normalize = function (option) {
  1238. return Object.assign({},
  1239. ...attributes.map(attr => ({ [attr.name]: attrNormalize(option[attr.name], attr) }))
  1240. );
  1241. };
  1242. /**
  1243. * Convert file content to Blob which describe the file
  1244. * @param {string} content
  1245. * @returns {Blob}
  1246. */
  1247. const convertToBlob = content => {
  1248. const encoder = new TextEncoder();
  1249. // Add a BOM to make some ass parser library happier
  1250. const bom = '\ufeff';
  1251. const encoded = encoder.encode(bom + content);
  1252. const blob = new Blob([encoded], { type: 'application/octet-stream' });
  1253. return blob;
  1254. };
  1255. /***
  1256. * Copyright (C) 2018 Qli5. All Rights Reserved.
  1257. *
  1258. * @author qli5 <goodlq11[at](163|gmail).com>
  1259. *
  1260. * This Source Code Form is subject to the terms of the Mozilla Public
  1261. * License, v. 2.0. If a copy of the MPL was not distributed with this
  1262. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  1263. */
  1264. /**
  1265. * An API wrapper of tiansh/ass-danmaku for liqi0816/bilitwin
  1266. */
  1267. class ASSConverter {
  1268. /**
  1269. * @typedef {ExtOption}
  1270. * @property {number} resolutionX canvas width for drawing danmaku (px)
  1271. * @property {number} resolutionY canvas height for drawing danmaku (px)
  1272. * @property {number} bottomReserved reserved height at bottom for drawing danmaku (px)
  1273. * @property {string} fontFamily danmaku font family
  1274. * @property {number} fontSize danmaku font size (ratio)
  1275. * @property {number} textSpace space between danmaku (px)
  1276. * @property {number} rtlDuration duration of right to left moving danmaku appeared on screen (s)
  1277. * @property {number} fixDuration duration of keep bottom / top danmaku appeared on screen (s)
  1278. * @property {number} maxDelay // maxinum amount of allowed delay (s)
  1279. * @property {number} textOpacity // opacity of text, in range of [0, 1]
  1280. * @property {number} maxOverlap // maxinum layers of danmaku
  1281. */
  1282. /**
  1283. * @param {ExtOption} option tiansh/ass-danmaku compatible option
  1284. */
  1285. constructor(option = {}) {
  1286. this.option = option;
  1287. }
  1288. get option() {
  1289. return this.normalizedOption;
  1290. }
  1291. set option(e) {
  1292. return this.normalizedOption = normalize(e);
  1293. }
  1294. /**
  1295. * @param {Danmaku[]} danmaku use ASSConverter.parseXML
  1296. * @param {string} title
  1297. * @param {string} originalURL
  1298. */
  1299. async genASS(danmaku, title = 'danmaku', originalURL = 'anonymous xml') {
  1300. const layout$$1 = await layout(danmaku, this.option);
  1301. const ass$$1 = ass({
  1302. content: danmaku,
  1303. layout: layout$$1,
  1304. meta: {
  1305. name: title,
  1306. url: originalURL
  1307. }
  1308. }, this.option);
  1309. return ass$$1;
  1310. }
  1311. async genASSBlob(danmaku, title = 'danmaku', originalURL = 'anonymous xml') {
  1312. return convertToBlob(await this.genASS(danmaku, title, originalURL));
  1313. }
  1314. /**
  1315. * @typedef DanmakuColor
  1316. * @property {number} r
  1317. * @property {number} g
  1318. * @property {number} b
  1319. */
  1320. /**
  1321. * @typedef Danmaku
  1322. * @property {string} text
  1323. * @property {number} time
  1324. * @property {string} mode
  1325. * @property {number} size
  1326. * @property {DanmakuColor} color
  1327. * @property {boolean} bottom
  1328. */
  1329. /**
  1330. * @param {string} xml bilibili danmaku xml
  1331. * @returns {Danmaku[]}
  1332. */
  1333. static parseXML(xml) {
  1334. return parser.bilibili(xml).danmaku;
  1335. }
  1336. static _UNIT_TEST() {
  1337. const e = new ASSConverter();
  1338. const xml = `<?xml version="1.0" encoding="UTF-8"?><i><chatserver>chat.bilibili.com</chatserver><chatid>32873758</chatid><mission>0</mission><maxlimit>6000</maxlimit><state>0</state><realname>0</realname><source>k-v</source><d p="0.00000,1,25,16777215,1519733589,0,d286a97b,4349604072">真第一</d><d p="7.29900,1,25,16777215,1519733812,0,3548796c,4349615908">五分钟前</d><d p="587.05100,1,25,16777215,1519734291,0,f2ed792f,4349641325">惊呆了!</d><d p="136.82200,1,25,16777215,1519734458,0,1e5784f,4349652071">神王代表虚空</d><d p="0.00000,1,25,16777215,1519736251,0,f16cbf44,4349751461">66666666666666666</d><d p="590.60400,1,25,16777215,1519736265,0,fbb3d1b3,4349752331">这要吹多长时间</d><d p="537.15500,1,25,16777215,1519736280,0,1e5784f,4349753170">反而不是,疾病是个恶魔,别人说她伪装成了精灵</d><d p="872.08200,1,25,16777215,1519736881,0,1e5784f,4349787709">精灵都会吃</d><d p="2648.42500,1,25,16777215,1519737840,0,e9e6b2b4,4349844463">就不能大部分都是铜币么?</d><d p="2115.09400,1,25,16777215,1519738271,0,3548796c,4349870808">吓死我了。。。</d><d p="11.45400,1,25,16777215,1519739974,0,9937b428,4349974512">???</d><d p="1285.73900,1,25,16777215,1519748274,0,3bb4c9ee,4350512859">儿砸</d><d p="595.48600,1,25,16777215,1519757148,0,f3ed26b6,4350787048">怕是要吹到缺氧哦</d><d p="1206.31500,1,25,16777215,1519767204,0,62a9186a,4350882680">233333333333333</d><d p="638.68700,1,25,16777215,1519769219,0,de0a99ae,4350893310">菜#的借口</d><d p="655.76500,1,25,16777215,1519769236,0,de0a99ae,4350893397">竟然吹蜡烛打医生</d><d p="2235.89600,1,25,16777215,1519769418,0,de0a99ae,4350894325">这暴击率太高了</d><d p="389.88700,1,25,16777215,1519780435,0,8879732c,4351021740">医生好想进10万,血,上万甲</d><d p="2322.47900,1,25,16777215,1519780901,0,e509a801,4351032321">前一个命都没了</d><d p="2408.93600,1,25,16777215,1519801350,0,1a692eb6,4351826484">23333333333333</d><d p="1290.62000,1,25,16777215,1519809649,0,af8f12dc,4352159267">儿砸~</d><d p="917.96300,1,25,16777215,1519816770,0,fef64b6a,4352474878">应该姆西自己控制洛斯 七#点太快了差评</d><d p="2328.03100,1,25,16777215,1519825291,0,8549205d,4352919003">现在前一个连命都没了啊喂</d><d p="1246.16700,1,25,16777215,1519827514,0,fef64b6a,4353052309">不如走到面前用扫射 基本全中 伤害爆表</d><d p="592.38100,1,25,16777215,1519912489,0,edc3f0a9,4355960085">这是这个游戏最震撼的几幕之一</d></i>`;
  1339. console.log(window.ass = e.genASSBlob(ASSConverter.parseXML(xml)));
  1340. }
  1341. }
  1342. /***
  1343. * Copyright (C) 2018 Qli5. All Rights Reserved.
  1344. *
  1345. * @author qli5 <goodlq11[at](163|gmail).com>
  1346. *
  1347. * This Source Code Form is subject to the terms of the Mozilla Public
  1348. * License, v. 2.0. If a copy of the MPL was not distributed with this
  1349. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  1350. */
  1351. /**
  1352. * A util to hook a function
  1353. */
  1354. class HookedFunction extends Function {
  1355. constructor(...init) {
  1356. // 1. init parameter
  1357. const { raw, pre, post } = HookedFunction.parseParameter(...init);
  1358. // 2. build bundle
  1359. const self = function (...args) {
  1360. const { raw, pre, post } = self;
  1361. const context = { args, target: raw, ret: undefined, hook: self };
  1362. pre.forEach(e => e.call(this, context));
  1363. if (context.target) context.ret = context.target.apply(this, context.args);
  1364. post.forEach(e => e.call(this, context));
  1365. return context.ret;
  1366. };
  1367. Object.setPrototypeOf(self, HookedFunction.prototype);
  1368. self.raw = raw;
  1369. self.pre = pre;
  1370. self.post = post;
  1371. // 3. cheat babel - it complains about missing super(), even if it is actual valid
  1372. try {
  1373. return self;
  1374. } catch (e) {
  1375. super();
  1376. return self;
  1377. }
  1378. }
  1379. addPre(...func) {
  1380. this.pre.push(...func);
  1381. }
  1382. addPost(...func) {
  1383. this.post.push(...func);
  1384. }
  1385. addCallback(...func) {
  1386. this.addPost(...func);
  1387. }
  1388. removePre(func) {
  1389. this.pre = this.pre.filter(e => e != func);
  1390. }
  1391. removePost(func) {
  1392. this.post = this.post.filter(e => e != func);
  1393. }
  1394. removeCallback(func) {
  1395. this.removePost(func);
  1396. }
  1397. static parseParameter(...init) {
  1398. // 1. clone init
  1399. init = init.slice();
  1400. // 2. default
  1401. let raw = null;
  1402. let pre = [];
  1403. let post = [];
  1404. // 3. (raw, ...others)
  1405. if (typeof init[0] === 'function') raw = init.shift();
  1406. // 4. iterate through parameters
  1407. for (const e of init) {
  1408. if (!e) {
  1409. continue;
  1410. }
  1411. else if (Array.isArray(e)) {
  1412. pre = post;
  1413. post = e;
  1414. }
  1415. else if (typeof e == 'object') {
  1416. if (typeof e.raw == 'function') raw = e.raw;
  1417. if (typeof e.pre == 'function') pre.push(e.pre);
  1418. if (typeof e.post == 'function') post.push(e.post);
  1419. if (Array.isArray(e.pre)) pre = e.pre;
  1420. if (Array.isArray(e.post)) post = e.post;
  1421. }
  1422. else if (typeof e == 'function') {
  1423. post.push(e);
  1424. }
  1425. else {
  1426. throw new TypeError(`HookedFunction: cannot recognize paramter ${e} of type ${typeof e}`);
  1427. }
  1428. }
  1429. return { raw, pre, post };
  1430. }
  1431. static hook(...init) {
  1432. // 1. init parameter
  1433. const { raw, pre, post } = HookedFunction.parseParameter(...init);
  1434. // 2 wrap
  1435. // 2.1 already wrapped => concat
  1436. if (raw instanceof HookedFunction) {
  1437. raw.pre.push(...pre);
  1438. raw.post.push(...post);
  1439. return raw;
  1440. }
  1441. // 2.2 otherwise => new
  1442. else {
  1443. return new HookedFunction({ raw, pre, post });
  1444. }
  1445. }
  1446. static hookDebugger(raw, pre = true, post = false) {
  1447. // 1. init hook
  1448. if (!HookedFunction.hookDebugger.hook) HookedFunction.hookDebugger.hook = function (ctx) { debugger };
  1449. // 2 wrap
  1450. // 2.1 already wrapped => concat
  1451. if (raw instanceof HookedFunction) {
  1452. if (pre && !raw.pre.includes(HookedFunction.hookDebugger.hook)) {
  1453. raw.pre.push(HookedFunction.hookDebugger.hook);
  1454. }
  1455. if (post && !raw.post.includes(HookedFunction.hookDebugger.hook)) {
  1456. raw.post.push(HookedFunction.hookDebugger.hook);
  1457. }
  1458. return raw;
  1459. }
  1460. // 2.2 otherwise => new
  1461. else {
  1462. return new HookedFunction({
  1463. raw,
  1464. pre: pre && HookedFunction.hookDebugger.hook || undefined,
  1465. post: post && HookedFunction.hookDebugger.hook || undefined,
  1466. });
  1467. }
  1468. }
  1469. }
  1470. /***
  1471. * BiliMonkey
  1472. * A bilibili user script
  1473. * Copyright (C) 2018 Qli5. All Rights Reserved.
  1474. *
  1475. * @author qli5 <goodlq11[at](163|gmail).com>
  1476. *
  1477. * This Source Code Form is subject to the terms of the Mozilla Public
  1478. * License, v. 2.0. If a copy of the MPL was not distributed with this
  1479. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  1480. *
  1481. * The FLV merge utility is a Javascript translation of
  1482. * https://github.com/grepmusic/flvmerge
  1483. * by grepmusic
  1484. *
  1485. * The ASS convert utility is a fork of
  1486. * https://github.com/tiansh/ass-danmaku
  1487. * by tiansh
  1488. *
  1489. * The FLV demuxer is from
  1490. * https://github.com/Bilibili/flv.js/
  1491. * by zheng qian
  1492. *
  1493. * The EMBL builder is from
  1494. * <https://www.npmjs.com/package/simple-ebml-builder>
  1495. * by ryiwamoto
  1496. */
  1497. class BiliMonkey {
  1498. constructor(playerWin, option = BiliMonkey.optionDefaults) {
  1499. this.playerWin = playerWin;
  1500. this.protocol = playerWin.location.protocol;
  1501. this.cid = null;
  1502. this.flvs = null;
  1503. this.mp4 = null;
  1504. this.ass = null;
  1505. this.flvFormatName = null;
  1506. this.mp4FormatName = null;
  1507. this.fallbackFormatName = null;
  1508. this.cidAsyncContainer = new AsyncContainer();
  1509. this.cidAsyncContainer.then(cid => { this.cid = cid; this.ass = this.getASS(); });
  1510. if (typeof top.cid === 'string') this.cidAsyncContainer.resolve(top.cid);
  1511. /***
  1512. * cache + proxy = Service Worker
  1513. * Hope bilibili will have a SW as soon as possible.
  1514. * partial = Stream
  1515. * Hope the fetch API will be stabilized as soon as possible.
  1516. * If you are using your grandpa's browser, do not enable these functions.
  1517. */
  1518. this.cache = option.cache;
  1519. this.partial = option.partial;
  1520. this.proxy = option.proxy;
  1521. this.blocker = option.blocker;
  1522. this.font = option.font;
  1523. this.option = option;
  1524. if (this.cache && (!(this.cache instanceof CacheDB))) this.cache = new CacheDB('biliMonkey', 'flv', 'name');
  1525. this.flvsDetailedFetch = [];
  1526. this.flvsBlob = [];
  1527. this.defaultFormatPromise = null;
  1528. this.queryInfoMutex = new Mutex();
  1529. this.queryInfoMutex.lockAndAwait(() => this.getPlayerButtons());
  1530. this.queryInfoMutex.lockAndAwait(() => this.getAvailableFormatName());
  1531. this.destroy = new HookedFunction();
  1532. }
  1533. /***
  1534. * Guide: for ease of debug, please use format name(flv720) instead of format value(64) unless necessary
  1535. * Guide: for ease of html concat, please use string format value('64') instead of number(parseInt('64'))
  1536. */
  1537. lockFormat(format) {
  1538. // null => uninitialized
  1539. // async pending => another one is working on it
  1540. // async resolve => that guy just finished work
  1541. // sync value => someone already finished work
  1542. const toast = this.playerWin.document.getElementsByClassName('bilibili-player-video-toast-top')[0];
  1543. if (toast) toast.style.visibility = 'hidden';
  1544. if (format == this.fallbackFormatName) return null;
  1545. switch (format) {
  1546. // Single writer is not a must.
  1547. // Plus, if one writer fail, others should be able to overwrite its garbage.
  1548. case 'flv_p60':
  1549. case 'flv720_p60':
  1550. case 'hdflv2':
  1551. case 'flv':
  1552. case 'flv720':
  1553. case 'flv480':
  1554. case 'flv360':
  1555. //if (this.flvs) return this.flvs;
  1556. return this.flvs = new AsyncContainer();
  1557. case 'hdmp4':
  1558. case 'mp4':
  1559. //if (this.mp4) return this.mp4;
  1560. return this.mp4 = new AsyncContainer();
  1561. default:
  1562. throw `lockFormat error: ${format} is a unrecognizable format`;
  1563. }
  1564. }
  1565. resolveFormat(res, shouldBe) {
  1566. const toast = this.playerWin.document.getElementsByClassName('bilibili-player-video-toast-top')[0];
  1567. if (toast) {
  1568. toast.style.visibility = '';
  1569. if (toast.children.length) toast.children[0].style.visibility = 'hidden';
  1570. const video = this.playerWin.document.getElementsByTagName('video')[0];
  1571. if (video) {
  1572. const h = () => {
  1573. if (toast.children.length) toast.children[0].style.visibility = 'hidden';
  1574. };
  1575. video.addEventListener('emptied', h, { once: true });
  1576. setTimeout(() => video.removeEventListener('emptied', h), 500);
  1577. }
  1578. }
  1579. if (res.format == this.fallbackFormatName) return null;
  1580. switch (res.format) {
  1581. case 'flv_p60':
  1582. case 'flv720_p60':
  1583. case 'hdflv2':
  1584. case 'flv':
  1585. case 'flv720':
  1586. case 'flv480':
  1587. case 'flv360':
  1588. if (shouldBe && shouldBe != res.format) {
  1589. this.flvs = null;
  1590. throw `URL interface error: response is not ${shouldBe}`;
  1591. }
  1592. return this.flvs = this.flvs.resolve(res.durl.map(e => e.url.replace('http:', this.protocol)));
  1593. case 'hdmp4':
  1594. case 'mp4':
  1595. if (shouldBe && shouldBe != res.format) {
  1596. this.mp4 = null;
  1597. throw `URL interface error: response is not ${shouldBe}`;
  1598. }
  1599. return this.mp4 = this.mp4.resolve(res.durl[0].url.replace('http:', this.protocol));
  1600. default:
  1601. throw `resolveFormat error: ${res.format} is a unrecognizable format`;
  1602. }
  1603. }
  1604. getVIPStatus() {
  1605. const data = this.playerWin.sessionStorage.getItem('bili_login_status');
  1606. try {
  1607. return JSON.parse(data).some(e => e instanceof Object && e.vipStatus);
  1608. }
  1609. catch (e) {
  1610. return false;
  1611. }
  1612. }
  1613. getAvailableFormatName(accept_quality) {
  1614. if (!Array.isArray(accept_quality)) accept_quality = Array.from(this.playerWin.document.querySelector('div.bilibili-player-video-btn-quality > div ul').getElementsByTagName('li')).map(e => e.getAttribute('data-value'));
  1615. const accept_format = accept_quality.map(e => BiliMonkey.valueToFormat(e));
  1616. const vipExclusiveFormatSet = new Set(['flv_p60', 'hdflv2', 'flv720_p60']);
  1617. const candidateFormatSet = new Set(this.getVIPStatus() ? accept_format : accept_format.filter(e => !vipExclusiveFormatSet.has(e)));
  1618. this.flvFormatName = ['flv_p60', 'hdflv2', 'flv', 'flv720_p60', 'flv720', 'flv480', 'flv360']
  1619. .find(e => candidateFormatSet.has(e))
  1620. || 'does_not_exist';
  1621. this.mp4FormatName = ['hdmp4', 'mp4']
  1622. .find(e => candidateFormatSet.has(e))
  1623. || 'does_not_exist';
  1624. if (this.flvFormatName == 'does_not_exist' || this.mp4FormatName == 'does_not_exist') {
  1625. this.fallbackFormatName = ['mp4', 'flv360'].find(e => candidateFormatSet.has(e));
  1626. if (!this.fallbackFormatName) throw 'BiliMonkey: cannot get available format names (this video has only one available quality?)';
  1627. }
  1628. }
  1629. async execOptions() {
  1630. if (this.option.autoDefault) await this.sniffDefaultFormat();
  1631. if (this.option.autoFLV) this.queryInfo('flv');
  1632. if (this.option.autoMP4) this.queryInfo('mp4');
  1633. }
  1634. async sniffDefaultFormat() {
  1635. if (this.defaultFormatPromise) return this.defaultFormatPromise;
  1636. if (this.playerWin.document.querySelector('div.bilibili-player-video-btn-quality > div ul li')) return this.defaultFormatPromise = Promise.resolve();
  1637. const jq = this.playerWin.jQuery;
  1638. const _ajax = jq.ajax;
  1639. this.defaultFormatPromise = new Promise(resolve => {
  1640. let timeout = setTimeout(() => { jq.ajax = _ajax; resolve(); }, 3000);
  1641. let self = this;
  1642. jq.ajax = function (a, c) {
  1643. if (typeof c === 'object') { if (typeof a === 'string') c.url = a; a = c; c = undefined; } if (a.url.includes('interface.bilibili.com/v2/playurl?') || a.url.includes('bangumi.bilibili.com/player/web_api/v2/playurl?')) {
  1644. clearTimeout(timeout);
  1645. self.cidAsyncContainer.resolve(a.url.match(/cid=\d+/)[0].slice(4));
  1646. let _success = a.success;
  1647. a.success = res => {
  1648. // 1. determine available format names
  1649. self.getAvailableFormatName(res.accept_quality);
  1650. // 2. determine if we should take this response
  1651. const format = res.format;
  1652. if (format == self.mp4FormatName || format == self.flvFormatName) {
  1653. self.lockFormat(format);
  1654. self.resolveFormat(res, format);
  1655. }
  1656. // 3. callback
  1657. if (self.proxy && self.flvs) {
  1658. self.setupProxy(res, _success);
  1659. }
  1660. else {
  1661. _success(res);
  1662. }
  1663. // 4. return to await
  1664. resolve(res);
  1665. };
  1666. jq.ajax = _ajax;
  1667. }
  1668. return _ajax.call(jq, a, c);
  1669. };
  1670. });
  1671. return this.defaultFormatPromise;
  1672. }
  1673. async getBackgroundFormat(format) {
  1674. if (format == 'hdmp4' || format == 'mp4') {
  1675. let src = this.playerWin.document.getElementsByTagName('video')[0].src;
  1676. if ((src.includes('hd') || format == 'mp4') && src.includes('.mp4')) {
  1677. let pendingFormat = this.lockFormat(format);
  1678. this.resolveFormat({ durl: [{ url: src }] }, format);
  1679. return pendingFormat;
  1680. }
  1681. }
  1682. const jq = this.playerWin.jQuery;
  1683. const _ajax = jq.ajax;
  1684. let pendingFormat = this.lockFormat(format);
  1685. let self = this;
  1686. jq.ajax = function (a, c) {
  1687. if (typeof c === 'object') { if (typeof a === 'string') c.url = a; a = c; c = undefined; } if (a.url.includes('interface.bilibili.com/v2/playurl?') || a.url.includes('bangumi.bilibili.com/player/web_api/v2/playurl?')) {
  1688. self.cidAsyncContainer.resolve(a.url.match(/cid=\d+/)[0].slice(4));
  1689. let _success = a.success;
  1690. a.success = res => {
  1691. if (format == 'hdmp4') res.durl = [res.durl[0].backup_url.find(e => e.includes('hd') && e.includes('.mp4'))];
  1692. if (format == 'mp4') res.durl = [res.durl[0].backup_url.find(e => !e.includes('hd') && e.includes('.mp4'))];
  1693. self.resolveFormat(res, format);
  1694. };
  1695. jq.ajax = _ajax;
  1696. }
  1697. return _ajax.call(jq, a, c);
  1698. };
  1699. this.playerWin.player.reloadAccess();
  1700. return pendingFormat;
  1701. }
  1702. async getCurrentFormat(format) {
  1703. const jq = this.playerWin.jQuery;
  1704. const _ajax = jq.ajax;
  1705. const _setItem = this.playerWin.localStorage.setItem;
  1706. const siblingFormat = this.fallbackFormatName || (format == this.flvFormatName ? this.mp4FormatName : this.flvFormatName);
  1707. const fakedRes = { 'from': 'local', 'r###lt': 'suee', 'format': 'faked_mp4', 'timelength': 10, 'accept_format': 'hdflv2,flv,hdmp4,faked_mp4,mp4', 'accept_quality': [112, 80, 64, 32, 16], 'seek_param': 'start', 'seek_type': 'second', 'durl': [{ 'order': 1, 'length': 1000, 'size': 30000, 'url': 'https://static.hdslb.com/encoding.mp4', 'backup_url': ['https://static.hdslb.com/encoding.mp4'] }] };
  1708. let pendingFormat = this.lockFormat(format);
  1709. let self = this;
  1710. let blockedRequest = await new Promise(resolve => {
  1711. jq.ajax = function (a, c) {
  1712. if (typeof c === 'object') { if (typeof a === 'string') c.url = a; a = c; c = undefined; } if (a.url.includes('interface.bilibili.com/v2/playurl?') || a.url.includes('bangumi.bilibili.com/player/web_api/v2/playurl?')) {
  1713. // Send back a fake response to enable the change-format button.
  1714. self.cidAsyncContainer.resolve(a.url.match(/cid=\d+/)[0].slice(4));
  1715. a.success(fakedRes);
  1716. self.playerWin.document.getElementsByTagName('video')[1].loop = true;
  1717. self.playerWin.document.getElementsByTagName('video')[0].addEventListener('emptied', () => resolve([a, c]), { once: true });
  1718. }
  1719. else {
  1720. return _ajax.call(jq, a, c);
  1721. }
  1722. };
  1723. this.playerWin.localStorage.setItem = () => this.playerWin.localStorage.setItem = _setItem;
  1724. this.playerWin.document.querySelector(`div.bilibili-player-video-btn-quality > div ul li[data-value="${BiliMonkey.formatToValue(siblingFormat)}"]`).click();
  1725. });
  1726. let siblingOK = siblingFormat == this.fallbackFormatName ? true : siblingFormat == this.flvFormatName ? this.flvs : this.mp4;
  1727. if (!siblingOK) {
  1728. this.lockFormat(siblingFormat);
  1729. blockedRequest[0].success = res => this.resolveFormat(res, siblingFormat);
  1730. _ajax.call(jq, ...blockedRequest);
  1731. }
  1732. jq.ajax = function (a, c) {
  1733. if (typeof c === 'object') { if (typeof a === 'string') c.url = a; a = c; c = undefined; } if (a.url.includes('interface.bilibili.com/v2/playurl?') || a.url.includes('bangumi.bilibili.com/player/web_api/v2/playurl?')) {
  1734. let _success = a.success;
  1735. a.success = res => {
  1736. if (self.proxy) {
  1737. self.resolveFormat(res, format);
  1738. if (self.flvs) self.setupProxy(res, _success);
  1739. }
  1740. else {
  1741. _success(res);
  1742. self.resolveFormat(res, format);
  1743. }
  1744. };
  1745. jq.ajax = _ajax;
  1746. }
  1747. return _ajax.call(jq, a, c);
  1748. };
  1749. this.playerWin.localStorage.setItem = () => this.playerWin.localStorage.setItem = _setItem;
  1750. this.playerWin.document.querySelector(`div.bilibili-player-video-btn-quality > div ul li[data-value="${BiliMonkey.formatToValue(format)}"]`).click();
  1751. return pendingFormat;
  1752. }
  1753. async getNonCurrentFormat(format) {
  1754. const jq = this.playerWin.jQuery;
  1755. const _ajax = jq.ajax;
  1756. const _setItem = this.playerWin.localStorage.setItem;
  1757. let pendingFormat = this.lockFormat(format);
  1758. let self = this;
  1759. jq.ajax = function (a, c) {
  1760. if (typeof c === 'object') { if (typeof a === 'string') c.url = a; a = c; c = undefined; } if (a.url.includes('interface.bilibili.com/v2/playurl?') || a.url.includes('bangumi.bilibili.com/player/web_api/v2/playurl?')) {
  1761. self.cidAsyncContainer.resolve(a.url.match(/cid=\d+/)[0].slice(4));
  1762. let _success = a.success;
  1763. _success({});
  1764. a.success = res => self.resolveFormat(res, format);
  1765. jq.ajax = _ajax;
  1766. }
  1767. return _ajax.call(jq, a, c);
  1768. };
  1769. this.playerWin.localStorage.setItem = () => this.playerWin.localStorage.setItem = _setItem;
  1770. this.playerWin.document.querySelector(`div.bilibili-player-video-btn-quality > div ul li[data-value="${BiliMonkey.formatToValue(format)}"]`).click();
  1771. return pendingFormat;
  1772. }
  1773. async getASS(clickableFormat) {
  1774. if (this.ass) return this.ass;
  1775. this.ass = new Promise(async resolve => {
  1776. // 1. cid
  1777. if (!this.cid) this.cid = await new Promise((resolve, reject) => {
  1778. clickableFormat = this.fallbackFormatName || clickableFormat;
  1779. if (!clickableFormat) reject('get ASS Error: cid unavailable, nor clickable format given.');
  1780. const jq = this.playerWin.jQuery;
  1781. const _ajax = jq.ajax;
  1782. const _setItem = this.playerWin.localStorage.setItem;
  1783. if (!this.fallbackFormatName) this.lockFormat(clickableFormat);
  1784. let self = this;
  1785. jq.ajax = function (a, c) {
  1786. if (typeof c === 'object') { if (typeof a === 'string') c.url = a; a = c; c = undefined; } if (a.url.includes('interface.bilibili.com/v2/playurl?') || a.url.includes('bangumi.bilibili.com/player/web_api/v2/playurl?')) {
  1787. resolve(self.cid = a.url.match(/cid=\d+/)[0].slice(4));
  1788. let _success = a.success;
  1789. _success({});
  1790. a.success = res => {
  1791. if (!this.fallbackFormatName) self.resolveFormat(res, clickableFormat);
  1792. };
  1793. jq.ajax = _ajax;
  1794. }
  1795. return _ajax.call(jq, a, c);
  1796. };
  1797. this.playerWin.localStorage.setItem = () => this.playerWin.localStorage.setItem = _setItem;
  1798. this.playerWin.document.querySelector(`div.bilibili-player-video-btn-quality > div ul li[data-value="${BiliMonkey.formatToValue(clickableFormat)}"]`).click();
  1799. });
  1800. // 2. options
  1801. const bilibili_player_settings = this.playerWin.localStorage.bilibili_player_settings && JSON.parse(this.playerWin.localStorage.bilibili_player_settings);
  1802. // 2.1 blocker
  1803. let danmaku = await BiliMonkey.fetchDanmaku(this.cid);
  1804. if (bilibili_player_settings && this.blocker) {
  1805. const i = bilibili_player_settings.block.list.map(e => e.v).join('|');
  1806. if (i) {
  1807. const regexp = new RegExp(i);
  1808. danmaku = danmaku.filter(e => !regexp.test(e.text));
  1809. }
  1810. }
  1811. // 2.2 font
  1812. const option = bilibili_player_settings && this.font && {
  1813. 'fontFamily': bilibili_player_settings.setting_config['fontfamily'] != 'custom' ? bilibili_player_settings.setting_config['fontfamily'].split(/, ?/) : bilibili_player_settings.setting_config['fontfamilycustom'].split(/, ?/),
  1814. 'fontSize': parseFloat(bilibili_player_settings.setting_config['fontsize']),
  1815. 'textOpacity': parseFloat(bilibili_player_settings.setting_config['opacity']),
  1816. 'bold': bilibili_player_settings.setting_config['bold'] ? 1 : 0,
  1817. } || undefined;
  1818. // 3. generate
  1819. resolve(this.ass = top.URL.createObjectURL(await new ASSConverter(option).genASSBlob(
  1820. danmaku, top.document.title, top.location.href
  1821. )));
  1822. });
  1823. return this.ass;
  1824. }
  1825. async queryInfo(format) {
  1826. return this.queryInfoMutex.lockAndAwait(async () => {
  1827. switch (format) {
  1828. case 'flv':
  1829. if (this.flvs)
  1830. return this.flvs;
  1831. else if (this.flvFormatName == 'does_not_exist')
  1832. return this.flvFormatName;
  1833. else if (this.playerWin.document.querySelector('div.bilibili-player-video-btn-quality > div ul li[data-selected]').getAttribute('data-value') == BiliMonkey.formatToValue(this.flvFormatName))
  1834. return this.getCurrentFormat(this.flvFormatName);
  1835. else
  1836. return this.getNonCurrentFormat(this.flvFormatName);
  1837. case 'mp4':
  1838. if (this.mp4)
  1839. return this.mp4;
  1840. else if (this.mp4FormatName == 'does_not_exist')
  1841. return this.mp4FormatName;
  1842. else if (this.playerWin.document.querySelector('div.bilibili-player-video-btn-quality > div ul li[data-selected]').getAttribute('data-value') == BiliMonkey.formatToValue(this.mp4FormatName))
  1843. return this.getCurrentFormat(this.mp4FormatName);
  1844. else
  1845. return this.getNonCurrentFormat(this.mp4FormatName);
  1846. case 'ass':
  1847. if (this.ass)
  1848. return this.ass;
  1849. else if (this.playerWin.document.querySelector('div.bilibili-player-video-btn-quality > div ul li[data-selected]').getAttribute('data-value') == BiliMonkey.formatToValue(this.flvFormatName))
  1850. return this.getASS(this.mp4FormatName);
  1851. else
  1852. return this.getASS(this.flvFormatName);
  1853. default:
  1854. throw `Bilimonkey: What is format ${format}?`;
  1855. }
  1856. });
  1857. }
  1858. async getPlayerButtons() {
  1859. if (this.playerWin.document.querySelector('div.bilibili-player-video-btn-quality > div ul li')) {
  1860. return this.playerWin;
  1861. }
  1862. else {
  1863. return new Promise(resolve => {
  1864. let observer = new MutationObserver(() => {
  1865. if (this.playerWin.document.querySelector('div.bilibili-player-video-btn-quality > div ul li')) {
  1866. observer.disconnect();
  1867. resolve(this.playerWin);
  1868. }
  1869. });
  1870. observer.observe(this.playerWin.document.getElementById('bilibiliPlayer'), { childList: true });
  1871. });
  1872. }
  1873. }
  1874. async hangPlayer() {
  1875. const fakedRes = { 'from': 'local', 'r###lt': 'suee', 'format': 'faked_mp4', 'timelength': 10, 'accept_format': 'hdflv2,flv,hdmp4,faked_mp4,mp4', 'accept_quality': [112, 80, 64, 32, 16], 'seek_param': 'start', 'seek_type': 'second', 'durl': [{ 'order': 1, 'length': 1000, 'size': 30000, 'url': '' }] };
  1876. const jq = this.playerWin.jQuery;
  1877. const _ajax = jq.ajax;
  1878. const _setItem = this.playerWin.localStorage.setItem;
  1879. return this.queryInfoMutex.lockAndAwait(() => new Promise(async resolve => {
  1880. let blockerTimeout;
  1881. jq.ajax = function (a, c) {
  1882. if (typeof c === 'object') { if (typeof a === 'string') c.url = a; a = c; c = undefined; } if (a.url.includes('interface.bilibili.com/v2/playurl?') || a.url.includes('bangumi.bilibili.com/player/web_api/v2/playurl?')) {
  1883. clearTimeout(blockerTimeout);
  1884. a.success(fakedRes);
  1885. blockerTimeout = setTimeout(() => {
  1886. jq.ajax = _ajax;
  1887. resolve();
  1888. }, 2500);
  1889. }
  1890. else {
  1891. return _ajax.call(jq, a, c);
  1892. }
  1893. };
  1894. this.playerWin.localStorage.setItem = () => this.playerWin.localStorage.setItem = _setItem;
  1895. let button = Array.from(this.playerWin.document.querySelector('div.bilibili-player-video-btn-quality > div ul').getElementsByTagName('li'))
  1896. .find(e => !e.getAttribute('data-selected') && e.children.length == 2);
  1897. button.click();
  1898. }));
  1899. }
  1900. async loadFLVFromCache(index) {
  1901. if (!this.cache) return;
  1902. if (!this.flvs) throw 'BiliMonkey: info uninitialized';
  1903. let name = this.flvs[index].match(/\d+-\d+(?:\d|-|hd)*\.flv/)[0];
  1904. let item = await this.cache.getData(name);
  1905. if (!item) return;
  1906. return this.flvsBlob[index] = item.data;
  1907. }
  1908. async loadPartialFLVFromCache(index) {
  1909. if (!this.cache) return;
  1910. if (!this.flvs) throw 'BiliMonkey: info uninitialized';
  1911. let name = this.flvs[index].match(/\d+-\d+(?:\d|-|hd)*\.flv/)[0];
  1912. name = 'PC_' + name;
  1913. let item = await this.cache.getData(name);
  1914. if (!item) return;
  1915. return item.data;
  1916. }
  1917. async loadAllFLVFromCache() {
  1918. if (!this.cache) return;
  1919. if (!this.flvs) throw 'BiliMonkey: info uninitialized';
  1920. let promises = [];
  1921. for (let i = 0; i < this.flvs.length; i++) promises.push(this.loadFLVFromCache(i));
  1922. return Promise.all(promises);
  1923. }
  1924. async saveFLVToCache(index, blob) {
  1925. if (!this.cache) return;
  1926. if (!this.flvs) throw 'BiliMonkey: info uninitialized';
  1927. let name = this.flvs[index].match(/\d+-\d+(?:\d|-|hd)*\.flv/)[0];
  1928. return this.cache.addData({ name, data: blob });
  1929. }
  1930. async savePartialFLVToCache(index, blob) {
  1931. if (!this.cache) return;
  1932. if (!this.flvs) throw 'BiliMonkey: info uninitialized';
  1933. let name = this.flvs[index].match(/\d+-\d+(?:\d|-|hd)*\.flv/)[0];
  1934. name = 'PC_' + name;
  1935. return this.cache.putData({ name, data: blob });
  1936. }
  1937. async cleanPartialFLVInCache(index) {
  1938. if (!this.cache) return;
  1939. if (!this.flvs) throw 'BiliMonkey: info uninitialized';
  1940. let name = this.flvs[index].match(/\d+-\d+(?:\d|-|hd)*\.flv/)[0];
  1941. name = 'PC_' + name;
  1942. return this.cache.deleteData(name);
  1943. }
  1944. async getFLV(index, progressHandler) {
  1945. if (this.flvsBlob[index]) return this.flvsBlob[index];
  1946. if (!this.flvs) throw 'BiliMonkey: info uninitialized';
  1947. this.flvsBlob[index] = (async () => {
  1948. let cache = await this.loadFLVFromCache(index);
  1949. if (cache) return this.flvsBlob[index] = cache;
  1950. let partialFLVFromCache = await this.loadPartialFLVFromCache(index);
  1951. let burl = this.flvs[index];
  1952. if (partialFLVFromCache) burl += `&bstart=${partialFLVFromCache.size}`;
  1953. let opt = {
  1954. fetch: this.playerWin.fetch,
  1955. method: 'GET',
  1956. mode: 'cors',
  1957. cache: 'default',
  1958. referrerPolicy: 'no-referrer-when-downgrade',
  1959. cacheLoaded: partialFLVFromCache ? partialFLVFromCache.size : 0,
  1960. headers: partialFLVFromCache && (!burl.includes('wsTime')) ? { Range: `bytes=${partialFLVFromCache.size}-` } : undefined
  1961. };
  1962. opt.onprogress = progressHandler;
  1963. opt.onerror = opt.onabort = ({ target, type }) => {
  1964. let partialFLV = target.getPartialBlob();
  1965. if (partialFLVFromCache) partialFLV = new Blob([partialFLVFromCache, partialFLV]);
  1966. this.savePartialFLVToCache(index, partialFLV);
  1967. };
  1968. let fch = new DetailedFetchBlob(burl, opt);
  1969. this.flvsDetailedFetch[index] = fch;
  1970. let fullFLV = await fch.getBlob();
  1971. this.flvsDetailedFetch[index] = undefined;
  1972. if (partialFLVFromCache) {
  1973. fullFLV = new Blob([partialFLVFromCache, fullFLV]);
  1974. this.cleanPartialFLVInCache(index);
  1975. }
  1976. this.saveFLVToCache(index, fullFLV);
  1977. return (this.flvsBlob[index] = fullFLV);
  1978. })();
  1979. return this.flvsBlob[index];
  1980. }
  1981. async abortFLV(index) {
  1982. if (this.flvsDetailedFetch[index]) return this.flvsDetailedFetch[index].abort();
  1983. }
  1984. async getAllFLVs(progressHandler) {
  1985. if (!this.flvs) throw 'BiliMonkey: info uninitialized';
  1986. let promises = [];
  1987. for (let i = 0; i < this.flvs.length; i++) promises.push(this.getFLV(i, progressHandler));
  1988. return Promise.all(promises);
  1989. }
  1990. async cleanAllFLVsInCache() {
  1991. if (!this.cache) return;
  1992. if (!this.flvs) throw 'BiliMonkey: info uninitialized';
  1993. let ret = [];
  1994. for (let flv of this.flvs) {
  1995. let name = flv.match(/\d+-\d+(?:\d|-|hd)*\.flv/)[0];
  1996. ret.push(await this.cache.deleteData(name));
  1997. ret.push(await this.cache.deleteData('PC_' + name));
  1998. }
  1999. return ret;
  2000. }
  2001. async setupProxy(res, onsuccess) {
  2002. if (!this.setupProxy._fetch) {
  2003. const _fetch = this.setupProxy._fetch = this.playerWin.fetch;
  2004. this.playerWin.fetch = function (input, init) {
  2005. if (!input.slice || input.slice(0, 5) != 'blob:') {
  2006. return _fetch(input, init);
  2007. }
  2008. let bstart = input.indexOf('?bstart=');
  2009. if (bstart < 0) {
  2010. return _fetch(input, init);
  2011. }
  2012. if (!init.headers instanceof Headers) init.headers = new Headers(init.headers || {});
  2013. init.headers.set('Range', `bytes=${input.slice(bstart + 8)}-`);
  2014. return _fetch(input.slice(0, bstart), init)
  2015. };
  2016. this.destroy.addCallback(() => this.playerWin.fetch = _fetch);
  2017. }
  2018. await this.loadAllFLVFromCache();
  2019. let resProxy = Object.assign({}, res);
  2020. for (let i = 0; i < this.flvsBlob.length; i++) {
  2021. if (this.flvsBlob[i]) resProxy.durl[i].url = this.playerWin.URL.createObjectURL(this.flvsBlob[i]);
  2022. }
  2023. return onsuccess(resProxy);
  2024. }
  2025. static async fetchDanmaku(cid) {
  2026. return ASSConverter.parseXML(
  2027. await new Promise((resolve, reject) => {
  2028. const e = new XMLHttpRequest();
  2029. e.onload = () => resolve(e.responseText);
  2030. e.onerror = reject;
  2031. e.open('get', `https://comment.bilibili.com/${cid}.xml`, );
  2032. e.send();
  2033. })
  2034. );
  2035. }
  2036. static async getAllPageDefaultFormats(playerWin = top) {
  2037. const jq = playerWin.jQuery;
  2038. const _ajax = jq.ajax;
  2039. // 1. mutex => you must send requests one by one
  2040. const queryInfoMutex = new Mutex();
  2041. // 2. bilibili has a misconfigured lazy loading => keep trying
  2042. const list = await new Promise(resolve => {
  2043. const i = setInterval(() => {
  2044. const ret = playerWin.player.getPlaylist();
  2045. if (ret) {
  2046. clearInterval(i);
  2047. resolve(ret);
  2048. }
  2049. }, 500);
  2050. });
  2051. // 3. build {cid: information} dict
  2052. const index = list.reduce((acc, cur) => { acc[cur.cid] = cur; return acc }, {});
  2053. // 4. find where to stop
  2054. const end = list[list.length - 1].cid.toString();
  2055. // 5. collect information
  2056. const ret = [];
  2057. jq.ajax = function (a, c) {
  2058. if (typeof c === 'object') { if (typeof a === 'string') c.url = a; a = c; c = undefined; } if (a.url.includes('comment.bilibili.com') || a.url.includes('interface.bilibili.com/player?') || a.url.includes('api.bilibili.com/x/player/playurl/token')) return _ajax.call(jq, a, c);
  2059. if (a.url.includes('interface.bilibili.com/v2/playurl?') || a.url.includes('bangumi.bilibili.com/player/web_api/v2/playurl?')) {
  2060. (async () => {
  2061. // 5.1 suppress success handler
  2062. a.success = undefined;
  2063. // 5.2 find cid
  2064. const cid = a.url.match(/cid=\d+/)[0].slice(4);
  2065. // 5.3 grab information
  2066. const [danmuku, res] = await Promise.all([
  2067. // 5.3.1 grab danmuku
  2068. (async () => top.URL.createObjectURL(await new ASSConverter().genASSBlob(
  2069. await BiliMonkey.fetchDanmaku(cid), top.document.title, top.location.href
  2070. )))(),
  2071. // 5.3.2 grab download res
  2072. _ajax.call(jq, a, c)
  2073. ]);
  2074. // 5.4 save information
  2075. ret.push({
  2076. durl: res.durl.map(({ url }) => url.replace('http:', playerWin.location.protocol)),
  2077. danmuku,
  2078. name: index[cid].part || index[cid].index,
  2079. outputName: res.durl[0].url.match(/\d+-\d+(?:\d|-|hd)*(?=\.flv)/) ?
  2080. /***
  2081. * see #28
  2082. * Firefox lookbehind assertion not implemented https://bugzilla.mozilla.org/show_bug.cgi?id=1225665
  2083. * try replace /-\d+(?=(?:\d|-|hd)*\.flv)/ => /(?<=\d+)-\d+(?=(?:\d|-|hd)*\.flv)/ in the future
  2084. */
  2085. res.durl[0].url.match(/\d+-\d+(?:\d|-|hd)*(?=\.flv)/)[0].replace(/-\d+(?=(?:\d|-|hd)*\.flv)/, '')
  2086. : res.durl[0].url.match(/\d(?:\d|-|hd)*(?=\.mp4)/) ?
  2087. res.durl[0].url.match(/\d(?:\d|-|hd)*(?=\.mp4)/)[0]
  2088. : cid,
  2089. cid,
  2090. res,
  2091. });
  2092. // 5.5 finish job
  2093. queryInfoMutex.unlock();
  2094. })();
  2095. }
  2096. return _ajax.call(jq, { url: '//0.0.0.0' });
  2097. };
  2098. // 6.1 from the first page
  2099. await queryInfoMutex.lock();
  2100. playerWin.player.next(1);
  2101. while (1) {
  2102. // 6.2 to the last page
  2103. await queryInfoMutex.lock();
  2104. if (ret[ret.length - 1].cid == end) break;
  2105. playerWin.player.next();
  2106. }
  2107. return ret;
  2108. }
  2109. static formatToValue(format) {
  2110. if (format == 'does_not_exist') throw `formatToValue: cannot lookup does_not_exist`;
  2111. if (typeof BiliMonkey.formatToValue.dict == 'undefined') BiliMonkey.formatToValue.dict = {
  2112. 'flv_p60': '116',
  2113. 'flv720_p60': '74',
  2114. 'flv': '80',
  2115. 'flv720': '64',
  2116. 'flv480': '32',
  2117. 'flv360': '15',
  2118. // legacy - late 2017
  2119. 'hdflv2': '112',
  2120. 'hdmp4': '64', // data-value is still '64' instead of '48'. '48',
  2121. 'mp4': '16',
  2122. };
  2123. return BiliMonkey.formatToValue.dict[format] || null;
  2124. }
  2125. static valueToFormat(value) {
  2126. if (typeof BiliMonkey.valueToFormat.dict == 'undefined') BiliMonkey.valueToFormat.dict = {
  2127. '116': 'flv_p60',
  2128. '74': 'flv720_p60',
  2129. '80': 'flv',
  2130. '64': 'flv720',
  2131. '32': 'flv480',
  2132. '15': 'flv360',
  2133. // legacy - late 2017
  2134. '112': 'hdflv2',
  2135. '48': 'hdmp4',
  2136. '16': 'mp4',
  2137. // legacy - early 2017
  2138. '3': 'flv',
  2139. '2': 'hdmp4',
  2140. '1': 'mp4',
  2141. };
  2142. return BiliMonkey.valueToFormat.dict[value] || null;
  2143. }
  2144. static get optionDescriptions() {
  2145. return [
  2146. // 1. automation
  2147. ['autoDefault', '尝试自动抓取:不会拖慢页面,抓取默认清晰度,但可能抓不到。'],
  2148. ['autoFLV', '强制自动抓取FLV:会拖慢页面,如果默认清晰度也是超清会更慢,但保证抓到。'],
  2149. ['autoMP4', '强制自动抓取MP4:会拖慢页面,如果默认清晰度也是高清会更慢,但保证抓到。'],
  2150. // 2. cache
  2151. ['cache', '关标签页不清缓存:保留完全下载好的分段到缓存,忘记另存为也没关系。'],
  2152. ['partial', '断点续传:点击“取消”保留部分下载的分段到缓存,忘记点击会弹窗确认。'],
  2153. ['proxy', '用缓存加速播放器:如果缓存里有完全下载好的分段,直接喂给网页播放器,不重新访问网络。小水管利器,播放只需500k流量。如果实在搞不清怎么播放ASS弹幕,也可以就这样用。'],
  2154. // 3. customizing
  2155. ['blocker', '弹幕过滤:在网页播放器里设置的屏蔽词也对下载的弹幕生效。'],
  2156. ['font', '自定义字体:在网页播放器里设置的字体、大小、加粗、透明度也对下载的弹幕生效。']
  2157. ];
  2158. }
  2159. static get optionDefaults() {
  2160. return {
  2161. // 1. automation
  2162. autoDefault: true,
  2163. autoFLV: false,
  2164. autoMP4: false,
  2165. // 2. cache
  2166. cache: true,
  2167. partial: true,
  2168. proxy: true,
  2169. // 3. customizing
  2170. blocker: true,
  2171. font: true,
  2172. }
  2173. }
  2174. static _UNIT_TEST() {
  2175. return (async () => {
  2176. let playerWin = await BiliUserJS.getPlayerWin();
  2177. window.m = new BiliMonkey(playerWin);
  2178. console.warn('sniffDefaultFormat test');
  2179. await m.sniffDefaultFormat();
  2180. console.log(m);
  2181. console.warn('data race test');
  2182. m.queryInfo('mp4');
  2183. console.log(m.queryInfo('mp4'));
  2184. console.warn('getNonCurrentFormat test');
  2185. console.log(await m.queryInfo('mp4'));
  2186. console.warn('getCurrentFormat test');
  2187. console.log(await m.queryInfo('flv'));
  2188. //location.reload();
  2189. })();
  2190. }
  2191. }
  2192. /***
  2193. * BiliPolyfill
  2194. * A bilibili user script
  2195. * Copyright (C) 2018 Qli5. All Rights Reserved.
  2196. *
  2197. * @author qli5 <goodlq11[at](163|gmail).com>
  2198. *
  2199. * This Source Code Form is subject to the terms of the Mozilla Public
  2200. * License, v. 2.0. If a copy of the MPL was not distributed with this
  2201. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  2202. */
  2203. class BiliPolyfill {
  2204. /***
  2205. * Assumption: aid, cid, pageno does not change during lifecycle
  2206. * Create a new BiliPolyfill if assumption breaks
  2207. */
  2208. constructor(playerWin, option = BiliPolyfill.optionDefaults, hintInfo = () => { }) {
  2209. this.playerWin = playerWin;
  2210. this.option = option;
  2211. this.hintInfo = hintInfo;
  2212. this.video = null;
  2213. this.series = [];
  2214. this.userdata = { oped: {}, restore: {} };
  2215. this.destroy = new HookedFunction();
  2216. this.playerWin.addEventListener('beforeunload', this.destroy);
  2217. this.destroy.addCallback(() => this.playerWin.removeEventListener('beforeunload', this.destroy));
  2218. }
  2219. saveUserdata() {
  2220. this.option.setStorage('biliPolyfill', JSON.stringify(this.userdata));
  2221. }
  2222. retrieveUserdata() {
  2223. try {
  2224. this.userdata = this.option.getStorage('biliPolyfill');
  2225. if (this.userdata.length > 1073741824) top.alert('BiliPolyfill脚本数据已经快满了,在播放器上右键->BiliPolyfill->片头片尾->检视数据,删掉一些吧。');
  2226. this.userdata = JSON.parse(this.userdata);
  2227. }
  2228. catch (e) { }
  2229. finally {
  2230. if (!this.userdata) this.userdata = {};
  2231. if (!(this.userdata.oped instanceof Object)) this.userdata.oped = {};
  2232. if (!(this.userdata.restore instanceof Object)) this.userdata.restore = {};
  2233. }
  2234. }
  2235. async setFunctions({ videoRefresh = false } = {}) {
  2236. // 1. initialize
  2237. this.video = await this.getPlayerVideo();
  2238. // 2. if not enabled, run the process without real actions
  2239. if (!this.option.betabeta) return this.getPlayerMenu();
  2240. // 3. set up functions that are cid static
  2241. if (!videoRefresh) {
  2242. this.retrieveUserdata();
  2243. if (this.option.badgeWatchLater) this.badgeWatchLater();
  2244. if (this.option.scroll) this.scrollToPlayer();
  2245. if (this.option.series) this.inferNextInSeries();
  2246. if (this.option.recommend) this.showRecommendTab();
  2247. if (this.option.focus) this.focusOnPlayer();
  2248. if (this.option.restorePrevent) this.restorePreventShade();
  2249. if (this.option.restoreDanmuku) this.restoreDanmukuSwitch();
  2250. if (this.option.restoreSpeed) this.restoreSpeed();
  2251. if (this.option.restoreWide) this.restoreWideScreen();
  2252. if (this.option.autoR###me) this.autoR###me();
  2253. if (this.option.autoPlay) this.autoPlay();
  2254. if (this.option.autoFullScreen) this.autoFullScreen();
  2255. if (this.option.limitedKeydown) this.limitedKeydownFullScreenPlay();
  2256. this.destroy.addCallback(() => this.saveUserdata());
  2257. }
  2258. // 4. set up functions that are binded to the video DOM
  2259. if (this.option.dblclick) this.dblclickFullScreen();
  2260. if (this.option.electric) this.reallocateElectricPanel();
  2261. if (this.option.oped) this.skipOPED();
  2262. this.video.addEventListener('emptied', () => this.setFunctions({ videoRefresh: true }), { once: true });
  2263. // 5. set up functions that require everything to be ready
  2264. await this.getPlayerMenu();
  2265. if (this.option.menuFocus) this.menuFocusOnPlayer();
  2266. // 6. set up experimental functions
  2267. if (this.option.speech) top.document.body.addEventListener('click', e => e.detail > 2 && this.speechRecognition());
  2268. }
  2269. async inferNextInSeries() {
  2270. // 1. find current title
  2271. const title = top.document.getElementsByTagName('h1')[0].textContent.replace(/\(\d+\)$/, '').trim();
  2272. // 2. find current ep number
  2273. const ep = title.match(/\d+(?=[^\d]*$)/);
  2274. if (!ep) return this.series = [];
  2275. // 3. current title - current ep number => series common title
  2276. const seriesTitle = title.slice(0, title.lastIndexOf(ep)).trim();
  2277. // 4. find sibling ep number
  2278. const epNumber = parseInt(ep);
  2279. const epSibling = ep[0] == '0' ?
  2280. [(epNumber - 1).toString().padStart(ep.length, '0'), (epNumber + 1).toString().padStart(ep.length, '0')] :
  2281. [(epNumber - 1).toString(), (epNumber + 1).toString()];
  2282. // 5. build search keywords
  2283. // [self, seriesTitle + epSibling, epSibling]
  2284. const keywords = [title, ...epSibling.map(e => seriesTitle + e), ...epSibling];
  2285. // 6. find mid
  2286. const midParent = top.document.getElementById('r-info-rank') || top.document.querySelector('.user');
  2287. if (!midParent) return this.series = [];
  2288. const mid = midParent.children[0].href.match(/\d+/)[0];
  2289. // 7. fetch query
  2290. const vlist = await Promise.all(keywords.map(keyword => new Promise((resolve, reject) => {
  2291. const req = new XMLHttpRequest();
  2292. req.onload = () => resolve((req.response.status && req.response.data.vlist) || []);
  2293. req.onerror = reject;
  2294. req.open('get', `https://space.bilibili.com/ajax/member/getSubmitVideos?mid=${mid}&keyword=${keyword}`);
  2295. req.responseType = 'json';
  2296. req.send();
  2297. })));
  2298. // 8. verify current video exists
  2299. vlist[0] = vlist[0].filter(e => e.title == title);
  2300. if (!vlist[0][0]) { console && console.warn('BiliPolyfill: inferNextInSeries: cannot find current video in mid space'); return this.series = []; }
  2301. // 9. if seriesTitle + epSibling qurey has reasonable r###lts => pick
  2302. this.series = [vlist[1].find(e => e.created < vlist[0][0].created), vlist[2].reverse().find(e => e.created > vlist[0][0].created)];
  2303. // 10. fallback: if epSibling qurey has reasonable r###lts => pick
  2304. if (!this.series[0]) this.series[0] = vlist[3].find(e => e.created < vlist[0][0].created);
  2305. if (!this.series[1]) this.series[1] = vlist[4].reverse().find(e => e.created > vlist[0][0].created);
  2306. return this.series;
  2307. }
  2308. badgeWatchLater() {
  2309. // 1. find watchlater button
  2310. const li = top.document.getElementById('i_menu_watchLater_btn') || top.document.getElementById('i_menu_later_btn') || top.document.querySelector('li.nav-item[report-id=playpage_watchlater]');
  2311. if (!li) return;
  2312. // 2. initialize watchlater panel
  2313. const observer = new MutationObserver(() => {
  2314. // 3. hide watchlater panel
  2315. observer.disconnect();
  2316. li.children[1].style.visibility = 'hidden';
  2317. // 4. loading => wait
  2318. if (li.children[1].children[0].children[0].className == 'm-w-loading') {
  2319. const observer = new MutationObserver(() => {
  2320. // 5. clean up watchlater panel
  2321. observer.disconnect();
  2322. li.dispatchEvent(new Event('mouseleave'));
  2323. setTimeout(() => li.children[1].style.visibility = '', 700);
  2324. // 6.1 empty list => do nothing
  2325. if (li.children[1].children[0].children[0].className == 'no-data') return;
  2326. // 6.2 otherwise => append div
  2327. const div = top.document.createElement('div');
  2328. div.className = 'num';
  2329. if (li.children[1].children[0].children[0].children.length > 5) {
  2330. div.textContent = '5+';
  2331. }
  2332. else {
  2333. div.textContent = li.children[1].children[0].children[0].children.length;
  2334. }
  2335. li.children[0].append(div);
  2336. this.destroy.addCallback(() => div.remove());
  2337. });
  2338. observer.observe(li.children[1].children[0], { childList: true });
  2339. }
  2340. // 4.2 otherwise => error
  2341. else {
  2342. throw 'badgeWatchLater: cannot find m-w-loading panel';
  2343. }
  2344. });
  2345. observer.observe(li, { childList: true });
  2346. li.dispatchEvent(new Event('mouseenter'));
  2347. }
  2348. dblclickFullScreen() {
  2349. this.video.addEventListener('dblclick', () =>
  2350. this.playerWin.document.querySelector('#bilibiliPlayer div.bilibili-player-video-btn-fullscreen').click()
  2351. );
  2352. }
  2353. scrollToPlayer() {
  2354. if (top.scrollY < 200) top.document.getElementById('bofqi').scrollIntoView();
  2355. }
  2356. showRecommendTab() {
  2357. const h = this.playerWin.document.querySelector('#bilibiliPlayer div.bilibili-player-filter-btn-recommend');
  2358. if (h) h.click();
  2359. }
  2360. getCoverImage() {
  2361. // 1. search for img tag
  2362. const img = top.document.querySelector('.cover_image')
  2363. || top.document.querySelector('div.info-cover > a > img')
  2364. || top.document.querySelector('[data-state-play="true"] img');
  2365. // 2. search for ld+jason
  2366. const script = top.document.querySelector('script[type="application/ld+json"]');
  2367. // 3. find src
  2368. let ret = (img && img.src) || (script && JSON.parse(script.textContent).images[0]);
  2369. if (!ret) return null;
  2370. // 4. trim parameters
  2371. let i;
  2372. i = ret.indexOf('.jpg');
  2373. if (i != -1) ret = ret.slice(0, i + 4);
  2374. i = ret.indexOf('.png');
  2375. if (i != -1) ret = ret.slice(0, i + 4);
  2376. return ret;
  2377. }
  2378. reallocateElectricPanel() {
  2379. // 1. autopart == wait => ok
  2380. if (!this.playerWin.localStorage.bilibili_player_settings) return;
  2381. if (!this.playerWin.localStorage.bilibili_player_settings.includes('"autopart":1') && !this.option.electricSkippable) return;
  2382. // 2. wait for electric panel
  2383. this.video.addEventListener('ended', () => {
  2384. setTimeout(() => {
  2385. // 3. click skip
  2386. const electricPanel = this.playerWin.document.getElementsByClassName('bilibili-player-electric-panel')[0];
  2387. if (!electricPanel) return;
  2388. electricPanel.children[2].click();
  2389. // 4. but display a fake electric panel
  2390. electricPanel.style.display = 'block';
  2391. electricPanel.style.zIndex = 233;
  2392. // 5. and perform a fake countdown
  2393. let countdown = 5;
  2394. const h = setInterval(() => {
  2395. // 5.1 yield to next part hint
  2396. if (this.playerWin.document.getElementsByClassName('bilibili-player-video-toast-item-jump')[0]) electricPanel.style.zIndex = '';
  2397. // 5.2 countdown > 0 => update textContent
  2398. if (countdown > 0) {
  2399. electricPanel.children[2].children[0].textContent = `0${countdown}`;
  2400. countdown--;
  2401. }
  2402. // 5.3 countdown == 0 => clean up
  2403. else {
  2404. clearInterval(h);
  2405. electricPanel.remove();
  2406. }
  2407. }, 1000);
  2408. }, 0);
  2409. }, { once: true });
  2410. }
  2411. /**
  2412. * As of March 2018:
  2413. * opacity:
  2414. * bilibili_player_settings.setting_config.opacity
  2415. * persist :)
  2416. * preventshade:
  2417. * bilibili_player_settings.setting_config.preventshade
  2418. * will be overwritten
  2419. * bilibili has a broken setting roaming scheme where the preventshade default is always used
  2420. * type_bottom, type_scroll, type_top:
  2421. * bilibili_player_settings.setting_config.type_(bottom|scroll|top)
  2422. * sessionStorage ONLY
  2423. * not sure if it is a feature or a bug
  2424. * danmaku switch:
  2425. * not stored
  2426. * videospeed:
  2427. * bilibili_player_settings.video_status.videospeed
  2428. * sessionStorage ONLY
  2429. * same as above
  2430. * widescreen:
  2431. * same as above
  2432. */
  2433. restorePreventShade() {
  2434. // 1. restore option should be an array
  2435. if (!Array.isArray(this.userdata.restore.preventShade)) this.userdata.restore.preventShade = [];
  2436. // 2. find corresponding option index
  2437. const index = top.location.href.includes('bangumi') ? 0 : 1;
  2438. // 3. MUST initialize setting panel before click
  2439. this.playerWin.document.getElementsByClassName('bilibili-player-video-btn-danmaku')[0].dispatchEvent(new Event('mouseover'));
  2440. // 4. restore if true
  2441. const input = this.playerWin.document.getElementsByName('ctlbar_danmuku_prevent')[0];
  2442. if (this.userdata.restore.preventShade[index] && !input.nextElementSibling.classList.contains('bpui-state-active')) {
  2443. input.click();
  2444. }
  2445. // 5. clean up setting panel
  2446. this.playerWin.document.getElementsByClassName('bilibili-player-video-btn-danmaku')[0].dispatchEvent(new Event('mouseout'));
  2447. // 6. memorize option
  2448. this.destroy.addCallback(() => {
  2449. this.userdata.restore.preventShade[index] = input.nextElementSibling.classList.contains('bpui-state-active');
  2450. });
  2451. }
  2452. restoreDanmukuSwitch() {
  2453. // 1. restore option should be an array
  2454. if (!Array.isArray(this.userdata.restore.danmukuSwitch)) this.userdata.restore.danmukuSwitch = [];
  2455. if (!Array.isArray(this.userdata.restore.danmukuTopSwitch)) this.userdata.restore.danmukuTopSwitch = [];
  2456. if (!Array.isArray(this.userdata.restore.danmukuBottomSwitch)) this.userdata.restore.danmukuBottomSwitch = [];
  2457. if (!Array.isArray(this.userdata.restore.danmukuScrollSwitch)) this.userdata.restore.danmukuScrollSwitch = [];
  2458. // 2. find corresponding option index
  2459. const index = top.location.href.includes('bangumi') ? 0 : 1;
  2460. // 3. MUST initialize setting panel before click
  2461. this.playerWin.document.getElementsByClassName('bilibili-player-video-btn-danmaku')[0].dispatchEvent(new Event('mouseover'));
  2462. // 4. restore if true
  2463. // 4.1 danmukuSwitch
  2464. const danmukuSwitchDiv = this.playerWin.document.getElementsByClassName('bilibili-player-video-btn-danmaku')[0];
  2465. if (this.userdata.restore.danmukuSwitch[index] && !danmukuSwitchDiv.classList.contains('video-state-danmaku-off')) {
  2466. danmukuSwitchDiv.click();
  2467. }
  2468. // 4.2 danmukuTopSwitch danmukuBottomSwitch danmukuScrollSwitch
  2469. const [danmukuTopSwitchDiv, danmukuBottomSwitchDiv, danmukuScrollSwitchDiv] = this.playerWin.document.getElementsByClassName('bilibili-player-danmaku-setting-lite-type-list')[0].children;
  2470. if (this.userdata.restore.danmukuTopSwitch[index] && !danmukuTopSwitchDiv.classList.contains('disabled')) {
  2471. danmukuTopSwitchDiv.click();
  2472. }
  2473. if (this.userdata.restore.danmukuBottomSwitch[index] && !danmukuBottomSwitchDiv.classList.contains('disabled')) {
  2474. danmukuBottomSwitchDiv.click();
  2475. }
  2476. if (this.userdata.restore.danmukuScrollSwitch[index] && !danmukuScrollSwitchDiv.classList.contains('disabled')) {
  2477. danmukuScrollSwitchDiv.click();
  2478. }
  2479. // 5. clean up setting panel
  2480. this.playerWin.document.getElementsByClassName('bilibili-player-video-btn-danmaku')[0].dispatchEvent(new Event('mouseout'));
  2481. // 6. memorize final option
  2482. this.destroy.addCallback(() => {
  2483. this.userdata.restore.danmukuSwitch[index] = danmukuSwitchDiv.classList.contains('video-state-danmaku-off');
  2484. this.userdata.restore.danmukuTopSwitch[index] = danmukuTopSwitchDiv.classList.contains('disabled');
  2485. this.userdata.restore.danmukuBottomSwitch[index] = danmukuBottomSwitchDiv.classList.contains('disabled');
  2486. this.userdata.restore.danmukuScrollSwitch[index] = danmukuScrollSwitchDiv.classList.contains('disabled');
  2487. });
  2488. }
  2489. restoreSpeed() {
  2490. // 1. restore option should be an array
  2491. if (!Array.isArray(this.userdata.restore.speed)) this.userdata.restore.speed = [];
  2492. // 2. find corresponding option index
  2493. const index = top.location.href.includes('bangumi') ? 0 : 1;
  2494. // 3. restore if different
  2495. if (this.userdata.restore.speed[index] && this.userdata.restore.speed[index] != this.video.playbackRate) {
  2496. this.video.playbackRate = this.userdata.restore.speed[index];
  2497. }
  2498. // 4. memorize option
  2499. this.destroy.addCallback(() => {
  2500. this.userdata.restore.speed[index] = this.video.playbackRate;
  2501. });
  2502. }
  2503. restoreWideScreen() {
  2504. // 1. restore option should be an array
  2505. if (!Array.isArray(this.userdata.restore.wideScreen)) this.userdata.restore.wideScreen = [];
  2506. // 2. find corresponding option index
  2507. const index = top.location.href.includes('bangumi') ? 0 : 1;
  2508. // 3. restore if different
  2509. const i = this.playerWin.document.getElementsByClassName('bilibili-player-iconfont-widescreen')[0];
  2510. if (this.userdata.restore.wideScreen[index] && !i.classList.contains('icon-24wideon')) {
  2511. i.click();
  2512. }
  2513. // 4. memorize option
  2514. this.destroy.addCallback(() => {
  2515. this.userdata.restore.wideScreen[index] = i.classList.contains('icon-24wideon');
  2516. });
  2517. }
  2518. loadOffin###btitles() {
  2519. // NO. NOBODY WILL NEED THIS。
  2520. // Hint: https://github.com/jamiees2/ass-to-vtt
  2521. throw 'Not implemented';
  2522. }
  2523. autoR###me() {
  2524. // 1. wait for canplay => wait for r###me popup
  2525. const h = () => {
  2526. // 2. parse r###me popup
  2527. const span = this.playerWin.document.querySelector('#bilibiliPlayer div.bilibili-player-video-toast-bottom div.bilibili-player-video-toast-item-text span:nth-child(2)');
  2528. if (!span) return;
  2529. const [min, sec] = span.textContent.split(':');
  2530. if (!min || !sec) return;
  2531. // 3. parse last playback progress
  2532. const time = parseInt(min) * 60 + parseInt(sec);
  2533. // 3.1 still far from end => reasonable to r###me => click
  2534. if (time < this.video.duration - 10) {
  2535. // 3.1.1 already playing => no need to pause => simply jump
  2536. if (!this.video.paused || this.video.autoplay) {
  2537. this.playerWin.document.querySelector('#bilibiliPlayer div.bilibili-player-video-toast-bottom div.bilibili-player-video-toast-item-jump').click();
  2538. }
  2539. // 3.1.2 paused => should remain paused after jump => hook video.play
  2540. else {
  2541. const play = this.video.play;
  2542. this.video.play = () => setTimeout(() => {
  2543. this.playerWin.document.querySelector('#bilibiliPlayer div.bilibili-player-video-btn-start').click();
  2544. this.video.play = play;
  2545. }, 0);
  2546. this.playerWin.document.querySelector('#bilibiliPlayer div.bilibili-player-video-toast-bottom div.bilibili-player-video-toast-item-jump').click();
  2547. }
  2548. }
  2549. // 3.2 near end => silent popup
  2550. else {
  2551. this.playerWin.document.querySelector('#bilibiliPlayer div.bilibili-player-video-toast-bottom div.bilibili-player-video-toast-item-close').click();
  2552. this.playerWin.document.querySelector('#bilibiliPlayer div.bilibili-player-video-toast-bottom').children[0].style.visibility = 'hidden';
  2553. }
  2554. };
  2555. this.video.addEventListener('canplay', h, { once: true });
  2556. setTimeout(() => this.video && this.video.removeEventListener && this.video.removeEventListener('canplay', h), 3000);
  2557. }
  2558. autoPlay() {
  2559. this.video.autoplay = true;
  2560. setTimeout(() => {
  2561. if (this.video.paused) this.playerWin.document.querySelector('#bilibiliPlayer div.bilibili-player-video-btn-start').click();
  2562. }, 0);
  2563. }
  2564. autoFullScreen() {
  2565. if (this.playerWin.document.querySelector('#bilibiliPlayer div.video-state-fullscreen-off'))
  2566. this.playerWin.document.querySelector('#bilibiliPlayer div.bilibili-player-video-btn-fullscreen').click();
  2567. }
  2568. getCollectionId() {
  2569. return (top.location.pathname.match(/av\d+/) || top.location.hash.match(/av\d+/) || top.document.querySelector('div.bangumi-info a').href).toString();
  2570. }
  2571. markOPEDPosition(index) {
  2572. const collectionId = this.getCollectionId();
  2573. if (!Array.isArray(this.userdata.oped[collectionId])) this.userdata.oped[collectionId] = [];
  2574. this.userdata.oped[collectionId][index] = this.video.currentTime;
  2575. }
  2576. clearOPEDPosition() {
  2577. const collectionId = this.getCollectionId();
  2578. this.userdata.oped[collectionId] = undefined;
  2579. }
  2580. skipOPED() {
  2581. // 1. find corresponding userdata
  2582. const collectionId = this.getCollectionId();
  2583. if (!Array.isArray(this.userdata.oped[collectionId]) || !this.userdata.oped[collectionId].length) return;
  2584. /**
  2585. * structure:
  2586. * listen for time update -> || <- skip -> || <- remove event listenner
  2587. */
  2588. // 2. | 0 <- opening -> oped[collectionId][1] | <- play --
  2589. if (!this.userdata.oped[collectionId][0] && this.userdata.oped[collectionId][1]) {
  2590. const h = () => {
  2591. if (this.video.currentTime >= this.userdata.oped[collectionId][1] - 1) {
  2592. this.video.removeEventListener('timeupdate', h);
  2593. }
  2594. else {
  2595. this.video.currentTime = this.userdata.oped[collectionId][1];
  2596. this.hintInfo('BiliPolyfill: 已跳过片头');
  2597. }
  2598. };
  2599. this.video.addEventListener('timeupdate', h);
  2600. }
  2601. // 3. | <- play -> | oped[collectionId][0] <- opening -> oped[collectionId][1] | <- play --
  2602. if (this.userdata.oped[collectionId][0] && this.userdata.oped[collectionId][1]) {
  2603. const h = () => {
  2604. if (this.video.currentTime >= this.userdata.oped[collectionId][1] - 1) {
  2605. this.video.removeEventListener('timeupdate', h);
  2606. }
  2607. else if (this.video.currentTime > this.userdata.oped[collectionId][0]) {
  2608. this.video.currentTime = this.userdata.oped[collectionId][1];
  2609. this.hintInfo('BiliPolyfill: 已跳过片头');
  2610. }
  2611. };
  2612. this.video.addEventListener('timeupdate', h);
  2613. }
  2614. // 4. -- play -> | oped[collectionId][2] <- ending -> end |
  2615. if (this.userdata.oped[collectionId][2] && !this.userdata.oped[collectionId][3]) {
  2616. const h = () => {
  2617. if (this.video.currentTime >= this.video.duration - 1) {
  2618. this.video.removeEventListener('timeupdate', h);
  2619. }
  2620. else if (this.video.currentTime > this.userdata.oped[collectionId][2]) {
  2621. this.video.currentTime = this.video.duration;
  2622. this.hintInfo('BiliPolyfill: 已跳过片尾');
  2623. }
  2624. };
  2625. this.video.addEventListener('timeupdate', h);
  2626. }
  2627. // 5.-- play -> | oped[collectionId][2] <- ending -> oped[collectionId][3] | <- play -> end |
  2628. if (this.userdata.oped[collectionId][2] && this.userdata.oped[collectionId][3]) {
  2629. const h = () => {
  2630. if (this.video.currentTime >= this.userdata.oped[collectionId][3] - 1) {
  2631. this.video.removeEventListener('timeupdate', h);
  2632. }
  2633. else if (this.video.currentTime > this.userdata.oped[collectionId][2]) {
  2634. this.video.currentTime = this.userdata.oped[collectionId][3];
  2635. this.hintInfo('BiliPolyfill: 已跳过片尾');
  2636. }
  2637. };
  2638. this.video.addEventListener('timeupdate', h);
  2639. }
  2640. }
  2641. setVideoSpeed(speed) {
  2642. if (speed < 0 || speed > 10) return;
  2643. this.video.playbackRate = speed;
  2644. }
  2645. focusOnPlayer() {
  2646. this.playerWin.document.getElementsByClassName('bilibili-player-video-progress')[0].click();
  2647. }
  2648. menuFocusOnPlayer() {
  2649. this.playerWin.document.getElementsByClassName('bilibili-player-context-menu-container black')[0].addEventListener('click', () =>
  2650. setTimeout(() => this.focusOnPlayer(), 0)
  2651. );
  2652. }
  2653. limitedKeydownFullScreenPlay() {
  2654. // 1. listen for any user guesture
  2655. const h = e => {
  2656. // 2. not real user guesture => do nothing
  2657. if (!e.isTrusted) return;
  2658. // 3. key down is Enter => full screen play
  2659. if (e.key == 'Enter') {
  2660. // 3.1 full screen
  2661. if (this.playerWin.document.querySelector('#bilibiliPlayer div.video-state-fullscreen-off')) {
  2662. this.playerWin.document.querySelector('#bilibiliPlayer div.bilibili-player-video-btn-fullscreen').click();
  2663. }
  2664. // 3.2 play
  2665. if (this.video.paused) {
  2666. if (this.video.readyState) {
  2667. this.playerWin.document.querySelector('#bilibiliPlayer div.bilibili-player-video-btn-start').click();
  2668. }
  2669. else {
  2670. this.video.addEventListener('canplay', () => {
  2671. this.playerWin.document.querySelector('#bilibiliPlayer div.bilibili-player-video-btn-start').click();
  2672. }, { once: true });
  2673. }
  2674. }
  2675. }
  2676. // 4. clean up listener
  2677. top.document.removeEventListener('keydown', h);
  2678. top.document.removeEventListener('click', h);
  2679. };
  2680. top.document.addEventListener('keydown', h);
  2681. top.document.addEventListener('click', h);
  2682. }
  2683. speechRecognition() {
  2684. // 1. polyfill
  2685. const SpeechRecognition = top.SpeechRecognition || top.webkitSpeechRecognition;
  2686. const SpeechGrammarList = top.SpeechGrammarList || top.webkitSpeechGrammarList;
  2687. // 2. give hint
  2688. alert('Yahaha! You found me!\nBiliTwin支持的语音命令: 播放 暂停 全屏 关闭 加速 减速 下一集\nChrome may support Cantonese or Hakka as well. See BiliPolyfill::speechRecognition.');
  2689. if (!SpeechRecognition || !SpeechGrammarList) alert('浏览器太旧啦~彩蛋没法运行~');
  2690. // 3. setup recognition
  2691. const player = ['播放', '暂停', '全屏', '关闭', '加速', '减速', '下一集'];
  2692. const grammar = '#JSGF V1.0; grammar player; public <player> = ' + player.join(' | ') + ' ;';
  2693. const recognition = new SpeechRecognition();
  2694. const speechRecognitionList = new SpeechGrammarList();
  2695. speechRecognitionList.addFromString(grammar, 1);
  2696. recognition.grammars = speechRecognitionList;
  2697. // cmn: Mandarin(Putonghua), yue: Cantonese, hak: Hakka
  2698. // See https://www.iana.org/assignments/language-subtag-registry/language-subtag-registry
  2699. recognition.lang = 'cmn';
  2700. recognition.continuous = true;
  2701. recognition.interimR###lts = false;
  2702. recognition.maxAlternatives = 1;
  2703. recognition.start();
  2704. recognition.onr###lt = e => {
  2705. const last = e.r###lts.length - 1;
  2706. const transcript = e.r###lts[last][0].transcript;
  2707. switch (transcript) {
  2708. case '播放':
  2709. if (this.video.paused) this.playerWin.document.querySelector('#bilibiliPlayer div.bilibili-player-video-btn-start').click();
  2710. this.hintInfo(`BiliPolyfill: 语音:播放`);
  2711. break;
  2712. case '暂停':
  2713. if (!this.video.paused) this.playerWin.document.querySelector('#bilibiliPlayer div.bilibili-player-video-btn-start').click();
  2714. this.hintInfo(`BiliPolyfill: 语音:暂停`);
  2715. break;
  2716. case '全屏':
  2717. this.playerWin.document.querySelector('#bilibiliPlayer div.bilibili-player-video-btn-fullscreen').click();
  2718. this.hintInfo(`BiliPolyfill: 语音:全屏`);
  2719. break;
  2720. case '关闭':
  2721. top.close();
  2722. break;
  2723. case '加速':
  2724. this.setVideoSpeed(2);
  2725. this.hintInfo(`BiliPolyfill: 语音:加速`);
  2726. break;
  2727. case '减速':
  2728. this.setVideoSpeed(0.5);
  2729. this.hintInfo(`BiliPolyfill: 语音:减速`);
  2730. break;
  2731. case '下一集':
  2732. this.video.dispatchEvent(new Event('ended'));
  2733. default:
  2734. this.hintInfo(`BiliPolyfill: 语音:"${transcript}"?`);
  2735. break;
  2736. }
  2737. typeof console == "object" && console.log(e.r###lts);
  2738. typeof console == "object" && console.log(`transcript:${transcript} confidence:${e.r###lts[0][0].confidence}`);
  2739. };
  2740. }
  2741. substitudeFullscreenPlayer(option) {
  2742. // 1. check param
  2743. if (!option) throw 'usage: substitudeFullscreenPlayer({cid, aid[, p][, ...otherOptions]})';
  2744. if (!option.cid) throw 'player init: cid missing';
  2745. if (!option.aid) throw 'player init: aid missing';
  2746. // 2. hook exitFullscreen
  2747. const playerDoc = this.playerWin.document;
  2748. const hook = [playerDoc.webkitExitFullscreen, playerDoc.mozExitFullScreen, playerDoc.msExitFullscreen, playerDoc.exitFullscreen];
  2749. playerDoc.webkitExitFullscreen = playerDoc.mozExitFullScreen = playerDoc.msExitFullscreen = playerDoc.exitFullscreen = () => { };
  2750. // 3. substitude player
  2751. this.playerWin.player.destroy();
  2752. this.playerWin.player = new bilibiliPlayer(option);
  2753. if (option.p) this.playerWin.callAppointPart(option.p);
  2754. // 4. restore exitFullscreen
  2755. [playerDoc.webkitExitFullscreen, playerDoc.mozExitFullScreen, playerDoc.msExitFullscreen, playerDoc.exitFullscreen] = hook;
  2756. }
  2757. async getPlayerVideo() {
  2758. if (this.playerWin.document.getElementsByTagName('video').length) {
  2759. return this.video = this.playerWin.document.getElementsByTagName('video')[0];
  2760. }
  2761. else {
  2762. return new Promise(resolve => {
  2763. const observer = new MutationObserver(() => {
  2764. if (this.playerWin.document.getElementsByTagName('video').length) {
  2765. observer.disconnect();
  2766. resolve(this.video = this.playerWin.document.getElementsByTagName('video')[0]);
  2767. }
  2768. });
  2769. observer.observe(this.playerWin.document.getElementById('bilibiliPlayer'), { childList: true });
  2770. });
  2771. }
  2772. }
  2773. async getPlayerMenu() {
  2774. if (this.playerWin.document.getElementsByClassName('bilibili-player-context-menu-container black').length) {
  2775. return this.playerWin.document.getElementsByClassName('bilibili-player-context-menu-container black')[0];
  2776. }
  2777. else {
  2778. return new Promise(resolve => {
  2779. const observer = new MutationObserver(() => {
  2780. if (this.playerWin.document.getElementsByClassName('bilibili-player-context-menu-container black').length) {
  2781. observer.disconnect();
  2782. resolve(this.playerWin.document.getElementsByClassName('bilibili-player-context-menu-container black')[0]);
  2783. }
  2784. });
  2785. observer.observe(this.playerWin.document.getElementById('bilibiliPlayer'), { childList: true });
  2786. });
  2787. }
  2788. }
  2789. static async openMinimizedPlayer(option = { cid: top.cid, aid: top.aid, playerWin: top }) {
  2790. // 1. check param
  2791. if (!option) throw 'usage: openMinimizedPlayer({cid[, aid]})';
  2792. if (!option.cid) throw 'player init: cid missing';
  2793. if (!option.aid) option.aid = top.aid;
  2794. if (!option.playerWin) option.playerWin = top;
  2795. // 2. open a new window
  2796. const miniPlayerWin = top.open(`//www.bilibili.com/blackboard/html5player.html?cid=${option.cid}&aid=${option.aid}&crossDomain=${top.document.domain != 'www.bilibili.com' ? 'true' : ''}`, undefined, ' ');
  2797. // 3. bangumi => request referrer must match => hook response of current page
  2798. const res = top.location.href.includes('bangumi') && await new Promise(resolve => {
  2799. const jq = option.playerWin.jQuery;
  2800. const _ajax = jq.ajax;
  2801. jq.ajax = function (a, c) {
  2802. if (typeof c === 'object') { if (typeof a === 'string') c.url = a; a = c; c = undefined; } if (a.url.includes('interface.bilibili.com/v2/playurl?') || a.url.includes('bangumi.bilibili.com/player/web_api/v2/playurl?')) {
  2803. a.success = resolve;
  2804. jq.ajax = _ajax;
  2805. }
  2806. return _ajax.call(jq, a, c);
  2807. };
  2808. option.playerWin.player.reloadAccess();
  2809. });
  2810. // 4. wait for miniPlayerWin load
  2811. await new Promise(resolve => {
  2812. // 4.1 check for every500ms
  2813. const i = setInterval(() => miniPlayerWin.document.getElementById('bilibiliPlayer') && resolve(), 500);
  2814. // 4.2 explict event listener
  2815. miniPlayerWin.addEventListener('load', resolve, { once: true });
  2816. // 4.3 timeout after 6s
  2817. setTimeout(() => {
  2818. clearInterval(i);
  2819. miniPlayerWin.removeEventListener('load', resolve);
  2820. resolve();
  2821. }, 6000);
  2822. });
  2823. // 4.4 cannot find bilibiliPlayer => load timeout
  2824. const playerDiv = miniPlayerWin.document.getElementById('bilibiliPlayer');
  2825. if (!playerDiv) { console.warn('openMinimizedPlayer: document load timeout'); return; }
  2826. // 5. need to inject response => new bilibiliPlayer
  2827. if (res) {
  2828. await new Promise(resolve => {
  2829. const jq = miniPlayerWin.jQuery;
  2830. const _ajax = jq.ajax;
  2831. jq.ajax = function (a, c) {
  2832. if (typeof c === 'object') { if (typeof a === 'string') c.url = a; a = c; c = undefined; } if (a.url.includes('interface.bilibili.com/v2/playurl?') || a.url.includes('bangumi.bilibili.com/player/web_api/v2/playurl?')) {
  2833. a.success(res);
  2834. jq.ajax = _ajax;
  2835. resolve();
  2836. }
  2837. else {
  2838. return _ajax.call(jq, a, c);
  2839. }
  2840. };
  2841. miniPlayerWin.player = new miniPlayerWin.bilibiliPlayer({ cid: option.cid, aid: option.aid });
  2842. // miniPlayerWin.eval(`player = new bilibiliPlayer({ cid: ${option.cid}, aid: ${option.aid} })`);
  2843. // console.log(`player = new bilibiliPlayer({ cid: ${option.cid}, aid: ${option.aid} })`);
  2844. });
  2845. }
  2846. // 6. wait for bilibiliPlayer load
  2847. await new Promise(resolve => {
  2848. if (miniPlayerWin.document.querySelector('#bilibiliPlayer div.bilibili-player-video-btn-fullscreen')) resolve();
  2849. else {
  2850. const observer = new MutationObserver(() => {
  2851. if (miniPlayerWin.document.querySelector('#bilibiliPlayer div.bilibili-player-video-btn-fullscreen')) {
  2852. observer.disconnect();
  2853. resolve();
  2854. }
  2855. });
  2856. observer.observe(playerDiv, { childList: true });
  2857. }
  2858. });
  2859. // 7. adopt full screen player style withour really trigger full screen
  2860. // 7.1 hook requestFullscreen
  2861. const hook = [playerDiv.webkitRequestFullscreen, playerDiv.mozRequestFullScreen, playerDiv.msRequestFullscreen, playerDiv.requestFullscreen];
  2862. playerDiv.webkitRequestFullscreen = playerDiv.mozRequestFullScreen = playerDiv.msRequestFullscreen = playerDiv.requestFullscreen = () => { };
  2863. // 7.2 adopt full screen player style
  2864. if (miniPlayerWin.document.querySelector('#bilibiliPlayer div.video-state-fullscreen-off'))
  2865. miniPlayerWin.document.querySelector('#bilibiliPlayer div.bilibili-player-video-btn-fullscreen').click();
  2866. // 7.3 restore requestFullscreen
  2867. [playerDiv.webkitRequestFullscreen, playerDiv.mozRequestFullScreen, playerDiv.msRequestFullscreen, playerDiv.requestFullscreen] = hook;
  2868. }
  2869. static secondToReadable(s) {
  2870. if (s > 60) return `${parseInt(s / 60)}分${parseInt(s % 60)}秒`;
  2871. else return `${parseInt(s % 60)}秒`;
  2872. }
  2873. static clearAllUserdata(playerWin = top) {
  2874. if (playerWin.GM_setValue) return GM_setValue('biliPolyfill', '');
  2875. if (playerWin.GM.setValue) return GM.setValue('biliPolyfill', '');
  2876. playerWin.localStorage.removeItem('biliPolyfill');
  2877. }
  2878. static get optionDescriptions() {
  2879. return [
  2880. ['betabeta', '增强组件总开关 <---------更加懒得测试了,反正以后B站也会自己提供这些功能。也许吧。'],
  2881. // 1. user interface
  2882. ['badgeWatchLater', '稍后再看添加数字角标'],
  2883. ['recommend', '弹幕列表换成相关视频'],
  2884. ['electric', '整合充电榜与换P倒计时'],
  2885. ['electricSkippable', '跳过充电榜', 'disabled'],
  2886. // 2. automation
  2887. ['scroll', '自动滚动到播放器'],
  2888. ['focus', '自动聚焦到播放器(新页面直接按空格会播放而不是向下滚动)'],
  2889. ['menuFocus', '关闭菜单后聚焦到播放器'],
  2890. ['restorePrevent', '记住防挡字幕'],
  2891. ['restoreDanmuku', '记住弹幕开关(顶端/底端/滚动/全部)'],
  2892. ['restoreSpeed', '记住播放速度'],
  2893. ['restoreWide', '记住宽屏'],
  2894. ['autoR###me', '自动跳转上次看到'],
  2895. ['autoPlay', '自动播放'],
  2896. ['autoFullScreen', '自动全屏'],
  2897. ['oped', '标记后自动跳OP/ED'],
  2898. ['series', '尝试自动找上下集'],
  2899. // 3. interaction
  2900. ['limitedKeydown', '首次回车键可全屏自动播放'],
  2901. ['dblclick', '双击全屏'],
  2902. // 4. easter eggs
  2903. ['speech', '(彩蛋)(需墙外)任意三击鼠标左键开启语音识别'],
  2904. ];
  2905. }
  2906. static get optionDefaults() {
  2907. return {
  2908. betabeta: false,
  2909. // 1. user interface
  2910. badgeWatchLater: true,
  2911. recommend: true,
  2912. electric: true,
  2913. electricSkippable: false,
  2914. // 2. automation
  2915. scroll: true,
  2916. focus: true,
  2917. menuFocus: true,
  2918. restorePrevent: true,
  2919. restoreDanmuku: true,
  2920. restoreSpeed: true,
  2921. restoreWide: true,
  2922. autoR###me: true,
  2923. autoPlay: false,
  2924. autoFullScreen: false,
  2925. oped: true,
  2926. series: true,
  2927. // 3. interaction
  2928. limitedKeydown: true,
  2929. dblclick: true,
  2930. // 4. easter eggs
  2931. speech: false,
  2932. }
  2933. }
  2934. static _UNIT_TEST() {
  2935. console.warn('This test is impossible.');
  2936. console.warn('You need to close the tab, reopen it, etc.');
  2937. console.warn('Maybe you also want to test between bideo parts, etc.');
  2938. console.warn('I am too lazy to find workarounds.');
  2939. }
  2940. }
  2941. /***
  2942. * Copyright (C) 2018 Qli5. All Rights Reserved.
  2943. *
  2944. * @author qli5 <goodlq11[at](163|gmail).com>
  2945. *
  2946. * This Source Code Form is subject to the terms of the Mozilla Public
  2947. * License, v. 2.0. If a copy of the MPL was not distributed with this
  2948. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  2949. */
  2950. class Exporter {
  2951. static exportIDM(urls, referrer = top.location.origin) {
  2952. return urls.map(url => `<\r\n${url}\r\nreferer: ${referrer}\r\n>\r\n`).join('');
  2953. }
  2954. static exportM3U8(urls, referrer = top.location.origin, userAgent = top.navigator.userAgent) {
  2955. return '#EXTM3U\n' + urls.map(url => `#EXTVLCOPT:http-referrer=${referrer}\n#EXTVLCOPT:http-user-agent=${userAgent}\n#EXTINF:-1\n${url}\n`).join('');
  2956. }
  2957. static exportAria2(urls, referrer = top.location.origin) {
  2958. return urls.map(url => `${url}\r\n referer=${referrer}\r\n`).join('');
  2959. }
  2960. static async sendToAria2RPC(urls, referrer = top.location.origin, target = 'http://127.0.0.1:6800/jsonrpc') {
  2961. // 1. prepare body
  2962. const h = 'referer';
  2963. const body = JSON.stringify(urls.map((url, id) => ({
  2964. id,
  2965. jsonrpc: 2,
  2966. method: "aria2.addUri",
  2967. params: [
  2968. [url],
  2969. { [h]: referrer }
  2970. ]
  2971. })));
  2972. // 2. send to jsonrpc target
  2973. const method = 'POST';
  2974. while (1) {
  2975. try {
  2976. return await (await fetch(target, { method, body })).json();
  2977. }
  2978. catch (e) {
  2979. target = top.prompt('Aria2 connection failed. Please provide a valid server address:', target);
  2980. if (!target) return null;
  2981. }
  2982. }
  2983. }
  2984. static copyToClipboard(text) {
  2985. const textarea = document.createElement('textarea');
  2986. document.body.appendChild(textarea);
  2987. textarea.value = text;
  2988. textarea.select();
  2989. document.execCommand('copy');
  2990. document.body.removeChild(textarea);
  2991. }
  2992. }
  2993. /***
  2994. * Copyright (C) 2018 Qli5. All Rights Reserved.
  2995. *
  2996. * @author qli5 <goodlq11[at](163|gmail).com>
  2997. *
  2998. * This Source Code Form is subject to the terms of the Mozilla Public
  2999. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3000. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  3001. */
  3002. class TwentyFourDataView extends DataView {
  3003. getUint24(byteOffset, littleEndian) {
  3004. if (littleEndian) throw 'littleEndian int24 not implemented';
  3005. return this.getUint32(byteOffset - 1) & 0x00FFFFFF;
  3006. }
  3007. setUint24(byteOffset, value, littleEndian) {
  3008. if (littleEndian) throw 'littleEndian int24 not implemented';
  3009. if (value > 0x00FFFFFF) throw 'setUint24: number out of range';
  3010. let msb = value >> 16;
  3011. let lsb = value & 0xFFFF;
  3012. this.setUint8(byteOffset, msb);
  3013. this.setUint16(byteOffset + 1, lsb);
  3014. }
  3015. indexOf(search, startOffset = 0, endOffset = this.byteLength - search.length + 1) {
  3016. // I know it is NAIVE
  3017. if (search.charCodeAt) {
  3018. for (let i = startOffset; i < endOffset; i++) {
  3019. if (this.getUint8(i) != search.charCodeAt(0)) continue;
  3020. let found = 1;
  3021. for (let j = 0; j < search.length; j++) {
  3022. if (this.getUint8(i + j) != search.charCodeAt(j)) {
  3023. found = 0;
  3024. break;
  3025. }
  3026. }
  3027. if (found) return i;
  3028. }
  3029. return -1;
  3030. }
  3031. else {
  3032. for (let i = startOffset; i < endOffset; i++) {
  3033. if (this.getUint8(i) != search[0]) continue;
  3034. let found = 1;
  3035. for (let j = 0; j < search.length; j++) {
  3036. if (this.getUint8(i + j) != search[j]) {
  3037. found = 0;
  3038. break;
  3039. }
  3040. }
  3041. if (found) return i;
  3042. }
  3043. return -1;
  3044. }
  3045. }
  3046. }
  3047. /***
  3048. * Copyright (C) 2018 Qli5. All Rights Reserved.
  3049. *
  3050. * @author qli5 <goodlq11[at](163|gmail).com>
  3051. *
  3052. * This Source Code Form is subject to the terms of the Mozilla Public
  3053. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3054. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  3055. */
  3056. class FLVTag {
  3057. constructor(dataView, currentOffset = 0) {
  3058. this.tagHeader = new TwentyFourDataView(dataView.buffer, dataView.byteOffset + currentOffset, 11);
  3059. this.tagData = new TwentyFourDataView(dataView.buffer, dataView.byteOffset + currentOffset + 11, this.dataSize);
  3060. this.previousSize = new TwentyFourDataView(dataView.buffer, dataView.byteOffset + currentOffset + 11 + this.dataSize, 4);
  3061. }
  3062. get tagType() {
  3063. return this.tagHeader.getUint8(0);
  3064. }
  3065. get dataSize() {
  3066. return this.tagHeader.getUint24(1);
  3067. }
  3068. get timestamp() {
  3069. return this.tagHeader.getUint24(4);
  3070. }
  3071. get timestampExtension() {
  3072. return this.tagHeader.getUint8(7);
  3073. }
  3074. get streamID() {
  3075. return this.tagHeader.getUint24(8);
  3076. }
  3077. stripKeyframesScriptData() {
  3078. let hasKeyframes = 'hasKeyframes\x01';
  3079. if (this.tagType != 0x12) throw 'can not strip non-scriptdata\'s keyframes';
  3080. let index;
  3081. index = this.tagData.indexOf(hasKeyframes);
  3082. if (index != -1) {
  3083. //0x0101 => 0x0100
  3084. this.tagData.setUint8(index + hasKeyframes.length, 0x00);
  3085. }
  3086. // Well, I think it is unnecessary
  3087. /*index = this.tagData.indexOf(keyframes)
  3088. if (index != -1) {
  3089. this.dataSize = index;
  3090. this.tagHeader.setUint24(1, index);
  3091. this.tagData = new TwentyFourDataView(this.tagData.buffer, this.tagData.byteOffset, index);
  3092. }*/
  3093. }
  3094. getDuration() {
  3095. if (this.tagType != 0x12) throw 'can not find non-scriptdata\'s duration';
  3096. let duration = 'duration\x00';
  3097. let index = this.tagData.indexOf(duration);
  3098. if (index == -1) throw 'can not get flv meta duration';
  3099. index += 9;
  3100. return this.tagData.getFloat64(index);
  3101. }
  3102. getDurationAndView() {
  3103. if (this.tagType != 0x12) throw 'can not find non-scriptdata\'s duration';
  3104. let duration = 'duration\x00';
  3105. let index = this.tagData.indexOf(duration);
  3106. if (index == -1) throw 'can not get flv meta duration';
  3107. index += 9;
  3108. return {
  3109. duration: this.tagData.getFloat64(index),
  3110. durationDataView: new TwentyFourDataView(this.tagData.buffer, this.tagData.byteOffset + index, 8)
  3111. };
  3112. }
  3113. getCombinedTimestamp() {
  3114. return (this.timestampExtension << 24 | this.timestamp);
  3115. }
  3116. setCombinedTimestamp(timestamp) {
  3117. if (timestamp < 0) throw 'timestamp < 0';
  3118. this.tagHeader.setUint8(7, timestamp >> 24);
  3119. this.tagHeader.setUint24(4, timestamp & 0x00FFFFFF);
  3120. }
  3121. }
  3122. /***
  3123. * Copyright (C) 2018 Qli5. All Rights Reserved.
  3124. *
  3125. * @author qli5 <goodlq11[at](163|gmail).com>
  3126. *
  3127. * This Source Code Form is subject to the terms of the Mozilla Public
  3128. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3129. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  3130. *
  3131. * The FLV merge utility is a Javascript translation of
  3132. * https://github.com/grepmusic/flvmerge
  3133. * by grepmusic
  3134. */
  3135. /**
  3136. * A simple flv parser
  3137. */
  3138. class FLV {
  3139. constructor(dataView) {
  3140. if (dataView.indexOf('FLV', 0, 1) != 0) throw 'Invalid FLV header';
  3141. this.header = new TwentyFourDataView(dataView.buffer, dataView.byteOffset, 9);
  3142. this.firstPreviousTagSize = new TwentyFourDataView(dataView.buffer, dataView.byteOffset + 9, 4);
  3143. this.tags = [];
  3144. let offset = this.headerLength + 4;
  3145. while (offset < dataView.byteLength) {
  3146. let tag = new FLVTag(dataView, offset);
  3147. // debug for scrpit data tag
  3148. // if (tag.tagType != 0x08 && tag.tagType != 0x09)
  3149. offset += 11 + tag.dataSize + 4;
  3150. this.tags.push(tag);
  3151. }
  3152. if (offset != dataView.byteLength) throw 'FLV unexpected end of file';
  3153. }
  3154. get type() {
  3155. return 'FLV';
  3156. }
  3157. get version() {
  3158. return this.header.getUint8(3);
  3159. }
  3160. get typeFlag() {
  3161. return this.header.getUint8(4);
  3162. }
  3163. get headerLength() {
  3164. return this.header.getUint32(5);
  3165. }
  3166. static merge(flvs) {
  3167. if (flvs.length < 1) throw 'Usage: FLV.merge([flvs])';
  3168. let blobParts = [];
  3169. let basetimestamp = [0, 0];
  3170. let lasttimestamp = [0, 0];
  3171. let duration = 0.0;
  3172. let durationDataView;
  3173. blobParts.push(flvs[0].header);
  3174. blobParts.push(flvs[0].firstPreviousTagSize);
  3175. for (let flv of flvs) {
  3176. let bts = duration * 1000;
  3177. basetimestamp[0] = lasttimestamp[0];
  3178. basetimestamp[1] = lasttimestamp[1];
  3179. bts = Math.max(bts, basetimestamp[0], basetimestamp[1]);
  3180. let foundDuration = 0;
  3181. for (let tag of flv.tags) {
  3182. if (tag.tagType == 0x12 && !foundDuration) {
  3183. duration += tag.getDuration();
  3184. foundDuration = 1;
  3185. if (flv == flvs[0]) {
  3186. ({ duration, durationDataView } = tag.getDurationAndView());
  3187. tag.stripKeyframesScriptData();
  3188. blobParts.push(tag.tagHeader);
  3189. blobParts.push(tag.tagData);
  3190. blobParts.push(tag.previousSize);
  3191. }
  3192. }
  3193. else if (tag.tagType == 0x08 || tag.tagType == 0x09) {
  3194. lasttimestamp[tag.tagType - 0x08] = bts + tag.getCombinedTimestamp();
  3195. tag.setCombinedTimestamp(lasttimestamp[tag.tagType - 0x08]);
  3196. blobParts.push(tag.tagHeader);
  3197. blobParts.push(tag.tagData);
  3198. blobParts.push(tag.previousSize);
  3199. }
  3200. }
  3201. }
  3202. durationDataView.setFloat64(0, duration);
  3203. return new Blob(blobParts);
  3204. }
  3205. static async mergeBlobs(blobs) {
  3206. // Blobs can be swapped to disk, while Arraybuffers can not.
  3207. // This is a RAM saving workaround. Somewhat.
  3208. if (blobs.length < 1) throw 'Usage: FLV.mergeBlobs([blobs])';
  3209. let ret = [];
  3210. let basetimestamp = [0, 0];
  3211. let lasttimestamp = [0, 0];
  3212. let duration = 0.0;
  3213. let durationDataView;
  3214. for (let blob of blobs) {
  3215. let bts = duration * 1000;
  3216. basetimestamp[0] = lasttimestamp[0];
  3217. basetimestamp[1] = lasttimestamp[1];
  3218. bts = Math.max(bts, basetimestamp[0], basetimestamp[1]);
  3219. let foundDuration = 0;
  3220. let flv = await new Promise((resolve, reject) => {
  3221. let fr = new FileReader();
  3222. fr.onload = () => resolve(new FLV(new TwentyFourDataView(fr.r###lt)));
  3223. fr.readAsArrayBuffer(blob);
  3224. fr.onerror = reject;
  3225. });
  3226. let modifiedMediaTags = [];
  3227. for (let tag of flv.tags) {
  3228. if (tag.tagType == 0x12 && !foundDuration) {
  3229. duration += tag.getDuration();
  3230. foundDuration = 1;
  3231. if (blob == blobs[0]) {
  3232. ret.push(flv.header, flv.firstPreviousTagSize);
  3233. ({ duration, durationDataView } = tag.getDurationAndView());
  3234. tag.stripKeyframesScriptData();
  3235. ret.push(tag.tagHeader);
  3236. ret.push(tag.tagData);
  3237. ret.push(tag.previousSize);
  3238. }
  3239. }
  3240. else if (tag.tagType == 0x08 || tag.tagType == 0x09) {
  3241. lasttimestamp[tag.tagType - 0x08] = bts + tag.getCombinedTimestamp();
  3242. tag.setCombinedTimestamp(lasttimestamp[tag.tagType - 0x08]);
  3243. modifiedMediaTags.push(tag.tagHeader, tag.tagData, tag.previousSize);
  3244. }
  3245. }
  3246. ret.push(new Blob(modifiedMediaTags));
  3247. }
  3248. durationDataView.setFloat64(0, duration);
  3249. return new Blob(ret);
  3250. }
  3251. }
  3252. var embeddedHTML = `<html>
  3253. <body>
  3254. <p>
  3255. 加载文件…… loading files...
  3256. <progress value="0" max="100" id="fileProgress"></progress>
  3257. </p>
  3258. <p>
  3259. 构建mkv…… building mkv...
  3260. <progress value="0" max="100" id="mkvProgress"></progress>
  3261. </p>
  3262. <p>
  3263. <a id="a" download="merged.mkv">merged.mkv</a>
  3264. </p>
  3265. <footer>
  3266. author qli5 &lt;goodlq11[at](163|gmail).com&gt;
  3267. </footer>
  3268. <script>
  3269. var FLVASS2MKV = (function () {
  3270. 'use strict';
  3271. /***
  3272. * Copyright (C) 2018 Qli5. All Rights Reserved.
  3273. *
  3274. * @author qli5 <goodlq11[at](163|gmail).com>
  3275. *
  3276. * This Source Code Form is subject to the terms of the Mozilla Public
  3277. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3278. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  3279. */
  3280. const _navigator = typeof navigator === 'object' && navigator || { userAgent: 'chrome' };
  3281. const _Blob = typeof Blob === 'function' && Blob || class {
  3282. constructor(array) {
  3283. return Buffer.concat(array.map(Buffer.from.bind(Buffer)));
  3284. }
  3285. };
  3286. const _TextEncoder = typeof TextEncoder === 'function' && TextEncoder || class {
  3287. /**
  3288. * @param {string} chunk
  3289. * @returns {Uint8Array}
  3290. */
  3291. encode(chunk) {
  3292. return Buffer.from(chunk, 'utf-8');
  3293. }
  3294. };
  3295. const _TextDecoder = typeof TextDecoder === 'function' && TextDecoder || class extends require('string_decoder').StringDecoder {
  3296. /**
  3297. * @param {ArrayBuffer} chunk
  3298. * @returns {string}
  3299. */
  3300. decode(chunk) {
  3301. return this.end(Buffer.from(chunk));
  3302. }
  3303. };
  3304. /***
  3305. * The FLV demuxer is from flv.js
  3306. *
  3307. * Copyright (C) 2016 Bilibili. All Rights Reserved.
  3308. *
  3309. * @author zheng qian <xqq@xqq.im>
  3310. *
  3311. * Licensed under the Apache License, Version 2.0 (the "License");
  3312. * you may not use this file except in compliance with the License.
  3313. * You may obtain a copy of the License at
  3314. *
  3315. * http://www.apache.org/licenses/LICENSE-2.0
  3316. *
  3317. * Unless required by applicable law or agreed to in writing, software
  3318. * distributed under the License is distributed on an "AS IS" BASIS,
  3319. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  3320. * See the License for the specific language governing permissions and
  3321. * limitations under the License.
  3322. */
  3323. // import FLVDemuxer from 'flv.js/src/demux/flv-demuxer.js';
  3324. // ..import Log from '../utils/logger.js';
  3325. const Log = {
  3326. e: console.error.bind(console),
  3327. w: console.warn.bind(console),
  3328. i: console.log.bind(console),
  3329. v: console.log.bind(console),
  3330. };
  3331. // ..import AMF from './amf-parser.js';
  3332. // ....import Log from '../utils/logger.js';
  3333. // ....import decodeUTF8 from '../utils/utf8-conv.js';
  3334. function checkContinuation(uint8array, start, checkLength) {
  3335. let array = uint8array;
  3336. if (start + checkLength < array.length) {
  3337. while (checkLength--) {
  3338. if ((array[++start] & 0xC0) !== 0x80)
  3339. return false;
  3340. }
  3341. return true;
  3342. } else {
  3343. return false;
  3344. }
  3345. }
  3346. function decodeUTF8(uint8array) {
  3347. let out = [];
  3348. let input = uint8array;
  3349. let i = 0;
  3350. let length = uint8array.length;
  3351. while (i < length) {
  3352. if (input[i] < 0x80) {
  3353. out.push(String.fromCharCode(input[i]));
  3354. ++i;
  3355. continue;
  3356. } else if (input[i] < 0xC0) {
  3357. // fallthrough
  3358. } else if (input[i] < 0xE0) {
  3359. if (checkContinuation(input, i, 1)) {
  3360. let ucs4 = (input[i] & 0x1F) << 6 | (input[i + 1] & 0x3F);
  3361. if (ucs4 >= 0x80) {
  3362. out.push(String.fromCharCode(ucs4 & 0xFFFF));
  3363. i += 2;
  3364. continue;
  3365. }
  3366. }
  3367. } else if (input[i] < 0xF0) {
  3368. if (checkContinuation(input, i, 2)) {
  3369. let ucs4 = (input[i] & 0xF) << 12 | (input[i + 1] & 0x3F) << 6 | input[i + 2] & 0x3F;
  3370. if (ucs4 >= 0x800 && (ucs4 & 0xF800) !== 0xD800) {
  3371. out.push(String.fromCharCode(ucs4 & 0xFFFF));
  3372. i += 3;
  3373. continue;
  3374. }
  3375. }
  3376. } else if (input[i] < 0xF8) {
  3377. if (checkContinuation(input, i, 3)) {
  3378. let ucs4 = (input[i] & 0x7) << 18 | (input[i + 1] & 0x3F) << 12
  3379. | (input[i + 2] & 0x3F) << 6 | (input[i + 3] & 0x3F);
  3380. if (ucs4 > 0x10000 && ucs4 < 0x110000) {
  3381. ucs4 -= 0x10000;
  3382. out.push(String.fromCharCode((ucs4 >>> 10) | 0xD800));
  3383. out.push(String.fromCharCode((ucs4 & 0x3FF) | 0xDC00));
  3384. i += 4;
  3385. continue;
  3386. }
  3387. }
  3388. }
  3389. out.push(String.fromCharCode(0xFFFD));
  3390. ++i;
  3391. }
  3392. return out.join('');
  3393. }
  3394. // ....import {IllegalStateException} from '../utils/exception.js';
  3395. class IllegalStateException extends Error { }
  3396. let le = (function () {
  3397. let buf = new ArrayBuffer(2);
  3398. (new DataView(buf)).setInt16(0, 256, true); // little-endian write
  3399. return (new Int16Array(buf))[0] === 256; // platform-spec read, if equal then LE
  3400. })();
  3401. class AMF {
  3402. static parseScriptData(arrayBuffer, dataOffset, dataSize) {
  3403. let data = {};
  3404. try {
  3405. let name = AMF.parseValue(arrayBuffer, dataOffset, dataSize);
  3406. let value = AMF.parseValue(arrayBuffer, dataOffset + name.size, dataSize - name.size);
  3407. data[name.data] = value.data;
  3408. } catch (e) {
  3409. Log.e('AMF', e.toString());
  3410. }
  3411. return data;
  3412. }
  3413. static parseObject(arrayBuffer, dataOffset, dataSize) {
  3414. if (dataSize < 3) {
  3415. throw new IllegalStateException('Data not enough when parse ScriptDataObject');
  3416. }
  3417. let name = AMF.parseString(arrayBuffer, dataOffset, dataSize);
  3418. let value = AMF.parseValue(arrayBuffer, dataOffset + name.size, dataSize - name.size);
  3419. let isObjectEnd = value.objectEnd;
  3420. return {
  3421. data: {
  3422. name: name.data,
  3423. value: value.data
  3424. },
  3425. size: name.size + value.size,
  3426. objectEnd: isObjectEnd
  3427. };
  3428. }
  3429. static parseVariable(arrayBuffer, dataOffset, dataSize) {
  3430. return AMF.parseObject(arrayBuffer, dataOffset, dataSize);
  3431. }
  3432. static parseString(arrayBuffer, dataOffset, dataSize) {
  3433. if (dataSize < 2) {
  3434. throw new IllegalStateException('Data not enough when parse String');
  3435. }
  3436. let v = new DataView(arrayBuffer, dataOffset, dataSize);
  3437. let length = v.getUint16(0, !le);
  3438. let str;
  3439. if (length > 0) {
  3440. str = decodeUTF8(new Uint8Array(arrayBuffer, dataOffset + 2, length));
  3441. } else {
  3442. str = '';
  3443. }
  3444. return {
  3445. data: str,
  3446. size: 2 + length
  3447. };
  3448. }
  3449. static parseLongString(arrayBuffer, dataOffset, dataSize) {
  3450. if (dataSize < 4) {
  3451. throw new IllegalStateException('Data not enough when parse LongString');
  3452. }
  3453. let v = new DataView(arrayBuffer, dataOffset, dataSize);
  3454. let length = v.getUint32(0, !le);
  3455. let str;
  3456. if (length > 0) {
  3457. str = decodeUTF8(new Uint8Array(arrayBuffer, dataOffset + 4, length));
  3458. } else {
  3459. str = '';
  3460. }
  3461. return {
  3462. data: str,
  3463. size: 4 + length
  3464. };
  3465. }
  3466. static parseDate(arrayBuffer, dataOffset, dataSize) {
  3467. if (dataSize < 10) {
  3468. throw new IllegalStateException('Data size invalid when parse Date');
  3469. }
  3470. let v = new DataView(arrayBuffer, dataOffset, dataSize);
  3471. let timestamp = v.getFloat64(0, !le);
  3472. let localTimeOffset = v.getInt16(8, !le);
  3473. timestamp += localTimeOffset * 60 * 1000; // get UTC time
  3474. return {
  3475. data: new Date(timestamp),
  3476. size: 8 + 2
  3477. };
  3478. }
  3479. static parseValue(arrayBuffer, dataOffset, dataSize) {
  3480. if (dataSize < 1) {
  3481. throw new IllegalStateException('Data not enough when parse Value');
  3482. }
  3483. let v = new DataView(arrayBuffer, dataOffset, dataSize);
  3484. let offset = 1;
  3485. let type = v.getUint8(0);
  3486. let value;
  3487. let objectEnd = false;
  3488. try {
  3489. switch (type) {
  3490. case 0: // Number(Double) type
  3491. value = v.getFloat64(1, !le);
  3492. offset += 8;
  3493. break;
  3494. case 1: { // Boolean type
  3495. let b = v.getUint8(1);
  3496. value = b ? true : false;
  3497. offset += 1;
  3498. break;
  3499. }
  3500. case 2: { // String type
  3501. let amfstr = AMF.parseString(arrayBuffer, dataOffset + 1, dataSize - 1);
  3502. value = amfstr.data;
  3503. offset += amfstr.size;
  3504. break;
  3505. }
  3506. case 3: { // Object(s) type
  3507. value = {};
  3508. let terminal = 0; // workaround for malformed Objects which has missing ScriptDataObjectEnd
  3509. if ((v.getUint32(dataSize - 4, !le) & 0x00FFFFFF) === 9) {
  3510. terminal = 3;
  3511. }
  3512. while (offset < dataSize - 4) { // 4 === type(UI8) + ScriptDataObjectEnd(UI24)
  3513. let amfobj = AMF.parseObject(arrayBuffer, dataOffset + offset, dataSize - offset - terminal);
  3514. if (amfobj.objectEnd)
  3515. break;
  3516. value[amfobj.data.name] = amfobj.data.value;
  3517. offset += amfobj.size;
  3518. }
  3519. if (offset <= dataSize - 3) {
  3520. let marker = v.getUint32(offset - 1, !le) & 0x00FFFFFF;
  3521. if (marker === 9) {
  3522. offset += 3;
  3523. }
  3524. }
  3525. break;
  3526. }
  3527. case 8: { // ECMA array type (Mixed array)
  3528. value = {};
  3529. offset += 4; // ECMAArrayLength(UI32)
  3530. let terminal = 0; // workaround for malformed MixedArrays which has missing ScriptDataObjectEnd
  3531. if ((v.getUint32(dataSize - 4, !le) & 0x00FFFFFF) === 9) {
  3532. terminal = 3;
  3533. }
  3534. while (offset < dataSize - 8) { // 8 === type(UI8) + ECMAArrayLength(UI32) + ScriptDataVariableEnd(UI24)
  3535. let amfvar = AMF.parseVariable(arrayBuffer, dataOffset + offset, dataSize - offset - terminal);
  3536. if (amfvar.objectEnd)
  3537. break;
  3538. value[amfvar.data.name] = amfvar.data.value;
  3539. offset += amfvar.size;
  3540. }
  3541. if (offset <= dataSize - 3) {
  3542. let marker = v.getUint32(offset - 1, !le) & 0x00FFFFFF;
  3543. if (marker === 9) {
  3544. offset += 3;
  3545. }
  3546. }
  3547. break;
  3548. }
  3549. case 9: // ScriptDataObjectEnd
  3550. value = undefined;
  3551. offset = 1;
  3552. objectEnd = true;
  3553. break;
  3554. case 10: { // Strict array type
  3555. // ScriptDataValue[n]. NOTE: according to video_file_format_spec_v10_1.pdf
  3556. value = [];
  3557. let strictArrayLength = v.getUint32(1, !le);
  3558. offset += 4;
  3559. for (let i = 0; i < strictArrayLength; i++) {
  3560. let val = AMF.parseValue(arrayBuffer, dataOffset + offset, dataSize - offset);
  3561. value.push(val.data);
  3562. offset += val.size;
  3563. }
  3564. break;
  3565. }
  3566. case 11: { // Date type
  3567. let date = AMF.parseDate(arrayBuffer, dataOffset + 1, dataSize - 1);
  3568. value = date.data;
  3569. offset += date.size;
  3570. break;
  3571. }
  3572. case 12: { // Long string type
  3573. let amfLongStr = AMF.parseString(arrayBuffer, dataOffset + 1, dataSize - 1);
  3574. value = amfLongStr.data;
  3575. offset += amfLongStr.size;
  3576. break;
  3577. }
  3578. default:
  3579. // ignore and skip
  3580. offset = dataSize;
  3581. Log.w('AMF', 'Unsupported AMF value type ' + type);
  3582. }
  3583. } catch (e) {
  3584. Log.e('AMF', e.toString());
  3585. }
  3586. return {
  3587. data: value,
  3588. size: offset,
  3589. objectEnd: objectEnd
  3590. };
  3591. }
  3592. }
  3593. // ..import SPSParser from './sps-parser.js';
  3594. // ....import ExpGolomb from './exp-golomb.js';
  3595. // ......import {IllegalStateException, InvalidArgumentException} from '../utils/exception.js';
  3596. class InvalidArgumentException extends Error { }
  3597. class ExpGolomb {
  3598. constructor(uint8array) {
  3599. this.TAG = 'ExpGolomb';
  3600. this._buffer = uint8array;
  3601. this._buffer_index = 0;
  3602. this._total_bytes = uint8array.byteLength;
  3603. this._total_bits = uint8array.byteLength * 8;
  3604. this._current_word = 0;
  3605. this._current_word_bits_left = 0;
  3606. }
  3607. destroy() {
  3608. this._buffer = null;
  3609. }
  3610. _fillCurrentWord() {
  3611. let buffer_bytes_left = this._total_bytes - this._buffer_index;
  3612. if (buffer_bytes_left <= 0)
  3613. throw new IllegalStateException('ExpGolomb: _fillCurrentWord() but no bytes available');
  3614. let bytes_read = Math.min(4, buffer_bytes_left);
  3615. let word = new Uint8Array(4);
  3616. word.set(this._buffer.subarray(this._buffer_index, this._buffer_index + bytes_read));
  3617. this._current_word = new DataView(word.buffer).getUint32(0, false);
  3618. this._buffer_index += bytes_read;
  3619. this._current_word_bits_left = bytes_read * 8;
  3620. }
  3621. readBits(bits) {
  3622. if (bits > 32)
  3623. throw new InvalidArgumentException('ExpGolomb: readBits() bits exceeded max 32bits!');
  3624. if (bits <= this._current_word_bits_left) {
  3625. let r###lt = this._current_word >>> (32 - bits);
  3626. this._current_word <<= bits;
  3627. this._current_word_bits_left -= bits;
  3628. return r###lt;
  3629. }
  3630. let r###lt = this._current_word_bits_left ? this._current_word : 0;
  3631. r###lt = r###lt >>> (32 - this._current_word_bits_left);
  3632. let bits_need_left = bits - this._current_word_bits_left;
  3633. this._fillCurrentWord();
  3634. let bits_read_next = Math.min(bits_need_left, this._current_word_bits_left);
  3635. let r###lt2 = this._current_word >>> (32 - bits_read_next);
  3636. this._current_word <<= bits_read_next;
  3637. this._current_word_bits_left -= bits_read_next;
  3638. r###lt = (r###lt << bits_read_next) | r###lt2;
  3639. return r###lt;
  3640. }
  3641. readBool() {
  3642. return this.readBits(1) === 1;
  3643. }
  3644. readByte() {
  3645. return this.readBits(8);
  3646. }
  3647. _skipLeadingZero() {
  3648. let zero_count;
  3649. for (zero_count = 0; zero_count < this._current_word_bits_left; zero_count++) {
  3650. if (0 !== (this._current_word & (0x80000000 >>> zero_count))) {
  3651. this._current_word <<= zero_count;
  3652. this._current_word_bits_left -= zero_count;
  3653. return zero_count;
  3654. }
  3655. }
  3656. this._fillCurrentWord();
  3657. return zero_count + this._skipLeadingZero();
  3658. }
  3659. readUEG() { // unsigned exponential golomb
  3660. let leading_zeros = this._skipLeadingZero();
  3661. return this.readBits(leading_zeros + 1) - 1;
  3662. }
  3663. readSEG() { // signed exponential golomb
  3664. let value = this.readUEG();
  3665. if (value & 0x01) {
  3666. return (value + 1) >>> 1;
  3667. } else {
  3668. return -1 * (value >>> 1);
  3669. }
  3670. }
  3671. }
  3672. class SPSParser {
  3673. static _ebsp2rbsp(uint8array) {
  3674. let src = uint8array;
  3675. let src_length = src.byteLength;
  3676. let dst = new Uint8Array(src_length);
  3677. let dst_idx = 0;
  3678. for (let i = 0; i < src_length; i++) {
  3679. if (i >= 2) {
  3680. // Unescape: Skip 0x03 after 00 00
  3681. if (src[i] === 0x03 && src[i - 1] === 0x00 && src[i - 2] === 0x00) {
  3682. continue;
  3683. }
  3684. }
  3685. dst[dst_idx] = src[i];
  3686. dst_idx++;
  3687. }
  3688. return new Uint8Array(dst.buffer, 0, dst_idx);
  3689. }
  3690. static parseSPS(uint8array) {
  3691. let rbsp = SPSParser._ebsp2rbsp(uint8array);
  3692. let gb = new ExpGolomb(rbsp);
  3693. gb.readByte();
  3694. let profile_idc = gb.readByte(); // profile_idc
  3695. gb.readByte(); // constraint_set_flags[5] + reserved_zero[3]
  3696. let level_idc = gb.readByte(); // level_idc
  3697. gb.readUEG(); // seq_parameter_set_id
  3698. let profile_string = SPSParser.getProfileString(profile_idc);
  3699. let level_string = SPSParser.getLevelString(level_idc);
  3700. let chroma_format_idc = 1;
  3701. let chroma_format = 420;
  3702. let chroma_format_table = [0, 420, 422, 444];
  3703. let bit_depth = 8;
  3704. if (profile_idc === 100 || profile_idc === 110 || profile_idc === 122 ||
  3705. profile_idc === 244 || profile_idc === 44 || profile_idc === 83 ||
  3706. profile_idc === 86 || profile_idc === 118 || profile_idc === 128 ||
  3707. profile_idc === 138 || profile_idc === 144) {
  3708. chroma_format_idc = gb.readUEG();
  3709. if (chroma_format_idc === 3) {
  3710. gb.readBits(1); // separate_colour_plane_flag
  3711. }
  3712. if (chroma_format_idc <= 3) {
  3713. chroma_format = chroma_format_table[chroma_format_idc];
  3714. }
  3715. bit_depth = gb.readUEG() + 8; // bit_depth_luma_minus8
  3716. gb.readUEG(); // bit_depth_chroma_minus8
  3717. gb.readBits(1); // qpprime_y_zero_transform_bypass_flag
  3718. if (gb.readBool()) { // seq_scaling_matrix_present_flag
  3719. let scaling_list_count = (chroma_format_idc !== 3) ? 8 : 12;
  3720. for (let i = 0; i < scaling_list_count; i++) {
  3721. if (gb.readBool()) { // seq_scaling_list_present_flag
  3722. if (i < 6) {
  3723. SPSParser._skipScalingList(gb, 16);
  3724. } else {
  3725. SPSParser._skipScalingList(gb, 64);
  3726. }
  3727. }
  3728. }
  3729. }
  3730. }
  3731. gb.readUEG(); // log2_max_frame_num_minus4
  3732. let pic_order_cnt_type = gb.readUEG();
  3733. if (pic_order_cnt_type === 0) {
  3734. gb.readUEG(); // log2_max_pic_order_cnt_lsb_minus_4
  3735. } else if (pic_order_cnt_type === 1) {
  3736. gb.readBits(1); // delta_pic_order_always_zero_flag
  3737. gb.readSEG(); // offset_for_non_ref_pic
  3738. gb.readSEG(); // offset_for_top_to_bottom_field
  3739. let num_ref_frames_in_pic_order_cnt_cycle = gb.readUEG();
  3740. for (let i = 0; i < num_ref_frames_in_pic_order_cnt_cycle; i++) {
  3741. gb.readSEG(); // offset_for_ref_frame
  3742. }
  3743. }
  3744. gb.readUEG(); // max_num_ref_frames
  3745. gb.readBits(1); // gaps_in_frame_num_value_allowed_flag
  3746. let pic_width_in_mbs_minus1 = gb.readUEG();
  3747. let pic_height_in_map_units_minus1 = gb.readUEG();
  3748. let frame_mbs_only_flag = gb.readBits(1);
  3749. if (frame_mbs_only_flag === 0) {
  3750. gb.readBits(1); // mb_adaptive_frame_field_flag
  3751. }
  3752. gb.readBits(1); // direct_8x8_inference_flag
  3753. let frame_crop_left_offset = 0;
  3754. let frame_crop_right_offset = 0;
  3755. let frame_crop_top_offset = 0;
  3756. let frame_crop_bottom_offset = 0;
  3757. let frame_cropping_flag = gb.readBool();
  3758. if (frame_cropping_flag) {
  3759. frame_crop_left_offset = gb.readUEG();
  3760. frame_crop_right_offset = gb.readUEG();
  3761. frame_crop_top_offset = gb.readUEG();
  3762. frame_crop_bottom_offset = gb.readUEG();
  3763. }
  3764. let sar_width = 1, sar_height = 1;
  3765. let fps = 0, fps_fixed = true, fps_num = 0, fps_den = 0;
  3766. let vui_parameters_present_flag = gb.readBool();
  3767. if (vui_parameters_present_flag) {
  3768. if (gb.readBool()) { // aspect_ratio_info_present_flag
  3769. let aspect_ratio_idc = gb.readByte();
  3770. let sar_w_table = [1, 12, 10, 16, 40, 24, 20, 32, 80, 18, 15, 64, 160, 4, 3, 2];
  3771. let sar_h_table = [1, 11, 11, 11, 33, 11, 11, 11, 33, 11, 11, 33, 99, 3, 2, 1];
  3772. if (aspect_ratio_idc > 0 && aspect_ratio_idc < 16) {
  3773. sar_width = sar_w_table[aspect_ratio_idc - 1];
  3774. sar_height = sar_h_table[aspect_ratio_idc - 1];
  3775. } else if (aspect_ratio_idc === 255) {
  3776. sar_width = gb.readByte() << 8 | gb.readByte();
  3777. sar_height = gb.readByte() << 8 | gb.readByte();
  3778. }
  3779. }
  3780. if (gb.readBool()) { // overscan_info_present_flag
  3781. gb.readBool(); // overscan_appropriate_flag
  3782. }
  3783. if (gb.readBool()) { // video_signal_type_present_flag
  3784. gb.readBits(4); // video_format & video_full_range_flag
  3785. if (gb.readBool()) { // colour_description_present_flag
  3786. gb.readBits(24); // colour_primaries & transfer_characteristics & matrix_coefficients
  3787. }
  3788. }
  3789. if (gb.readBool()) { // chroma_loc_info_present_flag
  3790. gb.readUEG(); // chroma_sample_loc_type_top_field
  3791. gb.readUEG(); // chroma_sample_loc_type_bottom_field
  3792. }
  3793. if (gb.readBool()) { // timing_info_present_flag
  3794. let num_units_in_tick = gb.readBits(32);
  3795. let time_scale = gb.readBits(32);
  3796. fps_fixed = gb.readBool(); // fixed_frame_rate_flag
  3797. fps_num = time_scale;
  3798. fps_den = num_units_in_tick * 2;
  3799. fps = fps_num / fps_den;
  3800. }
  3801. }
  3802. let sarScale = 1;
  3803. if (sar_width !== 1 || sar_height !== 1) {
  3804. sarScale = sar_width / sar_height;
  3805. }
  3806. let crop_unit_x = 0, crop_unit_y = 0;
  3807. if (chroma_format_idc === 0) {
  3808. crop_unit_x = 1;
  3809. crop_unit_y = 2 - frame_mbs_only_flag;
  3810. } else {
  3811. let sub_wc = (chroma_format_idc === 3) ? 1 : 2;
  3812. let sub_hc = (chroma_format_idc === 1) ? 2 : 1;
  3813. crop_unit_x = sub_wc;
  3814. crop_unit_y = sub_hc * (2 - frame_mbs_only_flag);
  3815. }
  3816. let codec_width = (pic_width_in_mbs_minus1 + 1) * 16;
  3817. let codec_height = (2 - frame_mbs_only_flag) * ((pic_height_in_map_units_minus1 + 1) * 16);
  3818. codec_width -= (frame_crop_left_offset + frame_crop_right_offset) * crop_unit_x;
  3819. codec_height -= (frame_crop_top_offset + frame_crop_bottom_offset) * crop_unit_y;
  3820. let present_width = Math.ceil(codec_width * sarScale);
  3821. gb.destroy();
  3822. gb = null;
  3823. return {
  3824. profile_string: profile_string, // baseline, high, high10, ...
  3825. level_string: level_string, // 3, 3.1, 4, 4.1, 5, 5.1, ...
  3826. bit_depth: bit_depth, // 8bit, 10bit, ...
  3827. chroma_format: chroma_format, // 4:2:0, 4:2:2, ...
  3828. chroma_format_string: SPSParser.getChromaFormatString(chroma_format),
  3829. frame_rate: {
  3830. fixed: fps_fixed,
  3831. fps: fps,
  3832. fps_den: fps_den,
  3833. fps_num: fps_num
  3834. },
  3835. sar_ratio: {
  3836. width: sar_width,
  3837. height: sar_height
  3838. },
  3839. codec_size: {
  3840. width: codec_width,
  3841. height: codec_height
  3842. },
  3843. present_size: {
  3844. width: present_width,
  3845. height: codec_height
  3846. }
  3847. };
  3848. }
  3849. static _skipScalingList(gb, count) {
  3850. let last_scale = 8, next_scale = 8;
  3851. let delta_scale = 0;
  3852. for (let i = 0; i < count; i++) {
  3853. if (next_scale !== 0) {
  3854. delta_scale = gb.readSEG();
  3855. next_scale = (last_scale + delta_scale + 256) % 256;
  3856. }
  3857. last_scale = (next_scale === 0) ? last_scale : next_scale;
  3858. }
  3859. }
  3860. static getProfileString(profile_idc) {
  3861. switch (profile_idc) {
  3862. case 66:
  3863. return 'Baseline';
  3864. case 77:
  3865. return 'Main';
  3866. case 88:
  3867. return 'Extended';
  3868. case 100:
  3869. return 'High';
  3870. case 110:
  3871. return 'High10';
  3872. case 122:
  3873. return 'High422';
  3874. case 244:
  3875. return 'High444';
  3876. default:
  3877. return 'Unknown';
  3878. }
  3879. }
  3880. static getLevelString(level_idc) {
  3881. return (level_idc / 10).toFixed(1);
  3882. }
  3883. static getChromaFormatString(chroma) {
  3884. switch (chroma) {
  3885. case 420:
  3886. return '4:2:0';
  3887. case 422:
  3888. return '4:2:2';
  3889. case 444:
  3890. return '4:4:4';
  3891. default:
  3892. return 'Unknown';
  3893. }
  3894. }
  3895. }
  3896. // ..import DemuxErrors from './demux-errors.js';
  3897. const DemuxErrors = {
  3898. OK: 'OK',
  3899. FORMAT_ERROR: 'FormatError',
  3900. FORMAT_UNSUPPORTED: 'FormatUnsupported',
  3901. CODEC_UNSUPPORTED: 'CodecUnsupported'
  3902. };
  3903. // ..import MediaInfo from '../core/media-info.js';
  3904. class MediaInfo {
  3905. constructor() {
  3906. this.mimeType = null;
  3907. this.duration = null;
  3908. this.hasAudio = null;
  3909. this.hasVideo = null;
  3910. this.audioCodec = null;
  3911. this.videoCodec = null;
  3912. this.audioDataRate = null;
  3913. this.videoDataRate = null;
  3914. this.audioSampleRate = null;
  3915. this.audioChannelCount = null;
  3916. this.width = null;
  3917. this.height = null;
  3918. this.fps = null;
  3919. this.profile = null;
  3920. this.level = null;
  3921. this.chromaFormat = null;
  3922. this.sarNum = null;
  3923. this.sarDen = null;
  3924. this.metadata = null;
  3925. this.segments = null; // MediaInfo[]
  3926. this.segmentCount = null;
  3927. this.hasKeyframesIndex = null;
  3928. this.keyframesIndex = null;
  3929. }
  3930. isComplete() {
  3931. let audioInfoComplete = (this.hasAudio === false) ||
  3932. (this.hasAudio === true &&
  3933. this.audioCodec != null &&
  3934. this.audioSampleRate != null &&
  3935. this.audioChannelCount != null);
  3936. let videoInfoComplete = (this.hasVideo === false) ||
  3937. (this.hasVideo === true &&
  3938. this.videoCodec != null &&
  3939. this.width != null &&
  3940. this.height != null &&
  3941. this.fps != null &&
  3942. this.profile != null &&
  3943. this.level != null &&
  3944. this.chromaFormat != null &&
  3945. this.sarNum != null &&
  3946. this.sarDen != null);
  3947. // keyframesIndex may not be present
  3948. return this.mimeType != null &&
  3949. this.duration != null &&
  3950. this.metadata != null &&
  3951. this.hasKeyframesIndex != null &&
  3952. audioInfoComplete &&
  3953. videoInfoComplete;
  3954. }
  3955. isSeekable() {
  3956. return this.hasKeyframesIndex === true;
  3957. }
  3958. getNearestKeyframe(milliseconds) {
  3959. if (this.keyframesIndex == null) {
  3960. return null;
  3961. }
  3962. let table = this.keyframesIndex;
  3963. let keyframeIdx = this._search(table.times, milliseconds);
  3964. return {
  3965. index: keyframeIdx,
  3966. milliseconds: table.times[keyframeIdx],
  3967. fileposition: table.filepositions[keyframeIdx]
  3968. };
  3969. }
  3970. _search(list, value) {
  3971. let idx = 0;
  3972. let last = list.length - 1;
  3973. let mid = 0;
  3974. let lbound = 0;
  3975. let ubound = last;
  3976. if (value < list[0]) {
  3977. idx = 0;
  3978. lbound = ubound + 1; // skip search
  3979. }
  3980. while (lbound <= ubound) {
  3981. mid = lbound + Math.floor((ubound - lbound) / 2);
  3982. if (mid === last || (value >= list[mid] && value < list[mid + 1])) {
  3983. idx = mid;
  3984. break;
  3985. } else if (list[mid] < value) {
  3986. lbound = mid + 1;
  3987. } else {
  3988. ubound = mid - 1;
  3989. }
  3990. }
  3991. return idx;
  3992. }
  3993. }
  3994. function ReadBig32(array, index) {
  3995. return ((array[index] << 24) |
  3996. (array[index + 1] << 16) |
  3997. (array[index + 2] << 8) |
  3998. (array[index + 3]));
  3999. }
  4000. class FLVDemuxer {
  4001. /**
  4002. * Create a new FLV demuxer
  4003. * @param {Object} probeData
  4004. * @param {boolean} probeData.match
  4005. * @param {number} probeData.consumed
  4006. * @param {number} probeData.dataOffset
  4007. * @param {booleam} probeData.hasAudioTrack
  4008. * @param {boolean} probeData.hasVideoTrack
  4009. * @param {*} config
  4010. */
  4011. constructor(probeData, config) {
  4012. this.TAG = 'FLVDemuxer';
  4013. this._config = config;
  4014. this._onError = null;
  4015. this._onMediaInfo = null;
  4016. this._onTrackMetadata = null;
  4017. this._onDataAvailable = null;
  4018. this._dataOffset = probeData.dataOffset;
  4019. this._firstParse = true;
  4020. this._dispatch = false;
  4021. this._hasAudio = probeData.hasAudioTrack;
  4022. this._hasVideo = probeData.hasVideoTrack;
  4023. this._hasAudioFlagOverrided = false;
  4024. this._hasVideoFlagOverrided = false;
  4025. this._audioInitialMetadataDispatched = false;
  4026. this._videoInitialMetadataDispatched = false;
  4027. this._mediaInfo = new MediaInfo();
  4028. this._mediaInfo.hasAudio = this._hasAudio;
  4029. this._mediaInfo.hasVideo = this._hasVideo;
  4030. this._metadata = null;
  4031. this._audioMetadata = null;
  4032. this._videoMetadata = null;
  4033. this._naluLengthSize = 4;
  4034. this._timestampBase = 0; // int32, in milliseconds
  4035. this._timescale = 1000;
  4036. this._duration = 0; // int32, in milliseconds
  4037. this._durationOverrided = false;
  4038. this._referenceFrameRate = {
  4039. fixed: true,
  4040. fps: 23.976,
  4041. fps_num: 23976,
  4042. fps_den: 1000
  4043. };
  4044. this._flvSoundRateTable = [5500, 11025, 22050, 44100, 48000];
  4045. this._mpegSamplingRates = [
  4046. 96000, 88200, 64000, 48000, 44100, 32000,
  4047. 24000, 22050, 16000, 12000, 11025, 8000, 7350
  4048. ];
  4049. this._mpegAudioV10SampleRateTable = [44100, 48000, 32000, 0];
  4050. this._mpegAudioV20SampleRateTable = [22050, 24000, 16000, 0];
  4051. this._mpegAudioV25SampleRateTable = [11025, 12000, 8000, 0];
  4052. this._mpegAudioL1BitRateTable = [0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448, -1];
  4053. this._mpegAudioL2BitRateTable = [0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, -1];
  4054. this._mpegAudioL3BitRateTable = [0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, -1];
  4055. this._videoTrack = { type: 'video', id: 1, sequenceNumber: 0, samples: [], length: 0 };
  4056. this._audioTrack = { type: 'audio', id: 2, sequenceNumber: 0, samples: [], length: 0 };
  4057. this._littleEndian = (function () {
  4058. let buf = new ArrayBuffer(2);
  4059. (new DataView(buf)).setInt16(0, 256, true); // little-endian write
  4060. return (new Int16Array(buf))[0] === 256; // platform-spec read, if equal then LE
  4061. })();
  4062. }
  4063. destroy() {
  4064. this._mediaInfo = null;
  4065. this._metadata = null;
  4066. this._audioMetadata = null;
  4067. this._videoMetadata = null;
  4068. this._videoTrack = null;
  4069. this._audioTrack = null;
  4070. this._onError = null;
  4071. this._onMediaInfo = null;
  4072. this._onTrackMetadata = null;
  4073. this._onDataAvailable = null;
  4074. }
  4075. /**
  4076. * Probe the flv data
  4077. * @param {ArrayBuffer} buffer
  4078. * @returns {Object} - probeData to be feed into constructor
  4079. */
  4080. static probe(buffer) {
  4081. let data = new Uint8Array(buffer);
  4082. let mismatch = { match: false };
  4083. if (data[0] !== 0x46 || data[1] !== 0x4C || data[2] !== 0x56 || data[3] !== 0x01) {
  4084. return mismatch;
  4085. }
  4086. let hasAudio = ((data[4] & 4) >>> 2) !== 0;
  4087. let hasVideo = (data[4] & 1) !== 0;
  4088. let offset = ReadBig32(data, 5);
  4089. if (offset < 9) {
  4090. return mismatch;
  4091. }
  4092. return {
  4093. match: true,
  4094. consumed: offset,
  4095. dataOffset: offset,
  4096. hasAudioTrack: hasAudio,
  4097. hasVideoTrack: hasVideo
  4098. };
  4099. }
  4100. bindDataSource(loader) {
  4101. loader.onDataArrival = this.parseChunks.bind(this);
  4102. return this;
  4103. }
  4104. // prototype: function(type: string, metadata: any): void
  4105. get onTrackMetadata() {
  4106. return this._onTrackMetadata;
  4107. }
  4108. set onTrackMetadata(callback) {
  4109. this._onTrackMetadata = callback;
  4110. }
  4111. // prototype: function(mediaInfo: MediaInfo): void
  4112. get onMediaInfo() {
  4113. return this._onMediaInfo;
  4114. }
  4115. set onMediaInfo(callback) {
  4116. this._onMediaInfo = callback;
  4117. }
  4118. // prototype: function(type: number, info: string): void
  4119. get onError() {
  4120. return this._onError;
  4121. }
  4122. set onError(callback) {
  4123. this._onError = callback;
  4124. }
  4125. // prototype: function(videoTrack: any, audioTrack: any): void
  4126. get onDataAvailable() {
  4127. return this._onDataAvailable;
  4128. }
  4129. set onDataAvailable(callback) {
  4130. this._onDataAvailable = callback;
  4131. }
  4132. // timestamp base for output samples, must be in milliseconds
  4133. get timestampBase() {
  4134. return this._timestampBase;
  4135. }
  4136. set timestampBase(base) {
  4137. this._timestampBase = base;
  4138. }
  4139. get overridedDuration() {
  4140. return this._duration;
  4141. }
  4142. // Force-override media duration. Must be in milliseconds, int32
  4143. set overridedDuration(duration) {
  4144. this._durationOverrided = true;
  4145. this._duration = duration;
  4146. this._mediaInfo.duration = duration;
  4147. }
  4148. // Force-override audio track present flag, boolean
  4149. set overridedHasAudio(hasAudio) {
  4150. this._hasAudioFlagOverrided = true;
  4151. this._hasAudio = hasAudio;
  4152. this._mediaInfo.hasAudio = hasAudio;
  4153. }
  4154. // Force-override video track present flag, boolean
  4155. set overridedHasVideo(hasVideo) {
  4156. this._hasVideoFlagOverrided = true;
  4157. this._hasVideo = hasVideo;
  4158. this._mediaInfo.hasVideo = hasVideo;
  4159. }
  4160. resetMediaInfo() {
  4161. this._mediaInfo = new MediaInfo();
  4162. }
  4163. _isInitialMetadataDispatched() {
  4164. if (this._hasAudio && this._hasVideo) { // both audio & video
  4165. return this._audioInitialMetadataDispatched && this._videoInitialMetadataDispatched;
  4166. }
  4167. if (this._hasAudio && !this._hasVideo) { // audio only
  4168. return this._audioInitialMetadataDispatched;
  4169. }
  4170. if (!this._hasAudio && this._hasVideo) { // video only
  4171. return this._videoInitialMetadataDispatched;
  4172. }
  4173. return false;
  4174. }
  4175. // function parseChunks(chunk: ArrayBuffer, byteStart: number): number;
  4176. parseChunks(chunk, byteStart) {
  4177. if (!this._onError || !this._onMediaInfo || !this._onTrackMetadata || !this._onDataAvailable) {
  4178. throw new IllegalStateException('Flv: onError & onMediaInfo & onTrackMetadata & onDataAvailable callback must be specified');
  4179. }
  4180. // qli5: fix nonzero byteStart
  4181. let offset = byteStart || 0;
  4182. let le = this._littleEndian;
  4183. if (byteStart === 0) { // buffer with FLV header
  4184. if (chunk.byteLength > 13) {
  4185. let probeData = FLVDemuxer.probe(chunk);
  4186. offset = probeData.dataOffset;
  4187. } else {
  4188. return 0;
  4189. }
  4190. }
  4191. if (this._firstParse) { // handle PreviousTagSize0 before Tag1
  4192. this._firstParse = false;
  4193. if (offset !== this._dataOffset) {
  4194. Log.w(this.TAG, 'First time parsing but chunk byteStart invalid!');
  4195. }
  4196. let v = new DataView(chunk, offset);
  4197. let prevTagSize0 = v.getUint32(0, !le);
  4198. if (prevTagSize0 !== 0) {
  4199. Log.w(this.TAG, 'PrevTagSize0 !== 0 !!!');
  4200. }
  4201. offset += 4;
  4202. }
  4203. while (offset < chunk.byteLength) {
  4204. this._dispatch = true;
  4205. let v = new DataView(chunk, offset);
  4206. if (offset + 11 + 4 > chunk.byteLength) {
  4207. // data not enough for parsing an flv tag
  4208. break;
  4209. }
  4210. let tagType = v.getUint8(0);
  4211. let dataSize = v.getUint32(0, !le) & 0x00FFFFFF;
  4212. if (offset + 11 + dataSize + 4 > chunk.byteLength) {
  4213. // data not enough for parsing actual data body
  4214. break;
  4215. }
  4216. if (tagType !== 8 && tagType !== 9 && tagType !== 18) {
  4217. Log.w(this.TAG, \`Unsupported tag type \${tagType}, skipped\`);
  4218. // consume the whole tag (skip it)
  4219. offset += 11 + dataSize + 4;
  4220. continue;
  4221. }
  4222. let ts2 = v.getUint8(4);
  4223. let ts1 = v.getUint8(5);
  4224. let ts0 = v.getUint8(6);
  4225. let ts3 = v.getUint8(7);
  4226. let timestamp = ts0 | (ts1 << 8) | (ts2 << 16) | (ts3 << 24);
  4227. let streamId = v.getUint32(7, !le) & 0x00FFFFFF;
  4228. if (streamId !== 0) {
  4229. Log.w(this.TAG, 'Meet tag which has StreamID != 0!');
  4230. }
  4231. let dataOffset = offset + 11;
  4232. switch (tagType) {
  4233. case 8: // Audio
  4234. this._parseAudioData(chunk, dataOffset, dataSize, timestamp);
  4235. break;
  4236. case 9: // Video
  4237. this._parseVideoData(chunk, dataOffset, dataSize, timestamp, byteStart + offset);
  4238. break;
  4239. case 18: // ScriptDataObject
  4240. this._parseScriptData(chunk, dataOffset, dataSize);
  4241. break;
  4242. }
  4243. let prevTagSize = v.getUint32(11 + dataSize, !le);
  4244. if (prevTagSize !== 11 + dataSize) {
  4245. Log.w(this.TAG, \`Invalid PrevTagSize \${prevTagSize}\`);
  4246. }
  4247. offset += 11 + dataSize + 4; // tagBody + dataSize + prevTagSize
  4248. }
  4249. // dispatch parsed frames to consumer (typically, the remuxer)
  4250. if (this._isInitialMetadataDispatched()) {
  4251. if (this._dispatch && (this._audioTrack.length || this._videoTrack.length)) {
  4252. this._onDataAvailable(this._audioTrack, this._videoTrack);
  4253. }
  4254. }
  4255. return offset; // consumed bytes, just equals latest offset index
  4256. }
  4257. _parseScriptData(arrayBuffer, dataOffset, dataSize) {
  4258. let scriptData = AMF.parseScriptData(arrayBuffer, dataOffset, dataSize);
  4259. if (scriptData.hasOwnProperty('onMetaData')) {
  4260. if (scriptData.onMetaData == null || typeof scriptData.onMetaData !== 'object') {
  4261. Log.w(this.TAG, 'Invalid onMetaData structure!');
  4262. return;
  4263. }
  4264. if (this._metadata) {
  4265. Log.w(this.TAG, 'Found another onMetaData tag!');
  4266. }
  4267. this._metadata = scriptData;
  4268. let onMetaData = this._metadata.onMetaData;
  4269. if (typeof onMetaData.hasAudio === 'boolean') { // hasAudio
  4270. if (this._hasAudioFlagOverrided === false) {
  4271. this._hasAudio = onMetaData.hasAudio;
  4272. this._mediaInfo.hasAudio = this._hasAudio;
  4273. }
  4274. }
  4275. if (typeof onMetaData.hasVideo === 'boolean') { // hasVideo
  4276. if (this._hasVideoFlagOverrided === false) {
  4277. this._hasVideo = onMetaData.hasVideo;
  4278. this._mediaInfo.hasVideo = this._hasVideo;
  4279. }
  4280. }
  4281. if (typeof onMetaData.audiodatarate === 'number') { // audiodatarate
  4282. this._mediaInfo.audioDataRate = onMetaData.audiodatarate;
  4283. }
  4284. if (typeof onMetaData.videodatarate === 'number') { // videodatarate
  4285. this._mediaInfo.videoDataRate = onMetaData.videodatarate;
  4286. }
  4287. if (typeof onMetaData.width === 'number') { // width
  4288. this._mediaInfo.width = onMetaData.width;
  4289. }
  4290. if (typeof onMetaData.height === 'number') { // height
  4291. this._mediaInfo.height = onMetaData.height;
  4292. }
  4293. if (typeof onMetaData.duration === 'number') { // duration
  4294. if (!this._durationOverrided) {
  4295. let duration = Math.floor(onMetaData.duration * this._timescale);
  4296. this._duration = duration;
  4297. this._mediaInfo.duration = duration;
  4298. }
  4299. } else {
  4300. this._mediaInfo.duration = 0;
  4301. }
  4302. if (typeof onMetaData.framerate === 'number') { // framerate
  4303. let fps_num = Math.floor(onMetaData.framerate * 1000);
  4304. if (fps_num > 0) {
  4305. let fps = fps_num / 1000;
  4306. this._referenceFrameRate.fixed = true;
  4307. this._referenceFrameRate.fps = fps;
  4308. this._referenceFrameRate.fps_num = fps_num;
  4309. this._referenceFrameRate.fps_den = 1000;
  4310. this._mediaInfo.fps = fps;
  4311. }
  4312. }
  4313. if (typeof onMetaData.keyframes === 'object') { // keyframes
  4314. this._mediaInfo.hasKeyframesIndex = true;
  4315. let keyframes = onMetaData.keyframes;
  4316. this._mediaInfo.keyframesIndex = this._parseKeyframesIndex(keyframes);
  4317. onMetaData.keyframes = null; // keyframes has been extracted, remove it
  4318. } else {
  4319. this._mediaInfo.hasKeyframesIndex = false;
  4320. }
  4321. this._dispatch = false;
  4322. this._mediaInfo.metadata = onMetaData;
  4323. Log.v(this.TAG, 'Parsed onMetaData');
  4324. if (this._mediaInfo.isComplete()) {
  4325. this._onMediaInfo(this._mediaInfo);
  4326. }
  4327. }
  4328. }
  4329. _parseKeyframesIndex(keyframes) {
  4330. let times = [];
  4331. let filepositions = [];
  4332. // ignore first keyframe which is actually AVC Sequence Header (AVCDecoderConfigurationRecord)
  4333. for (let i = 1; i < keyframes.times.length; i++) {
  4334. let time = this._timestampBase + Math.floor(keyframes.times[i] * 1000);
  4335. times.push(time);
  4336. filepositions.push(keyframes.filepositions[i]);
  4337. }
  4338. return {
  4339. times: times,
  4340. filepositions: filepositions
  4341. };
  4342. }
  4343. _parseAudioData(arrayBuffer, dataOffset, dataSize, tagTimestamp) {
  4344. if (dataSize <= 1) {
  4345. Log.w(this.TAG, 'Flv: Invalid audio packet, missing SoundData payload!');
  4346. return;
  4347. }
  4348. if (this._hasAudioFlagOverrided === true && this._hasAudio === false) {
  4349. // If hasAudio: false indicated explicitly in MediaDataSource,
  4350. // Ignore all the audio packets
  4351. return;
  4352. }
  4353. let le = this._littleEndian;
  4354. let v = new DataView(arrayBuffer, dataOffset, dataSize);
  4355. let soundSpec = v.getUint8(0);
  4356. let soundFormat = soundSpec >>> 4;
  4357. if (soundFormat !== 2 && soundFormat !== 10) { // MP3 or AAC
  4358. this._onError(DemuxErrors.CODEC_UNSUPPORTED, 'Flv: Unsupported audio codec idx: ' + soundFormat);
  4359. return;
  4360. }
  4361. let soundRate = 0;
  4362. let soundRateIndex = (soundSpec & 12) >>> 2;
  4363. if (soundRateIndex >= 0 && soundRateIndex <= 4) {
  4364. soundRate = this._flvSoundRateTable[soundRateIndex];
  4365. } else {
  4366. this._onError(DemuxErrors.FORMAT_ERROR, 'Flv: Invalid audio sample rate idx: ' + soundRateIndex);
  4367. return;
  4368. }
  4369. let soundType = (soundSpec & 1);
  4370. let meta = this._audioMetadata;
  4371. let track = this._audioTrack;
  4372. if (!meta) {
  4373. if (this._hasAudio === false && this._hasAudioFlagOverrided === false) {
  4374. this._hasAudio = true;
  4375. this._mediaInfo.hasAudio = true;
  4376. }
  4377. // initial metadata
  4378. meta = this._audioMetadata = {};
  4379. meta.type = 'audio';
  4380. meta.id = track.id;
  4381. meta.timescale = this._timescale;
  4382. meta.duration = this._duration;
  4383. meta.audioSampleRate = soundRate;
  4384. meta.channelCount = (soundType === 0 ? 1 : 2);
  4385. }
  4386. if (soundFormat === 10) { // AAC
  4387. let aacData = this._parseAACAudioData(arrayBuffer, dataOffset + 1, dataSize - 1);
  4388. if (aacData == undefined) {
  4389. return;
  4390. }
  4391. if (aacData.packetType === 0) { // AAC sequence header (AudioSpecificConfig)
  4392. if (meta.config) {
  4393. Log.w(this.TAG, 'Found another AudioSpecificConfig!');
  4394. }
  4395. let misc = aacData.data;
  4396. meta.audioSampleRate = misc.samplingRate;
  4397. meta.channelCount = misc.channelCount;
  4398. meta.codec = misc.codec;
  4399. meta.originalCodec = misc.originalCodec;
  4400. meta.config = misc.config;
  4401. // added by qli5
  4402. meta.configRaw = misc.configRaw;
  4403. // The decode r###lt of an aac sample is #### PCM samples
  4404. meta.refSampleDuration = #### / meta.audioSampleRate * meta.timescale;
  4405. Log.v(this.TAG, 'Parsed AudioSpecificConfig');
  4406. if (this._isInitialMetadataDispatched()) {
  4407. // Non-initial metadata, force dispatch (or flush) parsed frames to remuxer
  4408. if (this._dispatch && (this._audioTrack.length || this._videoTrack.length)) {
  4409. this._onDataAvailable(this._audioTrack, this._videoTrack);
  4410. }
  4411. } else {
  4412. this._audioInitialMetadataDispatched = true;
  4413. }
  4414. // then notify new metadata
  4415. this._dispatch = false;
  4416. this._onTrackMetadata('audio', meta);
  4417. let mi = this._mediaInfo;
  4418. mi.audioCodec = meta.originalCodec;
  4419. mi.audioSampleRate = meta.audioSampleRate;
  4420. mi.audioChannelCount = meta.channelCount;
  4421. if (mi.hasVideo) {
  4422. if (mi.videoCodec != null) {
  4423. mi.mimeType = 'video/x-flv; codecs="' + mi.videoCodec + ',' + mi.audioCodec + '"';
  4424. }
  4425. } else {
  4426. mi.mimeType = 'video/x-flv; codecs="' + mi.audioCodec + '"';
  4427. }
  4428. if (mi.isComplete()) {
  4429. this._onMediaInfo(mi);
  4430. }
  4431. } else if (aacData.packetType === 1) { // AAC raw frame data
  4432. let dts = this._timestampBase + tagTimestamp;
  4433. let aacSample = { unit: aacData.data, length: aacData.data.byteLength, dts: dts, pts: dts };
  4434. track.samples.push(aacSample);
  4435. track.length += aacData.data.length;
  4436. } else {
  4437. Log.e(this.TAG, \`Flv: Unsupported AAC data type \${aacData.packetType}\`);
  4438. }
  4439. } else if (soundFormat === 2) { // MP3
  4440. if (!meta.codec) {
  4441. // We need metadata for mp3 audio track, extract info from frame header
  4442. let misc = this._parseMP3AudioData(arrayBuffer, dataOffset + 1, dataSize - 1, true);
  4443. if (misc == undefined) {
  4444. return;
  4445. }
  4446. meta.audioSampleRate = misc.samplingRate;
  4447. meta.channelCount = misc.channelCount;
  4448. meta.codec = misc.codec;
  4449. meta.originalCodec = misc.originalCodec;
  4450. // The decode r###lt of an mp3 sample is 1152 PCM samples
  4451. meta.refSampleDuration = 1152 / meta.audioSampleRate * meta.timescale;
  4452. Log.v(this.TAG, 'Parsed MPEG Audio Frame Header');
  4453. this._audioInitialMetadataDispatched = true;
  4454. this._onTrackMetadata('audio', meta);
  4455. let mi = this._mediaInfo;
  4456. mi.audioCodec = meta.codec;
  4457. mi.audioSampleRate = meta.audioSampleRate;
  4458. mi.audioChannelCount = meta.channelCount;
  4459. mi.audioDataRate = misc.bitRate;
  4460. if (mi.hasVideo) {
  4461. if (mi.videoCodec != null) {
  4462. mi.mimeType = 'video/x-flv; codecs="' + mi.videoCodec + ',' + mi.audioCodec + '"';
  4463. }
  4464. } else {
  4465. mi.mimeType = 'video/x-flv; codecs="' + mi.audioCodec + '"';
  4466. }
  4467. if (mi.isComplete()) {
  4468. this._onMediaInfo(mi);
  4469. }
  4470. }
  4471. // This packet is always a valid audio packet, extract it
  4472. let data = this._parseMP3AudioData(arrayBuffer, dataOffset + 1, dataSize - 1, false);
  4473. if (data == undefined) {
  4474. return;
  4475. }
  4476. let dts = this._timestampBase + tagTimestamp;
  4477. let mp3Sample = { unit: data, length: data.byteLength, dts: dts, pts: dts };
  4478. track.samples.push(mp3Sample);
  4479. track.length += data.length;
  4480. }
  4481. }
  4482. _parseAACAudioData(arrayBuffer, dataOffset, dataSize) {
  4483. if (dataSize <= 1) {
  4484. Log.w(this.TAG, 'Flv: Invalid AAC packet, missing AACPacketType or/and Data!');
  4485. return;
  4486. }
  4487. let r###lt = {};
  4488. let array = new Uint8Array(arrayBuffer, dataOffset, dataSize);
  4489. r###lt.packetType = array[0];
  4490. if (array[0] === 0) {
  4491. r###lt.data = this._parseAACAudioSpecificConfig(arrayBuffer, dataOffset + 1, dataSize - 1);
  4492. } else {
  4493. r###lt.data = array.subarray(1);
  4494. }
  4495. return r###lt;
  4496. }
  4497. _parseAACAudioSpecificConfig(arrayBuffer, dataOffset, dataSize) {
  4498. let array = new Uint8Array(arrayBuffer, dataOffset, dataSize);
  4499. let config = null;
  4500. /* Audio Object Type:
  4501. 0: Null
  4502. 1: AAC Main
  4503. 2: AAC LC
  4504. 3: AAC SSR (Scalable Sample Rate)
  4505. 4: AAC LTP (Long Term Prediction)
  4506. 5: HE-AAC / SBR (Spectral Band Replication)
  4507. 6: AAC Scalable
  4508. */
  4509. let audioObjectType = 0;
  4510. let originalAudioObjectType = 0;
  4511. let samplingIndex = 0;
  4512. let extensionSamplingIndex = null;
  4513. // 5 bits
  4514. audioObjectType = originalAudioObjectType = array[0] >>> 3;
  4515. // 4 bits
  4516. samplingIndex = ((array[0] & 0x07) << 1) | (array[1] >>> 7);
  4517. if (samplingIndex < 0 || samplingIndex >= this._mpegSamplingRates.length) {
  4518. this._onError(DemuxErrors.FORMAT_ERROR, 'Flv: AAC invalid sampling frequency index!');
  4519. return;
  4520. }
  4521. let samplingFrequence = this._mpegSamplingRates[samplingIndex];
  4522. // 4 bits
  4523. let channelConfig = (array[1] & 0x78) >>> 3;
  4524. if (channelConfig < 0 || channelConfig >= 8) {
  4525. this._onError(DemuxErrors.FORMAT_ERROR, 'Flv: AAC invalid channel configuration');
  4526. return;
  4527. }
  4528. if (audioObjectType === 5) { // HE-AAC?
  4529. // 4 bits
  4530. extensionSamplingIndex = ((array[1] & 0x07) << 1) | (array[2] >>> 7);
  4531. }
  4532. // workarounds for various browsers
  4533. let userAgent = _navigator.userAgent.toLowerCase();
  4534. if (userAgent.indexOf('firefox') !== -1) {
  4535. // firefox: use SBR (HE-AAC) if freq less than 24kHz
  4536. if (samplingIndex >= 6) {
  4537. audioObjectType = 5;
  4538. config = new Array(4);
  4539. extensionSamplingIndex = samplingIndex - 3;
  4540. } else { // use LC-AAC
  4541. audioObjectType = 2;
  4542. config = new Array(2);
  4543. extensionSamplingIndex = samplingIndex;
  4544. }
  4545. } else if (userAgent.indexOf('android') !== -1) {
  4546. // android: always use LC-AAC
  4547. audioObjectType = 2;
  4548. config = new Array(2);
  4549. extensionSamplingIndex = samplingIndex;
  4550. } else {
  4551. // for other browsers, e.g. chrome...
  4552. // Always use HE-AAC to make it easier to switch aac codec profile
  4553. audioObjectType = 5;
  4554. extensionSamplingIndex = samplingIndex;
  4555. config = new Array(4);
  4556. if (samplingIndex >= 6) {
  4557. extensionSamplingIndex = samplingIndex - 3;
  4558. } else if (channelConfig === 1) { // Mono channel
  4559. audioObjectType = 2;
  4560. config = new Array(2);
  4561. extensionSamplingIndex = samplingIndex;
  4562. }
  4563. }
  4564. config[0] = audioObjectType << 3;
  4565. config[0] |= (samplingIndex & 0x0F) >>> 1;
  4566. config[1] = (samplingIndex & 0x0F) << 7;
  4567. config[1] |= (channelConfig & 0x0F) << 3;
  4568. if (audioObjectType === 5) {
  4569. config[1] |= ((extensionSamplingIndex & 0x0F) >>> 1);
  4570. config[2] = (extensionSamplingIndex & 0x01) << 7;
  4571. // extended audio object type: force to 2 (LC-AAC)
  4572. config[2] |= (2 << 2);
  4573. config[3] = 0;
  4574. }
  4575. return {
  4576. // configRaw: added by qli5
  4577. configRaw: array,
  4578. config: config,
  4579. samplingRate: samplingFrequence,
  4580. channelCount: channelConfig,
  4581. codec: 'mp4a.40.' + audioObjectType,
  4582. originalCodec: 'mp4a.40.' + originalAudioObjectType
  4583. };
  4584. }
  4585. _parseMP3AudioData(arrayBuffer, dataOffset, dataSize, requestHeader) {
  4586. if (dataSize < 4) {
  4587. Log.w(this.TAG, 'Flv: Invalid MP3 packet, header missing!');
  4588. return;
  4589. }
  4590. let le = this._littleEndian;
  4591. let array = new Uint8Array(arrayBuffer, dataOffset, dataSize);
  4592. let r###lt = null;
  4593. if (requestHeader) {
  4594. if (array[0] !== 0xFF) {
  4595. return;
  4596. }
  4597. let ver = (array[1] >>> 3) & 0x03;
  4598. let layer = (array[1] & 0x06) >> 1;
  4599. let bitrate_index = (array[2] & 0xF0) >>> 4;
  4600. let sampling_freq_index = (array[2] & 0x0C) >>> 2;
  4601. let channel_mode = (array[3] >>> 6) & 0x03;
  4602. let channel_count = channel_mode !== 3 ? 2 : 1;
  4603. let sample_rate = 0;
  4604. let bit_rate = 0;
  4605. let codec = 'mp3';
  4606. switch (ver) {
  4607. case 0: // MPEG 2.5
  4608. sample_rate = this._mpegAudioV25SampleRateTable[sampling_freq_index];
  4609. break;
  4610. case 2: // MPEG 2
  4611. sample_rate = this._mpegAudioV20SampleRateTable[sampling_freq_index];
  4612. break;
  4613. case 3: // MPEG 1
  4614. sample_rate = this._mpegAudioV10SampleRateTable[sampling_freq_index];
  4615. break;
  4616. }
  4617. switch (layer) {
  4618. case 1: // Layer 3
  4619. if (bitrate_index < this._mpegAudioL3BitRateTable.length) {
  4620. bit_rate = this._mpegAudioL3BitRateTable[bitrate_index];
  4621. }
  4622. break;
  4623. case 2: // Layer 2
  4624. if (bitrate_index < this._mpegAudioL2BitRateTable.length) {
  4625. bit_rate = this._mpegAudioL2BitRateTable[bitrate_index];
  4626. }
  4627. break;
  4628. case 3: // Layer 1
  4629. if (bitrate_index < this._mpegAudioL1BitRateTable.length) {
  4630. bit_rate = this._mpegAudioL1BitRateTable[bitrate_index];
  4631. }
  4632. break;
  4633. }
  4634. r###lt = {
  4635. bitRate: bit_rate,
  4636. samplingRate: sample_rate,
  4637. channelCount: channel_count,
  4638. codec: codec,
  4639. originalCodec: codec
  4640. };
  4641. } else {
  4642. r###lt = array;
  4643. }
  4644. return r###lt;
  4645. }
  4646. _parseVideoData(arrayBuffer, dataOffset, dataSize, tagTimestamp, tagPosition) {
  4647. if (dataSize <= 1) {
  4648. Log.w(this.TAG, 'Flv: Invalid video packet, missing VideoData payload!');
  4649. return;
  4650. }
  4651. if (this._hasVideoFlagOverrided === true && this._hasVideo === false) {
  4652. // If hasVideo: false indicated explicitly in MediaDataSource,
  4653. // Ignore all the video packets
  4654. return;
  4655. }
  4656. let spec = (new Uint8Array(arrayBuffer, dataOffset, dataSize))[0];
  4657. let frameType = (spec & 240) >>> 4;
  4658. let codecId = spec & 15;
  4659. if (codecId !== 7) {
  4660. this._onError(DemuxErrors.CODEC_UNSUPPORTED, \`Flv: Unsupported codec in video frame: \${codecId}\`);
  4661. return;
  4662. }
  4663. this._parseAVCVideoPacket(arrayBuffer, dataOffset + 1, dataSize - 1, tagTimestamp, tagPosition, frameType);
  4664. }
  4665. _parseAVCVideoPacket(arrayBuffer, dataOffset, dataSize, tagTimestamp, tagPosition, frameType) {
  4666. if (dataSize < 4) {
  4667. Log.w(this.TAG, 'Flv: Invalid AVC packet, missing AVCPacketType or/and CompositionTime');
  4668. return;
  4669. }
  4670. let le = this._littleEndian;
  4671. let v = new DataView(arrayBuffer, dataOffset, dataSize);
  4672. let packetType = v.getUint8(0);
  4673. let cts = v.getUint32(0, !le) & 0x00FFFFFF;
  4674. if (packetType === 0) { // AVCDecoderConfigurationRecord
  4675. this._parseAVCDecoderConfigurationRecord(arrayBuffer, dataOffset + 4, dataSize - 4);
  4676. } else if (packetType === 1) { // One or more Nalus
  4677. this._parseAVCVideoData(arrayBuffer, dataOffset + 4, dataSize - 4, tagTimestamp, tagPosition, frameType, cts);
  4678. } else if (packetType === 2) {
  4679. // empty, AVC end of sequence
  4680. } else {
  4681. this._onError(DemuxErrors.FORMAT_ERROR, \`Flv: Invalid video packet type \${packetType}\`);
  4682. return;
  4683. }
  4684. }
  4685. _parseAVCDecoderConfigurationRecord(arrayBuffer, dataOffset, dataSize) {
  4686. if (dataSize < 7) {
  4687. Log.w(this.TAG, 'Flv: Invalid AVCDecoderConfigurationRecord, lack of data!');
  4688. return;
  4689. }
  4690. let meta = this._videoMetadata;
  4691. let track = this._videoTrack;
  4692. let le = this._littleEndian;
  4693. let v = new DataView(arrayBuffer, dataOffset, dataSize);
  4694. if (!meta) {
  4695. if (this._hasVideo === false && this._hasVideoFlagOverrided === false) {
  4696. this._hasVideo = true;
  4697. this._mediaInfo.hasVideo = true;
  4698. }
  4699. meta = this._videoMetadata = {};
  4700. meta.type = 'video';
  4701. meta.id = track.id;
  4702. meta.timescale = this._timescale;
  4703. meta.duration = this._duration;
  4704. } else {
  4705. if (typeof meta.avcc !== 'undefined') {
  4706. Log.w(this.TAG, 'Found another AVCDecoderConfigurationRecord!');
  4707. }
  4708. }
  4709. let version = v.getUint8(0); // configurationVersion
  4710. let avcProfile = v.getUint8(1); // avcProfileIndication
  4711. let profileCompatibility = v.getUint8(2); // profile_compatibility
  4712. let avcLevel = v.getUint8(3); // AVCLevelIndication
  4713. if (version !== 1 || avcProfile === 0) {
  4714. this._onError(DemuxErrors.FORMAT_ERROR, 'Flv: Invalid AVCDecoderConfigurationRecord');
  4715. return;
  4716. }
  4717. this._naluLengthSize = (v.getUint8(4) & 3) + 1; // lengthSizeMinusOne
  4718. if (this._naluLengthSize !== 3 && this._naluLengthSize !== 4) { // holy shit!!!
  4719. this._onError(DemuxErrors.FORMAT_ERROR, \`Flv: Strange NaluLengthSizeMinusOne: \${this._naluLengthSize - 1}\`);
  4720. return;
  4721. }
  4722. let spsCount = v.getUint8(5) & 31; // numOfSequenceParameterSets
  4723. if (spsCount === 0) {
  4724. this._onError(DemuxErrors.FORMAT_ERROR, 'Flv: Invalid AVCDecoderConfigurationRecord: No SPS');
  4725. return;
  4726. } else if (spsCount > 1) {
  4727. Log.w(this.TAG, \`Flv: Strange AVCDecoderConfigurationRecord: SPS Count = \${spsCount}\`);
  4728. }
  4729. let offset = 6;
  4730. for (let i = 0; i < spsCount; i++) {
  4731. let len = v.getUint16(offset, !le); // sequenceParameterSetLength
  4732. offset += 2;
  4733. if (len === 0) {
  4734. continue;
  4735. }
  4736. // Notice: Nalu without startcode header (00 00 00 01)
  4737. let sps = new Uint8Array(arrayBuffer, dataOffset + offset, len);
  4738. offset += len;
  4739. let config = SPSParser.parseSPS(sps);
  4740. if (i !== 0) {
  4741. // ignore other sps's config
  4742. continue;
  4743. }
  4744. meta.codecWidth = config.codec_size.width;
  4745. meta.codecHeight = config.codec_size.height;
  4746. meta.presentWidth = config.present_size.width;
  4747. meta.presentHeight = config.present_size.height;
  4748. meta.profile = config.profile_string;
  4749. meta.level = config.level_string;
  4750. meta.bitDepth = config.bit_depth;
  4751. meta.chromaFormat = config.chroma_format;
  4752. meta.sarRatio = config.sar_ratio;
  4753. meta.frameRate = config.frame_rate;
  4754. if (config.frame_rate.fixed === false ||
  4755. config.frame_rate.fps_num === 0 ||
  4756. config.frame_rate.fps_den === 0) {
  4757. meta.frameRate = this._referenceFrameRate;
  4758. }
  4759. let fps_den = meta.frameRate.fps_den;
  4760. let fps_num = meta.frameRate.fps_num;
  4761. meta.refSampleDuration = meta.timescale * (fps_den / fps_num);
  4762. let codecArray = sps.subarray(1, 4);
  4763. let codecString = 'avc1.';
  4764. for (let j = 0; j < 3; j++) {
  4765. let h = codecArray[j].toString(16);
  4766. if (h.length < 2) {
  4767. h = '0' + h;
  4768. }
  4769. codecString += h;
  4770. }
  4771. meta.codec = codecString;
  4772. let mi = this._mediaInfo;
  4773. mi.width = meta.codecWidth;
  4774. mi.height = meta.codecHeight;
  4775. mi.fps = meta.frameRate.fps;
  4776. mi.profile = meta.profile;
  4777. mi.level = meta.level;
  4778. mi.chromaFormat = config.chroma_format_string;
  4779. mi.sarNum = meta.sarRatio.width;
  4780. mi.sarDen = meta.sarRatio.height;
  4781. mi.videoCodec = codecString;
  4782. if (mi.hasAudio) {
  4783. if (mi.audioCodec != null) {
  4784. mi.mimeType = 'video/x-flv; codecs="' + mi.videoCodec + ',' + mi.audioCodec + '"';
  4785. }
  4786. } else {
  4787. mi.mimeType = 'video/x-flv; codecs="' + mi.videoCodec + '"';
  4788. }
  4789. if (mi.isComplete()) {
  4790. this._onMediaInfo(mi);
  4791. }
  4792. }
  4793. let ppsCount = v.getUint8(offset); // numOfPictureParameterSets
  4794. if (ppsCount === 0) {
  4795. this._onError(DemuxErrors.FORMAT_ERROR, 'Flv: Invalid AVCDecoderConfigurationRecord: No PPS');
  4796. return;
  4797. } else if (ppsCount > 1) {
  4798. Log.w(this.TAG, \`Flv: Strange AVCDecoderConfigurationRecord: PPS Count = \${ppsCount}\`);
  4799. }
  4800. offset++;
  4801. for (let i = 0; i < ppsCount; i++) {
  4802. let len = v.getUint16(offset, !le); // pictureParameterSetLength
  4803. offset += 2;
  4804. if (len === 0) {
  4805. continue;
  4806. }
  4807. // pps is useless for extracting video information
  4808. offset += len;
  4809. }
  4810. meta.avcc = new Uint8Array(dataSize);
  4811. meta.avcc.set(new Uint8Array(arrayBuffer, dataOffset, dataSize), 0);
  4812. Log.v(this.TAG, 'Parsed AVCDecoderConfigurationRecord');
  4813. if (this._isInitialMetadataDispatched()) {
  4814. // flush parsed frames
  4815. if (this._dispatch && (this._audioTrack.length || this._videoTrack.length)) {
  4816. this._onDataAvailable(this._audioTrack, this._videoTrack);
  4817. }
  4818. } else {
  4819. this._videoInitialMetadataDispatched = true;
  4820. }
  4821. // notify new metadata
  4822. this._dispatch = false;
  4823. this._onTrackMetadata('video', meta);
  4824. }
  4825. _parseAVCVideoData(arrayBuffer, dataOffset, dataSize, tagTimestamp, tagPosition, frameType, cts) {
  4826. let le = this._littleEndian;
  4827. let v = new DataView(arrayBuffer, dataOffset, dataSize);
  4828. let units = [], length = 0;
  4829. let offset = 0;
  4830. const lengthSize = this._naluLengthSize;
  4831. let dts = this._timestampBase + tagTimestamp;
  4832. let keyframe = (frameType === 1); // from FLV Frame Type constants
  4833. let refIdc = 1; // added by qli5
  4834. while (offset < dataSize) {
  4835. if (offset + 4 >= dataSize) {
  4836. Log.w(this.TAG, \`Malformed Nalu near timestamp \${dts}, offset = \${offset}, dataSize = \${dataSize}\`);
  4837. break; // data not enough for next Nalu
  4838. }
  4839. // Nalu with length-header (AVC1)
  4840. let naluSize = v.getUint32(offset, !le); // Big-Endian read
  4841. if (lengthSize === 3) {
  4842. naluSize >>>= 8;
  4843. }
  4844. if (naluSize > dataSize - lengthSize) {
  4845. Log.w(this.TAG, \`Malformed Nalus near timestamp \${dts}, NaluSize > DataSize!\`);
  4846. return;
  4847. }
  4848. let unitType = v.getUint8(offset + lengthSize) & 0x1F;
  4849. // added by qli5
  4850. refIdc = v.getUint8(offset + lengthSize) & 0x60;
  4851. if (unitType === 5) { // IDR
  4852. keyframe = true;
  4853. }
  4854. let data = new Uint8Array(arrayBuffer, dataOffset + offset, lengthSize + naluSize);
  4855. let unit = { type: unitType, data: data };
  4856. units.push(unit);
  4857. length += data.byteLength;
  4858. offset += lengthSize + naluSize;
  4859. }
  4860. if (units.length) {
  4861. let track = this._videoTrack;
  4862. let avcSample = {
  4863. units: units,
  4864. length: length,
  4865. isKeyframe: keyframe,
  4866. refIdc: refIdc,
  4867. dts: dts,
  4868. cts: cts,
  4869. pts: (dts + cts)
  4870. };
  4871. if (keyframe) {
  4872. avcSample.fileposition = tagPosition;
  4873. }
  4874. track.samples.push(avcSample);
  4875. track.length += length;
  4876. }
  4877. }
  4878. }
  4879. /***
  4880. * Copyright (C) 2018 Qli5. All Rights Reserved.
  4881. *
  4882. * @author qli5 <goodlq11[at](163|gmail).com>
  4883. *
  4884. * This Source Code Form is subject to the terms of the Mozilla Public
  4885. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4886. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  4887. */
  4888. class ASS {
  4889. /**
  4890. * Extract sections from ass string
  4891. * @param {string} str
  4892. * @returns {Object} - object from sections
  4893. */
  4894. static extractSections(str) {
  4895. const regex = /^\\ufeff?\\[(.*)\\]\$/mg;
  4896. let match;
  4897. let matchArr = [];
  4898. while ((match = regex.exec(str)) !== null) {
  4899. matchArr.push({ name: match[1], index: match.index });
  4900. }
  4901. let ret = {};
  4902. matchArr.forEach((match, i) => ret[match.name] = str.slice(match.index, matchArr[i + 1] && matchArr[i + 1].index));
  4903. return ret;
  4904. }
  4905. /**
  4906. * Extract subtitle lines from section Events
  4907. * @param {string} str
  4908. * @returns {Array<Object>} - array of subtitle lines
  4909. */
  4910. static extractSubtitleLines(str) {
  4911. const lines = str.split('\\n');
  4912. if (lines[0] != '[Events]' && lines[0] != '[events]') throw new Error('ASSDemuxer: section is not [Events]');
  4913. if (lines[1].indexOf('Format:') != 0 && lines[1].indexOf('format:') != 0) throw new Error('ASSDemuxer: cannot find Format definition in section [Events]');
  4914. const format = lines[1].slice(lines[1].indexOf(':') + 1).split(',').map(e => e.trim());
  4915. return lines.slice(2).map(e => {
  4916. let j = {};
  4917. e.replace(/[d|D]ialogue:\\s*/, '')
  4918. .match(new RegExp(new Array(format.length - 1).fill('(.*?),').join('') + '(.*)'))
  4919. .slice(1)
  4920. .forEach((k, index) => j[format[index]] = k);
  4921. return j;
  4922. });
  4923. }
  4924. /**
  4925. * Create a new ASS Demuxer
  4926. */
  4927. constructor() {
  4928. this.info = '';
  4929. this.styles = '';
  4930. this.events = '';
  4931. this.eventsHeader = '';
  4932. this.pictures = '';
  4933. this.fonts = '';
  4934. this.lines = '';
  4935. }
  4936. get header() {
  4937. // return this.info + this.styles + this.eventsHeader;
  4938. return this.info + this.styles;
  4939. }
  4940. /**
  4941. * Load a file from an arraybuffer of a string
  4942. * @param {(ArrayBuffer|string)} chunk
  4943. */
  4944. parseFile(chunk) {
  4945. const str = typeof chunk == 'string' ? chunk : new _TextDecoder('utf-8').decode(chunk);
  4946. for (let [i, j] of Object.entries(ASS.extractSections(str))) {
  4947. if (i.match(/Script Info(?:mation)?/i)) this.info = j;
  4948. else if (i.match(/V4\\+? Styles?/i)) this.styles = j;
  4949. else if (i.match(/Events?/i)) this.events = j;
  4950. else if (i.match(/Pictures?/i)) this.pictures = j;
  4951. else if (i.match(/Fonts?/i)) this.fonts = j;
  4952. }
  4953. this.eventsHeader = this.events.split('\\n', 2).join('\\n') + '\\n';
  4954. this.lines = ASS.extractSubtitleLines(this.events);
  4955. return this;
  4956. }
  4957. }
  4958. /** Detect free variable \`global\` from Node.js. */
  4959. var freeGlobal = typeof global == 'object' && global && global.Object === Object && global;
  4960. /** Detect free variable \`self\`. */
  4961. var freeSelf = typeof self == 'object' && self && self.Object === Object && self;
  4962. /** Used as a reference to the global object. */
  4963. var root = freeGlobal || freeSelf || Function('return this')();
  4964. /** Built-in value references. */
  4965. var Symbol = root.Symbol;
  4966. /** Used for built-in method references. */
  4967. var objectProto = Object.prototype;
  4968. /** Used to check objects for own properties. */
  4969. var hasOwnProperty = objectProto.hasOwnProperty;
  4970. /**
  4971. * Used to resolve the
  4972. * [\`toStringTag\`](http://ecma-international.org/ecma-262/7.0/#sec-object.prototype.tostring)
  4973. * of values.
  4974. */
  4975. var nativeObjectToString = objectProto.toString;
  4976. /** Built-in value references. */
  4977. var symToStringTag = Symbol ? Symbol.toStringTag : undefined;
  4978. /**
  4979. * A specialized version of \`baseGetTag\` which ignores \`Symbol.toStringTag\` values.
  4980. *
  4981. * @private
  4982. * @param {*} value The value to query.
  4983. * @returns {string} Returns the raw \`toStringTag\`.
  4984. */
  4985. function getRawTag(value) {
  4986. var isOwn = hasOwnProperty.call(value, symToStringTag),
  4987. tag = value[symToStringTag];
  4988. try {
  4989. value[symToStringTag] = undefined;
  4990. var unmasked = true;
  4991. } catch (e) {}
  4992. var r###lt = nativeObjectToString.call(value);
  4993. if (unmasked) {
  4994. if (isOwn) {
  4995. value[symToStringTag] = tag;
  4996. } else {
  4997. delete value[symToStringTag];
  4998. }
  4999. }
  5000. return r###lt;
  5001. }
  5002. /** Used for built-in method references. */
  5003. var objectProto\$1 = Object.prototype;
  5004. /**
  5005. * Used to resolve the
  5006. * [\`toStringTag\`](http://ecma-international.org/ecma-262/7.0/#sec-object.prototype.tostring)
  5007. * of values.
  5008. */
  5009. var nativeObjectToString\$1 = objectProto\$1.toString;
  5010. /**
  5011. * Converts \`value\` to a string using \`Object.prototype.toString\`.
  5012. *
  5013. * @private
  5014. * @param {*} value The value to convert.
  5015. * @returns {string} Returns the converted string.
  5016. */
  5017. function objectToString(value) {
  5018. return nativeObjectToString\$1.call(value);
  5019. }
  5020. /** \`Object#toString\` r###lt references. */
  5021. var nullTag = '[object Null]',
  5022. undefinedTag = '[object Undefined]';
  5023. /** Built-in value references. */
  5024. var symToStringTag\$1 = Symbol ? Symbol.toStringTag : undefined;
  5025. /**
  5026. * The base implementation of \`getTag\` without fallbacks for buggy environments.
  5027. *
  5028. * @private
  5029. * @param {*} value The value to query.
  5030. * @returns {string} Returns the \`toStringTag\`.
  5031. */
  5032. function baseGetTag(value) {
  5033. if (value == null) {
  5034. return value === undefined ? undefinedTag : nullTag;
  5035. }
  5036. return (symToStringTag\$1 && symToStringTag\$1 in Object(value))
  5037. ? getRawTag(value)
  5038. : objectToString(value);
  5039. }
  5040. /**
  5041. * Checks if \`value\` is the
  5042. * [language type](http://www.ecma-international.org/ecma-262/7.0/#sec-ecmascript-language-types)
  5043. * of \`Object\`. (e.g. arrays, functions, objects, regexes, \`new Number(0)\`, and \`new String('')\`)
  5044. *
  5045. * @static
  5046. * @memberOf _
  5047. * @since 0.1.0
  5048. * @category Lang
  5049. * @param {*} value The value to check.
  5050. * @returns {boolean} Returns \`true\` if \`value\` is an object, else \`false\`.
  5051. * @example
  5052. *
  5053. * _.isObject({});
  5054. * // => true
  5055. *
  5056. * _.isObject([1, 2, 3]);
  5057. * // => true
  5058. *
  5059. * _.isObject(_.noop);
  5060. * // => true
  5061. *
  5062. * _.isObject(null);
  5063. * // => false
  5064. */
  5065. function isObject(value) {
  5066. var type = typeof value;
  5067. return value != null && (type == 'object' || type == 'function');
  5068. }
  5069. /** \`Object#toString\` r###lt references. */
  5070. var asyncTag = '[object AsyncFunction]',
  5071. funcTag = '[object Function]',
  5072. genTag = '[object GeneratorFunction]',
  5073. proxyTag = '[object Proxy]';
  5074. /**
  5075. * Checks if \`value\` is classified as a \`Function\` object.
  5076. *
  5077. * @static
  5078. * @memberOf _
  5079. * @since 0.1.0
  5080. * @category Lang
  5081. * @param {*} value The value to check.
  5082. * @returns {boolean} Returns \`true\` if \`value\` is a function, else \`false\`.
  5083. * @example
  5084. *
  5085. * _.isFunction(_);
  5086. * // => true
  5087. *
  5088. * _.isFunction(/abc/);
  5089. * // => false
  5090. */
  5091. function isFunction(value) {
  5092. if (!isObject(value)) {
  5093. return false;
  5094. }
  5095. // The use of \`Object#toString\` avoids issues with the \`typeof\` operator
  5096. // in Safari 9 which returns 'object' for typed arrays and other constructors.
  5097. var tag = baseGetTag(value);
  5098. return tag == funcTag || tag == genTag || tag == asyncTag || tag == proxyTag;
  5099. }
  5100. /** Used to detect overreaching core-js shims. */
  5101. var coreJsData = root['__core-js_shared__'];
  5102. /** Used to detect methods masquerading as native. */
  5103. var maskSrcKey = (function() {
  5104. var uid = /[^.]+\$/.exec(coreJsData && coreJsData.keys && coreJsData.keys.IE_PROTO || '');
  5105. return uid ? ('Symbol(src)_1.' + uid) : '';
  5106. }());
  5107. /**
  5108. * Checks if \`func\` has its source masked.
  5109. *
  5110. * @private
  5111. * @param {Function} func The function to check.
  5112. * @returns {boolean} Returns \`true\` if \`func\` is masked, else \`false\`.
  5113. */
  5114. function isMasked(func) {
  5115. return !!maskSrcKey && (maskSrcKey in func);
  5116. }
  5117. /** Used for built-in method references. */
  5118. var funcProto = Function.prototype;
  5119. /** Used to resolve the decompiled source of functions. */
  5120. var funcToString = funcProto.toString;
  5121. /**
  5122. * Converts \`func\` to its source code.
  5123. *
  5124. * @private
  5125. * @param {Function} func The function to convert.
  5126. * @returns {string} Returns the source code.
  5127. */
  5128. function toSource(func) {
  5129. if (func != null) {
  5130. try {
  5131. return funcToString.call(func);
  5132. } catch (e) {}
  5133. try {
  5134. return (func + '');
  5135. } catch (e) {}
  5136. }
  5137. return '';
  5138. }
  5139. /**
  5140. * Used to match \`RegExp\`
  5141. * [syntax characters](http://ecma-international.org/ecma-262/7.0/#sec-patterns).
  5142. */
  5143. var reRegExpChar = /[\\\\^\$.*+?()[\\]{}|]/g;
  5144. /** Used to detect host constructors (Safari). */
  5145. var reIsHostCtor = /^\\[object .+?Constructor\\]\$/;
  5146. /** Used for built-in method references. */
  5147. var funcProto\$1 = Function.prototype,
  5148. objectProto\$2 = Object.prototype;
  5149. /** Used to resolve the decompiled source of functions. */
  5150. var funcToString\$1 = funcProto\$1.toString;
  5151. /** Used to check objects for own properties. */
  5152. var hasOwnProperty\$1 = objectProto\$2.hasOwnProperty;
  5153. /** Used to detect if a method is native. */
  5154. var reIsNative = RegExp('^' +
  5155. funcToString\$1.call(hasOwnProperty\$1).replace(reRegExpChar, '\\\\\$&')
  5156. .replace(/hasOwnProperty|(function).*?(?=\\\\\\()| for .+?(?=\\\\\\])/g, '\$1.*?') + '\$'
  5157. );
  5158. /**
  5159. * The base implementation of \`_.isNative\` without bad shim checks.
  5160. *
  5161. * @private
  5162. * @param {*} value The value to check.
  5163. * @returns {boolean} Returns \`true\` if \`value\` is a native function,
  5164. * else \`false\`.
  5165. */
  5166. function baseIsNative(value) {
  5167. if (!isObject(value) || isMasked(value)) {
  5168. return false;
  5169. }
  5170. var pattern = isFunction(value) ? reIsNative : reIsHostCtor;
  5171. return pattern.test(toSource(value));
  5172. }
  5173. /**
  5174. * Gets the value at \`key\` of \`object\`.
  5175. *
  5176. * @private
  5177. * @param {Object} [object] The object to query.
  5178. * @param {string} key The key of the property to get.
  5179. * @returns {*} Returns the property value.
  5180. */
  5181. function getValue(object, key) {
  5182. return object == null ? undefined : object[key];
  5183. }
  5184. /**
  5185. * Gets the native function at \`key\` of \`object\`.
  5186. *
  5187. * @private
  5188. * @param {Object} object The object to query.
  5189. * @param {string} key The key of the method to get.
  5190. * @returns {*} Returns the function if it's native, else \`undefined\`.
  5191. */
  5192. function getNative(object, key) {
  5193. var value = getValue(object, key);
  5194. return baseIsNative(value) ? value : undefined;
  5195. }
  5196. /* Built-in method references that are verified to be native. */
  5197. var nativeCreate = getNative(Object, 'create');
  5198. /**
  5199. * Removes all key-value entries from the hash.
  5200. *
  5201. * @private
  5202. * @name clear
  5203. * @memberOf Hash
  5204. */
  5205. function hashClear() {
  5206. this.__data__ = nativeCreate ? nativeCreate(null) : {};
  5207. this.size = 0;
  5208. }
  5209. /**
  5210. * Removes \`key\` and its value from the hash.
  5211. *
  5212. * @private
  5213. * @name delete
  5214. * @memberOf Hash
  5215. * @param {Object} hash The hash to modify.
  5216. * @param {string} key The key of the value to remove.
  5217. * @returns {boolean} Returns \`true\` if the entry was removed, else \`false\`.
  5218. */
  5219. function hashDelete(key) {
  5220. var r###lt = this.has(key) && delete this.__data__[key];
  5221. this.size -= r###lt ? 1 : 0;
  5222. return r###lt;
  5223. }
  5224. /** Used to stand-in for \`undefined\` hash values. */
  5225. var HASH_UNDEFINED = '__lodash_hash_undefined__';
  5226. /** Used for built-in method references. */
  5227. var objectProto\$3 = Object.prototype;
  5228. /** Used to check objects for own properties. */
  5229. var hasOwnProperty\$2 = objectProto\$3.hasOwnProperty;
  5230. /**
  5231. * Gets the hash value for \`key\`.
  5232. *
  5233. * @private
  5234. * @name get
  5235. * @memberOf Hash
  5236. * @param {string} key The key of the value to get.
  5237. * @returns {*} Returns the entry value.
  5238. */
  5239. function hashGet(key) {
  5240. var data = this.__data__;
  5241. if (nativeCreate) {
  5242. var r###lt = data[key];
  5243. return r###lt === HASH_UNDEFINED ? undefined : r###lt;
  5244. }
  5245. return hasOwnProperty\$2.call(data, key) ? data[key] : undefined;
  5246. }
  5247. /** Used for built-in method references. */
  5248. var objectProto\$4 = Object.prototype;
  5249. /** Used to check objects for own properties. */
  5250. var hasOwnProperty\$3 = objectProto\$4.hasOwnProperty;
  5251. /**
  5252. * Checks if a hash value for \`key\` exists.
  5253. *
  5254. * @private
  5255. * @name has
  5256. * @memberOf Hash
  5257. * @param {string} key The key of the entry to check.
  5258. * @returns {boolean} Returns \`true\` if an entry for \`key\` exists, else \`false\`.
  5259. */
  5260. function hashHas(key) {
  5261. var data = this.__data__;
  5262. return nativeCreate ? (data[key] !== undefined) : hasOwnProperty\$3.call(data, key);
  5263. }
  5264. /** Used to stand-in for \`undefined\` hash values. */
  5265. var HASH_UNDEFINED\$1 = '__lodash_hash_undefined__';
  5266. /**
  5267. * Sets the hash \`key\` to \`value\`.
  5268. *
  5269. * @private
  5270. * @name set
  5271. * @memberOf Hash
  5272. * @param {string} key The key of the value to set.
  5273. * @param {*} value The value to set.
  5274. * @returns {Object} Returns the hash instance.
  5275. */
  5276. function hashSet(key, value) {
  5277. var data = this.__data__;
  5278. this.size += this.has(key) ? 0 : 1;
  5279. data[key] = (nativeCreate && value === undefined) ? HASH_UNDEFINED\$1 : value;
  5280. return this;
  5281. }
  5282. /**
  5283. * Creates a hash object.
  5284. *
  5285. * @private
  5286. * @constructor
  5287. * @param {Array} [entries] The key-value pairs to cache.
  5288. */
  5289. function Hash(entries) {
  5290. var index = -1,
  5291. length = entries == null ? 0 : entries.length;
  5292. this.clear();
  5293. while (++index < length) {
  5294. var entry = entries[index];
  5295. this.set(entry[0], entry[1]);
  5296. }
  5297. }
  5298. // Add methods to \`Hash\`.
  5299. Hash.prototype.clear = hashClear;
  5300. Hash.prototype['delete'] = hashDelete;
  5301. Hash.prototype.get = hashGet;
  5302. Hash.prototype.has = hashHas;
  5303. Hash.prototype.set = hashSet;
  5304. /**
  5305. * Removes all key-value entries from the list cache.
  5306. *
  5307. * @private
  5308. * @name clear
  5309. * @memberOf ListCache
  5310. */
  5311. function listCacheClear() {
  5312. this.__data__ = [];
  5313. this.size = 0;
  5314. }
  5315. /**
  5316. * Performs a
  5317. * [\`SameValueZero\`](http://ecma-international.org/ecma-262/7.0/#sec-samevaluezero)
  5318. * comparison between two values to determine if they are equivalent.
  5319. *
  5320. * @static
  5321. * @memberOf _
  5322. * @since 4.0.0
  5323. * @category Lang
  5324. * @param {*} value The value to compare.
  5325. * @param {*} other The other value to compare.
  5326. * @returns {boolean} Returns \`true\` if the values are equivalent, else \`false\`.
  5327. * @example
  5328. *
  5329. * var object = { 'a': 1 };
  5330. * var other = { 'a': 1 };
  5331. *
  5332. * _.eq(object, object);
  5333. * // => true
  5334. *
  5335. * _.eq(object, other);
  5336. * // => false
  5337. *
  5338. * _.eq('a', 'a');
  5339. * // => true
  5340. *
  5341. * _.eq('a', Object('a'));
  5342. * // => false
  5343. *
  5344. * _.eq(NaN, NaN);
  5345. * // => true
  5346. */
  5347. function eq(value, other) {
  5348. return value === other || (value !== value && other !== other);
  5349. }
  5350. /**
  5351. * Gets the index at which the \`key\` is found in \`array\` of key-value pairs.
  5352. *
  5353. * @private
  5354. * @param {Array} array The array to inspect.
  5355. * @param {*} key The key to search for.
  5356. * @returns {number} Returns the index of the matched value, else \`-1\`.
  5357. */
  5358. function assocIndexOf(array, key) {
  5359. var length = array.length;
  5360. while (length--) {
  5361. if (eq(array[length][0], key)) {
  5362. return length;
  5363. }
  5364. }
  5365. return -1;
  5366. }
  5367. /** Used for built-in method references. */
  5368. var arrayProto = Array.prototype;
  5369. /** Built-in value references. */
  5370. var splice = arrayProto.splice;
  5371. /**
  5372. * Removes \`key\` and its value from the list cache.
  5373. *
  5374. * @private
  5375. * @name delete
  5376. * @memberOf ListCache
  5377. * @param {string} key The key of the value to remove.
  5378. * @returns {boolean} Returns \`true\` if the entry was removed, else \`false\`.
  5379. */
  5380. function listCacheDelete(key) {
  5381. var data = this.__data__,
  5382. index = assocIndexOf(data, key);
  5383. if (index < 0) {
  5384. return false;
  5385. }
  5386. var lastIndex = data.length - 1;
  5387. if (index == lastIndex) {
  5388. data.pop();
  5389. } else {
  5390. splice.call(data, index, 1);
  5391. }
  5392. --this.size;
  5393. return true;
  5394. }
  5395. /**
  5396. * Gets the list cache value for \`key\`.
  5397. *
  5398. * @private
  5399. * @name get
  5400. * @memberOf ListCache
  5401. * @param {string} key The key of the value to get.
  5402. * @returns {*} Returns the entry value.
  5403. */
  5404. function listCacheGet(key) {
  5405. var data = this.__data__,
  5406. index = assocIndexOf(data, key);
  5407. return index < 0 ? undefined : data[index][1];
  5408. }
  5409. /**
  5410. * Checks if a list cache value for \`key\` exists.
  5411. *
  5412. * @private
  5413. * @name has
  5414. * @memberOf ListCache
  5415. * @param {string} key The key of the entry to check.
  5416. * @returns {boolean} Returns \`true\` if an entry for \`key\` exists, else \`false\`.
  5417. */
  5418. function listCacheHas(key) {
  5419. return assocIndexOf(this.__data__, key) > -1;
  5420. }
  5421. /**
  5422. * Sets the list cache \`key\` to \`value\`.
  5423. *
  5424. * @private
  5425. * @name set
  5426. * @memberOf ListCache
  5427. * @param {string} key The key of the value to set.
  5428. * @param {*} value The value to set.
  5429. * @returns {Object} Returns the list cache instance.
  5430. */
  5431. function listCacheSet(key, value) {
  5432. var data = this.__data__,
  5433. index = assocIndexOf(data, key);
  5434. if (index < 0) {
  5435. ++this.size;
  5436. data.push([key, value]);
  5437. } else {
  5438. data[index][1] = value;
  5439. }
  5440. return this;
  5441. }
  5442. /**
  5443. * Creates an list cache object.
  5444. *
  5445. * @private
  5446. * @constructor
  5447. * @param {Array} [entries] The key-value pairs to cache.
  5448. */
  5449. function ListCache(entries) {
  5450. var index = -1,
  5451. length = entries == null ? 0 : entries.length;
  5452. this.clear();
  5453. while (++index < length) {
  5454. var entry = entries[index];
  5455. this.set(entry[0], entry[1]);
  5456. }
  5457. }
  5458. // Add methods to \`ListCache\`.
  5459. ListCache.prototype.clear = listCacheClear;
  5460. ListCache.prototype['delete'] = listCacheDelete;
  5461. ListCache.prototype.get = listCacheGet;
  5462. ListCache.prototype.has = listCacheHas;
  5463. ListCache.prototype.set = listCacheSet;
  5464. /* Built-in method references that are verified to be native. */
  5465. var Map = getNative(root, 'Map');
  5466. /**
  5467. * Removes all key-value entries from the map.
  5468. *
  5469. * @private
  5470. * @name clear
  5471. * @memberOf MapCache
  5472. */
  5473. function mapCacheClear() {
  5474. this.size = 0;
  5475. this.__data__ = {
  5476. 'hash': new Hash,
  5477. 'map': new (Map || ListCache),
  5478. 'string': new Hash
  5479. };
  5480. }
  5481. /**
  5482. * Checks if \`value\` is suitable for use as unique object key.
  5483. *
  5484. * @private
  5485. * @param {*} value The value to check.
  5486. * @returns {boolean} Returns \`true\` if \`value\` is suitable, else \`false\`.
  5487. */
  5488. function isKeyable(value) {
  5489. var type = typeof value;
  5490. return (type == 'string' || type == 'number' || type == 'symbol' || type == 'boolean')
  5491. ? (value !== '__proto__')
  5492. : (value === null);
  5493. }
  5494. /**
  5495. * Gets the data for \`map\`.
  5496. *
  5497. * @private
  5498. * @param {Object} map The map to query.
  5499. * @param {string} key The reference key.
  5500. * @returns {*} Returns the map data.
  5501. */
  5502. function getMapData(map, key) {
  5503. var data = map.__data__;
  5504. return isKeyable(key)
  5505. ? data[typeof key == 'string' ? 'string' : 'hash']
  5506. : data.map;
  5507. }
  5508. /**
  5509. * Removes \`key\` and its value from the map.
  5510. *
  5511. * @private
  5512. * @name delete
  5513. * @memberOf MapCache
  5514. * @param {string} key The key of the value to remove.
  5515. * @returns {boolean} Returns \`true\` if the entry was removed, else \`false\`.
  5516. */
  5517. function mapCacheDelete(key) {
  5518. var r###lt = getMapData(this, key)['delete'](key);
  5519. this.size -= r###lt ? 1 : 0;
  5520. return r###lt;
  5521. }
  5522. /**
  5523. * Gets the map value for \`key\`.
  5524. *
  5525. * @private
  5526. * @name get
  5527. * @memberOf MapCache
  5528. * @param {string} key The key of the value to get.
  5529. * @returns {*} Returns the entry value.
  5530. */
  5531. function mapCacheGet(key) {
  5532. return getMapData(this, key).get(key);
  5533. }
  5534. /**
  5535. * Checks if a map value for \`key\` exists.
  5536. *
  5537. * @private
  5538. * @name has
  5539. * @memberOf MapCache
  5540. * @param {string} key The key of the entry to check.
  5541. * @returns {boolean} Returns \`true\` if an entry for \`key\` exists, else \`false\`.
  5542. */
  5543. function mapCacheHas(key) {
  5544. return getMapData(this, key).has(key);
  5545. }
  5546. /**
  5547. * Sets the map \`key\` to \`value\`.
  5548. *
  5549. * @private
  5550. * @name set
  5551. * @memberOf MapCache
  5552. * @param {string} key The key of the value to set.
  5553. * @param {*} value The value to set.
  5554. * @returns {Object} Returns the map cache instance.
  5555. */
  5556. function mapCacheSet(key, value) {
  5557. var data = getMapData(this, key),
  5558. size = data.size;
  5559. data.set(key, value);
  5560. this.size += data.size == size ? 0 : 1;
  5561. return this;
  5562. }
  5563. /**
  5564. * Creates a map cache object to store key-value pairs.
  5565. *
  5566. * @private
  5567. * @constructor
  5568. * @param {Array} [entries] The key-value pairs to cache.
  5569. */
  5570. function MapCache(entries) {
  5571. var index = -1,
  5572. length = entries == null ? 0 : entries.length;
  5573. this.clear();
  5574. while (++index < length) {
  5575. var entry = entries[index];
  5576. this.set(entry[0], entry[1]);
  5577. }
  5578. }
  5579. // Add methods to \`MapCache\`.
  5580. MapCache.prototype.clear = mapCacheClear;
  5581. MapCache.prototype['delete'] = mapCacheDelete;
  5582. MapCache.prototype.get = mapCacheGet;
  5583. MapCache.prototype.has = mapCacheHas;
  5584. MapCache.prototype.set = mapCacheSet;
  5585. /** Error message constants. */
  5586. var FUNC_ERROR_TEXT = 'Expected a function';
  5587. /**
  5588. * Creates a function that memoizes the r###lt of \`func\`. If \`resolver\` is
  5589. * provided, it determines the cache key for storing the r###lt based on the
  5590. * arguments provided to the memoized function. By default, the first argument
  5591. * provided to the memoized function is used as the map cache key. The \`func\`
  5592. * is invoked with the \`this\` binding of the memoized function.
  5593. *
  5594. * **Note:** The cache is exposed as the \`cache\` property on the memoized
  5595. * function. Its creation may be customized by replacing the \`_.memoize.Cache\`
  5596. * constructor with one whose instances implement the
  5597. * [\`Map\`](http://ecma-international.org/ecma-262/7.0/#sec-properties-of-the-map-prototype-object)
  5598. * method interface of \`clear\`, \`delete\`, \`get\`, \`has\`, and \`set\`.
  5599. *
  5600. * @static
  5601. * @memberOf _
  5602. * @since 0.1.0
  5603. * @category Function
  5604. * @param {Function} func The function to have its output memoized.
  5605. * @param {Function} [resolver] The function to resolve the cache key.
  5606. * @returns {Function} Returns the new memoized function.
  5607. * @example
  5608. *
  5609. * var object = { 'a': 1, 'b': 2 };
  5610. * var other = { 'c': 3, 'd': 4 };
  5611. *
  5612. * var values = _.memoize(_.values);
  5613. * values(object);
  5614. * // => [1, 2]
  5615. *
  5616. * values(other);
  5617. * // => [3, 4]
  5618. *
  5619. * object.a = 2;
  5620. * values(object);
  5621. * // => [1, 2]
  5622. *
  5623. * // Modify the r###lt cache.
  5624. * values.cache.set(object, ['a', 'b']);
  5625. * values(object);
  5626. * // => ['a', 'b']
  5627. *
  5628. * // Replace \`_.memoize.Cache\`.
  5629. * _.memoize.Cache = WeakMap;
  5630. */
  5631. function memoize(func, resolver) {
  5632. if (typeof func != 'function' || (resolver != null && typeof resolver != 'function')) {
  5633. throw new TypeError(FUNC_ERROR_TEXT);
  5634. }
  5635. var memoized = function() {
  5636. var args = arguments,
  5637. key = resolver ? resolver.apply(this, args) : args[0],
  5638. cache = memoized.cache;
  5639. if (cache.has(key)) {
  5640. return cache.get(key);
  5641. }
  5642. var r###lt = func.apply(this, args);
  5643. memoized.cache = cache.set(key, r###lt) || cache;
  5644. return r###lt;
  5645. };
  5646. memoized.cache = new (memoize.Cache || MapCache);
  5647. return memoized;
  5648. }
  5649. // Expose \`MapCache\`.
  5650. memoize.Cache = MapCache;
  5651. const numberToByteArray = (num, byteLength = getNumberByteLength(num)) => {
  5652. var byteArray;
  5653. if (byteLength == 1) {
  5654. byteArray = new DataView(new ArrayBuffer(1));
  5655. byteArray.setUint8(0, num);
  5656. }
  5657. else if (byteLength == 2) {
  5658. byteArray = new DataView(new ArrayBuffer(2));
  5659. byteArray.setUint16(0, num);
  5660. }
  5661. else if (byteLength == 3) {
  5662. byteArray = new DataView(new ArrayBuffer(3));
  5663. byteArray.setUint8(0, num >> 16);
  5664. byteArray.setUint16(1, num & 0xffff);
  5665. }
  5666. else if (byteLength == 4) {
  5667. byteArray = new DataView(new ArrayBuffer(4));
  5668. byteArray.setUint32(0, num);
  5669. }
  5670. else if (num < 0xffffffff) {
  5671. byteArray = new DataView(new ArrayBuffer(5));
  5672. byteArray.setUint32(1, num);
  5673. }
  5674. else if (byteLength == 5) {
  5675. byteArray = new DataView(new ArrayBuffer(5));
  5676. byteArray.setUint8(0, num / 0x100000000 | 0);
  5677. byteArray.setUint32(1, num % 0x100000000);
  5678. }
  5679. else if (byteLength == 6) {
  5680. byteArray = new DataView(new ArrayBuffer(6));
  5681. byteArray.setUint16(0, num / 0x100000000 | 0);
  5682. byteArray.setUint32(2, num % 0x100000000);
  5683. }
  5684. else if (byteLength == 7) {
  5685. byteArray = new DataView(new ArrayBuffer(7));
  5686. byteArray.setUint8(0, num / 0x1000000000000 | 0);
  5687. byteArray.setUint16(1, num / 0x100000000 & 0xffff);
  5688. byteArray.setUint32(3, num % 0x100000000);
  5689. }
  5690. else if (byteLength == 8) {
  5691. byteArray = new DataView(new ArrayBuffer(8));
  5692. byteArray.setUint32(0, num / 0x100000000 | 0);
  5693. byteArray.setUint32(4, num % 0x100000000);
  5694. }
  5695. else {
  5696. throw new Error("EBML.typedArrayUtils.numberToByteArray: byte length must be less than or equal to 8");
  5697. }
  5698. return new Uint8Array(byteArray.buffer);
  5699. };
  5700. const stringToByteArray = memoize((str) => {
  5701. return Uint8Array.from(Array.from(str).map(_ => _.codePointAt(0)));
  5702. });
  5703. function getNumberByteLength(num) {
  5704. if (num < 0) {
  5705. throw new Error("EBML.typedArrayUtils.getNumberByteLength: negative number not implemented");
  5706. }
  5707. else if (num < 0x100) {
  5708. return 1;
  5709. }
  5710. else if (num < 0x10000) {
  5711. return 2;
  5712. }
  5713. else if (num < 0x1000000) {
  5714. return 3;
  5715. }
  5716. else if (num < 0x100000000) {
  5717. return 4;
  5718. }
  5719. else if (num < 0x10000000000) {
  5720. return 5;
  5721. }
  5722. else if (num < 0x1000000000000) {
  5723. return 6;
  5724. }
  5725. else if (num < 0x20000000000000) {
  5726. return 7;
  5727. }
  5728. else {
  5729. throw new Error("EBML.typedArrayUtils.getNumberByteLength: number exceeds Number.MAX_SAFE_INTEGER");
  5730. }
  5731. }
  5732. const int16Bit = memoize((num) => {
  5733. const ab = new ArrayBuffer(2);
  5734. new DataView(ab).setInt16(0, num);
  5735. return new Uint8Array(ab);
  5736. });
  5737. const float32bit = memoize((num) => {
  5738. const ab = new ArrayBuffer(4);
  5739. new DataView(ab).setFloat32(0, num);
  5740. return new Uint8Array(ab);
  5741. });
  5742. const dumpBytes = (b) => {
  5743. return Array.from(new Uint8Array(b)).map(_ => \`0x\${_.toString(16)}\`).join(", ");
  5744. };
  5745. class Value {
  5746. constructor(bytes) {
  5747. this.bytes = bytes;
  5748. }
  5749. write(buf, pos) {
  5750. buf.set(this.bytes, pos);
  5751. return pos + this.bytes.length;
  5752. }
  5753. countSize() {
  5754. return this.bytes.length;
  5755. }
  5756. }
  5757. class Element {
  5758. constructor(id, children, isSizeUnknown) {
  5759. this.id = id;
  5760. this.children = children;
  5761. const bodySize = this.children.reduce((p, c) => p + c.countSize(), 0);
  5762. this.sizeMetaData = isSizeUnknown ?
  5763. UNKNOWN_SIZE :
  5764. vintEncode(numberToByteArray(bodySize, getEBMLByteLength(bodySize)));
  5765. this.size = this.id.length + this.sizeMetaData.length + bodySize;
  5766. }
  5767. write(buf, pos) {
  5768. buf.set(this.id, pos);
  5769. buf.set(this.sizeMetaData, pos + this.id.length);
  5770. return this.children.reduce((p, c) => c.write(buf, p), pos + this.id.length + this.sizeMetaData.length);
  5771. }
  5772. countSize() {
  5773. return this.size;
  5774. }
  5775. }
  5776. const bytes = memoize((data) => {
  5777. return new Value(data);
  5778. });
  5779. const number = memoize((num) => {
  5780. return bytes(numberToByteArray(num));
  5781. });
  5782. const vintEncodedNumber = memoize((num) => {
  5783. return bytes(vintEncode(numberToByteArray(num, getEBMLByteLength(num))));
  5784. });
  5785. const int16 = memoize((num) => {
  5786. return bytes(int16Bit(num));
  5787. });
  5788. const float = memoize((num) => {
  5789. return bytes(float32bit(num));
  5790. });
  5791. const string = memoize((str) => {
  5792. return bytes(stringToByteArray(str));
  5793. });
  5794. const element = (id, child) => {
  5795. return new Element(id, Array.isArray(child) ? child : [child], false);
  5796. };
  5797. const unknownSizeElement = (id, child) => {
  5798. return new Element(id, Array.isArray(child) ? child : [child], true);
  5799. };
  5800. const build = (v) => {
  5801. const b = new Uint8Array(v.countSize());
  5802. v.write(b, 0);
  5803. return b;
  5804. };
  5805. const getEBMLByteLength = (num) => {
  5806. if (num < 0x7f) {
  5807. return 1;
  5808. }
  5809. else if (num < 0x3fff) {
  5810. return 2;
  5811. }
  5812. else if (num < 0x1fffff) {
  5813. return 3;
  5814. }
  5815. else if (num < 0xfffffff) {
  5816. return 4;
  5817. }
  5818. else if (num < 0x7ffffffff) {
  5819. return 5;
  5820. }
  5821. else if (num < 0x3ffffffffff) {
  5822. return 6;
  5823. }
  5824. else if (num < 0x1ffffffffffff) {
  5825. return 7;
  5826. }
  5827. else if (num < 0x20000000000000) {
  5828. return 8;
  5829. }
  5830. else if (num < 0xffffffffffffff) {
  5831. throw new Error("EBMLgetEBMLByteLength: number exceeds Number.MAX_SAFE_INTEGER");
  5832. }
  5833. else {
  5834. throw new Error("EBMLgetEBMLByteLength: data size must be less than or equal to " + (Math.pow(2, 56) - 2));
  5835. }
  5836. };
  5837. const UNKNOWN_SIZE = new Uint8Array([0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]);
  5838. const vintEncode = (byteArray) => {
  5839. byteArray[0] = getSizeMask(byteArray.length) | byteArray[0];
  5840. return byteArray;
  5841. };
  5842. const getSizeMask = (byteLength) => {
  5843. return 0x80 >> (byteLength - 1);
  5844. };
  5845. /**
  5846. * @see https://www.matroska.org/technical/specs/index.html
  5847. */
  5848. const ID = {
  5849. EBML: Uint8Array.of(0x1A, 0x45, 0xDF, 0xA3),
  5850. EBMLVersion: Uint8Array.of(0x42, 0x86),
  5851. EBMLReadVersion: Uint8Array.of(0x42, 0xF7),
  5852. EBMLMaxIDLength: Uint8Array.of(0x42, 0xF2),
  5853. EBMLMaxSizeLength: Uint8Array.of(0x42, 0xF3),
  5854. DocType: Uint8Array.of(0x42, 0x82),
  5855. DocTypeVersion: Uint8Array.of(0x42, 0x87),
  5856. DocTypeReadVersion: Uint8Array.of(0x42, 0x85),
  5857. Void: Uint8Array.of(0xEC),
  5858. CRC32: Uint8Array.of(0xBF),
  5859. Segment: Uint8Array.of(0x18, 0x53, 0x80, 0x67),
  5860. SeekHead: Uint8Array.of(0x11, 0x4D, 0x9B, 0x74),
  5861. Seek: Uint8Array.of(0x4D, 0xBB),
  5862. SeekID: Uint8Array.of(0x53, 0xAB),
  5863. SeekPosition: Uint8Array.of(0x53, 0xAC),
  5864. Info: Uint8Array.of(0x15, 0x49, 0xA9, 0x66),
  5865. SegmentUID: Uint8Array.of(0x73, 0xA4),
  5866. SegmentFilename: Uint8Array.of(0x73, 0x84),
  5867. PrevUID: Uint8Array.of(0x3C, 0xB9, 0x23),
  5868. PrevFilename: Uint8Array.of(0x3C, 0x83, 0xAB),
  5869. NextUID: Uint8Array.of(0x3E, 0xB9, 0x23),
  5870. NextFilename: Uint8Array.of(0x3E, 0x83, 0xBB),
  5871. SegmentFamily: Uint8Array.of(0x44, 0x44),
  5872. ChapterTranslate: Uint8Array.of(0x69, 0x24),
  5873. ChapterTranslateEditionUID: Uint8Array.of(0x69, 0xFC),
  5874. ChapterTranslateCodec: Uint8Array.of(0x69, 0xBF),
  5875. ChapterTranslateID: Uint8Array.of(0x69, 0xA5),
  5876. TimecodeScale: Uint8Array.of(0x2A, 0xD7, 0xB1),
  5877. Duration: Uint8Array.of(0x44, 0x89),
  5878. DateUTC: Uint8Array.of(0x44, 0x61),
  5879. Title: Uint8Array.of(0x7B, 0xA9),
  5880. MuxingApp: Uint8Array.of(0x4D, 0x80),
  5881. WritingApp: Uint8Array.of(0x57, 0x41),
  5882. Cluster: Uint8Array.of(0x1F, 0x43, 0xB6, 0x75),
  5883. Timecode: Uint8Array.of(0xE7),
  5884. SilentTracks: Uint8Array.of(0x58, 0x54),
  5885. SilentTrackNumber: Uint8Array.of(0x58, 0xD7),
  5886. Position: Uint8Array.of(0xA7),
  5887. PrevSize: Uint8Array.of(0xAB),
  5888. SimpleBlock: Uint8Array.of(0xA3),
  5889. BlockGroup: Uint8Array.of(0xA0),
  5890. Block: Uint8Array.of(0xA1),
  5891. BlockAdditions: Uint8Array.of(0x75, 0xA1),
  5892. BlockMore: Uint8Array.of(0xA6),
  5893. BlockAddID: Uint8Array.of(0xEE),
  5894. BlockAdditional: Uint8Array.of(0xA5),
  5895. BlockDuration: Uint8Array.of(0x9B),
  5896. ReferencePriority: Uint8Array.of(0xFA),
  5897. ReferenceBlock: Uint8Array.of(0xFB),
  5898. CodecState: Uint8Array.of(0xA4),
  5899. DiscardPadding: Uint8Array.of(0x75, 0xA2),
  5900. Slices: Uint8Array.of(0x8E),
  5901. TimeSlice: Uint8Array.of(0xE8),
  5902. LaceNumber: Uint8Array.of(0xCC),
  5903. Tracks: Uint8Array.of(0x16, 0x54, 0xAE, 0x6B),
  5904. TrackEntry: Uint8Array.of(0xAE),
  5905. TrackNumber: Uint8Array.of(0xD7),
  5906. TrackUID: Uint8Array.of(0x73, 0xC5),
  5907. TrackType: Uint8Array.of(0x83),
  5908. FlagEnabled: Uint8Array.of(0xB9),
  5909. FlagDefault: Uint8Array.of(0x88),
  5910. FlagForced: Uint8Array.of(0x55, 0xAA),
  5911. FlagLacing: Uint8Array.of(0x9C),
  5912. MinCache: Uint8Array.of(0x6D, 0xE7),
  5913. MaxCache: Uint8Array.of(0x6D, 0xF8),
  5914. DefaultDuration: Uint8Array.of(0x23, 0xE3, 0x83),
  5915. DefaultDecodedFieldDuration: Uint8Array.of(0x23, 0x4E, 0x7A),
  5916. MaxBlockAdditionID: Uint8Array.of(0x55, 0xEE),
  5917. Name: Uint8Array.of(0x53, 0x6E),
  5918. Language: Uint8Array.of(0x22, 0xB5, 0x9C),
  5919. CodecID: Uint8Array.of(0x86),
  5920. CodecPrivate: Uint8Array.of(0x63, 0xA2),
  5921. CodecName: Uint8Array.of(0x25, 0x86, 0x88),
  5922. AttachmentLink: Uint8Array.of(0x74, 0x46),
  5923. CodecDecodeAll: Uint8Array.of(0xAA),
  5924. TrackOverlay: Uint8Array.of(0x6F, 0xAB),
  5925. CodecDelay: Uint8Array.of(0x56, 0xAA),
  5926. SeekPreRoll: Uint8Array.of(0x56, 0xBB),
  5927. TrackTranslate: Uint8Array.of(0x66, 0x24),
  5928. TrackTranslateEditionUID: Uint8Array.of(0x66, 0xFC),
  5929. TrackTranslateCodec: Uint8Array.of(0x66, 0xBF),
  5930. TrackTranslateTrackID: Uint8Array.of(0x66, 0xA5),
  5931. Video: Uint8Array.of(0xE0),
  5932. FlagInterlaced: Uint8Array.of(0x9A),
  5933. FieldOrder: Uint8Array.of(0x9D),
  5934. StereoMode: Uint8Array.of(0x53, 0xB8),
  5935. AlphaMode: Uint8Array.of(0x53, 0xC0),
  5936. PixelWidth: Uint8Array.of(0xB0),
  5937. PixelHeight: Uint8Array.of(0xBA),
  5938. PixelCropBottom: Uint8Array.of(0x54, 0xAA),
  5939. PixelCropTop: Uint8Array.of(0x54, 0xBB),
  5940. PixelCropLeft: Uint8Array.of(0x54, 0xCC),
  5941. PixelCropRight: Uint8Array.of(0x54, 0xDD),
  5942. DisplayWidth: Uint8Array.of(0x54, 0xB0),
  5943. DisplayHeight: Uint8Array.of(0x54, 0xBA),
  5944. DisplayUnit: Uint8Array.of(0x54, 0xB2),
  5945. AspectRatioType: Uint8Array.of(0x54, 0xB3),
  5946. ColourSpace: Uint8Array.of(0x2E, 0xB5, 0x24),
  5947. Colour: Uint8Array.of(0x55, 0xB0),
  5948. MatrixCoefficients: Uint8Array.of(0x55, 0xB1),
  5949. BitsPerChannel: Uint8Array.of(0x55, 0xB2),
  5950. ChromaSubsamplingHorz: Uint8Array.of(0x55, 0xB3),
  5951. ChromaSubsamplingVert: Uint8Array.of(0x55, 0xB4),
  5952. CbSubsamplingHorz: Uint8Array.of(0x55, 0xB5),
  5953. CbSubsamplingVert: Uint8Array.of(0x55, 0xB6),
  5954. ChromaSitingHorz: Uint8Array.of(0x55, 0xB7),
  5955. ChromaSitingVert: Uint8Array.of(0x55, 0xB8),
  5956. Range: Uint8Array.of(0x55, 0xB9),
  5957. TransferCharacteristics: Uint8Array.of(0x55, 0xBA),
  5958. Primaries: Uint8Array.of(0x55, 0xBB),
  5959. MaxCLL: Uint8Array.of(0x55, 0xBC),
  5960. MaxFALL: Uint8Array.of(0x55, 0xBD),
  5961. MasteringMetadata: Uint8Array.of(0x55, 0xD0),
  5962. PrimaryRChromaticityX: Uint8Array.of(0x55, 0xD1),
  5963. PrimaryRChromaticityY: Uint8Array.of(0x55, 0xD2),
  5964. PrimaryGChromaticityX: Uint8Array.of(0x55, 0xD3),
  5965. PrimaryGChromaticityY: Uint8Array.of(0x55, 0xD4),
  5966. PrimaryBChromaticityX: Uint8Array.of(0x55, 0xD5),
  5967. PrimaryBChromaticityY: Uint8Array.of(0x55, 0xD6),
  5968. WhitePointChromaticityX: Uint8Array.of(0x55, 0xD7),
  5969. WhitePointChromaticityY: Uint8Array.of(0x55, 0xD8),
  5970. LuminanceMax: Uint8Array.of(0x55, 0xD9),
  5971. LuminanceMin: Uint8Array.of(0x55, 0xDA),
  5972. Audio: Uint8Array.of(0xE1),
  5973. SamplingFrequency: Uint8Array.of(0xB5),
  5974. OutputSamplingFrequency: Uint8Array.of(0x78, 0xB5),
  5975. Channels: Uint8Array.of(0x9F),
  5976. BitDepth: Uint8Array.of(0x62, 0x64),
  5977. TrackOperation: Uint8Array.of(0xE2),
  5978. TrackCombinePlanes: Uint8Array.of(0xE3),
  5979. TrackPlane: Uint8Array.of(0xE4),
  5980. TrackPlaneUID: Uint8Array.of(0xE5),
  5981. TrackPlaneType: Uint8Array.of(0xE6),
  5982. TrackJoinBlocks: Uint8Array.of(0xE9),
  5983. TrackJoinUID: Uint8Array.of(0xED),
  5984. ContentEncodings: Uint8Array.of(0x6D, 0x80),
  5985. ContentEncoding: Uint8Array.of(0x62, 0x40),
  5986. ContentEncodingOrder: Uint8Array.of(0x50, 0x31),
  5987. ContentEncodingScope: Uint8Array.of(0x50, 0x32),
  5988. ContentEncodingType: Uint8Array.of(0x50, 0x33),
  5989. ContentCompression: Uint8Array.of(0x50, 0x34),
  5990. ContentCompAlgo: Uint8Array.of(0x42, 0x54),
  5991. ContentCompSettings: Uint8Array.of(0x42, 0x55),
  5992. ContentEncryption: Uint8Array.of(0x50, 0x35),
  5993. ContentEncAlgo: Uint8Array.of(0x47, 0xE1),
  5994. ContentEncKeyID: Uint8Array.of(0x47, 0xE2),
  5995. ContentSignature: Uint8Array.of(0x47, 0xE3),
  5996. ContentSigKeyID: Uint8Array.of(0x47, 0xE4),
  5997. ContentSigAlgo: Uint8Array.of(0x47, 0xE5),
  5998. ContentSigHashAlgo: Uint8Array.of(0x47, 0xE6),
  5999. Cues: Uint8Array.of(0x1C, 0x53, 0xBB, 0x6B),
  6000. CuePoint: Uint8Array.of(0xBB),
  6001. CueTime: Uint8Array.of(0xB3),
  6002. CueTrackPositions: Uint8Array.of(0xB7),
  6003. CueTrack: Uint8Array.of(0xF7),
  6004. CueClusterPosition: Uint8Array.of(0xF1),
  6005. CueRelativePosition: Uint8Array.of(0xF0),
  6006. CueDuration: Uint8Array.of(0xB2),
  6007. CueBlockNumber: Uint8Array.of(0x53, 0x78),
  6008. CueCodecState: Uint8Array.of(0xEA),
  6009. CueReference: Uint8Array.of(0xDB),
  6010. CueRefTime: Uint8Array.of(0x96),
  6011. Attachments: Uint8Array.of(0x19, 0x41, 0xA4, 0x69),
  6012. AttachedFile: Uint8Array.of(0x61, 0xA7),
  6013. FileDescription: Uint8Array.of(0x46, 0x7E),
  6014. FileName: Uint8Array.of(0x46, 0x6E),
  6015. FileMimeType: Uint8Array.of(0x46, 0x60),
  6016. FileData: Uint8Array.of(0x46, 0x5C),
  6017. FileUID: Uint8Array.of(0x46, 0xAE),
  6018. Chapters: Uint8Array.of(0x10, 0x43, 0xA7, 0x70),
  6019. EditionEntry: Uint8Array.of(0x45, 0xB9),
  6020. EditionUID: Uint8Array.of(0x45, 0xBC),
  6021. EditionFlagHidden: Uint8Array.of(0x45, 0xBD),
  6022. EditionFlagDefault: Uint8Array.of(0x45, 0xDB),
  6023. EditionFlagOrdered: Uint8Array.of(0x45, 0xDD),
  6024. ChapterAtom: Uint8Array.of(0xB6),
  6025. ChapterUID: Uint8Array.of(0x73, 0xC4),
  6026. ChapterStringUID: Uint8Array.of(0x56, 0x54),
  6027. ChapterTimeStart: Uint8Array.of(0x91),
  6028. ChapterTimeEnd: Uint8Array.of(0x92),
  6029. ChapterFlagHidden: Uint8Array.of(0x98),
  6030. ChapterFlagEnabled: Uint8Array.of(0x45, 0x98),
  6031. ChapterSegmentUID: Uint8Array.of(0x6E, 0x67),
  6032. ChapterSegmentEditionUID: Uint8Array.of(0x6E, 0xBC),
  6033. ChapterPhysicalEquiv: Uint8Array.of(0x63, 0xC3),
  6034. ChapterTrack: Uint8Array.of(0x8F),
  6035. ChapterTrackNumber: Uint8Array.of(0x89),
  6036. ChapterDisplay: Uint8Array.of(0x80),
  6037. ChapString: Uint8Array.of(0x85),
  6038. ChapLanguage: Uint8Array.of(0x43, 0x7C),
  6039. ChapCountry: Uint8Array.of(0x43, 0x7E),
  6040. ChapProcess: Uint8Array.of(0x69, 0x44),
  6041. ChapProcessCodecID: Uint8Array.of(0x69, 0x55),
  6042. ChapProcessPrivate: Uint8Array.of(0x45, 0x0D),
  6043. ChapProcessCommand: Uint8Array.of(0x69, 0x11),
  6044. ChapProcessTime: Uint8Array.of(0x69, 0x22),
  6045. ChapProcessData: Uint8Array.of(0x69, 0x33),
  6046. Tags: Uint8Array.of(0x12, 0x54, 0xC3, 0x67),
  6047. Tag: Uint8Array.of(0x73, 0x73),
  6048. Targets: Uint8Array.of(0x63, 0xC0),
  6049. TargetTypeValue: Uint8Array.of(0x68, 0xCA),
  6050. TargetType: Uint8Array.of(0x63, 0xCA),
  6051. TagTrackUID: Uint8Array.of(0x63, 0xC5),
  6052. TagEditionUID: Uint8Array.of(0x63, 0xC9),
  6053. TagChapterUID: Uint8Array.of(0x63, 0xC4),
  6054. TagAttachmentUID: Uint8Array.of(0x63, 0xC6),
  6055. SimpleTag: Uint8Array.of(0x67, 0xC8),
  6056. TagName: Uint8Array.of(0x45, 0xA3),
  6057. TagLanguage: Uint8Array.of(0x44, 0x7A),
  6058. TagDefault: Uint8Array.of(0x44, 0x84),
  6059. TagString: Uint8Array.of(0x44, 0x87),
  6060. TagBinary: Uint8Array.of(0x44, 0x85),
  6061. };
  6062. var EBML = /*#__PURE__*/Object.freeze({
  6063. Value: Value,
  6064. Element: Element,
  6065. bytes: bytes,
  6066. number: number,
  6067. vintEncodedNumber: vintEncodedNumber,
  6068. int16: int16,
  6069. float: float,
  6070. string: string,
  6071. element: element,
  6072. unknownSizeElement: unknownSizeElement,
  6073. build: build,
  6074. getEBMLByteLength: getEBMLByteLength,
  6075. UNKNOWN_SIZE: UNKNOWN_SIZE,
  6076. vintEncode: vintEncode,
  6077. getSizeMask: getSizeMask,
  6078. ID: ID,
  6079. numberToByteArray: numberToByteArray,
  6080. stringToByteArray: stringToByteArray,
  6081. getNumberByteLength: getNumberByteLength,
  6082. int16Bit: int16Bit,
  6083. float32bit: float32bit,
  6084. dumpBytes: dumpBytes
  6085. });
  6086. /***
  6087. * The EMBL builder is from simple-ebml-builder
  6088. *
  6089. * Copyright 2017 ryiwamoto
  6090. *
  6091. * @author ryiwamoto, qli5
  6092. *
  6093. * Permission is hereby granted, free of charge, to any person obtaining
  6094. * a copy of this software and associated documentation files (the
  6095. * "Software"), to deal in the Software without restriction, including
  6096. * without limitation the rights to use, copy, modify, merge, publish,
  6097. * distribute, sublicense, and/or sell copies of the Software, and to
  6098. * permit persons to whom the Software is furnished to do so, subject
  6099. * to the following conditions:
  6100. *
  6101. * The above copyright notice and this permission notice shall be
  6102. * included in all copies or substantial portions of the Software.
  6103. *
  6104. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  6105. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  6106. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  6107. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  6108. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  6109. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  6110. * DEALINGS IN THE SOFTWARE.
  6111. */
  6112. /***
  6113. * Copyright (C) 2018 Qli5. All Rights Reserved.
  6114. *
  6115. * @author qli5 <goodlq11[at](163|gmail).com>
  6116. *
  6117. * This Source Code Form is subject to the terms of the Mozilla Public
  6118. * License, v. 2.0. If a copy of the MPL was not distributed with this
  6119. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6120. */
  6121. class MKV {
  6122. constructor(config) {
  6123. this.min = true;
  6124. this.onprogress = null;
  6125. Object.assign(this, config);
  6126. this.segmentUID = MKV.randomBytes(16);
  6127. this.trackUIDBase = Math.trunc(Math.random() * 2 ** 16);
  6128. this.trackMetadata = { h264: null, aac: null, ass: null };
  6129. this.duration = 0;
  6130. this.blocks = { h264: [], aac: [], ass: [] };
  6131. }
  6132. static randomBytes(num) {
  6133. return Array.from(new Array(num), () => Math.trunc(Math.random() * 256));
  6134. }
  6135. static textToMS(str) {
  6136. const [, h, mm, ss, ms10] = str.match(/(\\d+):(\\d+):(\\d+).(\\d+)/);
  6137. return h * 3600000 + mm * 60000 + ss * 1000 + ms10 * 10;
  6138. }
  6139. static mimeToCodecID(str) {
  6140. if (str.startsWith('avc1')) {
  6141. return 'V_MPEG4/ISO/AVC';
  6142. }
  6143. else if (str.startsWith('mp4a')) {
  6144. return 'A_AAC';
  6145. }
  6146. else {
  6147. throw new Error(\`MKVRemuxer: unknown codec \${str}\`);
  6148. }
  6149. }
  6150. static uint8ArrayConcat(...array) {
  6151. // if (Array.isArray(array[0])) array = array[0];
  6152. if (array.length == 1) return array[0];
  6153. if (typeof Buffer != 'undefined') return Buffer.concat(array);
  6154. const ret = new Uint8Array(array.reduce((i, j) => i + j.byteLength, 0));
  6155. let length = 0;
  6156. for (let e of array) {
  6157. ret.set(e, length);
  6158. length += e.byteLength;
  6159. }
  6160. return ret;
  6161. }
  6162. addH264Metadata(h264) {
  6163. this.trackMetadata.h264 = {
  6164. codecId: MKV.mimeToCodecID(h264.codec),
  6165. codecPrivate: h264.avcc,
  6166. defaultDuration: h264.refSampleDuration * 1000000,
  6167. pixelWidth: h264.codecWidth,
  6168. pixelHeight: h264.codecHeight,
  6169. displayWidth: h264.presentWidth,
  6170. displayHeight: h264.presentHeight
  6171. };
  6172. this.duration = Math.max(this.duration, h264.duration);
  6173. }
  6174. addAACMetadata(aac) {
  6175. this.trackMetadata.aac = {
  6176. codecId: MKV.mimeToCodecID(aac.originalCodec),
  6177. codecPrivate: aac.configRaw,
  6178. defaultDuration: aac.refSampleDuration * 1000000,
  6179. samplingFrequence: aac.audioSampleRate,
  6180. channels: aac.channelCount
  6181. };
  6182. this.duration = Math.max(this.duration, aac.duration);
  6183. }
  6184. addASSMetadata(ass) {
  6185. this.trackMetadata.ass = {
  6186. codecId: 'S_TEXT/ASS',
  6187. codecPrivate: new _TextEncoder().encode(ass.header)
  6188. };
  6189. }
  6190. addH264Stream(h264) {
  6191. this.blocks.h264 = this.blocks.h264.concat(h264.samples.map(e => ({
  6192. track: 1,
  6193. frame: MKV.uint8ArrayConcat(...e.units.map(i => i.data)),
  6194. isKeyframe: e.isKeyframe,
  6195. discardable: Boolean(e.refIdc),
  6196. timestamp: e.pts,
  6197. simple: true,
  6198. })));
  6199. }
  6200. addAACStream(aac) {
  6201. this.blocks.aac = this.blocks.aac.concat(aac.samples.map(e => ({
  6202. track: 2,
  6203. frame: e.unit,
  6204. timestamp: e.pts,
  6205. simple: true,
  6206. })));
  6207. }
  6208. addASSStream(ass) {
  6209. this.blocks.ass = this.blocks.ass.concat(ass.lines.map((e, i) => ({
  6210. track: 3,
  6211. frame: new _TextEncoder().encode(\`\${i},\${e['Layer'] || ''},\${e['Style'] || ''},\${e['Name'] || ''},\${e['MarginL'] || ''},\${e['MarginR'] || ''},\${e['MarginV'] || ''},\${e['Effect'] || ''},\${e['Text'] || ''}\`),
  6212. timestamp: MKV.textToMS(e['Start']),
  6213. duration: MKV.textToMS(e['End']) - MKV.textToMS(e['Start']),
  6214. })));
  6215. }
  6216. build() {
  6217. return new _Blob([
  6218. this.buildHeader(),
  6219. this.buildBody()
  6220. ]);
  6221. }
  6222. buildHeader() {
  6223. return new _Blob([EBML.build(EBML.element(EBML.ID.EBML, [
  6224. EBML.element(EBML.ID.EBMLVersion, EBML.number(1)),
  6225. EBML.element(EBML.ID.EBMLReadVersion, EBML.number(1)),
  6226. EBML.element(EBML.ID.EBMLMaxIDLength, EBML.number(4)),
  6227. EBML.element(EBML.ID.EBMLMaxSizeLength, EBML.number(8)),
  6228. EBML.element(EBML.ID.DocType, EBML.string('matroska')),
  6229. EBML.element(EBML.ID.DocTypeVersion, EBML.number(4)),
  6230. EBML.element(EBML.ID.DocTypeReadVersion, EBML.number(2)),
  6231. ]))]);
  6232. }
  6233. buildBody() {
  6234. if (this.min) {
  6235. return new _Blob([EBML.build(EBML.element(EBML.ID.Segment, [
  6236. this.getSegmentInfo(),
  6237. this.getTracks(),
  6238. ...this.getClusterArray()
  6239. ]))]);
  6240. }
  6241. else {
  6242. return new _Blob([EBML.build(EBML.element(EBML.ID.Segment, [
  6243. this.getSeekHead(),
  6244. this.getVoid(4100),
  6245. this.getSegmentInfo(),
  6246. this.getTracks(),
  6247. this.getVoid(1100),
  6248. ...this.getClusterArray()
  6249. ]))]);
  6250. }
  6251. }
  6252. getSeekHead() {
  6253. return EBML.element(EBML.ID.SeekHead, [
  6254. EBML.element(EBML.ID.Seek, [
  6255. EBML.element(EBML.ID.SeekID, EBML.bytes(EBML.ID.Info)),
  6256. EBML.element(EBML.ID.SeekPosition, EBML.number(4050))
  6257. ]),
  6258. EBML.element(EBML.ID.Seek, [
  6259. EBML.element(EBML.ID.SeekID, EBML.bytes(EBML.ID.Tracks)),
  6260. EBML.element(EBML.ID.SeekPosition, EBML.number(4200))
  6261. ]),
  6262. ]);
  6263. }
  6264. getVoid(length = 2000) {
  6265. return EBML.element(EBML.ID.Void, EBML.bytes(new Uint8Array(length)));
  6266. }
  6267. getSegmentInfo() {
  6268. return EBML.element(EBML.ID.Info, [
  6269. EBML.element(EBML.ID.TimecodeScale, EBML.number(1000000)),
  6270. EBML.element(EBML.ID.MuxingApp, EBML.string('flv.js + assparser_qli5 -> simple-ebml-builder')),
  6271. EBML.element(EBML.ID.WritingApp, EBML.string('flvass2mkv.js by qli5')),
  6272. EBML.element(EBML.ID.Duration, EBML.float(this.duration)),
  6273. EBML.element(EBML.ID.SegmentUID, EBML.bytes(this.segmentUID)),
  6274. ]);
  6275. }
  6276. getTracks() {
  6277. return EBML.element(EBML.ID.Tracks, [
  6278. this.getVideoTrackEntry(),
  6279. this.getAudioTrackEntry(),
  6280. this.getSubtitleTrackEntry()
  6281. ]);
  6282. }
  6283. getVideoTrackEntry() {
  6284. return EBML.element(EBML.ID.TrackEntry, [
  6285. EBML.element(EBML.ID.TrackNumber, EBML.number(1)),
  6286. EBML.element(EBML.ID.TrackUID, EBML.number(this.trackUIDBase + 1)),
  6287. EBML.element(EBML.ID.TrackType, EBML.number(0x01)),
  6288. EBML.element(EBML.ID.FlagLacing, EBML.number(0x00)),
  6289. EBML.element(EBML.ID.CodecID, EBML.string(this.trackMetadata.h264.codecId)),
  6290. EBML.element(EBML.ID.CodecPrivate, EBML.bytes(this.trackMetadata.h264.codecPrivate)),
  6291. EBML.element(EBML.ID.DefaultDuration, EBML.number(this.trackMetadata.h264.defaultDuration)),
  6292. EBML.element(EBML.ID.Language, EBML.string('und')),
  6293. EBML.element(EBML.ID.Video, [
  6294. EBML.element(EBML.ID.PixelWidth, EBML.number(this.trackMetadata.h264.pixelWidth)),
  6295. EBML.element(EBML.ID.PixelHeight, EBML.number(this.trackMetadata.h264.pixelHeight)),
  6296. EBML.element(EBML.ID.DisplayWidth, EBML.number(this.trackMetadata.h264.displayWidth)),
  6297. EBML.element(EBML.ID.DisplayHeight, EBML.number(this.trackMetadata.h264.displayHeight)),
  6298. ]),
  6299. ]);
  6300. }
  6301. getAudioTrackEntry() {
  6302. return EBML.element(EBML.ID.TrackEntry, [
  6303. EBML.element(EBML.ID.TrackNumber, EBML.number(2)),
  6304. EBML.element(EBML.ID.TrackUID, EBML.number(this.trackUIDBase + 2)),
  6305. EBML.element(EBML.ID.TrackType, EBML.number(0x02)),
  6306. EBML.element(EBML.ID.FlagLacing, EBML.number(0x00)),
  6307. EBML.element(EBML.ID.CodecID, EBML.string(this.trackMetadata.aac.codecId)),
  6308. EBML.element(EBML.ID.CodecPrivate, EBML.bytes(this.trackMetadata.aac.codecPrivate)),
  6309. EBML.element(EBML.ID.DefaultDuration, EBML.number(this.trackMetadata.aac.defaultDuration)),
  6310. EBML.element(EBML.ID.Language, EBML.string('und')),
  6311. EBML.element(EBML.ID.Audio, [
  6312. EBML.element(EBML.ID.SamplingFrequency, EBML.float(this.trackMetadata.aac.samplingFrequence)),
  6313. EBML.element(EBML.ID.Channels, EBML.number(this.trackMetadata.aac.channels)),
  6314. ]),
  6315. ]);
  6316. }
  6317. getSubtitleTrackEntry() {
  6318. return EBML.element(EBML.ID.TrackEntry, [
  6319. EBML.element(EBML.ID.TrackNumber, EBML.number(3)),
  6320. EBML.element(EBML.ID.TrackUID, EBML.number(this.trackUIDBase + 3)),
  6321. EBML.element(EBML.ID.TrackType, EBML.number(0x11)),
  6322. EBML.element(EBML.ID.FlagLacing, EBML.number(0x00)),
  6323. EBML.element(EBML.ID.CodecID, EBML.string(this.trackMetadata.ass.codecId)),
  6324. EBML.element(EBML.ID.CodecPrivate, EBML.bytes(this.trackMetadata.ass.codecPrivate)),
  6325. EBML.element(EBML.ID.Language, EBML.string('und')),
  6326. ]);
  6327. }
  6328. getClusterArray() {
  6329. // H264 codecState
  6330. this.blocks.h264[0].simple = false;
  6331. this.blocks.h264[0].codecState = this.trackMetadata.h264.codecPrivate;
  6332. let i = 0;
  6333. let j = 0;
  6334. let k = 0;
  6335. let clusterTimeCode = 0;
  6336. let clusterContent = [EBML.element(EBML.ID.Timecode, EBML.number(clusterTimeCode))];
  6337. let ret = [clusterContent];
  6338. const progressThrottler = Math.pow(2, Math.floor(Math.log(this.blocks.h264.length >> 7) / Math.log(2))) - 1;
  6339. for (i = 0; i < this.blocks.h264.length; i++) {
  6340. const e = this.blocks.h264[i];
  6341. for (; j < this.blocks.aac.length; j++) {
  6342. if (this.blocks.aac[j].timestamp < e.timestamp) {
  6343. clusterContent.push(this.getBlocks(this.blocks.aac[j], clusterTimeCode));
  6344. }
  6345. else {
  6346. break;
  6347. }
  6348. }
  6349. for (; k < this.blocks.ass.length; k++) {
  6350. if (this.blocks.ass[k].timestamp < e.timestamp) {
  6351. clusterContent.push(this.getBlocks(this.blocks.ass[k], clusterTimeCode));
  6352. }
  6353. else {
  6354. break;
  6355. }
  6356. }
  6357. if (e.isKeyframe/* || clusterContent.length > 72 */) {
  6358. // start new cluster
  6359. clusterTimeCode = e.timestamp;
  6360. clusterContent = [EBML.element(EBML.ID.Timecode, EBML.number(clusterTimeCode))];
  6361. ret.push(clusterContent);
  6362. }
  6363. clusterContent.push(this.getBlocks(e, clusterTimeCode));
  6364. if (this.onprogress && !(i & progressThrottler)) this.onprogress({ loaded: i, total: this.blocks.h264.length });
  6365. }
  6366. for (; j < this.blocks.aac.length; j++) clusterContent.push(this.getBlocks(this.blocks.aac[j], clusterTimeCode));
  6367. for (; k < this.blocks.ass.length; k++) clusterContent.push(this.getBlocks(this.blocks.ass[k], clusterTimeCode));
  6368. if (this.onprogress) this.onprogress({ loaded: i, total: this.blocks.h264.length });
  6369. if (ret[0].length == 1) ret.shift();
  6370. ret = ret.map(clusterContent => EBML.element(EBML.ID.Cluster, clusterContent));
  6371. return ret;
  6372. }
  6373. getBlocks(e, clusterTimeCode) {
  6374. if (e.simple) {
  6375. return EBML.element(EBML.ID.SimpleBlock, [
  6376. EBML.vintEncodedNumber(e.track),
  6377. EBML.int16(e.timestamp - clusterTimeCode),
  6378. EBML.bytes(e.isKeyframe ? [128] : [0]),
  6379. EBML.bytes(e.frame)
  6380. ]);
  6381. }
  6382. else {
  6383. let blockGroupContent = [EBML.element(EBML.ID.Block, [
  6384. EBML.vintEncodedNumber(e.track),
  6385. EBML.int16(e.timestamp - clusterTimeCode),
  6386. EBML.bytes([0]),
  6387. EBML.bytes(e.frame)
  6388. ])];
  6389. if (typeof e.duration != 'undefined') {
  6390. blockGroupContent.push(EBML.element(EBML.ID.BlockDuration, EBML.number(e.duration)));
  6391. }
  6392. if (typeof e.codecState != 'undefined') {
  6393. blockGroupContent.push(EBML.element(EBML.ID.CodecState, EBML.bytes(e.codecState)));
  6394. }
  6395. return EBML.element(EBML.ID.BlockGroup, blockGroupContent);
  6396. }
  6397. }
  6398. }
  6399. /***
  6400. * FLV + ASS => MKV transmuxer
  6401. * Demux FLV into H264 + AAC stream and ASS into line stream; then
  6402. * remux them into a MKV file.
  6403. *
  6404. * @author qli5 <goodlq11[at](163|gmail).com>
  6405. *
  6406. * This Source Code Form is subject to the terms of the Mozilla Public
  6407. * License, v. 2.0. If a copy of the MPL was not distributed with this
  6408. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6409. *
  6410. * The FLV demuxer is from flv.js <https://github.com/Bilibili/flv.js/>
  6411. * by zheng qian <xqq@xqq.im>, licensed under Apache 2.0.
  6412. *
  6413. * The EMBL builder is from simple-ebml-builder
  6414. * <https://www.npmjs.com/package/simple-ebml-builder> by ryiwamoto,
  6415. * licensed under MIT.
  6416. */
  6417. const FLVASS2MKV = class {
  6418. constructor(config = {}) {
  6419. this.onflvprogress = null;
  6420. this.onassprogress = null;
  6421. this.onurlrevokesafe = null;
  6422. this.onfileload = null;
  6423. this.onmkvprogress = null;
  6424. this.onload = null;
  6425. Object.assign(this, config);
  6426. this.mkvConfig = { onprogress: this.onmkvprogress };
  6427. Object.assign(this.mkvConfig, config.mkvConfig);
  6428. }
  6429. /**
  6430. * Demux FLV into H264 + AAC stream and ASS into line stream; then
  6431. * remux them into a MKV file.
  6432. * @param {Blob|string|ArrayBuffer} flv
  6433. * @param {Blob|string|ArrayBuffer} ass
  6434. */
  6435. async build(flv = './samples/gen_case.flv', ass = './samples/gen_case.ass') {
  6436. // load flv and ass as arraybuffer
  6437. await Promise.all([
  6438. new Promise((r, j) => {
  6439. if (flv instanceof _Blob) {
  6440. const e = new FileReader();
  6441. e.onprogress = this.onflvprogress;
  6442. e.onload = () => r(flv = e.r###lt);
  6443. e.onerror = j;
  6444. e.readAsArrayBuffer(flv);
  6445. }
  6446. else if (typeof flv == 'string') {
  6447. const e = new XMLHttpRequest();
  6448. e.responseType = 'arraybuffer';
  6449. e.onprogress = this.onflvprogress;
  6450. e.onload = () => r(flv = e.response);
  6451. e.onerror = j;
  6452. e.open('get', flv);
  6453. e.send();
  6454. flv = 2; // onurlrevokesafe
  6455. }
  6456. else if (flv instanceof ArrayBuffer) {
  6457. r(flv);
  6458. }
  6459. else {
  6460. j(new TypeError('flvass2mkv: flv {Blob|string|ArrayBuffer}'));
  6461. }
  6462. if (typeof ass != 'string' && this.onurlrevokesafe) this.onurlrevokesafe();
  6463. }),
  6464. new Promise((r, j) => {
  6465. if (ass instanceof _Blob) {
  6466. const e = new FileReader();
  6467. e.onprogress = this.onflvprogress;
  6468. e.onload = () => r(ass = e.r###lt);
  6469. e.onerror = j;
  6470. e.readAsArrayBuffer(ass);
  6471. }
  6472. else if (typeof ass == 'string') {
  6473. const e = new XMLHttpRequest();
  6474. e.responseType = 'arraybuffer';
  6475. e.onprogress = this.onflvprogress;
  6476. e.onload = () => r(ass = e.response);
  6477. e.onerror = j;
  6478. e.open('get', ass);
  6479. e.send();
  6480. ass = 2; // onurlrevokesafe
  6481. }
  6482. else if (ass instanceof ArrayBuffer) {
  6483. r(ass);
  6484. }
  6485. else {
  6486. j(new TypeError('flvass2mkv: ass {Blob|string|ArrayBuffer}'));
  6487. }
  6488. if (typeof flv != 'string' && this.onurlrevokesafe) this.onurlrevokesafe();
  6489. }),
  6490. ]);
  6491. if (this.onfileload) this.onfileload();
  6492. const mkv = new MKV(this.mkvConfig);
  6493. const assParser = new ASS();
  6494. ass = assParser.parseFile(ass);
  6495. mkv.addASSMetadata(ass);
  6496. mkv.addASSStream(ass);
  6497. const flvProbeData = FLVDemuxer.probe(flv);
  6498. const flvDemuxer = new FLVDemuxer(flvProbeData);
  6499. let mediaInfo = null;
  6500. let h264 = null;
  6501. let aac = null;
  6502. flvDemuxer.onDataAvailable = (...array) => {
  6503. array.forEach(e => {
  6504. if (e.type == 'video') h264 = e;
  6505. else if (e.type == 'audio') aac = e;
  6506. else throw new Error(\`MKVRemuxer: unrecoginzed data type \${e.type}\`);
  6507. });
  6508. };
  6509. flvDemuxer.onMediaInfo = i => mediaInfo = i;
  6510. flvDemuxer.onTrackMetadata = (i, e) => {
  6511. if (i == 'video') mkv.addH264Metadata(e);
  6512. else if (i == 'audio') mkv.addAACMetadata(e);
  6513. else throw new Error(\`MKVRemuxer: unrecoginzed metadata type \${i}\`);
  6514. };
  6515. flvDemuxer.onError = e => { throw new Error(e); };
  6516. const finalOffset = flvDemuxer.parseChunks(flv, flvProbeData.dataOffset);
  6517. if (finalOffset != flv.byteLength) throw new Error('FLVDemuxer: unexpected EOF');
  6518. mkv.addH264Stream(h264);
  6519. mkv.addAACStream(aac);
  6520. const ret = mkv.build();
  6521. if (this.onload) this.onload(ret);
  6522. return ret;
  6523. }
  6524. };
  6525. // if nodejs then test
  6526. if (typeof window == 'undefined') {
  6527. if (require.main == module) {
  6528. (async () => {
  6529. const fs = require('fs');
  6530. const assFileName = process.argv.slice(1).find(e => e.includes('.ass')) || './samples/gen_case.ass';
  6531. const flvFileName = process.argv.slice(1).find(e => e.includes('.flv')) || './samples/gen_case.flv';
  6532. const assFile = fs.readFileSync(assFileName).buffer;
  6533. const flvFile = fs.readFileSync(flvFileName).buffer;
  6534. fs.writeFileSync('out.mkv', await new FLVASS2MKV({ onmkvprogress: console.log.bind(console) }).build(flvFile, assFile));
  6535. })();
  6536. }
  6537. }
  6538. return FLVASS2MKV;
  6539. }());
  6540. //# sourceMappingURL=index.js.map
  6541. </script>
  6542. <script>
  6543. const fileProgress = document.getElementById('fileProgress');
  6544. const mkvProgress = document.getElementById('mkvProgress');
  6545. const a = document.getElementById('a');
  6546. window.exec = async option => {
  6547. const defaultOption = {
  6548. onflvprogress: ({ loaded, total }) => {
  6549. fileProgress.value = loaded;
  6550. fileProgress.max = total;
  6551. },
  6552. onfileload: () => {
  6553. console.timeEnd('file');
  6554. console.time('flvass2mkv');
  6555. },
  6556. onmkvprogress: ({ loaded, total }) => {
  6557. mkvProgress.value = loaded;
  6558. mkvProgress.max = total;
  6559. },
  6560. name: 'merged.mkv',
  6561. };
  6562. option = Object.assign(defaultOption, option);
  6563. a.download = a.textContent = option.name;
  6564. console.time('file');
  6565. const mkv = await new FLVASS2MKV(option).build(option.flv, option.ass);
  6566. console.timeEnd('flvass2mkv');
  6567. return a.href = URL.createObjectURL(mkv);
  6568. };
  6569. </script>
  6570. </body>
  6571. </html>
  6572. `;
  6573. /***
  6574. * Copyright (C) 2018 Qli5. All Rights Reserved.
  6575. *
  6576. * @author qli5 <goodlq11[at](163|gmail).com>
  6577. *
  6578. * This Source Code Form is subject to the terms of the Mozilla Public
  6579. * License, v. 2.0. If a copy of the MPL was not distributed with this
  6580. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6581. */
  6582. class MKVTransmuxer {
  6583. constructor(option) {
  6584. this.workerWin = null;
  6585. this.option = option;
  6586. }
  6587. /**
  6588. * FLV + ASS => MKV entry point
  6589. * @param {Blob|string|ArrayBuffer} flv
  6590. * @param {Blob|string|ArrayBuffer} ass
  6591. * @param {string=} name
  6592. */
  6593. exec(flv, ass, name) {
  6594. // 1. Allocate for a new window
  6595. if (!this.workerWin) this.workerWin = top.open('', undefined, ' ');
  6596. // 2. Inject scripts
  6597. this.workerWin.document.write(embeddedHTML);
  6598. this.workerWin.document.close();
  6599. // 3. Invoke exec
  6600. if (!(this.option instanceof Object)) this.option = null;
  6601. this.workerWin.exec(Object.assign({}, this.option, { flv, ass, name }));
  6602. URL.revokeObjectURL(flv);
  6603. URL.revokeObjectURL(ass);
  6604. // 4. Free parent window
  6605. // if (top.confirm('MKV打包中……要关掉这个窗口,释放内存吗?'))
  6606. top.location = 'about:blank';
  6607. }
  6608. }
  6609. /***
  6610. * Copyright (C) 2018 Qli5. All Rights Reserved.
  6611. *
  6612. * @author qli5 <goodlq11[at](163|gmail).com>
  6613. *
  6614. * This Source Code Form is subject to the terms of the Mozilla Public
  6615. * License, v. 2.0. If a copy of the MPL was not distributed with this
  6616. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6617. */
  6618. class UI {
  6619. constructor(twin, option = UI.optionDefaults) {
  6620. this.twin = twin;
  6621. this.option = option;
  6622. this.destroy = new HookedFunction();
  6623. this.dom = {};
  6624. this.cidSessionDestroy = new HookedFunction();
  6625. this.cidSessionDom = {};
  6626. this.destroy.addCallback(this.cidSessionDestroy.bind(this));
  6627. this.destroy.addCallback(() => {
  6628. Object.values(this.dom).forEach(e => e.remove());
  6629. this.dom = {};
  6630. });
  6631. this.cidSessionDestroy.addCallback(() => {
  6632. Object.values(this.cidSessionDom).forEach(e => e.remove());
  6633. this.cidSessionDom = {};
  6634. });
  6635. this.styleClearance();
  6636. }
  6637. styleClearance() {
  6638. let ret = `
  6639. .bilibili-player-context-menu-container.black ul.bilitwin li.context-menu-function > a:hover {
  6640. background: rgba(255,255,255,.12);
  6641. transition: all .3s ease-in-out;
  6642. cursor: pointer;
  6643. }
  6644. `;
  6645. if (top.getComputedStyle(top.document.body).color != 'rgb(34, 34, 34)') ret += `
  6646. .bilitwin a {
  6647. cursor: pointer;
  6648. color: #00a1d6;
  6649. }
  6650. .bilitwin a:hover {
  6651. color: #f25d8e;
  6652. }
  6653. .bilitwin button {
  6654. color: #fff;
  6655. cursor: pointer;
  6656. text-align: center;
  6657. border-radius: 4px;
  6658. background-color: #00a1d6;
  6659. vertical-align: middle;
  6660. border: 1px solid #00a1d6;
  6661. transition: .1s;
  6662. transition-property: background-color,border,color;
  6663. user-select: none;
  6664. }
  6665. .bilitwin button:hover {
  6666. background-color: #00b5e5;
  6667. border-color: #00b5e5;
  6668. }
  6669. .bilitwin progress {
  6670. -webkit-appearance: progress-bar;
  6671. -moz-appearance: progress-bar;
  6672. appearance: progress-bar;
  6673. }
  6674. .bilitwin input[type="checkbox" i] {
  6675. -webkit-appearance: checkbox;
  6676. -moz-appearance: checkbox;
  6677. appearance: checkbox;
  6678. }
  6679. `;
  6680. const style = document.createElement('style');
  6681. style.type = 'text/css';
  6682. style.textContent = ret;
  6683. document.head.append(style);
  6684. return this.dom.style = style;
  6685. }
  6686. cidSessionRender() {
  6687. this.buildTitle();
  6688. if (this.option.title) this.appendTitle();
  6689. if (this.option.menu) this.appendMenu();
  6690. }
  6691. // Title Append
  6692. buildTitle(monkey = this.twin.monkey) {
  6693. // 1. build flvA, mp4A, assA
  6694. const fontSize = '15px';
  6695. const flvA = document.createElement('a');
  6696. flvA.style.fontSize = fontSize;
  6697. flvA.textContent = '\u8D85\u6E05FLV';
  6698. const mp4A = document.createElement('a');
  6699. mp4A.style.fontSize = fontSize;
  6700. mp4A.textContent = '\u539F\u751FMP4';
  6701. const assA = document.createElement('a');
  6702. // 1.1 build flvA
  6703. assA.style.fontSize = fontSize;
  6704. assA.textContent = '\u5F39\u5E55ASS';
  6705. flvA.onmouseover = async () => {
  6706. // 1.1.1 give processing hint
  6707. flvA.textContent = '正在FLV';
  6708. flvA.onmouseover = null;
  6709. // 1.1.2 query flv
  6710. const href = await monkey.queryInfo('flv');
  6711. if (href == 'does_not_exist') return flvA.textContent = '没有FLV';
  6712. // 1.1.3 display flv
  6713. flvA.textContent = '超清FLV';
  6714. flvA.onclick = () => this.displayFLVDiv();
  6715. };
  6716. // 1.2 build mp4A
  6717. mp4A.onmouseover = async () => {
  6718. // 1.2.1 give processing hint
  6719. mp4A.textContent = '正在MP4';
  6720. mp4A.onmouseover = null;
  6721. if (this.option.autoDanmaku) {
  6722. await assA.onmouseover();
  6723. mp4A.onclick = () => assA.click();
  6724. }
  6725. // 1.2.2 query flv
  6726. let href = await monkey.queryInfo('mp4');
  6727. if (href == 'does_not_exist') return mp4A.textContent = '没有MP4';
  6728. // 1.2.3 response mp4
  6729. mp4A.href = href;
  6730. mp4A.textContent = '原生MP4';
  6731. mp4A.download = '';
  6732. mp4A.referrerPolicy = 'origin';
  6733. };
  6734. // 1.3 build assA
  6735. assA.onmouseover = async () => {
  6736. // 1.3.1 give processing hint
  6737. assA.textContent = '正在ASS';
  6738. assA.onmouseover = null;
  6739. // 1.3.2 query flv
  6740. assA.href = await monkey.queryInfo('ass');
  6741. // 1.3.3 response mp4
  6742. assA.textContent = '弹幕ASS';
  6743. if (monkey.mp4 && monkey.mp4.match) {
  6744. assA.download = monkey.mp4.match(/\d(?:\d|-|hd)*(?=\.mp4)/)[0] + '.ass';
  6745. } else {
  6746. assA.download = monkey.cid + '.ass';
  6747. }
  6748. };
  6749. // 2. save to cache
  6750. Object.assign(this.cidSessionDom, { flvA, mp4A, assA });
  6751. return this.cidSessionDom;
  6752. }
  6753. appendTitle({ flvA, mp4A, assA } = this.cidSessionDom) {
  6754. // 1. build div
  6755. const div = document.createElement('div');
  6756. // 2. append to title
  6757. div.addEventListener('click', e => e.stopPropagation());
  6758. div.style.float = 'left';
  6759. div.style.clear = 'left';
  6760. div.className = 'bilitwin';
  6761. div.append(...[flvA, ' ', mp4A, ' ', assA]);
  6762. const tminfo = document.querySelector('div.tminfo') || document.querySelector('div.info-second');
  6763. tminfo.style.float = 'none';
  6764. tminfo.style.marginLeft = '185px';
  6765. tminfo.parentElement.insertBefore(div, tminfo);
  6766. // 3. save to cache
  6767. this.cidSessionDom.titleDiv = div;
  6768. return div;
  6769. }
  6770. buildFLVDiv(monkey = this.twin.monkey, flvs = monkey.flvs, cache = monkey.cache) {
  6771. // 1. build flv splits
  6772. const flvTrs = flvs.map((href, index) => {
  6773. const tr = document.createElement('tr');
  6774. {
  6775. const td1 = document.createElement('td');
  6776. const a1 = document.createElement('a');
  6777. a1.href = href;
  6778. a1.textContent = `FLV分段 ${index + 1}`;
  6779. td1.append(a1);
  6780. tr.append(td1);
  6781. const td2 = document.createElement('td');
  6782. const a2 = document.createElement('a');
  6783. a2.onclick = e => this.downloadFLV({
  6784. monkey,
  6785. index,
  6786. a: e.target,
  6787. progress: tr.children[2].children[0]
  6788. });
  6789. a2.textContent = '\u7F13\u5B58\u672C\u6BB5';
  6790. td2.append(a2);
  6791. tr.append(td2);
  6792. const td3 = document.createElement('td');
  6793. const progress1 = document.createElement('progress');
  6794. progress1.setAttribute('value', '0');
  6795. progress1.setAttribute('max', '100');
  6796. progress1.textContent = '\u8FDB\u5EA6\u6761';
  6797. td3.append(progress1);
  6798. tr.append(td3);
  6799. }
  6800. return tr;
  6801. });
  6802. // 2. build exporter a
  6803. const exporterA = document.createElement('a');
  6804. if (this.option.aria2) {
  6805. exporterA.textContent = '导出Aria2';
  6806. exporterA.download = 'bilitwin.session';
  6807. exporterA.href = URL.createObjectURL(new Blob([Exporter.exportAria2(flvs, top.location.origin)]));
  6808. } else if (this.option.aria2RPC) {
  6809. exporterA.textContent = '发送Aria2 RPC';
  6810. exporterA.onclick = () => Exporter.sendToAria2RPC(flvs, top.location.origin);
  6811. } else if (this.option.m3u8) {
  6812. exporterA.textContent = '导出m3u8';
  6813. exporterA.download = 'bilitwin.m3u8';
  6814. exporterA.href = URL.createObjectURL(new Blob([Exporter.exportM3U8(flvs, top.location.origin, top.navigator.userAgent)]));
  6815. } else if (this.option.clipboard) {
  6816. exporterA.textContent = '全部复制到剪贴板';
  6817. exporterA.onclick = () => Exporter.copyToClipboard(flvs.join('\n'));
  6818. } else {
  6819. exporterA.textContent = '导出IDM';
  6820. exporterA.download = 'bilitwin.ef2';
  6821. exporterA.href = URL.createObjectURL(new Blob([Exporter.exportIDM(flvs, top.location.origin)]));
  6822. }
  6823. // 3. build body table
  6824. const table = document.createElement('table');
  6825. table.style.width = '100%';
  6826. table.style.lineHeight = '2em';
  6827. table.append(...flvTrs, (() => {
  6828. const tr1 = document.createElement('tr');
  6829. const td1 = document.createElement('td');
  6830. td1.append(...[exporterA]);
  6831. tr1.append(td1);
  6832. const td2 = document.createElement('td');
  6833. const a1 = document.createElement('a');
  6834. a1.onclick = e => this.downloadAllFLVs({
  6835. a: e.target,
  6836. monkey, table
  6837. });
  6838. a1.textContent = '\u7F13\u5B58\u5168\u90E8+\u81EA\u52A8\u5408\u5E76';
  6839. td2.append(a1);
  6840. tr1.append(td2);
  6841. const td3 = document.createElement('td');
  6842. const progress1 = document.createElement('progress');
  6843. progress1.setAttribute('value', '0');
  6844. progress1.setAttribute('max', flvs.length + 1);
  6845. progress1.textContent = '\u8FDB\u5EA6\u6761';
  6846. td3.append(progress1);
  6847. tr1.append(td3);
  6848. return tr1;
  6849. })(), (() => {
  6850. const tr1 = document.createElement('tr');
  6851. const td1 = document.createElement('td');
  6852. td1.colSpan = '3';
  6853. td1.textContent = '\u5408\u5E76\u529F\u80FD\u63A8\u8350\u914D\u7F6E\uFF1A\u81F3\u5C118G RAM\u3002\u628A\u81EA\u5DF1\u4E0B\u8F7D\u7684\u5206\u6BB5FLV\u62D6\u52A8\u5230\u8FD9\u91CC\uFF0C\u4E5F\u53EF\u4EE5\u5408\u5E76\u54E6~';
  6854. tr1.append(td1);
  6855. return tr1;
  6856. })(), cache ? (() => {
  6857. const tr1 = document.createElement('tr');
  6858. const td1 = document.createElement('td');
  6859. td1.colSpan = '3';
  6860. td1.textContent = '\u4E0B\u8F7D\u7684\u7F13\u5B58\u5206\u6BB5\u4F1A\u6682\u65F6\u505C\u7559\u5728\u7535\u8111\u91CC\uFF0C\u8FC7\u4E00\u6BB5\u65F6\u95F4\u4F1A\u81EA\u52A8\u6D88\u5931\u3002\u5EFA\u8BAE\u53EA\u5F00\u4E00\u4E2A\u6807\u7B7E\u9875\u3002';
  6861. tr1.append(td1);
  6862. return tr1;
  6863. })() : (() => {
  6864. const tr1 = document.createElement('tr');
  6865. const td1 = document.createElement('td');
  6866. td1.colSpan = '3';
  6867. td1.textContent = '\u5EFA\u8BAE\u53EA\u5F00\u4E00\u4E2A\u6807\u7B7E\u9875\u3002\u5173\u6389\u6807\u7B7E\u9875\u540E\uFF0C\u7F13\u5B58\u5C31\u4F1A\u88AB\u6E05\u7406\u3002\u522B\u5FD8\u4E86\u53E6\u5B58\u4E3A\uFF01';
  6868. tr1.append(td1);
  6869. return tr1;
  6870. })(), (() => {
  6871. const tr1 = document.createElement('tr');
  6872. const td1 = document.createElement('td');
  6873. td1.colSpan = '3';
  6874. this.displayQuota.bind(this)(td1);
  6875. tr1.append(td1);
  6876. return tr1;
  6877. })());
  6878. this.cidSessionDom.flvTable = table;
  6879. // 4. build container dlv
  6880. const div = UI.genDiv();
  6881. div.ondragenter = div.ondragover = e => UI.allowDrag(e);
  6882. div.ondrop = async e => {
  6883. // 4.1 allow drag
  6884. UI.allowDrag(e);
  6885. // 4.2 sort files if possible
  6886. const files = Array.from(e.dataTransfer.files);
  6887. if (files.every(e => e.name.search(/\d+-\d+(?:\d|-|hd)*\.flv/) != -1)) {
  6888. files.sort((a, b) => a.name.match(/\d+-(\d+)(?:\d|-|hd)*\.flv/)[1] - b.name.match(/\d+-(\d+)(?:\d|-|hd)*\.flv/)[1]);
  6889. }
  6890. // 4.3 give loaded files hint
  6891. table.append(...files.map(e => {
  6892. const tr1 = document.createElement('tr');
  6893. const td1 = document.createElement('td');
  6894. td1.colSpan = '3';
  6895. td1.textContent = e.name;
  6896. tr1.append(td1);
  6897. return tr1;
  6898. }));
  6899. // 4.4 determine output name
  6900. let outputName = files[0].name.match(/\d+-\d+(?:\d|-|hd)*\.flv/);
  6901. if (outputName) outputName = outputName[0].replace(/-\d/, "");else outputName = 'merge_' + files[0].name;
  6902. // 4.5 build output ui
  6903. const href = await this.twin.mergeFLVFiles(files);
  6904. table.append((() => {
  6905. const tr1 = document.createElement('tr');
  6906. const td1 = document.createElement('td');
  6907. td1.colSpan = '3';
  6908. const a1 = document.createElement('a');
  6909. a1.href = href;
  6910. a1.download = outputName;
  6911. a1.textContent = outputName;
  6912. td1.append(a1);
  6913. tr1.append(td1);
  6914. return tr1;
  6915. })());
  6916. };
  6917. // 5. build util buttons
  6918. div.append(table, (() => {
  6919. const button = document.createElement('button');
  6920. button.style.padding = '0.5em';
  6921. button.style.margin = '0.2em';
  6922. button.onclick = () => div.style.display = 'none';
  6923. button.textContent = '\u5173\u95ED';
  6924. return button;
  6925. })(), (() => {
  6926. const button = document.createElement('button');
  6927. button.style.padding = '0.5em';
  6928. button.style.margin = '0.2em';
  6929. button.onclick = () => monkey.cleanAllFLVsInCache();
  6930. button.textContent = '\u6E05\u7A7A\u8FD9\u4E2A\u89C6\u9891\u7684\u7F13\u5B58';
  6931. return button;
  6932. })(), (() => {
  6933. const button = document.createElement('button');
  6934. button.style.padding = '0.5em';
  6935. button.style.margin = '0.2em';
  6936. button.onclick = () => this.twin.clearCacheDB(cache);
  6937. button.textContent = '\u6E05\u7A7A\u6240\u6709\u89C6\u9891\u7684\u7F13\u5B58';
  6938. return button;
  6939. })());
  6940. // 6. cancel on destroy
  6941. this.cidSessionDestroy.addCallback(() => {
  6942. flvTrs.map(tr => {
  6943. const a = tr.children[1].children[0];
  6944. if (a.textContent == '取消') a.click();
  6945. });
  6946. });
  6947. return this.cidSessionDom.flvDiv = div;
  6948. }
  6949. displayFLVDiv(flvDiv = this.cidSessionDom.flvDiv) {
  6950. if (!flvDiv) {
  6951. flvDiv = this.buildFLVDiv();
  6952. document.body.append(flvDiv);
  6953. }
  6954. flvDiv.style.display = '';
  6955. return flvDiv;
  6956. }
  6957. async downloadAllFLVs({ a, monkey = this.twin.monkey, table = this.cidSessionDom.flvTable }) {
  6958. if (this.cidSessionDom.downloadAllTr) return;
  6959. // 1. hang player
  6960. monkey.hangPlayer();
  6961. // 2. give hang player hint
  6962. this.cidSessionDom.downloadAllTr = (() => {
  6963. const tr1 = document.createElement('tr');
  6964. const td1 = document.createElement('td');
  6965. td1.colSpan = '3';
  6966. td1.textContent = '\u5DF2\u5C4F\u853D\u7F51\u9875\u64AD\u653E\u5668\u7684\u7F51\u7EDC\u94FE\u63A5\u3002\u5207\u6362\u6E05\u6670\u5EA6\u53EF\u91CD\u65B0\u6FC0\u6D3B\u64AD\u653E\u5668\u3002';
  6967. tr1.append(td1);
  6968. return tr1;
  6969. })();
  6970. table.append(this.cidSessionDom.downloadAllTr);
  6971. // 3. click download all split
  6972. for (let i = 0; i < monkey.flvs.length; i++) {
  6973. if (table.rows[i].cells[1].children[0].textContent == '缓存本段') table.rows[i].cells[1].children[0].click();
  6974. }
  6975. // 4. set sprogress
  6976. const progress = a.parentElement.nextElementSibling.children[0];
  6977. progress.max = monkey.flvs.length + 1;
  6978. progress.value = 0;
  6979. for (let i = 0; i < monkey.flvs.length; i++) monkey.getFLV(i).then(e => progress.value++);
  6980. // 5. merge splits
  6981. const files = await monkey.getAllFLVs();
  6982. const href = await this.twin.mergeFLVFiles(files);
  6983. const ass = await monkey.ass;
  6984. const outputName = top.document.getElementsByTagName('h1')[0].textContent.trim();
  6985. // 6. build download all ui
  6986. progress.value++;
  6987. table.prepend((() => {
  6988. const tr1 = document.createElement('tr');
  6989. const td1 = document.createElement('td');
  6990. td1.colSpan = '3';
  6991. td1.style = 'border: 1px solid black';
  6992. const a1 = document.createElement('a');
  6993. a1.href = href;
  6994. a1.download = `${outputName}.flv`;
  6995. (a => {
  6996. if (this.option.autoDanmaku) a.onclick = () => a.nextElementSibling.click();
  6997. })(a1);
  6998. a1.textContent = '\u4FDD\u5B58\u5408\u5E76\u540EFLV';
  6999. td1.append(a1);
  7000. td1.append(' ');
  7001. const a2 = document.createElement('a');
  7002. a2.href = ass;
  7003. a2.download = `${outputName}.ass`;
  7004. a2.textContent = '\u5F39\u5E55ASS';
  7005. td1.append(a2);
  7006. td1.append(' ');
  7007. const a3 = document.createElement('a');
  7008. a3.onclick = () => new MKVTransmuxer().exec(href, ass, `${outputName}.mkv`);
  7009. a3.textContent = '\u6253\u5305MKV(\u8F6F\u5B57\u5E55\u5C01\u88C5)';
  7010. td1.append(a3);
  7011. td1.append(' ');
  7012. td1.append('\u8BB0\u5F97\u6E05\u7406\u5206\u6BB5\u7F13\u5B58\u54E6~');
  7013. tr1.append(td1);
  7014. return tr1;
  7015. })());
  7016. return href;
  7017. }
  7018. async downloadFLV({ a, monkey = this.twin.monkey, index, progress = {} }) {
  7019. // 1. add beforeUnloadHandler
  7020. const handler = e => UI.beforeUnloadHandler(e);
  7021. window.addEventListener('beforeunload', handler);
  7022. // 2. switch to cancel ui
  7023. a.textContent = '取消';
  7024. a.onclick = () => {
  7025. a.onclick = null;
  7026. window.removeEventListener('beforeunload', handler);
  7027. a.textContent = '已取消';
  7028. monkey.abortFLV(index);
  7029. };
  7030. // 3. try download
  7031. let url;
  7032. try {
  7033. url = await monkey.getFLV(index, (loaded, total) => {
  7034. progress.value = loaded;
  7035. progress.max = total;
  7036. });
  7037. url = URL.createObjectURL(url);
  7038. if (progress.value == 0) progress.value = progress.max = 1;
  7039. } catch (e) {
  7040. a.onclick = null;
  7041. window.removeEventListener('beforeunload', handler);
  7042. a.textContent = '错误';
  7043. throw e;
  7044. }
  7045. // 4. switch to complete ui
  7046. a.onclick = null;
  7047. window.removeEventListener('beforeunload', handler);
  7048. a.textContent = '另存为';
  7049. a.download = monkey.flvs[index].match(/\d+-\d+(?:\d|-|hd)*\.flv/)[0];
  7050. a.href = url;
  7051. return url;
  7052. }
  7053. async displayQuota(td) {
  7054. return new Promise(resolve => {
  7055. const temporaryStorage = window.navigator.temporaryStorage || window.navigator.webkitTemporaryStorage || window.navigator.mozTemporaryStorage || window.navigator.msTemporaryStorage;
  7056. if (!temporaryStorage) return resolve(td.textContent = '这个浏览器不支持缓存呢~关掉标签页后,缓存马上就会消失哦');
  7057. temporaryStorage.queryUsageAndQuota((usage, quota) => resolve(td.textContent = `缓存已用空间:${Math.round(usage / 1048576)} MB / ${Math.round(quota / 1048576)} MB 也包括了B站本来的缓存`));
  7058. });
  7059. }
  7060. // Menu Append
  7061. appendMenu(playerWin = this.twin.playerWin) {
  7062. // 1. build monkey menu and polyfill menu
  7063. const monkeyMenu = this.buildMonkeyMenu();
  7064. const polyfillMenu = this.buildPolyfillMenu();
  7065. // 2. build ul
  7066. const ul = document.createElement('ul');
  7067. // 3. append to menu
  7068. ul.className = 'bilitwin';
  7069. ul.style.borderBottom = '1px solid rgba(255,255,255,.12)';
  7070. ul.append(...[monkeyMenu, polyfillMenu]);
  7071. const div = playerWin.document.getElementsByClassName('bilibili-player-context-menu-container black')[0];
  7072. div.prepend(ul);
  7073. // 4. save to cache
  7074. this.cidSessionDom.menuUl = ul;
  7075. return ul;
  7076. }
  7077. buildMonkeyMenu({
  7078. playerWin = this.twin.playerWin,
  7079. BiliMonkey = this.twin.BiliMonkey,
  7080. monkey = this.twin.monkey,
  7081. flvA = this.cidSessionDom.flvA,
  7082. mp4A = this.cidSessionDom.mp4A,
  7083. assA = this.cidSessionDom.assA
  7084. } = {}) {
  7085. const li = document.createElement('li');
  7086. li.className = 'context-menu-menu bilitwin';
  7087. li.onclick = () => playerWin.document.getElementById('bilibiliPlayer').click();
  7088. const a1 = document.createElement('a');
  7089. a1.className = 'context-menu-a';
  7090. a1.append('BiliMonkey');
  7091. const span = document.createElement('span');
  7092. span.className = 'bpui-icon bpui-icon-arrow-down';
  7093. span.style = 'transform:rotate(-90deg);margin-top:3px;';
  7094. a1.append(span);
  7095. li.append(a1);
  7096. const ul1 = document.createElement('ul');
  7097. const li1 = document.createElement('li');
  7098. li1.className = 'context-menu-function';
  7099. li1.onclick = async () => {
  7100. if (flvA.onmouseover) await flvA.onmouseover();
  7101. flvA.click();
  7102. };
  7103. const a2 = document.createElement('a');
  7104. a2.className = 'context-menu-a';
  7105. const span1 = document.createElement('span');
  7106. span1.className = 'video-contextmenu-icon';
  7107. a2.append(span1);
  7108. a2.append(' \u4E0B\u8F7DFLV');
  7109. li1.append(a2);
  7110. ul1.append(li1);
  7111. const li2 = document.createElement('li');
  7112. li2.className = 'context-menu-function';
  7113. li2.onclick = async () => {
  7114. if (mp4A.onmouseover) await mp4A.onmouseover();
  7115. mp4A.click();
  7116. };
  7117. const a3 = document.createElement('a');
  7118. a3.className = 'context-menu-a';
  7119. const span2 = document.createElement('span');
  7120. span2.className = 'video-contextmenu-icon';
  7121. a3.append(span2);
  7122. a3.append(' \u4E0B\u8F7DMP4');
  7123. li2.append(a3);
  7124. ul1.append(li2);
  7125. const li3 = document.createElement('li');
  7126. li3.className = 'context-menu-function';
  7127. li3.onclick = async () => {
  7128. if (assA.onmouseover) await assA.onmouseover();
  7129. assA.click();
  7130. };
  7131. const a4 = document.createElement('a');
  7132. a4.className = 'context-menu-a';
  7133. const span3 = document.createElement('span');
  7134. span3.className = 'video-contextmenu-icon';
  7135. a4.append(span3);
  7136. a4.append(' \u4E0B\u8F7DASS');
  7137. li3.append(a4);
  7138. ul1.append(li3);
  7139. const li4 = document.createElement('li');
  7140. li4.className = 'context-menu-function';
  7141. li4.onclick = () => this.displayOptionDiv();
  7142. const a5 = document.createElement('a');
  7143. a5.className = 'context-menu-a';
  7144. const span4 = document.createElement('span');
  7145. span4.className = 'video-contextmenu-icon';
  7146. a5.append(span4);
  7147. a5.append(' \u8BBE\u7F6E/\u5E2E\u52A9/\u5173\u4E8E');
  7148. li4.append(a5);
  7149. ul1.append(li4);
  7150. const li5 = document.createElement('li');
  7151. li5.className = 'context-menu-function';
  7152. li5.onclick = async () => UI.displayDownloadAllPageDefaultFormatsBody((await BiliMonkey.getAllPageDefaultFormats(playerWin)));
  7153. const a6 = document.createElement('a');
  7154. a6.className = 'context-menu-a';
  7155. const span5 = document.createElement('span');
  7156. span5.className = 'video-contextmenu-icon';
  7157. a6.append(span5);
  7158. a6.append(' (\u6D4B)\u6279\u91CF\u4E0B\u8F7D');
  7159. li5.append(a6);
  7160. ul1.append(li5);
  7161. const li6 = document.createElement('li');
  7162. li6.className = 'context-menu-function';
  7163. li6.onclick = async () => {
  7164. monkey.proxy = true;
  7165. monkey.flvs = null;
  7166. UI.hintInfo('请稍候,可能需要10秒时间……', playerWin);
  7167. // Yes, I AM lazy.
  7168. playerWin.document.querySelector('div.bilibili-player-video-btn-quality > div ul li[data-value="80"]').click();
  7169. await new Promise(r => playerWin.document.getElementsByTagName('video')[0].addEventListener('emptied', r));
  7170. return monkey.queryInfo('flv');
  7171. };
  7172. const a7 = document.createElement('a');
  7173. a7.className = 'context-menu-a';
  7174. const span6 = document.createElement('span');
  7175. span6.className = 'video-contextmenu-icon';
  7176. a7.append(span6);
  7177. a7.append(' (\u6D4B)\u8F7D\u5165\u7F13\u5B58FLV');
  7178. li6.append(a7);
  7179. ul1.append(li6);
  7180. const li7 = document.createElement('li');
  7181. li7.className = 'context-menu-function';
  7182. li7.onclick = () => top.location.reload(true);
  7183. const a8 = document.createElement('a');
  7184. a8.className = 'context-menu-a';
  7185. const span7 = document.createElement('span');
  7186. span7.className = 'video-contextmenu-icon';
  7187. a8.append(span7);
  7188. a8.append(' (\u6D4B)\u5F3A\u5236\u5237\u65B0');
  7189. li7.append(a8);
  7190. ul1.append(li7);
  7191. const li8 = document.createElement('li');
  7192. li8.className = 'context-menu-function';
  7193. li8.onclick = () => this.cidSessionDestroy() && this.cidSessionRender();
  7194. const a9 = document.createElement('a');
  7195. a9.className = 'context-menu-a';
  7196. const span8 = document.createElement('span');
  7197. span8.className = 'video-contextmenu-icon';
  7198. a9.append(span8);
  7199. a9.append(' (\u6D4B)\u91CD\u542F\u811A\u672C');
  7200. li8.append(a9);
  7201. ul1.append(li8);
  7202. const li9 = document.createElement('li');
  7203. li9.className = 'context-menu-function';
  7204. li9.onclick = () => playerWin.player && playerWin.player.destroy();
  7205. const a10 = document.createElement('a');
  7206. a10.className = 'context-menu-a';
  7207. const span9 = document.createElement('span');
  7208. span9.className = 'video-contextmenu-icon';
  7209. a10.append(span9);
  7210. a10.append(' (\u6D4B)\u9500\u6BC1\u64AD\u653E\u5668');
  7211. li9.append(a10);
  7212. ul1.append(li9);
  7213. li.append(ul1);
  7214. return li;
  7215. }
  7216. buildPolyfillMenu({
  7217. playerWin = this.twin.playerWin,
  7218. BiliPolyfill = this.twin.BiliPolyfill,
  7219. polyfill = this.twin.polyfill
  7220. } = {}) {
  7221. let oped = [];
  7222. const refreshSession = new HookedFunction(() => oped = polyfill.userdata.oped[polyfill.getCollectionId()] || []); // as a convenient callback register
  7223. const li = document.createElement('li');
  7224. li.className = 'context-menu-menu bilitwin';
  7225. li.onclick = () => playerWin.document.getElementById('bilibiliPlayer').click();
  7226. const a1 = document.createElement('a');
  7227. a1.className = 'context-menu-a';
  7228. a1.onmouseover = () => refreshSession();
  7229. a1.append('BiliPolyfill');
  7230. a1.append(!polyfill.option.betabeta ? '(到设置开启)' : '');
  7231. const span = document.createElement('span');
  7232. span.className = 'bpui-icon bpui-icon-arrow-down';
  7233. span.style = 'transform:rotate(-90deg);margin-top:3px;';
  7234. a1.append(span);
  7235. li.append(a1);
  7236. const ul1 = document.createElement('ul');
  7237. const li1 = document.createElement('li');
  7238. li1.className = 'context-menu-function';
  7239. li1.onclick = () => top.window.open(polyfill.getCoverImage(), '_blank');
  7240. const a2 = document.createElement('a');
  7241. a2.className = 'context-menu-a';
  7242. const span1 = document.createElement('span');
  7243. span1.className = 'video-contextmenu-icon';
  7244. a2.append(span1);
  7245. a2.append(' \u83B7\u53D6\u5C01\u9762');
  7246. li1.append(a2);
  7247. ul1.append(li1);
  7248. const li2 = document.createElement('li');
  7249. li2.className = 'context-menu-menu';
  7250. const a3 = document.createElement('a');
  7251. a3.className = 'context-menu-a';
  7252. const span2 = document.createElement('span');
  7253. span2.className = 'video-contextmenu-icon';
  7254. a3.append(span2);
  7255. a3.append(' \u66F4\u591A\u64AD\u653E\u901F\u5EA6');
  7256. const span3 = document.createElement('span');
  7257. span3.className = 'bpui-icon bpui-icon-arrow-down';
  7258. span3.style = 'transform:rotate(-90deg);margin-top:3px;';
  7259. a3.append(span3);
  7260. li2.append(a3);
  7261. const ul2 = document.createElement('ul');
  7262. const li3 = document.createElement('li');
  7263. li3.className = 'context-menu-function';
  7264. li3.onclick = () => {
  7265. polyfill.setVideoSpeed(0.1);
  7266. };
  7267. const a4 = document.createElement('a');
  7268. a4.className = 'context-menu-a';
  7269. const span4 = document.createElement('span');
  7270. span4.className = 'video-contextmenu-icon';
  7271. a4.append(span4);
  7272. a4.append(' 0.1');
  7273. li3.append(a4);
  7274. ul2.append(li3);
  7275. const li4 = document.createElement('li');
  7276. li4.className = 'context-menu-function';
  7277. li4.onclick = () => {
  7278. polyfill.setVideoSpeed(3);
  7279. };
  7280. const a5 = document.createElement('a');
  7281. a5.className = 'context-menu-a';
  7282. const span5 = document.createElement('span');
  7283. span5.className = 'video-contextmenu-icon';
  7284. a5.append(span5);
  7285. a5.append(' 3');
  7286. li4.append(a5);
  7287. ul2.append(li4);
  7288. const li5 = document.createElement('li');
  7289. li5.className = 'context-menu-function';
  7290. li5.onclick = e => polyfill.setVideoSpeed(e.children[0].children[1].value);
  7291. const a6 = document.createElement('a');
  7292. a6.className = 'context-menu-a';
  7293. const span6 = document.createElement('span');
  7294. span6.className = 'video-contextmenu-icon';
  7295. a6.append(span6);
  7296. a6.append(' \u70B9\u51FB\u786E\u8BA4');
  7297. const input = document.createElement('input');
  7298. input.type = 'text';
  7299. input.style = 'width: 35px; height: 70%';
  7300. input.onclick = e => e.stopPropagation();
  7301. (e => refreshSession.addCallback(() => e.value = polyfill.video.playbackRate))(input);
  7302. a6.append(input);
  7303. li5.append(a6);
  7304. ul2.append(li5);
  7305. li2.append(ul2);
  7306. ul1.append(li2);
  7307. const li6 = document.createElement('li');
  7308. li6.className = 'context-menu-menu';
  7309. const a7 = document.createElement('a');
  7310. a7.className = 'context-menu-a';
  7311. const span7 = document.createElement('span');
  7312. span7.className = 'video-contextmenu-icon';
  7313. a7.append(span7);
  7314. a7.append(' \u7247\u5934\u7247\u5C3E');
  7315. const span8 = document.createElement('span');
  7316. span8.className = 'bpui-icon bpui-icon-arrow-down';
  7317. span8.style = 'transform:rotate(-90deg);margin-top:3px;';
  7318. a7.append(span8);
  7319. li6.append(a7);
  7320. const ul3 = document.createElement('ul');
  7321. const li7 = document.createElement('li');
  7322. li7.className = 'context-menu-function';
  7323. li7.onclick = () => polyfill.markOPEDPosition(0);
  7324. const a8 = document.createElement('a');
  7325. a8.className = 'context-menu-a';
  7326. const span9 = document.createElement('span');
  7327. span9.className = 'video-contextmenu-icon';
  7328. a8.append(span9);
  7329. a8.append(' \u7247\u5934\u5F00\u59CB:');
  7330. const span10 = document.createElement('span');
  7331. (e => refreshSession.addCallback(() => e.textContent = oped[0] ? BiliPolyfill.secondToReadable(oped[0]) : '无'))(span10);
  7332. a8.append(span10);
  7333. li7.append(a8);
  7334. ul3.append(li7);
  7335. const li8 = document.createElement('li');
  7336. li8.className = 'context-menu-function';
  7337. li8.onclick = () => polyfill.markOPEDPosition(1);
  7338. const a9 = document.createElement('a');
  7339. a9.className = 'context-menu-a';
  7340. const span11 = document.createElement('span');
  7341. span11.className = 'video-contextmenu-icon';
  7342. a9.append(span11);
  7343. a9.append(' \u7247\u5934\u7ED3\u675F:');
  7344. const span12 = document.createElement('span');
  7345. (e => refreshSession.addCallback(() => e.textContent = oped[1] ? BiliPolyfill.secondToReadable(oped[1]) : '无'))(span12);
  7346. a9.append(span12);
  7347. li8.append(a9);
  7348. ul3.append(li8);
  7349. const li9 = document.createElement('li');
  7350. li9.className = 'context-menu-function';
  7351. li9.onclick = () => polyfill.markOPEDPosition(2);
  7352. const a10 = document.createElement('a');
  7353. a10.className = 'context-menu-a';
  7354. const span13 = document.createElement('span');
  7355. span13.className = 'video-contextmenu-icon';
  7356. a10.append(span13);
  7357. a10.append(' \u7247\u5C3E\u5F00\u59CB:');
  7358. const span14 = document.createElement('span');
  7359. (e => refreshSession.addCallback(() => e.textContent = oped[2] ? BiliPolyfill.secondToReadable(oped[2]) : '无'))(span14);
  7360. a10.append(span14);
  7361. li9.append(a10);
  7362. ul3.append(li9);
  7363. const li10 = document.createElement('li');
  7364. li10.className = 'context-menu-function';
  7365. li10.onclick = () => polyfill.markOPEDPosition(3);
  7366. const a11 = document.createElement('a');
  7367. a11.className = 'context-menu-a';
  7368. const span15 = document.createElement('span');
  7369. span15.className = 'video-contextmenu-icon';
  7370. a11.append(span15);
  7371. a11.append(' \u7247\u5C3E\u7ED3\u675F:');
  7372. const span16 = document.createElement('span');
  7373. (e => refreshSession.addCallback(() => e.textContent = oped[3] ? BiliPolyfill.secondToReadable(oped[3]) : '无'))(span16);
  7374. a11.append(span16);
  7375. li10.append(a11);
  7376. ul3.append(li10);
  7377. const li11 = document.createElement('li');
  7378. li11.className = 'context-menu-function';
  7379. li11.onclick = () => polyfill.clearOPEDPosition();
  7380. const a12 = document.createElement('a');
  7381. a12.className = 'context-menu-a';
  7382. const span17 = document.createElement('span');
  7383. span17.className = 'video-contextmenu-icon';
  7384. a12.append(span17);
  7385. a12.append(' \u53D6\u6D88\u6807\u8BB0');
  7386. li11.append(a12);
  7387. ul3.append(li11);
  7388. const li12 = document.createElement('li');
  7389. li12.className = 'context-menu-function';
  7390. li12.onclick = () => this.displayPolyfillDataDiv();
  7391. const a13 = document.createElement('a');
  7392. a13.className = 'context-menu-a';
  7393. const span18 = document.createElement('span');
  7394. span18.className = 'video-contextmenu-icon';
  7395. a13.append(span18);
  7396. a13.append(' \u68C0\u89C6\u6570\u636E/\u8BF4\u660E');
  7397. li12.append(a13);
  7398. ul3.append(li12);
  7399. li6.append(ul3);
  7400. ul1.append(li6);
  7401. const li13 = document.createElement('li');
  7402. li13.className = 'context-menu-menu';
  7403. const a14 = document.createElement('a');
  7404. a14.className = 'context-menu-a';
  7405. const span19 = document.createElement('span');
  7406. span19.className = 'video-contextmenu-icon';
  7407. a14.append(span19);
  7408. a14.append(' \u627E\u4E0A\u4E0B\u96C6');
  7409. const span20 = document.createElement('span');
  7410. span20.className = 'bpui-icon bpui-icon-arrow-down';
  7411. span20.style = 'transform:rotate(-90deg);margin-top:3px;';
  7412. a14.append(span20);
  7413. li13.append(a14);
  7414. const ul4 = document.createElement('ul');
  7415. const li14 = document.createElement('li');
  7416. li14.className = 'context-menu-function';
  7417. li14.onclick = () => {
  7418. if (polyfill.series[0]) {
  7419. top.window.open(`https://www.bilibili.com/video/av${polyfill.series[0].aid}`, '_blank');
  7420. }
  7421. };
  7422. const a15 = document.createElement('a');
  7423. a15.className = 'context-menu-a';
  7424. a15.style.width = 'initial';
  7425. const span21 = document.createElement('span');
  7426. span21.className = 'video-contextmenu-icon';
  7427. a15.append(span21);
  7428. const span22 = document.createElement('span');
  7429. (e => refreshSession.addCallback(() => e.textContent = polyfill.series[0] ? polyfill.series[0].title : '找不到'))(span22);
  7430. a15.append(span22);
  7431. li14.append(a15);
  7432. ul4.append(li14);
  7433. const li15 = document.createElement('li');
  7434. li15.className = 'context-menu-function';
  7435. li15.onclick = () => {
  7436. if (polyfill.series[1]) {
  7437. top.window.open(`https://www.bilibili.com/video/av${polyfill.series[1].aid}`, '_blank');
  7438. }
  7439. };
  7440. const a16 = document.createElement('a');
  7441. a16.className = 'context-menu-a';
  7442. a16.style.width = 'initial';
  7443. const span23 = document.createElement('span');
  7444. span23.className = 'video-contextmenu-icon';
  7445. a16.append(span23);
  7446. const span24 = document.createElement('span');
  7447. (e => refreshSession.addCallback(() => e.textContent = polyfill.series[1] ? polyfill.series[1].title : '找不到'))(span24);
  7448. a16.append(span24);
  7449. li15.append(a16);
  7450. ul4.append(li15);
  7451. li13.append(ul4);
  7452. ul1.append(li13);
  7453. const li16 = document.createElement('li');
  7454. li16.className = 'context-menu-function';
  7455. li16.onclick = () => BiliPolyfill.openMinimizedPlayer();
  7456. const a17 = document.createElement('a');
  7457. a17.className = 'context-menu-a';
  7458. const span25 = document.createElement('span');
  7459. span25.className = 'video-contextmenu-icon';
  7460. a17.append(span25);
  7461. a17.append(' \u5C0F\u7A97\u64AD\u653E');
  7462. li16.append(a17);
  7463. ul1.append(li16);
  7464. const li17 = document.createElement('li');
  7465. li17.className = 'context-menu-function';
  7466. li17.onclick = () => this.displayOptionDiv();
  7467. const a18 = document.createElement('a');
  7468. a18.className = 'context-menu-a';
  7469. const span26 = document.createElement('span');
  7470. span26.className = 'video-contextmenu-icon';
  7471. a18.append(span26);
  7472. a18.append(' \u8BBE\u7F6E/\u5E2E\u52A9/\u5173\u4E8E');
  7473. li17.append(a18);
  7474. ul1.append(li17);
  7475. const li18 = document.createElement('li');
  7476. li18.className = 'context-menu-function';
  7477. li18.onclick = () => polyfill.saveUserdata();
  7478. const a19 = document.createElement('a');
  7479. a19.className = 'context-menu-a';
  7480. const span27 = document.createElement('span');
  7481. span27.className = 'video-contextmenu-icon';
  7482. a19.append(span27);
  7483. a19.append(' (\u6D4B)\u7ACB\u5373\u4FDD\u5B58\u6570\u636E');
  7484. li18.append(a19);
  7485. ul1.append(li18);
  7486. const li19 = document.createElement('li');
  7487. li19.className = 'context-menu-function';
  7488. li19.onclick = () => {
  7489. BiliPolyfill.clearAllUserdata(playerWin);
  7490. polyfill.retrieveUserdata();
  7491. };
  7492. const a20 = document.createElement('a');
  7493. a20.className = 'context-menu-a';
  7494. const span28 = document.createElement('span');
  7495. span28.className = 'video-contextmenu-icon';
  7496. a20.append(span28);
  7497. a20.append(' (\u6D4B)\u5F3A\u5236\u6E05\u7A7A\u6570\u636E');
  7498. li19.append(a20);
  7499. ul1.append(li19);
  7500. li.append(ul1);
  7501. return li;
  7502. }
  7503. buildOptionDiv(twin = this.twin) {
  7504. const div = UI.genDiv();
  7505. div.append(this.buildMonkeyOptionTable(), this.buildPolyfillOptionTable(), this.buildUIOptionTable(), (() => {
  7506. const table1 = document.createElement('table');
  7507. table1.style.width = '100%';
  7508. table1.style.lineHeight = '2em';
  7509. const tr1 = document.createElement('tr');
  7510. const td1 = document.createElement('td');
  7511. td1.textContent = '\u8BBE\u7F6E\u81EA\u52A8\u4FDD\u5B58\uFF0C\u5237\u65B0\u540E\u751F\u6548\u3002';
  7512. tr1.append(td1);
  7513. table1.append(tr1);
  7514. const tr2 = document.createElement('tr');
  7515. const td2 = document.createElement('td');
  7516. td2.textContent = '\u89C6\u9891\u4E0B\u8F7D\u7EC4\u4EF6\u7684\u7F13\u5B58\u529F\u80FD\u53EA\u5728Windows+Chrome\u6D4B\u8BD5\u8FC7\uFF0C\u5982\u679C\u51FA\u73B0\u95EE\u9898\uFF0C\u8BF7\u5173\u95ED\u7F13\u5B58\u3002';
  7517. tr2.append(td2);
  7518. table1.append(tr2);
  7519. const tr3 = document.createElement('tr');
  7520. const td3 = document.createElement('td');
  7521. td3.textContent = '\u529F\u80FD\u589E\u5F3A\u7EC4\u4EF6\u5C3D\u91CF\u4FDD\u8BC1\u4E86\u517C\u5BB9\u6027\u3002\u4F46\u5982\u679C\u6709\u540C\u529F\u80FD\u811A\u672C/\u63D2\u4EF6\uFF0C\u8BF7\u5173\u95ED\u672C\u63D2\u4EF6\u7684\u5BF9\u5E94\u529F\u80FD\u3002';
  7522. tr3.append(td3);
  7523. table1.append(tr3);
  7524. const tr4 = document.createElement('tr');
  7525. const td4 = document.createElement('td');
  7526. td4.textContent = '\u8FD9\u4E2A\u811A\u672C\u4E43\u201C\u6309\u539F\u6837\u201D\u63D0\u4F9B\uFF0C\u4E0D\u9644\u5E26\u4EFB\u4F55\u660E\u793A\uFF0C\u6697\u793A\u6216\u6CD5\u5B9A\u7684\u4FDD\u8BC1\uFF0C\u5305\u62EC\u4F46\u4E0D\u9650\u4E8E\u5176\u6CA1\u6709\u7F3A\u9677\uFF0C\u9002\u5408\u7279\u5B9A\u76EE\u7684\u6216\u975E\u4FB5\u6743\u3002';
  7527. tr4.append(td4);
  7528. table1.append(tr4);
  7529. const tr5 = document.createElement('tr');
  7530. const td5 = document.createElement('td');
  7531. const a1 = document.createElement('a');
  7532. a1.href = 'https://greasyfork.org/zh-CN/scripts/27819';
  7533. a1.target = '_blank';
  7534. a1.textContent = '\u66F4\u65B0/\u8BA8\u8BBA';
  7535. td5.append(a1);
  7536. td5.append(' ');
  7537. const a2 = document.createElement('a');
  7538. a2.href = 'https://github.com/liqi0816/bilitwin/';
  7539. a2.target = '_blank';
  7540. a2.textContent = 'GitHub';
  7541. td5.append(a2);
  7542. td5.append(' ');
  7543. td5.append('Author: qli5. Copyright: qli5, 2014+, \u7530\u751F, grepmusic');
  7544. tr5.append(td5);
  7545. table1.append(tr5);
  7546. return table1;
  7547. })(), (() => {
  7548. const button = document.createElement('button');
  7549. button.style.padding = '0.5em';
  7550. button.style.margin = '0.2em';
  7551. button.onclick = () => div.style.display = 'none';
  7552. button.textContent = '\u5173\u95ED';
  7553. return button;
  7554. })(), (() => {
  7555. const button = document.createElement('button');
  7556. button.style.padding = '0.5em';
  7557. button.style.margin = '0.2em';
  7558. button.onclick = () => top.location.reload();
  7559. button.textContent = '\u4FDD\u5B58\u5E76\u5237\u65B0';
  7560. return button;
  7561. })(), (() => {
  7562. const button = document.createElement('button');
  7563. button.style.padding = '0.5em';
  7564. button.style.margin = '0.2em';
  7565. button.onclick = () => twin.resetOption() && top.location.reload();
  7566. button.textContent = '\u91CD\u7F6E\u5E76\u5237\u65B0';
  7567. return button;
  7568. })());
  7569. return this.dom.optionDiv = div;
  7570. }
  7571. buildMonkeyOptionTable(twin = this.twin, BiliMonkey = this.twin.BiliMonkey) {
  7572. const table = document.createElement('table');
  7573. {
  7574. table.style.width = '100%';
  7575. table.style.lineHeight = '2em';
  7576. const tr1 = document.createElement('tr');
  7577. const td1 = document.createElement('td');
  7578. td1.style = 'text-align:center';
  7579. td1.textContent = 'BiliMonkey\uFF08\u89C6\u9891\u6293\u53D6\u7EC4\u4EF6\uFF09';
  7580. tr1.append(td1);
  7581. table.append(tr1);
  7582. const tr2 = document.createElement('tr');
  7583. const td2 = document.createElement('td');
  7584. td2.style = 'text-align:center';
  7585. td2.textContent = '\u56E0\u4E3A\u4F5C\u8005\u5077\u61D2\u4E86\uFF0C\u7F13\u5B58\u7684\u4E09\u4E2A\u9009\u9879\u6700\u597D\u8981\u4E48\u5168\u5F00\uFF0C\u8981\u4E48\u5168\u5173\u3002\u6700\u597D\u3002';
  7586. tr2.append(td2);
  7587. table.append(tr2);
  7588. }
  7589. table.append(...BiliMonkey.optionDescriptions.map(([name, description]) => {
  7590. const tr1 = document.createElement('tr');
  7591. const label = document.createElement('label');
  7592. const input = document.createElement('input');
  7593. input.type = 'checkbox';
  7594. input.checked = twin.option[name];
  7595. input.onchange = e => {
  7596. twin.option[name] = e.target.checked;
  7597. twin.saveOption(twin.option);
  7598. };
  7599. label.append(input);
  7600. label.append(description);
  7601. tr1.append(label);
  7602. return tr1;
  7603. }));
  7604. return table;
  7605. }
  7606. buildPolyfillOptionTable(twin = this.twin, BiliPolyfill = this.twin.BiliPolyfill) {
  7607. const table = document.createElement('table');
  7608. {
  7609. table.style.width = '100%';
  7610. table.style.lineHeight = '2em';
  7611. const tr1 = document.createElement('tr');
  7612. const td1 = document.createElement('td');
  7613. td1.style = 'text-align:center';
  7614. td1.textContent = 'BiliPolyfill\uFF08\u529F\u80FD\u589E\u5F3A\u7EC4\u4EF6\uFF09';
  7615. tr1.append(td1);
  7616. table.append(tr1);
  7617. const tr2 = document.createElement('tr');
  7618. const td2 = document.createElement('td');
  7619. td2.style = 'text-align:center';
  7620. td2.textContent = '\u61D2\u9B3C\u4F5C\u8005\u8FD8\u5728\u6D4B\u8BD5\u7684\u65F6\u5019\uFF0CB\u7AD9\u5DF2\u7ECF\u4E0A\u7EBF\u4E86\u539F\u751F\u7684\u7A0D\u540E\u518D\u770B(\u0E51\u2022\u0300\u3142\u2022\u0301)\u0648\u2727';
  7621. tr2.append(td2);
  7622. table.append(tr2);
  7623. }
  7624. table.append(...BiliPolyfill.optionDescriptions.map(([name, description, disabled]) => {
  7625. const tr1 = document.createElement('tr');
  7626. const label = document.createElement('label');
  7627. label.style.textDecoration = disabled == 'disabled' ? 'line-through' : undefined;
  7628. const input = document.createElement('input');
  7629. input.type = 'checkbox';
  7630. input.checked = twin.option[name];
  7631. input.onchange = e => {
  7632. twin.option[name] = e.target.checked;
  7633. twin.saveOption(twin.option);
  7634. };
  7635. input.disabled = disabled == 'disabled';
  7636. label.append(input);
  7637. label.append(description);
  7638. tr1.append(label);
  7639. return tr1;
  7640. }));
  7641. return table;
  7642. }
  7643. buildUIOptionTable(twin = this.twin) {
  7644. const table = document.createElement('table');
  7645. {
  7646. table.style.width = '100%';
  7647. table.style.lineHeight = '2em';
  7648. const tr1 = document.createElement('tr');
  7649. const td1 = document.createElement('td');
  7650. td1.style = 'text-align:center';
  7651. td1.textContent = 'UI\uFF08\u7528\u6237\u754C\u9762\uFF09';
  7652. tr1.append(td1);
  7653. table.append(tr1);
  7654. }
  7655. table.append(...UI.optionDescriptions.map(([name, description]) => {
  7656. const tr1 = document.createElement('tr');
  7657. const label = document.createElement('label');
  7658. const input = document.createElement('input');
  7659. input.type = 'checkbox';
  7660. input.checked = twin.option[name];
  7661. input.onchange = e => {
  7662. twin.option[name] = e.target.checked;
  7663. twin.saveOption(twin.option);
  7664. };
  7665. label.append(input);
  7666. label.append(description);
  7667. tr1.append(label);
  7668. return tr1;
  7669. }));
  7670. return table;
  7671. }
  7672. displayOptionDiv(optionDiv = this.dom.optionDiv) {
  7673. if (!optionDiv) {
  7674. optionDiv = this.buildOptionDiv();
  7675. document.body.append(optionDiv);
  7676. }
  7677. optionDiv.style.display = '';
  7678. return optionDiv;
  7679. }
  7680. buildPolyfillDataDiv(polyfill = this.twin.polyfill) {
  7681. const textarea = document.createElement('textarea');
  7682. textarea.style.resize = 'vertical';
  7683. textarea.style.width = '100%';
  7684. textarea.style.height = '200px';
  7685. textarea.textContent = `
  7686. ${JSON.stringify(polyfill.userdata.oped).replace(/{/, '{\n').replace(/}/, '\n}').replace(/],/g, '],\n')}
  7687. `;
  7688. const div = UI.genDiv();
  7689. div.append((() => {
  7690. const p = document.createElement('p');
  7691. p.style.margin = '0.3em';
  7692. p.textContent = '\u8FD9\u91CC\u662F\u811A\u672C\u50A8\u5B58\u7684\u6570\u636E\u3002\u6240\u6709\u6570\u636E\u90FD\u53EA\u5B58\u5728\u6D4F\u89C8\u5668\u91CC\uFF0C\u522B\u4EBA\u4E0D\u77E5\u9053\uFF0CB\u7AD9\u4E5F\u4E0D\u77E5\u9053\uFF0C\u811A\u672C\u4F5C\u8005\u66F4\u4E0D\u77E5\u9053(\u8FD9\u4E2A\u5BB6\u4F19\u8FDE\u670D\u52A1\u5668\u90FD\u79DF\u4E0D\u8D77 \u6454';
  7693. return p;
  7694. })(), (() => {
  7695. const p = document.createElement('p');
  7696. p.style.margin = '0.3em';
  7697. p.textContent = 'B\u7AD9\u5DF2\u4E0A\u7EBF\u539F\u751F\u7684\u7A0D\u540E\u89C2\u770B\u529F\u80FD\u3002';
  7698. return p;
  7699. })(), (() => {
  7700. const p = document.createElement('p');
  7701. p.style.margin = '0.3em';
  7702. p.textContent = '\u8FD9\u91CC\u662F\u7247\u5934\u7247\u5C3E\u3002\u683C\u5F0F\u662F\uFF0Cav\u53F7\u6216\u756A\u5267\u53F7:[\u7247\u5934\u5F00\u59CB(\u9ED8\u8BA4=0),\u7247\u5934\u7ED3\u675F(\u9ED8\u8BA4=\u4E0D\u8DF3),\u7247\u5C3E\u5F00\u59CB(\u9ED8\u8BA4=\u4E0D\u8DF3),\u7247\u5C3E\u7ED3\u675F(\u9ED8\u8BA4=\u65E0\u7A77\u5927)]\u3002\u53EF\u4EE5\u4EFB\u610F\u586B\u5199null\uFF0C\u811A\u672C\u4F1A\u81EA\u52A8\u91C7\u7528\u9ED8\u8BA4\u503C\u3002';
  7703. return p;
  7704. })(), textarea, (() => {
  7705. const p = document.createElement('p');
  7706. p.style.margin = '0.3em';
  7707. p.textContent = '\u5F53\u7136\u53EF\u4EE5\u76F4\u63A5\u6E05\u7A7A\u5566\u3002\u53EA\u5220\u9664\u5176\u4E2D\u7684\u4E00\u4E9B\u884C\u7684\u8BDD\uFF0C\u4E00\u5B9A\u8981\u8BB0\u5F97\u5220\u6389\u591A\u4F59\u7684\u9017\u53F7\u3002';
  7708. return p;
  7709. })(), (() => {
  7710. const button = document.createElement('button');
  7711. button.style.padding = '0.5em';
  7712. button.style.margin = '0.2em';
  7713. button.onclick = () => div.remove();
  7714. button.textContent = '\u5173\u95ED';
  7715. return button;
  7716. })(), (() => {
  7717. const button = document.createElement('button');
  7718. button.style.padding = '0.5em';
  7719. button.style.margin = '0.2em';
  7720. button.onclick = e => {
  7721. if (!textarea.value) textarea.value = '{\n\n}';
  7722. textarea.value = textarea.value.replace(/,(\s|\n)*}/, '\n}').replace(/,(\s|\n),/g, ',\n').replace(/,(\s|\n)*]/g, ']');
  7723. const userdata = {};
  7724. try {
  7725. userdata.oped = JSON.parse(textarea.value);
  7726. } catch (e) {
  7727. alert('片头片尾: ' + e);throw e;
  7728. }
  7729. e.target.textContent = '格式没有问题!';
  7730. return userdata;
  7731. };
  7732. button.textContent = '\u9A8C\u8BC1\u683C\u5F0F';
  7733. return button;
  7734. })(), (() => {
  7735. const button = document.createElement('button');
  7736. button.style.padding = '0.5em';
  7737. button.style.margin = '0.2em';
  7738. button.onclick = e => {
  7739. polyfill.userdata = e.target.previousElementSibling.onclick({ target: e.target.previousElementSibling });
  7740. polyfill.saveUserdata();
  7741. e.target.textContent = '保存成功';
  7742. };
  7743. button.textContent = '\u5C1D\u8BD5\u4FDD\u5B58';
  7744. return button;
  7745. })());
  7746. return div;
  7747. }
  7748. displayPolyfillDataDiv(polyfill) {
  7749. const div = this.buildPolyfillDataDiv();
  7750. document.body.append(div);
  7751. div.style.display = 'block';
  7752. return div;
  7753. }
  7754. // Common
  7755. static buildDownloadAllPageDefaultFormatsBody(ret) {
  7756. const table = document.createElement('table');
  7757. table.onclick = e => e.stopPropagation();
  7758. for (const i of ret) {
  7759. table.append((() => {
  7760. const tr1 = document.createElement('tr');
  7761. const td1 = document.createElement('td');
  7762. td1.textContent = `
  7763. ${i.name}
  7764. `;
  7765. tr1.append(td1);
  7766. const td2 = document.createElement('td');
  7767. const a1 = document.createElement('a');
  7768. a1.href = i.durl[0];
  7769. a1.download = '';
  7770. a1.setAttribute('referrerpolicy', 'origin');
  7771. a1.textContent = i.durl[0];
  7772. td2.append(a1);
  7773. tr1.append(td2);
  7774. const td3 = document.createElement('td');
  7775. const a2 = document.createElement('a');
  7776. a2.href = i.danmuku;
  7777. a2.download = `${i.outputName}.ass`;
  7778. a2.setAttribute('referrerpolicy', 'origin');
  7779. a2.textContent = i.danmuku;
  7780. td3.append(a2);
  7781. tr1.append(td3);
  7782. return tr1;
  7783. })(), ...i.durl.slice(1).map(href => {
  7784. const tr1 = document.createElement('tr');
  7785. const td1 = document.createElement('td');
  7786. td1.textContent = `
  7787. `;
  7788. tr1.append(td1);
  7789. const td2 = document.createElement('td');
  7790. const a1 = document.createElement('a');
  7791. a1.href = href;
  7792. a1.download = '';
  7793. a1.setAttribute('referrerpolicy', 'origin');
  7794. a1.textContent = href;
  7795. td2.append(a1);
  7796. tr1.append(td2);
  7797. const td3 = document.createElement('td');
  7798. td3.textContent = `
  7799. `;
  7800. tr1.append(td3);
  7801. return tr1;
  7802. }));
  7803. }
  7804. const fragment = document.createDocumentFragment();
  7805. const style1 = document.createElement('style');
  7806. style1.textContent = `
  7807. table {
  7808. width: 100%;
  7809. table-layout: fixed;
  7810. }
  7811. td {
  7812. overflow: hidden;
  7813. white-space: nowrap;
  7814. text-overflow: ellipsis;
  7815. text-align: center;
  7816. }
  7817. `;
  7818. fragment.append(style1);
  7819. const h1 = document.createElement('h1');
  7820. h1.textContent = '(\u6D4B\u8BD5) \u6279\u91CF\u6293\u53D6';
  7821. fragment.append(h1);
  7822. const ul1 = document.createElement('ul');
  7823. const li = document.createElement('li');
  7824. const p = document.createElement('p');
  7825. p.textContent = '\u53EA\u6293\u53D6\u9ED8\u8BA4\u6E05\u6670\u5EA6';
  7826. li.append(p);
  7827. ul1.append(li);
  7828. const li1 = document.createElement('li');
  7829. const p1 = document.createElement('p');
  7830. p1.textContent = '\u590D\u5236\u94FE\u63A5\u5730\u5740\u65E0\u6548\uFF0C\u8BF7\u5DE6\u952E\u5355\u51FB/\u53F3\u952E\u53E6\u5B58\u4E3A/\u53F3\u952E\u8C03\u7528\u4E0B\u8F7D\u5DE5\u5177';
  7831. li1.append(p1);
  7832. const p2 = document.createElement('p');
  7833. const em = document.createElement('em');
  7834. em.textContent = '\u5F00\u53D1\u8005\uFF1A\u9700\u8981\u6821\u9A8Creferrer\u548Cuser agent';
  7835. p2.append(em);
  7836. li1.append(p2);
  7837. ul1.append(li1);
  7838. const li2 = document.createElement('li');
  7839. const p3 = document.createElement('p');
  7840. p3.append('flv\u5408\u5E76');
  7841. const a1 = document.createElement('a');
  7842. a1.href = 'http://www.flvcd.com/teacher2.htm';
  7843. a1.textContent = '\u7855\u9F20';
  7844. p3.append(a1);
  7845. li2.append(p3);
  7846. const p4 = document.createElement('p');
  7847. p4.textContent = '\u6279\u91CF\u5408\u5E76\u5BF9\u5355\u6807\u7B7E\u9875\u8D1F\u8377\u592A\u5927';
  7848. li2.append(p4);
  7849. const p5 = document.createElement('p');
  7850. const em1 = document.createElement('em');
  7851. em1.textContent = '\u5F00\u53D1\u8005\uFF1A\u53EF\u4EE5\u7528webworker\uFF0C\u4F46\u662F\u6211\u6CA1\u9700\u6C42\uFF0C\u53C8\u61D2';
  7852. p5.append(em1);
  7853. li2.append(p5);
  7854. ul1.append(li2);
  7855. fragment.append(ul1);
  7856. fragment.append(table);
  7857. return fragment;
  7858. }
  7859. static displayDownloadAllPageDefaultFormatsBody(ret) {
  7860. top.document.open();
  7861. top.document.close();
  7862. top.document.body.append(UI.buildDownloadAllPageDefaultFormatsBody(ret));
  7863. return ret;
  7864. }
  7865. static genDiv() {
  7866. const div1 = document.createElement('div');
  7867. div1.style.position = 'fixed';
  7868. div1.style.zIndex = '10036';
  7869. div1.style.top = '50%';
  7870. div1.style.marginTop = '-200px';
  7871. div1.style.left = '50%';
  7872. div1.style.marginLeft = '-320px';
  7873. div1.style.width = '540px';
  7874. div1.style.maxHeight = '400px';
  7875. div1.style.overflowY = 'auto';
  7876. div1.style.padding = '30px 50px';
  7877. div1.style.backgroundColor = 'white';
  7878. div1.style.borderRadius = '6px';
  7879. div1.style.boxShadow = 'rgba(0, 0, 0, 0.6) 1px 1px 40px 0px';
  7880. div1.style.display = 'none';
  7881. div1.addEventListener('click', e => e.stopPropagation());
  7882. div1.className = 'bilitwin';
  7883. return div1;
  7884. }
  7885. static requestH5Player() {
  7886. const h = document.querySelector('div.tminfo');
  7887. h.prepend('[[脚本需要HTML5播放器(弹幕列表右上角三个点的按钮切换)]] ');
  7888. }
  7889. static allowDrag(e) {
  7890. e.stopPropagation();
  7891. e.preventDefault();
  7892. }
  7893. static beforeUnloadHandler(e) {
  7894. return e.returnValue = '脚本还没做完工作,真的要退出吗?';
  7895. }
  7896. static hintInfo(text, playerWin) {
  7897. const div = document.createElement('div');
  7898. {
  7899. div.className = 'bilibili-player-video-toast-bottom';
  7900. const div1 = document.createElement('div');
  7901. div1.className = 'bilibili-player-video-toast-item';
  7902. const div2 = document.createElement('div');
  7903. div2.className = 'bilibili-player-video-toast-item-text';
  7904. const span = document.createElement('span');
  7905. span.textContent = text;
  7906. div2.append(span);
  7907. div1.append(div2);
  7908. div.append(div1);
  7909. }
  7910. playerWin.document.getElementsByClassName('bilibili-player-video-toast-wrp')[0].append(div);
  7911. setTimeout(() => div.remove(), 3000);
  7912. }
  7913. static get optionDescriptions() {
  7914. return [
  7915. // 1. automation
  7916. ['autoDanmaku', '下载视频也触发下载弹幕'],
  7917. // 2. user interface
  7918. ['title', '在视频标题旁添加链接'], ['menu', '在视频菜单栏添加链接'],
  7919. // 3. download
  7920. ['aria2', '导出aria2'], ['aria2RPC', '发送到aria2 RPC'], ['m3u8', '(限VLC兼容播放器)导出m3u8'], ['clipboard', '(测)(请自行解决referrer)强制导出剪贴板']];
  7921. }
  7922. static get optionDefaults() {
  7923. return {
  7924. // 1. automation
  7925. autoDanmaku: false,
  7926. // 2. user interface
  7927. title: true,
  7928. menu: true,
  7929. // 3. download
  7930. aria2: false,
  7931. aria2RPC: false,
  7932. m3u8: false,
  7933. clipboard: false
  7934. };
  7935. }
  7936. }
  7937. /***
  7938. * Copyright (C) 2018 Qli5. All Rights Reserved.
  7939. *
  7940. * @author qli5 <goodlq11[at](163|gmail).com>
  7941. *
  7942. * This Source Code Form is subject to the terms of the Mozilla Public
  7943. * License, v. 2.0. If a copy of the MPL was not distributed with this
  7944. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  7945. */
  7946. let debugOption = { debug: 1 };
  7947. class BiliTwin extends BiliUserJS {
  7948. static get debugOption() {
  7949. return debugOption;
  7950. }
  7951. static set debugOption(option) {
  7952. debugOption = option;
  7953. }
  7954. constructor(option = {}, ui) {
  7955. super();
  7956. this.BiliMonkey = BiliMonkey;
  7957. this.BiliPolyfill = BiliPolyfill;
  7958. this.playerWin = null;
  7959. this.monkey = null;
  7960. this.polifill = null;
  7961. this.ui = ui || new UI(this);
  7962. this.option = option;
  7963. }
  7964. async runCidSession() {
  7965. // 1. playerWin and option
  7966. try {
  7967. // you know what? it is a race, data race for jq! try not to yield to others!
  7968. this.playerWin = BiliUserJS.tryGetPlayerWinSync() || await BiliTwin.getPlayerWin();
  7969. }
  7970. catch (e) {
  7971. if (e == 'Need H5 Player') UI.requestH5Player();
  7972. throw e;
  7973. }
  7974. const href = location.href;
  7975. this.option = this.getOption();
  7976. if (this.option.debug) {
  7977. if (top.console) top.console.clear();
  7978. }
  7979. // 2. monkey and polyfill
  7980. this.monkey = new BiliMonkey(this.playerWin, this.option);
  7981. this.polyfill = new BiliPolyfill(this.playerWin, this.option, t => UI.hintInfo(t, this.playerWin));
  7982. await Promise.all([this.monkey.execOptions(), this.polyfill.setFunctions()]);
  7983. // 3. async consistent => render UI
  7984. const cidRefresh = BiliTwin.getCidRefreshPromise(this.playerWin);
  7985. if (href == location.href) {
  7986. this.ui.option = this.option;
  7987. this.ui.cidSessionRender();
  7988. }
  7989. else {
  7990. cidRefresh.resolve();
  7991. }
  7992. // 4. debug
  7993. if (this.option.debug) {
  7994. [(top.unsafeWindow || top).monkey, (top.unsafeWindow || top).polyfill] = [this.monkey, this.polyfill];
  7995. }
  7996. // 5. refresh => session expire
  7997. await cidRefresh;
  7998. this.monkey.destroy();
  7999. this.polyfill.destroy();
  8000. this.ui.cidSessionDestroy();
  8001. }
  8002. async mergeFLVFiles(files) {
  8003. return URL.createObjectURL(await FLV.mergeBlobs(files));
  8004. }
  8005. async clearCacheDB(cache) {
  8006. if (cache) return cache.deleteEntireDB();
  8007. }
  8008. resetOption(option = this.option) {
  8009. option.setStorage('BiliTwin', JSON.stringify({}));
  8010. return this.option = {};
  8011. }
  8012. getOption(playerWin = this.playerWin) {
  8013. let rawOption = null;
  8014. try {
  8015. rawOption = JSON.parse(playerWin.localStorage.getItem('BiliTwin'));
  8016. }
  8017. catch (e) { }
  8018. finally {
  8019. if (!rawOption) rawOption = {};
  8020. rawOption.setStorage = (n, i) => playerWin.localStorage.setItem(n, i);
  8021. rawOption.getStorage = n => playerWin.localStorage.getItem(n);
  8022. return Object.assign(
  8023. {},
  8024. BiliMonkey.optionDefaults,
  8025. BiliPolyfill.optionDefaults,
  8026. UI.optionDefaults,
  8027. rawOption,
  8028. BiliTwin.debugOption,
  8029. );
  8030. }
  8031. }
  8032. saveOption(option = this.option) {
  8033. return option.setStorage('BiliTwin', JSON.stringify(option));
  8034. }
  8035. static async init() {
  8036. if (!document.body) return;
  8037. BiliTwin.outdatedEngineClearance();
  8038. BiliTwin.firefoxClearance();
  8039. const twin = new BiliTwin();
  8040. while (1) {
  8041. await twin.runCidSession();
  8042. }
  8043. }
  8044. static outdatedEngineClearance() {
  8045. if (typeof Promise != 'function' || typeof MutationObserver != 'function') {
  8046. alert('这个浏览器实在太老了,脚本决定罢工。');
  8047. throw 'BiliTwin: browser outdated: Promise or MutationObserver unsupported';
  8048. }
  8049. }
  8050. static firefoxClearance() {
  8051. if (navigator.userAgent.includes('Firefox')) {
  8052. BiliTwin.debugOption.proxy = false;
  8053. if (!window.navigator.temporaryStorage && !window.navigator.mozTemporaryStorage) window.navigator.temporaryStorage = { queryUsageAndQuota: func => func(-1048576, 10484711424) };
  8054. }
  8055. }
  8056. static xpcWrapperClearance() {
  8057. if (top.unsafeWindow) {
  8058. Object.defineProperty(window, 'cid', {
  8059. configurable: true,
  8060. get: () => String(unsafeWindow.cid)
  8061. });
  8062. Object.defineProperty(window, 'player', {
  8063. configurable: true,
  8064. get: () => ({ destroy: unsafeWindow.player.destroy, reloadAccess: unsafeWindow.player.reloadAccess })
  8065. });
  8066. Object.defineProperty(window, 'jQuery', {
  8067. configurable: true,
  8068. get: () => unsafeWindow.jQuery,
  8069. });
  8070. Object.defineProperty(window, 'fetch', {
  8071. configurable: true,
  8072. get: () => unsafeWindow.fetch.bind(unsafeWindow),
  8073. set: _fetch => unsafeWindow.fetch = _fetch.bind(unsafeWindow)
  8074. });
  8075. }
  8076. }
  8077. }
  8078. BiliTwin.domContentLoadedThen(BiliTwin.init);