Skip to content

Implement MPEG-TS demuxer with PAT/PMT/PES parsing#1

Draft
Copilot wants to merge 3 commits intomainfrom
copilot/add-vlc-style-ts-demuxer
Draft

Implement MPEG-TS demuxer with PAT/PMT/PES parsing#1
Copilot wants to merge 3 commits intomainfrom
copilot/add-vlc-style-ts-demuxer

Conversation

Copy link

Copilot AI commented Nov 1, 2025

Implements a complete MPEG Transport Stream demuxer following ISO/IEC 13818-1 specification, similar to VLC's approach.

Core Components

  • Packet parser - 188-byte TS packets with sync detection (0x47), adaptation fields, PCR/OPCR extraction
  • PSI parsers - PAT/PMT table parsing for program discovery and stream mapping
  • PES parser - Elementary stream packet parsing with PTS/DTS timestamp extraction (33-bit)
  • Demuxer - Event-driven API with automatic sync recovery and stream type identification

Usage

import { TSDemuxer } from 'tsdemuxer';
import * as fs from 'fs';

const demuxer = new TSDemuxer();

demuxer.on('pmt', (pmt) => {
  console.log(`Program ${pmt.programNumber}, PCR PID: 0x${pmt.pcrPid.toString(16)}`);
  pmt.streams.forEach(s => console.log(`  PID 0x${s.pid.toString(16)}: type 0x${s.streamType.toString(16)}`));
});

demuxer.on('pes', ({ pid, pes }) => {
  if (pes.pts) console.log(`PTS: ${pes.pts}`);
});

fs.createReadStream('video.ts').on('data', chunk => demuxer.push(chunk));

Stream Types Supported

H.264/AVC (0x1B), H.265/HEVC (0x24), MPEG-2 Video (0x02), AAC (0x0F), AC-3 (0x81), MPEG-1/2 Audio (0x03/0x04)

Implementation Details

  • Handles sync loss with pattern verification (requires two consecutive sync bytes)
  • Buffers incomplete PES packets across TS packets
  • BigInt arithmetic for 33-bit PTS/DTS and 42-bit PCR values
  • TypeScript with strict typing throughout
Original prompt

vlc style .ts format demuxer


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits November 1, 2025 02:00
Co-authored-by: mewza <4809989+mewza@users.noreply.github.com>
Co-authored-by: mewza <4809989+mewza@users.noreply.github.com>
Copilot AI changed the title [WIP] Add VLC style .ts format demuxer Implement MPEG-TS demuxer with PAT/PMT/PES parsing Nov 1, 2025
Copilot AI requested a review from mewza November 1, 2025 02:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants