go-astits

Parse and demux MPEG Transport Streams (.ts) natively in GO

Github星跟踪图

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. 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 packets

$ astits packets -i <path to your file>

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

主要指标

概览
名称与所有者asticode/go-astits
主编程语言Go
编程语言Go (语言数: 1)
平台
许可证MIT License
所有者活动
创建于2017-07-04 13:06:15
推送于2024-12-22 09:45:44
最后一次提交2024-12-22 01:45:44
发布数16
最新版本名称v1.13.0 (发布于 )
第一版名称v1.0.0 (发布于 )
用户参与
星数571
关注者数23
派生数54
提交数94
已启用问题?
问题数28
打开的问题数4
拉请求数23
打开的拉请求数4
关闭的拉请求数9
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?