GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - asticode/go-astits: Demux and mux MPEG Transport Streams (.ts) natively in GO

BranchesTags
Open more actions menu

Latest commit

 

History

106 Commits
106 Commits

Folders and files

NameName
Last commit message
Last commit date
.devcontainer
.devcontainer
 
 
.github/workflows
.github/workflows
 
 
cmd
cmd
 
 
doc
doc
 
 
internal/cmd/crc32_table
internal/cmd/crc32_table
 
 
testdata
testdata
 
 
.gitignore
.gitignore
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
clock_reference.go
clock_reference.go
 
 
clock_reference_test.go
clock_reference_test.go
 
 
crc32.go
crc32.go
 
 
crc32_table.go
crc32_table.go
 
 
crc32_test.go
crc32_test.go
 
 
data.go
data.go
 
 
data_eit.go
data_eit.go
 
 
data_eit_test.go
data_eit_test.go
 
 
data_nit.go
data_nit.go
 
 
data_nit_test.go
data_nit_test.go
 
 
data_pat.go
data_pat.go
 
 
data_pat_test.go
data_pat_test.go
 
 
data_pes.go
data_pes.go
 
 
data_pes_test.go
data_pes_test.go
 
 
data_pmt.go
data_pmt.go
 
 
data_pmt_test.go
data_pmt_test.go
 
 
data_psi.go
data_psi.go
 
 
data_psi_test.go
data_psi_test.go
 
 
data_sdt.go
data_sdt.go
 
 
data_sdt_test.go
data_sdt_test.go
 
 
data_test.go
data_test.go
 
 
data_tot.go
data_tot.go
 
 
data_tot_test.go
data_tot_test.go
 
 
demuxer.go
demuxer.go
 
 
demuxer_test.go
demuxer_test.go
 
 
descriptor.go
descriptor.go
 
 
descriptor_test.go
descriptor_test.go
 
 
dvb.go
dvb.go
 
 
dvb_test.go
dvb_test.go
 
 
go.mod
go.mod
 
 
go.sum
go.sum
 
 
muxer.go
muxer.go
 
 
muxer_test.go
muxer_test.go
 
 
packet.go
packet.go
 
 
packet_buffer.go
packet_buffer.go
 
 
packet_buffer_test.go
packet_buffer_test.go
 
 
packet_pool.go
packet_pool.go
 
 
packet_pool_test.go
packet_pool_test.go
 
 
packet_test.go
packet_test.go
 
 
pools.go
pools.go
 
 
program_map.go
program_map.go
 
 
program_map_test.go
program_map_test.go
 
 
roundtrip_test.go
roundtrip_test.go
 
 
stream_map.go
stream_map.go
 
 
stream_map_test.go
stream_map_test.go
 
 
wrapping_counter.go
wrapping_counter.go
 
 

Repository files navigation

GoReportCard GoDoc Test Coveralls

This is a Golang library to natively demux and mux 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/...

To install the executables use the following:

go install github.com/asticode/go-astits/cmd

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!

Demux

// 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
// Buffering using bufio.Reader is recommended for performance
f, _ := os.Open("/path/to/file.ts")
defer f.Close()

// Create the demuxer
dmx := astits.NewDemuxer(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
    }
}

Mux

// Create a cancellable context in case you want to stop writing 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()
}()

// Create your file or initialize any kind of io.Writer
// Buffering using bufio.Writer is recommended for performance
f, _ := os.Create("/path/to/file.ts")
defer f.Close()

// Create the muxer
mx := astits.NewMuxer(ctx, f)

// Add an elementary stream
mx.AddElementaryStream(astits.PMTElementaryStream{
    ElementaryPID: 1,
    StreamType:    astits.StreamTypeMetadata,
})

// Write tables
// Using that function is not mandatory, WriteData will retransmit tables from time to time 
mx.WriteTables()

// Write data
mx.WriteData(&astits.MuxerData{
    PES: &astits.PESData{
        Data: []byte("test"),
    },
    PID: 1,
})

Options

In order to pass options to the demuxer or the muxer, look for the methods prefixed with DemuxerOpt or MuxerOpt and add them upon calling NewDemuxer or NewMuxer :

// 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 := NewDemuxer(ctx, f, DemuxerOptPacketSize(192), DemuxerOptPacketsParser(p))

CLI

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

astits-probe

List streams

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

List packets

$ astits-probe packets -i <path to your file>

List data

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

astits-es-split

Split streams into separate .ts files

$ astits-es-split <path to your file> -o <path to output dir>

Features and roadmap

  • Add demuxer
  • Add muxer
  • Demux PES packets
  • Mux PES packets
  • Demux PAT packets
  • Mux PAT packets
  • Demux PMT packets
  • Mux PMT packets
  • Demux EIT packets
  • Mux EIT packets
  • Demux NIT packets
  • Mux NIT packets
  • Demux SDT packets
  • Mux SDT packets
  • Demux TOT packets
  • Mux TOT packets
  • Demux BAT packets
  • Mux BAT packets
  • Demux DIT packets
  • Mux DIT packets
  • Demux RST packets
  • Mux RST packets
  • Demux SIT packets
  • Mux SIT packets
  • Mux ST packets
  • Demux TDT packets
  • Mux TDT packets
  • Demux TSDT packets
  • Mux TSDT packets

About

Demux and mux MPEG Transport Streams (.ts) natively in GO

Topics

gogolanggolang-librarympegmpeg-tsmpeg2mpegtstransport-stream

Resources

Readme
MIT license
Activity

Stars

618 stars

Watchers

20 watching

Forks

73 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.