Parse and demux MPEG Transport Streams (.ts) natively in GO
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
astits Updated main Feb 11, 2018
doc Fixed iso639 descriptor Jan 5, 2019
.gitignore Initial commit Nov 25, 2017
.travis.yml Initial commit Nov 25, 2017
LICENSE Initial commit Nov 25, 2017
README.md Updated readme Feb 25, 2018
clock_reference.go Initial commit Nov 25, 2017
clock_reference_test.go Initial commit Nov 25, 2017
data.go Added first packet to data Feb 18, 2018
data_eit.go Added local time offset descriptor Nov 25, 2017
data_eit_test.go Added local time offset descriptor Nov 25, 2017
data_nit.go Initial commit Nov 25, 2017
data_nit_test.go Initial commit Nov 25, 2017
data_pat.go Initial commit Nov 25, 2017
data_pat_test.go Initial commit Nov 25, 2017
data_pes.go Not processing incomplete PES data anymore Feb 18, 2018
data_pes_test.go Not processing incomplete PES data anymore Feb 18, 2018
data_pmt.go Initial commit Nov 25, 2017
data_pmt_test.go Initial commit Nov 25, 2017
data_psi.go Added first packet to data Feb 18, 2018
data_psi_test.go Added first packet to data Feb 18, 2018
data_sdt.go Initial commit Nov 25, 2017
data_sdt_test.go Initial commit Nov 25, 2017
data_test.go Added first packet to data Feb 18, 2018
data_tot.go Initial commit Nov 25, 2017
data_tot_test.go Initial commit Nov 25, 2017
demuxer.go Removed packet pool dump Feb 11, 2018
demuxer_test.go Added first packet to data Feb 18, 2018
descriptor.go Fixed iso639 descriptor Jan 5, 2019
descriptor_test.go Added descriptor 0x5 Feb 17, 2018
dvb.go Added local time offset descriptor Nov 25, 2017
dvb_test.go Added local time offset descriptor Nov 25, 2017
packet.go Initial commit Nov 25, 2017
packet_buffer.go Moved things around Dec 3, 2017
packet_buffer_test.go Moved things around Dec 3, 2017
packet_pool.go Remove useless log Feb 11, 2018
packet_pool_test.go Updated packet pool add strategy regarding errors Dec 16, 2017
packet_test.go Updated packet pool add strategy regarding errors Dec 16, 2017
program_map.go Initial commit Nov 25, 2017
program_map_test.go Initial commit Nov 25, 2017

README.md

GoReportCard GoDoc Travis Coveralls

This is a Golang library to natively parse and demux MPEG Transport Streams (ts) in GO.

WARNING: this library is not yet production ready. For instance, while parsing a slice of bytes, it doesn't check whether the length of the slice is sufficient and rather panic on purpose. Use at your own risks!

Installation

To install the library use the following:

go get -u github.com/asticode/go-astits/...

Before looking at the code...

The transport stream is made of packets.
Each packet has a header, an optional adaptation field and a payload.
Several payloads can be appended and parsed as a data.

                                           TRANSPORT STREAM
 +--------------------------------------------------------------------------------------------------+
 |                                                                                                  |
 
                       PACKET                                         PACKET
 +----------------------------------------------+----------------------------------------------+----
 |                                              |                                              |
 
 +--------+---------------------------+---------+--------+---------------------------+---------+
 | HEADER | OPTIONAL ADAPTATION FIELD | PAYLOAD | HEADER | OPTIONAL ADAPTATION FIELD | PAYLOAD | ...
 +--------+---------------------------+---------+--------+---------------------------+---------+
 
                                      |         |                                    |         |
                                      +---------+                                    +---------+
                                           |                                              |
                                           +----------------------------------------------+
                                                                DATA

Using the library in your code

WARNING: the code below doesn't handle errors for readability purposes. However you SHOULD!

// Create a cancellable context in case you want to stop reading packets/data any time you want
ctx, cancel := context.WithCancel(context.Background())

// Handle SIGTERM signal
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGTERM)
go func() {
    <-ch
    cancel()
}()

// Open your file or initialize any kind of io.Reader
f, _ := os.Open("/path/to/file.ts")
defer f.Close()

// Create the demuxer
dmx := astits.New(ctx, f)
for {
    // Get the next data
    d, _ := dmx.NextData()
    
    // Data is a PMT data
    if d.PMT != nil {
        // Loop through elementary streams
        for _, es := range d.PMT.ElementaryStreams {
                fmt.Printf("Stream detected: %d\n", es.ElementaryPID)
        }
        return
    }
}

Options

In order to pass options to the demuxer, look for the methods prefixed with Opt and add them upon calling New:

// This is your custom packets parser
p := func(ps []*astits.Packet) (ds []*astits.Data, skip bool, err error) {
        // This is your logic
        skip = true
        return
}

// Now you can create a demuxer with the proper options
dmx := New(ctx, f, OptPacketSize(192), OptPacketsParser(p))

CLI

This library provides a CLI that will automatically get installed in GOPATH/bin on go get execution.

List streams

$ astits -i <path to your file> -f <format: text|json (default: text)>

List data

$ astits data -i <path to your file> -d <data type: eit|nit|... (repeatable argument | if empty, all data types are shown)>

Features and roadmap

  • Parse PES packets
  • Parse PAT packets
  • Parse PMT packets
  • Parse EIT packets
  • Parse NIT packets
  • Parse SDT packets
  • Parse TOT packets
  • Parse BAT packets
  • Parse DIT packets
  • Parse RST packets
  • Parse SIT packets
  • Parse ST packets
  • Parse TDT packets
  • Parse TSDT packets