Skip to main content

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

Project description

:rocket: threefive

SCTE35 Decoder

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

Heads Up! Changes as of 10/23/2020


Changes

  • As of version 2.1.95, threefive.version returns a string for the current version.
  >>> import threefive
  >>> threefive.version
 '2.1.95'
  • Stream.decode, Stream.decode_pid, and Stream.decode_proxy now all take an optional function as an arg. See Stream
  • Stream.decode_until_found() is now Stream.decode_next()

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
  • pip
pip3 install threefive

# for pypy3

pypy3 -mpip install threefive

#If you don't have pip installed, try this.

pypy3 -mensurepip install pip 

🡡 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"

cue = Cue(B64)
Return cue as dict
cue.get()

# Splice Info Section
cue.get_info_section()

# Splice Command
cue.get_command()

# Splice Descriptors
cue.get_descriptors()
Return cue as JSON
jason = cue.get_json()
Print cue as JSON
cue.show()

🡡 top

Stream Class

  • source stream.py

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

  • init

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

 threefive.Stream(tsdata, show_null = False)
  • Methods
Method Description
Stream.decode(func = show_cue) Prints SCTE-35 cues for SCTE-35 packets. Accepts an optional function, func, as arg.
Stream.decode_next() Returns a Cue instance for a SCTE-35 packet.
Stream.decode_pid(the_pid,func = show_cue) Same as Stream.decode except only packets where pid == the_pid
Stream.decode_proxy(func = show_cue) Same as Stream.decode except raw packets are written to stdout for piping to another program.

Stream.decode(func=show_cue)

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:
       sp = Stream(tsdata)
       sp.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()' 
Pass in custom function
  • func should match the interface
    func(cue)
  • example
import sys
import threefive

def display(cue):
   print(f'\033[92m{cue.packet_data}\033[00m')
   print(f'{cue.command.name}')

def do():
   with open(sys.argv[1],'rb') as tsdata:
    sp = threefive.Stream(tsdata)
    sp.decode(func = display)       

if __name__ == '__main__':
    do()

Stream.decode_next()

  • Returns a threefive.Cue instance when a SCTE-35 packet is found.
import sys
from threefive import Stream

def display(cue):
   print(f'\033[92m{cue.command.name}\033[00m')
   print(f'{cue.packet_data}')

def do():
   with open(sys.argv[1],'rb') as tsdata:
      sp = Stream(tsdata)
      while tsdata:
         cue = sp.decode_next()
         if not cue :
            sys.exit()
         display(cue)

if __name__ == '__main__':
   do()

Stream.decode_pid(the_pid, func = show_cue)

  • Use Stream.decode_pid() instead of Stream.decode() to decode SCTE-35 from packets where pid == the_pid
import threefive

with open('../35.ts','rb') as tsdata:
    threefive.Stream(tsdata).decode_pid(1035)

Stream.decode_proxy(func = show_cue)

  • Writes all packets to sys.stdout.

  • Writes scte35 data to sys.stderr.

   import threefive

   with open('vid.ts','rb') as tsdata:
      sp = threefive.Stream(tsdata)
      sp.proxy_decode()
  • 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.2.1.tar.gz (16.5 kB view hashes)

Uploaded Source

Built Distribution

threefive-2.2.1-py3-none-any.whl (16.3 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