• Stars
    star
    479
  • Rank 88,299 (Top 2 %)
  • Language
    Go
  • License
    MIT License
  • Created almost 7 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

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

More Repositories

1

go-astilectron

Build cross platform GUI apps with GO and HTML/JS/CSS (powered by Electron)
Go
4,745
star
2

go-astisub

Manipulate subtitles in GO (.srt, .ssa/.ass, .stl, .ttml, .vtt (webvtt), teletext, etc.)
Go
463
star
3

go-astilectron-demo

Discover the power of Astilectron through a demo app
Go
397
star
4

go-astiencoder

Go
308
star
5

astilectron

Electron app that provides an API over a TCP socket that allows executing Electron's method as well as capturing Electron's events
JavaScript
274
star
6

go-astibob

Golang framework to build an AI that can understand and speak back to you, and everything else you want
Go
239
star
7

go-astideepspeech

Golang bindings for Mozilla's DeepSpeech speech-to-text library
Go
170
star
8

go-astilectron-bundler

Bundle your Astilectron app with ease
Go
125
star
9

go-astiav

Better C bindings for ffmpeg in GO
Go
86
star
10

go-astitodo

Parse TODOs in your GO code
Go
60
star
11

go-astilectron-bootstrap

Create a one-window application using Astilectron
Go
60
star
12

go-asticoqui

Golang bindings for Coqui's speech-to-text library
Go
29
star
13

go-astikit

Set of golang helpers that don't require any external dependencies
Go
29
star
14

go-astivid

Set of video tools available through a nice UI
Go
27
star
15

go-astisrt

SRT server, client and socket in GO
Go
26
star
16

go-astitools

Set of augmented functions for the GO programming language (http://golang.org)
Go
17
star
17

go-astitello

Golang implementation of DJI Tello SDK
Go
10
star
18

go-texttospeech

Text to speech manager relying on the OS speech recognition software
Go
10
star
19

go-astiffprobe

Use your FFProbe binary to gather quality information about your video files
Go
10
star
20

go-astisplash

Cross platform splash screen
Go
9
star
21

go-astichat

A lightweight encrypted chat written in GO
Go
8
star
22

go-astilog

Golang logger
Go
8
star
23

js-toolbox

Set of components and methods to ease HTML/CSS/JS developments
JavaScript
6
star
24

go-astiws

Wrapper on top of websockets
Go
5
star
25

go-bindata

Hard fork from https://github.com/jteeuwen/go-bindata after it has disappeared
Go
5
star
26

go-stopwatch

Deprecated
Go
5
star
27

go-astitesseract

Wrapper for the Tesseract OCR project
Go
5
star
28

go-asticrypt

Send encrypted messages through your favourite apps
Go
4
star
29

go-astiffmpeg

Use your FFMpeg binary to manipulate your video files
Go
4
star
30

go-astiocr

Go
4
star
31

go-keyboardemulator

Cross-OS keyboard emulator that can take control of your keyboard through code
Go
4
star
32

go-slack

TODO: clone and rename
Go
2
star
33

go-astimysql

Wrapper on top of mysql to provide proper configuration
Go
2
star
34

go-astiratp

Clients for the RATP APIs
Go
2
star
35

go-bob

Bob is an AI capable of taking over your keyboard based on voice commands
Go
2
star
36

go-astiamqp

Wrapper on top of amqp to provide proper configuration and error handling
Go
2
star
37

php-deployment-manager

Deployment manager to enable automatic deployment of PHP or GO projects on your server after a GIT push
PHP
2
star
38

go-astiproxy

Wrapper on top of http and url to provide proper proxy configuration
Go
2
star
39

go-astichartjs

Go
2
star
40

asticrypt

Encrypt your conversations and stop being tracked when using your favorite apps
JavaScript
2
star
41

go-astislack

Clear your Slack history easily
Go
1
star
42

go-astimgo

Wrapper on top of mgo to provide proper configuration
Go
1
star
43

go-ftp

TODO: clone and rename
Go
1
star
44

go-speechtotext

Speech to text manager relying on the OS speech recognition software
1
star
45

go-astiredis

Wrapper on top of redis to provide proper configuration
Go
1
star
46

go-test

Sandbox for GO
Go
1
star
47

go-astiudp

Go
1
star
48

go-astilectron-deployer

Go
1
star
49

go-astitwitter

Wrapper on top of Twitter API
Go
1
star
50

go-astibank

Simple tool to monitor your bank accounts
Go
1
star
51

test

1
star
52

php-cache-manager

Cache manager for PHP
PHP
1
star
53

go-gozzle

Deprecated
Go
1
star
54

go-astibike

Should I travel by bike this week?
Go
1
star
55

go-pprof

TODO: merge stopwatch + add ticker
Go
1
star
56

php-data-mapper

Mapper and Repository factories that implements the Data Mapper structure
PHP
1
star
57

go-astipatch

Patch manager written in GO
Go
1
star
58

go-astibob-demos

Official astibob demos
Go
1
star
59

python-asticredits

1
star
60

js-astiyoga

JavaScript
1
star
61

php-file-manager

File manager to handle cross-datasources copy as well as simple file actions on the most common datasources
PHP
1
star