@ts-videos/ts 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -134,7 +134,7 @@ Made with 💙
134
134
  [releases]: https://github.com/stacksjs/stacks/releases
135
135
  [contributing-guide]: https://github.com/stacksjs/contributing
136
136
  [discussions-on-github]: https://github.com/stacksjs/stacks/discussions
137
- [join-the-stacks-discord-server]: https://discord.gg/stacksjs
137
+ [join-the-stacks-discord-server]: https://stacksjs.com/discord
138
138
  [jetbrains]: https://www.jetbrains.com/
139
139
  [the-solana-foundation]: https://solana.com/
140
140
  [chris-breuer]: https://github.com/chrisbbreuer
@@ -0,0 +1,89 @@
1
+ import { InputFormat, OutputFormat, Demuxer, Muxer } from 'ts-videos';
2
+ import type { Source, Target, EncodedPacket } from 'ts-videos';
3
+ // Export types
4
+ export type { TsPacket, AdaptationField, ProgramAssociationTable, ProgramMapTable, PmtStream, ElementaryStream, TsElementaryPacket };
5
+ export declare const MPEGTS: TsInputFormat;
6
+ export declare const MPEGTS_OUTPUT: TsOutputFormat;
7
+ declare interface TsPacket {
8
+ syncByte: number
9
+ transportErrorIndicator: boolean
10
+ payloadUnitStartIndicator: boolean
11
+ transportPriority: boolean
12
+ pid: number
13
+ scramblingControl: number
14
+ adaptationFieldControl: number
15
+ continuityCounter: number
16
+ adaptationField?: AdaptationField
17
+ payload?: Uint8Array
18
+ }
19
+ declare interface AdaptationField {
20
+ length: number
21
+ discontinuityIndicator: boolean
22
+ randomAccessIndicator: boolean
23
+ elementaryStreamPriorityIndicator: boolean
24
+ pcrFlag: boolean
25
+ opcrFlag: boolean
26
+ splicingPointFlag: boolean
27
+ transportPrivateDataFlag: boolean
28
+ adaptationFieldExtensionFlag: boolean
29
+ pcr?: bigint
30
+ opcr?: bigint
31
+ }
32
+ declare interface ProgramAssociationTable {
33
+ transportStreamId: number
34
+ programs: Map<number, number>
35
+ }
36
+ declare interface ProgramMapTable {
37
+ programNumber: number
38
+ pcrPid: number
39
+ streams: PmtStream[]
40
+ }
41
+ declare interface PmtStream {
42
+ streamType: number
43
+ elementaryPid: number
44
+ esInfo: Uint8Array
45
+ }
46
+ declare interface ElementaryStream {
47
+ pid: number
48
+ streamType: number
49
+ track: Track | null
50
+ packets: TsElementaryPacket[]
51
+ pesBuffer: Uint8Array[]
52
+ pesLength: number
53
+ }
54
+ declare interface TsElementaryPacket {
55
+ data: Uint8Array
56
+ pts?: number
57
+ dts?: number
58
+ isKeyframe: boolean
59
+ }
60
+ export declare class TsDemuxer extends Demuxer {
61
+ get formatName(): string;
62
+ get mimeType(): string;
63
+ init(): Promise<void>;
64
+ readPacket(trackId: number): Promise<EncodedPacket | null>;
65
+ seek(timeInSeconds: number): Promise<void>;
66
+ }
67
+ export declare class TsMuxer extends Muxer {
68
+ get formatName(): string;
69
+ get mimeType(): string;
70
+ protected onTrackAdded(track: { type: string, config: unknown }): void;
71
+ protected writeHeader(): Promise<void>;
72
+ protected writeVideoPacket(track: unknown, packet: EncodedPacket): Promise<void>;
73
+ protected writeAudioPacket(track: unknown, packet: EncodedPacket): Promise<void>;
74
+ protected writeSubtitlePacket(): Promise<void>;
75
+ protected writeTrailer(): Promise<void>;
76
+ }
77
+ export declare class TsInputFormat extends InputFormat {
78
+ get name(): string;
79
+ get mimeType(): string;
80
+ get extensions(): string[];
81
+ canRead(source: Source): Promise<boolean>;
82
+ createDemuxer(source: Source): Demuxer;
83
+ }
84
+ export declare class TsOutputFormat extends OutputFormat {
85
+ get name(): string;
86
+ get mimeType(): string;
87
+ get extension(): string;
88
+ createMuxer(target: Target): Muxer;
89
+ }
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ import{InputFormat as C,OutputFormat as U,Demuxer as B,Muxer as D,Reader as L}from"ts-videos";var p=188,m=71,b=0;var h=1,g=2,T=3,k=4;var y=15,f=27,P=36,E=129,A=130,w=131;function R(t,r){if(r+p>t.length)return null;let e=t[r];if(e!==m)return null;let s=t[r+1],i=t[r+2],n=t[r+3],o={syncByte:e,transportErrorIndicator:(s&128)!==0,payloadUnitStartIndicator:(s&64)!==0,transportPriority:(s&32)!==0,pid:(s&31)<<8|i,scramblingControl:n>>6&3,adaptationFieldControl:n>>4&3,continuityCounter:n&15},a=r+4;if(o.adaptationFieldControl===2||o.adaptationFieldControl===3){let c=t[a];if(a++,c>0){let u=t[a];o.adaptationField={length:c,discontinuityIndicator:(u&128)!==0,randomAccessIndicator:(u&64)!==0,elementaryStreamPriorityIndicator:(u&32)!==0,pcrFlag:(u&16)!==0,opcrFlag:(u&8)!==0,splicingPointFlag:(u&4)!==0,transportPrivateDataFlag:(u&2)!==0,adaptationFieldExtensionFlag:(u&1)!==0};let l=a+1;if(o.adaptationField.pcrFlag&&l+6<=a+c){let x=BigInt(t[l])<<25n|BigInt(t[l+1])<<17n|BigInt(t[l+2])<<9n|BigInt(t[l+3])<<1n|BigInt(t[l+4])>>7n,d=(t[l+4]&1)<<8|t[l+5];o.adaptationField.pcr=x*300n+BigInt(d),l+=6}}a+=c}if(o.adaptationFieldControl===1||o.adaptationFieldControl===3){let c=r+p;if(a<c)o.payload=t.subarray(a,c)}return o}function N(t){if(t.length<8)return null;let r=t[0]+1;if(t[r]!==0)return null;let s=(t[r+1]&15)<<8|t[r+2],n={transportStreamId:t[r+3]<<8|t[r+4],programs:new Map},o=r+3+s-4;for(let a=r+8;a<o&&a+4<=t.length;a+=4){let c=t[a]<<8|t[a+1],u=(t[a+2]&31)<<8|t[a+3];n.programs.set(c,u)}return n}function K(t){if(t.length<12)return null;let r=t[0]+1;if(t[r]!==2)return null;let s=(t[r+1]&15)<<8|t[r+2],i=t[r+3]<<8|t[r+4],n=(t[r+8]&31)<<8|t[r+9],o=(t[r+10]&15)<<8|t[r+11],a={programNumber:i,pcrPid:n,streams:[]},c=r+12+o,u=r+3+s-4;while(c+5<=u&&c+5<=t.length){let l=t[c],x=(t[c+1]&31)<<8|t[c+2],d=(t[c+3]&15)<<8|t[c+4],M=t.subarray(c+5,c+5+d);a.streams.push({streamType:l,elementaryPid:x,esInfo:M}),c+=5+d}return a}function Y(t){if(t.length<9)return null;if(t[0]!==0||t[1]!==0||t[2]!==1)return null;let r=t[3],e=t[4]<<8|t[5],s=t[8],i,n,o=t[7]>>6&3;if(o>=2&&t.length>=14)i=(t[9]>>1&7)*4294967296+(t[10]<<22|t[11]>>1<<15|t[12]<<7|t[13]>>1),i=i/90000;if(o===3&&t.length>=19)n=(t[14]>>1&7)*4294967296+(t[15]<<22|t[16]>>1<<15|t[17]<<7|t[18]>>1),n=n/90000;let a=9+s,c=t.subarray(a);return{pts:i,dts:n,payload:c}}function O(t){switch(t){case h:return"mpeg1";case g:return"mpeg2";case T:return"mp3";case k:return"mp3";case y:return"aac";case f:return"h264";case P:return"h265";case E:return"ac3";case A:return"dts";case w:return"truehd";default:return"unknown"}}function F(t){return t===h||t===g||t===f||t===P}function V(t){return t===T||t===k||t===y||t===E||t===A||t===w}class S extends B{pat=null;pmts=new Map;streams=new Map;currentPacketIndex=new Map;_initialized=!1;get formatName(){return"mpegts"}get mimeType(){return"video/mp2t"}async init(){if(this._initialized)return;this._initialized=!0,await this.parseStream(),this.buildTracks()}async parseStream(){this.reader.position=0;let t=await this.reader.getSize()??0,r=p*1000,e=new Set;while(this.reader.position<t){let s=await this.reader.readBytes(Math.min(r,t-this.reader.position));if(!s)break;let i=0;while(i<s.length&&s[i]!==m)i++;while(i+p<=s.length){let n=R(s,i);if(!n){i++;continue}if(n.pid===b&&n.payload){let o=N(n.payload);if(o){this.pat=o;for(let[a,c]of o.programs)if(a!==0)e.add(c)}}else if(e.has(n.pid)&&n.payload){let o=K(n.payload);if(o){this.pmts.set(n.pid,o);for(let a of o.streams)if(!this.streams.has(a.elementaryPid))this.streams.set(a.elementaryPid,{pid:a.elementaryPid,streamType:a.streamType,track:null,packets:[],pesBuffer:[],pesLength:0})}}else if(this.streams.has(n.pid)&&n.payload){let o=this.streams.get(n.pid);if(n.payloadUnitStartIndicator){if(o.pesBuffer.length>0)this.flushPesPacket(o)}o.pesBuffer.push(n.payload),o.pesLength+=n.payload.length}i+=p}}for(let s of this.streams.values())if(s.pesBuffer.length>0)this.flushPesPacket(s)}flushPesPacket(t){let r=t.pesBuffer.reduce((n,o)=>n+o.length,0),e=new Uint8Array(r),s=0;for(let n of t.pesBuffer)e.set(n,s),s+=n.length;t.pesBuffer=[],t.pesLength=0;let i=Y(e);if(i&&i.payload.length>0){let n=F(t.streamType)?this.detectKeyframe(i.payload,t.streamType):!0;t.packets.push({data:i.payload,pts:i.pts,dts:i.dts,isKeyframe:n})}}detectKeyframe(t,r){if(r===f){for(let e=0;e<t.length-4;e++){if(t[e]===0&&t[e+1]===0&&t[e+2]===1){if((t[e+3]&31)===5)return!0}if(t[e]===0&&t[e+1]===0&&t[e+2]===0&&t[e+3]===1){if((t[e+4]&31)===5)return!0}}return!1}if(r===P){for(let e=0;e<t.length-5;e++){if(t[e]===0&&t[e+1]===0&&t[e+2]===1){let s=t[e+3]>>1&63;if(s===19||s===20)return!0}if(t[e]===0&&t[e+1]===0&&t[e+2]===0&&t[e+3]===1){let s=t[e+4]>>1&63;if(s===19||s===20)return!0}}return!1}if(r===h||r===g){for(let e=0;e<t.length-5;e++)if(t[e]===0&&t[e+1]===0&&t[e+2]===1&&t[e+3]===0){if((t[e+5]>>3&7)===1)return!0}return!1}return!0}buildTracks(){this._tracks=[];let t=1;for(let e of this.streams.values()){let s=O(e.streamType);if(F(e.streamType)){let i={type:"video",id:t++,index:this._tracks.length,codec:s,width:0,height:0,isDefault:this._tracks.filter((n)=>n.type==="video").length===0};e.track=i,this._tracks.push(i)}else if(V(e.streamType)){let i={type:"audio",id:t++,index:this._tracks.length,codec:s,sampleRate:48000,channels:2,isDefault:this._tracks.filter((n)=>n.type==="audio").length===0};e.track=i,this._tracks.push(i)}this.currentPacketIndex.set(e.pid,0)}let r=0;for(let e of this.streams.values())if(e.packets.length>0){let s=e.packets[e.packets.length-1];if(s.pts!==void 0&&s.pts>r)r=s.pts}this._duration=r,this._metadata={}}async readPacket(t){if(!this._tracks?.find((e)=>e.id===t))return null;for(let e of this.streams.values())if(e.track?.id===t){let s=this.currentPacketIndex.get(e.pid)??0;if(s>=e.packets.length)return null;let i=e.packets[s];return this.currentPacketIndex.set(e.pid,s+1),{data:i.data,timestamp:i.pts??0,duration:void 0,isKeyframe:i.isKeyframe,trackId:t,pts:i.pts,dts:i.dts}}return null}async seek(t){for(let r of this.streams.values())for(let e=0;e<r.packets.length;e++){let s=r.packets[e];if(s.pts!==void 0&&s.pts>=t){for(let i=e;i>=0;i--)if(r.packets[i].isKeyframe){this.currentPacketIndex.set(r.pid,i);break}break}}}}class _ extends D{packets=[];nextPid=256;pcrPid=256;continuityCounters=new Map;programNumber=1;get formatName(){return"mpegts"}get mimeType(){return"video/mp2t"}onTrackAdded(t){let r=this.nextPid++;if(t.type==="video"&&this.pcrPid===256)this.pcrPid=r}async writeHeader(){}async writeVideoPacket(t,r){this.packets.push({pid:256,data:r.data,pts:r.timestamp,dts:r.dts??r.timestamp,isKeyframe:r.isKeyframe})}async writeAudioPacket(t,r){this.packets.push({pid:257,data:r.data,pts:r.timestamp,dts:r.dts??r.timestamp,isKeyframe:!0})}async writeSubtitlePacket(){throw Error("MPEG-TS subtitle support not implemented")}async writeTrailer(){await this.writePat(),await this.writePmt();for(let t of this.packets)await this.writeElementaryPacket(t)}async writePat(){let t=new Uint8Array(p);t[0]=m,t[1]=64,t[2]=0,t[3]=16|this.getNextContinuityCounter(b)&15;let r=new Uint8Array([0,0,176,13,0,1,193,0,0,0,1,240,0]),e=this.calculateCrc32(r.subarray(1));t.set(r,4),t[4+r.length]=e>>24&255,t[4+r.length+1]=e>>16&255,t[4+r.length+2]=e>>8&255,t[4+r.length+3]=e&255,t.fill(255,4+r.length+4),await this.writer.writeBytes(t)}async writePmt(){let t=new Uint8Array(p);t[0]=m,t[1]=80,t[2]=0,t[3]=16|this.getNextContinuityCounter(4096)&15;let r=[];for(let u of this.tracks){let l=u.type==="video"?256:257,x=u.type==="video"?f:y;r.push(new Uint8Array([x,224|l>>8&31,l&255,240,0]))}let e=new Uint8Array(r.reduce((u,l)=>u+l.length,0)),s=0;for(let u of r)e.set(u,s),s+=u.length;let i=13+e.length,n=new Uint8Array([0,2,176|i>>8&15,i&255,0,1,193,0,0,224|this.pcrPid>>8&31,this.pcrPid&255,240,0]);t.set(n,4),t.set(e,4+n.length);let o=5,a=4+n.length+e.length,c=this.calculateCrc32(t.subarray(o,a));t[a]=c>>24&255,t[a+1]=c>>16&255,t[a+2]=c>>8&255,t[a+3]=c&255,t.fill(255,a+4),await this.writer.writeBytes(t)}async writeElementaryPacket(t){let r=this.createPesHeader(t.pts,t.dts),e=new Uint8Array(r.length+t.data.length);e.set(r,0),e.set(t.data,r.length);let s=0,i=!0;while(s<e.length){let n=new Uint8Array(p);n[0]=m,n[1]=(i?64:0)|t.pid>>8&31,n[2]=t.pid&255;let o=Math.min(p-4,e.length-s),a=p-4-o;if(a>0){if(n[3]=48|this.getNextContinuityCounter(t.pid)&15,n[4]=a-1,a>1)n[5]=0,n.fill(255,6,4+a);n.set(e.subarray(s,s+o),4+a)}else n[3]=16|this.getNextContinuityCounter(t.pid)&15,n.set(e.subarray(s,s+o),4);await this.writer.writeBytes(n),s+=o,i=!1}}createPesHeader(t,r){let e=t!==void 0,s=r!==void 0&&r!==t,i=9;if(e)i+=5;if(s)i+=5;let n=new Uint8Array(i);if(n[0]=0,n[1]=0,n[2]=1,n[3]=224,n[4]=0,n[5]=0,n[6]=128,n[7]=(e?128:0)|(s?64:0),n[8]=(e?5:0)+(s?5:0),e){let o=Math.floor(t*90000);n[9]=32|o>>29&14|1,n[10]=o>>22&255,n[11]=o>>14&254|1,n[12]=o>>7&255,n[13]=o<<1&254|1}if(s){let o=Math.floor(r*90000),a=e?14:9;n[a]=16|o>>29&14|1,n[a+1]=o>>22&255,n[a+2]=o>>14&254|1,n[a+3]=o>>7&255,n[a+4]=o<<1&254|1}return n}getNextContinuityCounter(t){let r=this.continuityCounters.get(t)??0,e=r+1&15;return this.continuityCounters.set(t,e),r}calculateCrc32(t){let r=4294967295;for(let e of t)for(let s=0;s<8;s++)if((r>>31^e>>7-s&1)&1)r=r<<1^79764919;else r=r<<1;return r>>>0}}class v extends C{get name(){return"mpegts"}get mimeType(){return"video/mp2t"}get extensions(){return["ts","mts","m2ts","mts"]}async canRead(t){let r=L.fromSource(t);return r.position=0,await r.readU8()===m}createDemuxer(t){return new S(t)}}class I extends U{get name(){return"mpegts"}get mimeType(){return"video/mp2t"}get extension(){return"ts"}createMuxer(t){return new _(t)}}var G=new v,H=new I;export{G as MPEGTS,H as MPEGTS_OUTPUT,S as TsDemuxer,v as TsInputFormat,_ as TsMuxer,I as TsOutputFormat};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ts-videos/ts",
3
3
  "type": "module",
4
- "version": "0.1.0",
4
+ "version": "0.1.2",
5
5
  "description": "MPEG Transport Stream codec package for ts-videos - zero dependency TypeScript implementation.",
6
6
  "author": "Chris Breuer <chris@stacksjs.org>",
7
7
  "license": "MIT",
@@ -37,10 +37,10 @@
37
37
  "typecheck": "bunx tsc --noEmit"
38
38
  },
39
39
  "peerDependencies": {
40
- "ts-videos": "0.1.0"
40
+ "ts-videos": "0.1.2"
41
41
  },
42
42
  "devDependencies": {
43
- "ts-videos": "0.1.0",
44
- "bun-plugin-dtsx": "^0.9.9"
43
+ "ts-videos": "0.1.2",
44
+ "bun-plugin-dtsx": "^0.11.13"
45
45
  }
46
46
  }