This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
A collection of Go CLI tools for analyzing and manipulating MPEG-2 Transport Stream (TS) files. Each tool lives under cmd/mp2ts-<name>/ and shares core logic from the internal/ package.
# Build all tools (outputs to out/)
make build
# Run all tests
go test ./...
# Update golden files after intentional output changes
go test ./internal/... -update
# Lint (requires golangci-lint)
golangci-lint run
# Tidy dependencies
go mod tidyBuild injects version info via -ldflags -X — the Makefile sets internal.commitVersion and internal.commitDate at link time from git tags/timestamps.
There is no pkg/ directory. All shared code lives in internal/.
Key types:
Options(utils.go) — Configuration struct used by all tools. Each tool'sparseOptions()sets tool-specific defaults and wires upflagparsing.JsonPrinter(printer.go) — Conditional JSON output writer. Methods likePrint(data, show bool)only emit whenshowis true.AvcPS/HevcPS(avc.go,hevc.go) — Parameter set state machines that track SPS/PPS/VPS, detect changes via hex comparison, and avoid redundant output.NaluFrameData(nalu.go) — Per-frame output: PID, PTS/DTS, frame type (I/P/B), NAL unit details.StreamStatistics(statistics.go) — Frame rate and GoP duration calculated from timestamp steps.
Main parse functions in parser.go:
ParseAll()— Full analysis: stream info, parameter sets, NAL units, SEI, statisticsParseInfo()— Quick PMT/stream metadata only (used by mp2ts-info)ParseSCTE35()— Ad insertion splice commands (uses gots, not astits)FilterPids()— Drop specified PIDs, rewrite PAT/PMT
Other entry points:
ExtractES()(extract.go) — Elementary stream extraction to Annex B format
Two TS libraries used for different purposes:
go-astits— High-level demuxing (PAT/PMT/PES extraction). Used by most tools.gots/v2— Low-level packet manipulation. Used by SCTE-35 parsing and PID filtering.
Timestamp handling (const.go):
- 90kHz timescale (
TimeScale = 90000), 33-bit PTS wrap (PtsWrap = 1 << 33) SignedPTSDiff()/UnsignedPTSDiff()handle wraparound
Every tool follows the same 3-function pattern in main.go:
parseOptions()— Returnsinternal.Optionswith tool-specific flag defaults- A parse function — Calls the appropriate
internal.Parse*()function main()— Callsinternal.ParseParams(parseOptions)theninternal.Execute(os.Stdout, o, inFile, parseFn). Execute handles context/SIGINT, file open/close, and error reporting.
Tools: mp2ts-info, mp2ts-nallister, mp2ts-pslister, mp2ts-extract, mp2ts-timeshift, mp2ts-pidfilter, mp2ts-prepare.
Golden file tests in internal/parser_test.go. Test cases run various Options configurations against .ts files in internal/testdata/ and compare output to internal/testdata/golden_*.txt.
- Run
go test ./internal/... -updateto regenerate golden files after intentional output changes - Golden files normalize
\r\nto\nfor cross-platform compatibility
- Commit messages follow Conventional Commits (e.g.,
feat:,fix:,docs:,chore:) - Manual CHANGELOG.md tracks releases