Skip to main content

threefive, Pythonic SCTE-35.

Project description

threefive

  • threefive is a SCTE-35 Decoder / Parser library in Python3
  • threefive references the 2020 SCTE-35 Specification.
  • threefive decodes SCTE-35 from MPEG-TS video files and streams.
  • threefive decodes SCTE-35 from Base64, Hex, and Binary encoded strings.
  • threefive decodes SCTE-35 from ffmpeg Data: bin_data ([6][0][0][0] / 0x0006) streams.
  • threefive is now testing a Golang version too..


Versions and Releases


  • Odd numbered versions are releases.
  • Even numbered versions are testing builds between versions
a@fumatica:~/threefive$ pypy3

Python 3.6.12 (7.3.3+dfsg-3, Feb 25 2021, 22:28:03)
[PyPy 7.3.3 with GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>> from threefive import version
>>>> version()
'2.2.82'

Install


$ pip3 install threefive

# for pypy3
$ pypy3 -mpip install threefive

#If you don't have pip installed, try this.
$ pypy3 -mensurepip install pip 

OR

  • install from git
$ git clone https://github.com/futzu/threefive.git

$ cd threefive
$ make install

# for pypy3 
$ make pypy3

Easy threefive

The decode Function

  • src decode.py

  • threefive.decode is an all purpose function to decode SCTE 35 messages from a file or string.

  • MpegTS

import threefive
threefive.decode('/path/to/mpegwithscte35.ts') 
  • New in v.2.2.69 threefive.decode can parse MpegTS over http and https
import threefive
threefive.decode('https://futzu.com/xaa.ts') 
  • Base64
mesg='/DA4AAAAAAAA///wBQb+AAAAAAAiAiBDVUVJAAAAA3//AAApPWwDDEFCQ0QwMTIzNDU2SBAAAGgCL9A='
threefive.decode(mesg)
  • Hex
hexed = "0xFC301100000000000000FFFFFF0000004F253396"
threefive.decode(hexed)
  • Read a string directly from a file encoded in Base64, Binary or Hex
$ cat cue.dat
   /DBIAAAAAAAA///wBQb+ek2ItgAyAhdDVUVJSAAAGH+fCAgAAAAALMvDRBEAAAIXQ1VFSUgAABl/nwgIAAAAACyk26AQAACZcuND
from threefive import decode

decode('cue.dat')

Advanced threefive


Cue Class

  • src 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)
cue.decode()

___https://github.com/futzu/threefive/blob/master/README.md#social-injustice-and-stuff


  • All instance vars can be accessed via dot notation.
>>>> from threefive import Cue
>>>> cue = Cue(b64)
>>>> cue.decode()
True
>>>> cue.command
{'command_length': 5, 'command_type': 6, 'name': 'Time Signal', 'time_specified_flag': True, 'pts_time': 22798.906911}
>>>> cue.command.pts_time
22798.906911
>>>> 
  • call one or more of these methods after decode.
Cue Method Description
cue.get() returns cue as a dict
cue.get_json() returns cue as a JSON string
cue.show() prints cue as JSON

  • Full Example
from threefive import Cue
b64 = "/DBIAAAAAAAA///wBQb+ek2ItgAyAhdDVUVJSAAAGH+fCAgAAAAALMvDRBEAAAIXQ1VFSUgAABl/nwgIAAAAACyk26AQAACZcuND"
cue.decode(b64)
cue_data = cue.get()

Stream Class

 threefive.Stream(tsdata, show_null = False)
  • src stream.py

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

  • Supports

    • Multiple Programs.
    • Multiple SCTE35 Streams.
    • Multi-Packet PMT, and SCTE35 data.
    • Constant Data Parsing.
      • threefive.Stream is designed to run continuously
      • Longest run reported: a single Stream instance parsing video nonstop for 47 days.
  • tsdata is an open file handle.

  • show_null if set to True, enables showing SCTE 35 null commands.

Method Description
Stream.show() Prints Streams that will be checked for SCTE35
Stream.decode(func=show_cue) Prints SCTE-35 cues for SCTE-35 packets. Accepts an optional function, func, as arg.
Stream.decode_next() Returns the next SCTE35 cue as a threefive.Cue instance.
Stream.decode_program(the_program=None, func=show_cue) Same as Stream.decode except only packets where program == the_program
Stream.decode_proxy(func=show_cue) Same as Stream.decode except raw packets are written to stdout for piping to another program.

Stream.show()

  • List programs and streams that will be checked for SCTE35 data.
>>>> from threefive import Stream, version
>>>> version()
'2.2.69'
>>>> with open('video.ts','rb') as tsdata:
....     strm = Stream(tsdata)
....     strm.show()
....     

Program:1030
        PID: 1034(0x40a) Type: 0x6
        PID: 1035(0x40b) Type: 0x86 SCTE35

Program:1100
        PID: 1104(0x450) Type: 0x6
        PID: 1105(0x451) Type: 0x86 SCTE35

Program:1080
        PID: 1084(0x43c) Type: 0x6

Program:1010
        PID: 1014(0x3f6) Type: 0x6
        PID: 1015(0x3f7) Type: 0x86 SCTE35

Stream.decode(func=show_cue)

import sys
from threefive import Stream

if __name__ =='__main__':
   with open(sys.argv[1],'rb') as tsdata:
       sp = Stream(tsdata)
       sp.decode()
  • Pass in custom function

  • func should match the interface func(cue)

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()

  • Stream.decode_next returns the next SCTE35 cue as a threefive.Cue instance.
import sys
import threefive

def do():
    arg = sys.argv[1]
    with open(arg,'rb') as tsdata:
        st = threefive.Stream(tsdata)
        while True:
            cue = st.decode_next()
            if not cue:
                return False
            if cue:
                cue.show()

if __name__ == "__main__":
    do()

Stream.decode_program(the_program, func = show_cue)

  • Use Stream.decode_program() instead of Stream.decode() to decode SCTE-35 from packets where program == the_program
import threefive

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

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 -

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.91.tar.gz (20.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

threefive-2.2.91-py3-none-any.whl (19.7 kB view details)

Uploaded Python 3

File details

Details for the file threefive-2.2.91.tar.gz.

File metadata

  • Download URL: threefive-2.2.91.tar.gz
  • Upload date:
  • Size: 20.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.2

File hashes

Hashes for threefive-2.2.91.tar.gz
Algorithm Hash digest
SHA256 5500a8720b7df8b329913b0dbfc2a888c08425244dc14b103a8e3f658a96dc24
MD5 bd2beb6cea92227a0b353d2ee2bb399f
BLAKE2b-256 5e2df2bed186473ea0a1bb9e9de4c74f7ed8c6b204f5ccd32305c5a3fb4dc4a3

See more details on using hashes here.

File details

Details for the file threefive-2.2.91-py3-none-any.whl.

File metadata

  • Download URL: threefive-2.2.91-py3-none-any.whl
  • Upload date:
  • Size: 19.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.2

File hashes

Hashes for threefive-2.2.91-py3-none-any.whl
Algorithm Hash digest
SHA256 9d8e7eb554a9eb8925c29e97d78541fc165be0c532de8596dcd8590f4dba3934
MD5 e0fc616c5c9c114550e47dc1f99cec07
BLAKE2b-256 de6bbd62d7f1c53622f2480f0a3618a2436a229d6a16d82707f79447588e33fa

See more details on using hashes here.

Supported by

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