UNPKG

get-tracks-data

Version:

Get video / audio / subtitles tracks from MKV and MP4 files.

1 lines 15.5 kB
function e(e,t){if(t==null||t>e.length)t=e.length;for(var n=0,r=new Array(t);n<t;n++)r[n]=e[n];return r}function t(e){if(Array.isArray(e))return e}function n(e){if(e===void 0){throw new ReferenceError("this hasn't been initialised - super() hasn't been called")}return e}function r(e,t,n,r,a,u,i){try{var o=e[u](i);var f=o.value}catch(e){n(e);return}if(o.done){t(f)}else{Promise.resolve(f).then(r,a)}}function a(e){return function(){var t=this,n=arguments;return new Promise(function(a,u){var i=e.apply(t,n);function o(e){r(i,a,u,o,f,"next",e)}function f(e){r(i,a,u,o,f,"throw",e)}o(undefined)})}}function u(e,t){if(!(e instanceof t)){throw new TypeError("Cannot call a class as a function")}}function i(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||false;r.configurable=true;if("value"in r)r.writable=true;Object.defineProperty(e,r.key,r)}}function o(e,t,n){if(t)i(e.prototype,t);if(n)i(e,n);return e}function f(e){f=Object.setPrototypeOf?Object.getPrototypeOf:function e(e){return e.__proto__||Object.getPrototypeOf(e)};return f(e)}function c(e,t){if(typeof t!=="function"&&t!==null){throw new TypeError("Super expression must either be null or a function")}e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:true,configurable:true}});if(t)v(e,t)}function s(e,t){var n=e==null?null:typeof Symbol!=="undefined"&&e[Symbol.iterator]||e["@@iterator"];if(n==null)return;var r=[];var a=true;var u=false;var i,o;try{for(n=n.call(e);!(a=(i=n.next()).done);a=true){r.push(i.value);if(t&&r.length===t)break}}catch(e){u=true;o=e}finally{try{if(!a&&n["return"]!=null)n["return"]()}finally{if(u)throw o}}return r}function l(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function d(e,t){if(t&&(p(t)==="object"||typeof t==="function")){return t}return n(e)}function v(e,t){v=Object.setPrototypeOf||function e(e,t){e.__proto__=t;return e};return v(e,t)}function h(e,n){return t(e)||s(e,n)||y(e,n)||l()}function p(e){"@swc/helpers - typeof";return e&&typeof Symbol!=="undefined"&&e.constructor===Symbol?"symbol":typeof e}function y(t,n){if(!t)return;if(typeof t==="string")return e(t,n);var r=Object.prototype.toString.call(t).slice(8,-1);if(r==="Object"&&t.constructor)r=t.constructor.name;if(r==="Map"||r==="Set")return Array.from(r);if(r==="Arguments"||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r))return e(t,n)}function m(){if(typeof Reflect==="undefined"||!Reflect.construct)return false;if(Reflect.construct.sham)return false;if(typeof Proxy==="function")return true;try{Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}));return true}catch(e){return false}}function g(e){var t=m();return function n(){var n=f(e),r;if(t){var a=f(this).constructor;r=Reflect.construct(n,arguments,a)}else{r=n.apply(this,arguments)}return d(this,r)}}function b(e,t){var n,r,a,u,i={label:0,sent:function(){if(a[0]&1)throw a[1];return a[1]},trys:[],ops:[]};return u={next:o(0),"throw":o(1),"return":o(2)},typeof Symbol==="function"&&(u[Symbol.iterator]=function(){return this}),u;function o(e){return function(t){return f([e,t])}}function f(u){if(n)throw new TypeError("Generator is already executing.");while(i)try{if(n=1,r&&(a=u[0]&2?r["return"]:u[0]?r["throw"]||((a=r["return"])&&a.call(r),0):r.next)&&!(a=a.call(r,u[1])).done)return a;if(r=0,a)u=[u[0]&2,a.value];switch(u[0]){case 0:case 1:a=u;break;case 4:i.label++;return{value:u[1],done:false};case 5:i.label++;r=u[1];u=[0];continue;case 7:u=i.ops.pop();i.trys.pop();continue;default:if(!(a=i.trys,a=a.length>0&&a[a.length-1])&&(u[0]===6||u[0]===2)){i=0;continue}if(u[0]===3&&(!a||u[1]>a[0]&&u[1]<a[3])){i.label=u[1];break}if(u[0]===6&&i.label<a[1]){i.label=a[1];a=u;break}if(a&&i.label<a[2]){i.label=a[2];i.ops.push(u);break}if(a[2])i.ops.pop();i.trys.pop();continue}u=t.call(e,i)}catch(e){u=[6,e];r=0}finally{n=a=0}if(u[0]&5)throw u[1];return{value:u[0]?u[1]:void 0,done:true}}}var w=Object.defineProperty;var S=function(e,t,n){return t in e?w(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n};var k=function(e,t,n){return S(e,(typeof t==="undefined"?"undefined":p(t))!="symbol"?t+"":t,n)};import O from"fs";import{Readable as P}from"stream";import _ from"follow-redirects";var B=_.http,R=_.https,z=/bytes (\d+)-(\d+)\/(\d+)/,x=function(e){return e.startsWith("http://")||e.startsWith("https://")},T=function(e){"use strict";c(r,e);var t=g(r);function r(e){u(this,r);var i;i=t.call(this);k(n(i),"url");k(n(i),"contentLength");k(n(i),"bytesRead");k(n(i),"bytesOffset");k(n(i),"chunkSize");k(n(i),"_request",function(e){return new Promise(function(t,n){var r=h(i.url.split("//"),1),a=r[0],u={range:"bytes=".concat(e[0],"-").concat(e[1])};(a==="http:"?B:R).get(i.url,{headers:u},t).on("error",n)})});var o=n(i);k(n(i),"_requestRange",function(){var e=a(function(e){var t,n,r,a,u,i,f;return b(this,function(c){switch(c.label){case 0:return[4,o._request(e)];case 1:n=c.sent();o.url=n.responseUrl;r=function(e){return n.destroy(),Promise.reject(e)},a=(t=n.headers)===null||t===void 0?void 0:t["content-range"];if(!a)return[2,r("Failed to retrieve Content-Range from headers")];u=z.exec(a);if(!u)return[2,r("Failed to parse range from Content-Range header")];i=h(u,4),f=i[3];return[2,f?new Promise(function(e,t){var r=[];n.on("data",function(e){r.push(e)}),n.on("end",function(){e([Buffer.concat(r),parseInt(f)])}),n.on("error",t)}):r("Failed to parse length from Content-Range header")]}})});return function(t){return e.apply(this,arguments)}}());i.url=e,i.contentLength=i.readableHighWaterMark,i.bytesRead=0,i.bytesOffset=0,i.chunkSize=i.readableHighWaterMark;return i}o(r,[{key:"_read",value:function e(){var e=this;return a(function(){var t,n,r,a,u,i,o;return b(this,function(f){switch(f.label){case 0:f.trys.push([0,2,,3]);t=e.bytesOffset,n=Math.min(e.contentLength,e.bytesOffset+e.chunkSize)-1,r=[t,n];return[4,e._requestRange(r)];case 1:a=h.apply(void 0,[f.sent(),2]),u=a[0],i=a[1];e.contentLength=i,e.bytesRead+=u.length,e.bytesOffset=u.length,e.push(u);return[3,3];case 2:o=f.sent();e.emit("error",o),e.push(null);return[3,3];case 3:return[2]}})})()}}]);return r}(P),E=function(e){"use strict";c(r,e);var t=g(r);function r(e){u(this,r);var i;i=t.call(this);k(n(i),"path");k(n(i),"fileSize");k(n(i),"bytesRead");k(n(i),"bytesOffset");k(n(i),"chunkSize");var o=n(i);k(n(i),"_requestRange",function(){var e=a(function(e){return b(this,function(t){return[2,new Promise(function(t,n){var r=[],a=O.createReadStream(o.path,{start:e[0],end:e[1]});a.on("data",function(e){r.push(e)}),a.on("end",function(){t(Buffer.concat(r))}),a.on("error",n)})]})});return function(t){return e.apply(this,arguments)}}());var f=O.statSync(e);i.path=e,i.fileSize=f.size,i.bytesRead=0,i.bytesOffset=0,i.chunkSize=i.readableHighWaterMark;return i}o(r,[{key:"_read",value:function e(){var e=this;return a(function(){var t,n,r,a,u;return b(this,function(i){switch(i.label){case 0:i.trys.push([0,2,,3]);t=e.bytesOffset,n=Math.min(e.fileSize,e.bytesOffset+e.chunkSize)-1,r=[t,n];return[4,e._requestRange(r)];case 1:a=i.sent();e.bytesRead+=a.length,e.bytesOffset=a.length,e.push(a);return[3,3];case 2:u=i.sent();e.emit("error",u),e.push(null);return[3,3];case 3:return[2]}})})()}}]);return r}(P),I=function(){var e=a(function(e){return b(this,function(t){return[2,x(e)?new T(e):new E(e)]})});return function t(t){return e.apply(this,arguments)}}();var C=function(){"use strict";function e(t,n){u(this,e);k(this,"signature");k(this,"signatureOffset");this.signature=t,this.signatureOffset=n}o(e,[{key:"compare",value:function e(e){var t=Buffer.from(this.signature,"hex"),n=e.subarray(this.signatureOffset,t.length+this.signatureOffset);return Buffer.compare(t,n)===0}},{key:"decode",value:function e(e,t){var n=this;return a(function(){var r,a;return b(this,function(u){switch(u.label){case 0:u.trys.push([0,2,,3]);return[4,n._decode(e,t)];case 1:r=u.sent();return[2,Promise.resolve(r)];case 2:a=u.sent();return[2,(console.error(a),Promise.reject("Failed to decode buffer"))];case 3:return[2]}})})()}},{key:"format",value:function e(e){var t=this;return a(function(){var n,r;return b(this,function(a){switch(a.label){case 0:a.trys.push([0,2,,3]);return[4,t._format(e)];case 1:n=a.sent();return[2,Promise.resolve(n)];case 2:r=a.sent();return[2,(console.error(r),Promise.reject("Failed to decode buffer"))];case 3:return[2]}})})()}}]);return e}();var j=function(e){return e.toString(2).padStart(8,"0")},L=function(e){return parseInt(e,2)},A=function(e){return parseInt(e.toString("hex"),16)},U=function(e,t,n){return e.subarray(t,t+n).toString("hex").toUpperCase()};var D={18538067:"Segment","1654AE6B":"Tracks",AE:"TrackEntry",D7:"TrackNumber",83:"TrackType","22B59C":"Language","22B59D":"LanguageBCP47","536E":"Name",86:"CodecID"},q=function(e){var t=arguments.length>1&&arguments[1]!==void 0?arguments[1]:0;var n=e[t],r=j(n),a=r.indexOf("1")+1,u=e.subarray(t,t+a),i=r.substring(a,r.length);for(var o=1;o<u.length;o++)i+=j(u[o]);return{value:L(i),length:a}},F=function(e){var t=arguments.length>1&&arguments[1]!==void 0?arguments[1]:0;var n;var r=q(e,t),a=q(e,t+r.length),u=t+r.length+a.length,i=e.subarray(u,u+a.value),o=U(e,t,r.length),f=(n=D[o])!==null&&n!==void 0?n:o,c=r.length+a.length+a.value;return{name:f,size:c,data:i,dataSize:a.value,offset:t}},H=function(e){var t=[];for(var n=0;n<e.length;){var r=F(e,n);t.push(r),n+=r.size}return t};var M=function(e){if(!e)return null;var t=e.data;return A(t)},N=function(e){if(!e)return null;var t=e.data;return $[A(t)]},W=function(e){if(!e)return null;var t=e.data.toString();return t==="und"?null:t},V=function(e){return e?e.data.toString():null},G=function(e){return e?e.data.toString().replace("V_","").replace("A_","").replace("S_",""):null},$={1:"video",2:"audio",17:"text"},J="1A45DFA3",K=0,Q=function(e){"use strict";c(n,e);var t=g(n);function n(){u(this,n);return t.call(this,J,K)}o(n,[{key:"_decode",value:function e(e){return new Promise(function(t,n){var r=H(e).find(function(e){var t=e.name;return t==="Segment"});if(!r)return n();var a=H(r.data).find(function(e){var t=e.name;return t==="Tracks"});if(!a)return n();t(a)})}},{key:"_format",value:function e(e){return new Promise(function(t,n){var r=H(e.data).filter(function(e){var t=e.name;return t==="TrackEntry"});if(!r)return n();var a=r.map(function(e){return H(e.data)}).map(function(e){var t=e.find(function(e){var t=e.name;return t==="TrackNumber"}),n=e.find(function(e){var t=e.name;return t==="TrackType"}),r=e.find(function(e){var t=e.name;return t==="Language"}),a=e.find(function(e){var t=e.name;return t==="LanguageBCP47"}),u=e.find(function(e){var t=e.name;return t==="Name"}),i=e.find(function(e){var t=e.name;return t==="CodecID"});return{trackNumber:t,trackType:n,language:r,languageBCP47:a,name:u,codecID:i}}).map(function(e){var t=e.trackNumber,n=e.trackType,r=e.language,a=e.languageBCP47,u=e.name,i=e.codecID;var o;var f=M(t),c=N(n),s=(o=W(r))!==null&&o!==void 0?o:W(a),l=V(u),d=G(i);return{id:f,type:c,lang:s,label:l,codec:d}});t(a)})}}]);return n}(C),X=Q;var Y=function(e){var t=arguments.length>1&&arguments[1]!==void 0?arguments[1]:0;var n=e[t],r=e.readUInt32BE(t+4),a=e.readUInt32BE(t+8),u=e.readUInt32BE(t+12);return{version:n,creationTime:r,modificationTime:a,id:u,offset:t}},Z=function(e){var t=arguments.length>1&&arguments[1]!==void 0?arguments[1]:0;var n=e[t],r=t+(n===1?32:20),a=e.readUInt16BE(r),u=[];u[0]=a>>10&31,u[1]=a>>5&31,u[2]=a&31;var i=String.fromCharCode(u[0]+96,u[1]+96,u[2]+96);return{version:n,language:i,offset:t}},ee=function(e){var t=arguments.length>1&&arguments[1]!==void 0?arguments[1]:0;var n=e[t],r=t+4+4,a=e.subarray(r,r+4).toString(),u=r+4+12,i=e.subarray(u,e.length-1).toString();return{version:n,handlerType:a,name:i,offset:t}},et=function(e){var t=arguments.length>1&&arguments[1]!==void 0?arguments[1]:0;var n=e[t],r=e.readUInt32BE(t+4),a=[];for(var u=1;u<r+1;u++){var i=e.readUInt32BE(t+8*u),o=e.subarray(t+12*u,t+16*u).toString(),f=e.subarray(t+16*u,t+16*u+i);a.push({size:i,name:o,data:f})}return{version:n,samples:r,entries:a,offset:t}},en=function(e){var t=arguments.length>1&&arguments[1]!==void 0?arguments[1]:0;var n=e.readUInt32BE(t),r=e.subarray(t+4,t+8).toString(),a=t+8,u=n===1?A(e.subarray(a,a+8)):null,i=t+8+(u?8:0),o=u!==null&&u!==void 0?u:n,f=e.subarray(i,n===1?e.length:t+n),c=o-8;return{name:r,size:o,data:f,dataSize:c,offset:t}},er=function(e){var t=[];for(var n=0;n<e.length;){var r=en(e,n);t.push(r),n+=r.size}return t};var ea="66747970",eu=4,ei={vide:"video",soun:"audio",text:"text",sbtl:"text"},eo=["VideoHandler","SoundHandler","SubtitleHandler"],ef=["und","```"],ec=function(e){var t;return(t=ei[e.handlerType])!==null&&t!==void 0?t:null},es=function(e){return ef.includes(e.language)?null:e.language},el=function(e){return eo.includes(e.name)?null:e.name.length?e.name:null},ed=function(e){var t,n;var r;return((r=e===null||e===void 0?void 0:(n=e.entries)===null||n===void 0?void 0:(t=n[0])===null||t===void 0?void 0:t.name)!==null&&r!==void 0?r:null).replace("-","").toUpperCase()},ev=function(e){"use strict";c(r,e);var t=g(r);function r(){u(this,r);var e;e=t.call(this,ea,eu);k(n(e),"offset",0);return e}o(r,[{key:"_decode",value:function e(e,t){var n=this;return new Promise(function(r){var a=er(e),u=a.find(function(e){var t=e.name;return t==="moov"}),i=a.slice().reverse().find(function(e){var t=e.name;return t!=="moov"});if(console.log(a),!u&&i)return n.offset+=i.offset+i.size,t(n.offset);if(u&&u.data.length!==u.dataSize)return n.offset+=u.offset,t(n.offset,u.size);if(u&&u.data.length===u.dataSize)return r(u)})}},{key:"_format",value:function e(e){return new Promise(function(t){var n=er(e.data).filter(function(e){var t=e.name;return t==="trak"}).map(function(e){var t=er(e.data),n=t.find(function(e){var t=e.name;return t==="tkhd"}),r=t.find(function(e){var t=e.name;return t==="mdia"});if(!n||!r)return null;var a=Y(n.data),u=er(r.data),i=u.find(function(e){var t=e.name;return t==="mdhd"}),o=u.find(function(e){var t=e.name;return t==="hdlr"}),f=u.find(function(e){var t=e.name;return t==="minf"});if(!i||!o||!f)return null;var c=Z(i.data),s=ee(o.data),l=er(f.data).find(function(e){var t=e.name;return t==="stbl"});if(!l)return null;var d=er(l.data).find(function(e){var t=e.name;return t==="stsd"});if(!d)return null;var v;var h=et(d.data),p=(v=a.id)!==null&&v!==void 0?v:null,y=ec(s),m=es(c),g=el(s),b=ed(h);return{id:p,type:y,lang:m,label:g,codec:b}}).filter(function(e){return e!==null});t(n)})}}]);return r}(C),eh=ev;var ep=15*1024*1024,ey=function(e,t){return e.find(function(e){return e.compare(t)})},em=function(){var e=a(function(e,t){var n,r,u;return b(this,function(i){switch(i.label){case 0:return[4,I(e)];case 1:n=i.sent(),r=[new X,new eh];return[2,new Promise(function(e,i){var o=function(e,t){n.pause(),n.bytesOffset=e,n.chunkSize=t!==null&&t!==void 0?t:ep,n.resume()},f=function(t){n.destroy(),u&&u.format(t).then(e).catch(c)},c=function(e){n.destroy(),i(e)},s=function(){var e=a(function(e){return b(this,function(a){if((t===null||t===void 0?void 0:t.maxBytesLimit)&&n.bytesRead>=t.maxBytesLimit)return[2,c("Reached maxBytesLimit of ".concat(t.maxBytesLimit))];if(u=u!==null&&u!==void 0?u:ey(r,e),!u)return[2,c("This file type is not supported")];u.decode(e,o).then(f).catch(c);return[2]})});return function t(t){return e.apply(this,arguments)}}();n.on("error",c).on("data",s)})]}})});return function t(t,n){return e.apply(this,arguments)}}(),eg=em;export{eg as default};