Skip to main content

A Fast SCTE 35 Decoder for Mpeg-TS Video, and Base64 or Hex Encoded Messages.

Project description

threefive

SCTE35 Decoder

  • All 2019 SCTE-35 Splice Commands and Splice Descriptors are Fully Supported.

Heads Up! Changes as of 9/24/2020


Changes

  • Splice class has been renamed Cue. See Cue
  • Stream, StreamPlus, and StreamProxy classes have been consolidated. See Stream

Fast Start

Dependencies

  • Python 3.6+ or pypy3
  • bitn

Install

  • git
git clone https://github.com/futzu/SCTE35-threefive.git

cd SCTE-threefive

# you need root to install for the system

make install

# for pypy3 

make pypy3
  • pip3
pip3 install threefive
  • pip3 and pypy3
# If you don't have pip installed, try this.

pypy3 -mensurepip install pip 


pypy3 -mpip install threefive

🡡 top


Easy threefive

The decode Function

  • source decode.py
  • threefive.decode is an all purpose function to decode SCTE 35 messages from a file or string.
import threefive
  • MpegTS
threefive.decode('/path/to/mpegwithscte35.ts') 
  • Binary
threefive.decode('/mnt/build/file.bin')
  • Base64
mesg='/DBUAAAAAAAA///wBQb+AAAAAAA+AjxDVUVJAAAACn+/Dy11cm46dXVpZDphYTg1YmJiNi01YzQzLTRiNmEtYmViYi1lZTNiMTNlYjc5OTkRAAB2c6LA'
threefive.decode(mesg)
  • Hex
hexed='0xFC302F000000000000FFFFF014054800008F7FEFFE7369C02EFE0052CCF500000000000A0008435545490000013562DBA30A'
threefive.decode(hexed)

🡡 top

Advanced threefive


Cue Class

  • source cue.py
  • The threefive.Cue class decodes a SCTE35 binary, base64, or hex encoded string.
  • threefive.Cue provides several methods to access the parsed data.
from threefive import Cue

b64 = "/DBIAAAAAAAA///wBQb+ek2ItgAyAhdDVUVJSAAAGH+fCAgAAAAALMvDRBEAAAIXQ1VFSUgAABl/nwgIAAAAACyk26AQAACZcuND"

scte35 = Cue(B64)

Print SCTE-35 as JSON

scte35.show()

# Splice Info Section
scte35.show_info_section()

# Splice Command
scte35.show_command()

# Splice Descriptors
scte35.show_descriptors()

Return SCTE 35 as dict

scte35.get()

# Splice Info Section
scte35.get_info_section()

# Splice Command
scte35.get_command()

# Splice Descriptors
scte35.get_descriptors()

🡡 top


Stream Class

  • source stream.py

  • The threefive.Stream class parses SCTE35 messages from a file or stream.

  threefive.Stream(tsdata, show_null = False)
  • tsdata is an open file handle or sys.stdin.buffer to read 'piped' in data.
  • show_null if set to True, enables showing SCTE 35 null commands.

Stream.decode()

  • Calls Cue.show() when a SCTE-35 message is found

Parse a Local File with a Stream Instance

import sys
from threefive import Stream
'''

if __name__ =='__main__':
   with open(sys.argv[1],'rb') as tsdata:
       Stream(tsdata).decode()

Pipe a Video to Stream

curl -s https://futzu.com/xaa.ts -o -  \
  | python3 -c 'import sys;import threefive; threefive.Stream(sys.stdin.buffer).decode()' 

Stream.decode_until_found()

  • Use the Stream.decode_until_found() method instead of Stream.decode().
  • Returns Cue instances when SCTE-35 packets are found.
  • Allows for customized SCTE-35 message handling.
import sys
from threefive import Stream

def do():
   with open(sys.argv[1],'rb') as tsdata:
         while True:
            cuep = Stream(tsdata).decode_until_found() 
            if not cuep :
                sys.exit()
            else:
            # Customized output
               print('pid :',cuep.header.pid, 'command:',
                     cuep.command.name,'@',cuep.command.pts_time,
                     'Out of Network:',
                     cuep.command.out_of_network_indicator)

if __name__ == '__main__':
    do()
  • Output
pid : 1055 command: Splice Insert @ 21951.133267 Out of Network: True
pid : 1015 command: Splice Insert @ 22516.907656 Out of Network: True
pid : 1055 command: Splice Insert @ 22026.133267 Out of Network: False
pid : 1045 command: Splice Insert @ 22864.350067 Out of Network: True
pid : 1015 command: Splice Insert @ 22696.907656 Out of Network: False
pid : 1045 command: Splice Insert @ 22960.350067 Out of Network: False
pid : 1015 command: Splice Insert @ 23516.827656 Out of Network: True
pid : 1015 command: Splice Insert @ 23696.827656 Out of Network: False

Stream.proxy(func = None)

  • Writes all packets to sys.stdout.
  • Writes scte35 data to sys.stderr.
  • The optional func arg allows a function to be used for custom handling of the SCTE-35 cue instance.
  • If func is not set, threefive.Cue.show() is called.
   import threefive

   # Name this proxy.py

   with open('vid.ts','rb') as tsdata:
      sp = threefive.Stream(tsdata)
      sp.proxy()
  • the function should match the interface
    func(cuep)
  • cuep is an instance of threefive.Cue
import sys
import threefive
import json

def display(cuep):
   print(f'\033[92m{json.dumps(vars(cuep.header))}\033[00m', file=sys.stderr,end='\r')
   print(f'\033[92m{json.dumps(cuep.get_command(),indent=2)}\033[00m', file=sys.stderr)

def do():

   with open(sys.argv[1],'rb') as tsdata:
            sp = threefive.Stream(tsdata)
            cue = sp.proxy(func = display) 
            if not cue :
                sys.exit()

if __name__ == '__main__':
    do()
  • Pipe to mplayer
python3 proxy.py | mplayer -

🡡 top

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

threefive-2.1.89.tar.gz (14.8 kB view hashes)

Uploaded Source

Built Distribution

threefive-2.1.89-py3-none-any.whl (13.7 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page