Skip to main content

Pythonic SCTE35

项目描述

安装| 快速启动| 提示类 | 流类| 示例 | ffmpeg 和三五| x9k3 | 口香糖| m3ufu | 效果机器人

threefive 是一个 SCTE35 解析器。

在我们进入所有这些之前,我有一个简短的声明。

I am still looking for my Watergate, my defining scandal.
I dont mean to keep beating the same drum, 
but I still say that subliminal messages 
will be very effective over the Internet. 
It will take years before we see any laws 
against shttps.
(The first "s" is for subliminal)    


Requirements

  • 三五要求
  • 可选依赖项:
    • pyaes 如果您想要对 HLS 段进行 AES 解密。

Install

python3 -mpip  install  threefive

# and / or

pypy3 -m pip install threefive

To install the optional dependencies

python3 -mpip  install threefive[all]

# and / or

pypy3 -mpip  install  threefive[all]

Versions and Releases

Python 3.10.6 (main, Aug 10 2022, 11:19:32) [GCC 12.1.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from threefive import version
>>> version()
'2.3.45'
>>> 
  • 我做滚动发布
  • 发行版本很 奇怪
  • 不稳定的测试版本是偶数

Easy Examples

Base64
>>> from threefive import Cue
>>> stuff = '/DAvAAAAAAAA///wBQb+dGKQoAAZAhdDVUVJSAAAjn+fCAgAAAAALKChijUCAKnMZ1g='
>>> cue=Cue(stuff)
>>> cue.decode()
True
Bytes
>>> import threefive 

>>> stuff = b'\xfc0\x11\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\x00\x00\x00O%3\x96'
>>> cue=Cue(stuff)
>>> cue.decode()
True
>>> cue.show()
Hex
import threefive 

cue = threefive.Cue("0XFC301100000000000000FFFFFF0000004F253396")
cue.decode()
cue.show()
Mpegts Multicast
  • 在我的 Debian Sid笔记本电脑上,我设置了以下内容,
## <dev> is the network device

ip link set <dev> multicast on allmulticast on

ethtool  -G <dev> rx 4096

sysctl -w net.core.rmem_default=5000000

sysctl -w net.core.rmem_max=15000000
import threefive 

strm = threefive.Stream('udp://@239.35.0.35:1234')
strm.decode()

Cue Class

  • src cue.py
  • threefive.Cue类对SCTE35 二进制、base64 或十六进制编码字符串进行解码。
    >>>> import threefive
    >>>> Base64 = "/DAvAAAAAAAA///wBQb+dGKQoAAZAhdDVUVJSAAAjn+fCAgAAAAALKChijUCAKnMZ1g="
    >>>> cue = threefive.Cue(Base64)
  • cue.decode() 成功时返回 True,如果解码失败则返回 False
    >>>> cue.decode()
    True
  • 调用 cue.decode() 之后,可以通过点符号访问实例变量。
    >>>> cue.command
    {'calculated_length': 5, 'name': 'Time Signal', 'time_specified_flag': True, 'pts_time': 21695.740089}

    >>>> cue.command.pts_time
    21695.740089

    >>>> cue.info_section.table_id

    '0xfc'
  • 从 MPEGTS 解析 Cues 时,threefive 尝试包括,
    • 包的pid
    • pid的程序
    • 数据包的pts
    • 数据包的pcr
class Cue(threefive.base.SCTE35Base)
 |  Cue(data=None, packet_data=None)
 
 |  __init__(self, data=None, packet_data=None)
 |      data may be packet bites or encoded string
 |      packet_data is a instance passed from a Stream instance

Cue.decode()

 |  decode(self)
 |      Cue.decode() parses for SCTE35 data

Cue.get()

 |  get(self)
 |      Cue.get returns the SCTE-35 Cue
 |      data as a dict of dicts.

Cue.get() Example

>>> from threefive import Cue
>>> cue = Cue('0XFC301100000000000000FFFFFF0000004F253396')
>>> cue.decode()
True
>>> cue
{'bites': b'\xfc0\x11\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\x00\x00\x00O%3\x96', 
'info_section': {'table_id': '0xfc', 'section_syntax_indicator': False, 'private': False, 'sap_type': '0x3', 
'sap_details': 'No Sap Type', 'section_length': 17, 'protocol_version': 0, 'encrypted_packet': False, 
'encryption_algorithm': 0, 'pts_adjustment_ticks': 0, 'pts_adjustment': 0.0, 'cw_index': '0x0', 'tier': '0xfff',
'splice_command_length': 4095, 'splice_command_type': 0, 'descriptor_loop_length': 0, 'crc': '0x4f253396'},
'command': {'command_length': None, 'command_type': 0, 'name': 'Splice Null'},
'descriptors': [], 'packet_data': None}
  • Cue.get() 省略 cue.bites 和空值
>>> cue.get()
{'info_section': {'table_id': '0xfc', 'section_syntax_indicator': False,'private': False, 'sap_type': '0x3', 
'sap_details': 'No Sap Type', 'section_length': 17, 'protocol_version': 0, 'encrypted_packet': False,
'encryption_algorithm': 0, 'pts_adjustment_ticks': 0, 'pts_adjustment': 0.0, 'cw_index': '0x0', 'tier': '0xfff',
'splice_command_length': 4095, 'splice_command_type': 0, 'descriptor_loop_length': 0, 'crc': '0x4f253396'},
'command': {'command_type': 0, 'name': 'Splice Null'},
'descriptors': []}

Cue.get_descriptors()

 |  get_descriptors(self)
 |      Cue.get_descriptors returns a list of
 |      SCTE 35 splice descriptors as dicts.

Cue.get_json()

 |  get_json(self)
 |      Cue.get_json returns the Cue instance
 |      data in json.

Cue.show()

 |  show(self)
 |      Cue.show prints the Cue as JSON

Cue.to_stderr()

 |  to_stderr(self)
 |      Cue.to_stderr prints the Cue

Stream Class

  • 源流.py
  • 三五。Stream类从Mpegts解析SCTE35
  • 支持:
    • 文件Http(s)以及Udp多播协议。
    • 多个程序
    • 多包 PAT、PMT 和 SCTE35 表
class Stream(builtins.object)
 |  Stream(tsdata, show_null=True)
 |  
 |  Stream class for parsing MPEG-TS data.
|  __init__(self, tsdata, show_null=True)
|      
|      tsdata is a file or http, https, 
|       udp or multicast url.
|       
|      set show_null=False to exclude Splice Nulls
|      
|      Use like...
|      
|      from threefive import Stream
|      strm = Stream("vid.ts",show_null=False)
|      strm.decode()

Stream.decode(func=show_cue)

|  decode(self, func=show_cue)
|      Stream.decode reads self.tsdata to find SCTE35 packets.
|      func can be set to a custom function that accepts
|      a threefive.Cue instance as it's only argument.

Stream.decode Example

import sys
from threefive import Stream
>>>> Stream('plp0.ts').decode()
  • 传入自定义函数

  • func应该与接口匹配 func(cue)

Stream.decode with custom function Example

import sys
import threefive

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

def do():
   sp = threefive.Stream(tsdata)
   sp.decode(func = display)       

if __name__ == '__main__':
    do()

Stream.decode_next()

|  decode_next(self)
|      Stream.decode_next returns the next
|      SCTE35 cue as a threefive.Cue instance.

Stream.decode_next Example

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)

|  decode_program(self, the_program, func=show_cue)
|      Stream.decode_program limits SCTE35 parsing
|      to a specific MPEGTS program.

Stream.decode_program Example

import threefive
threefive.Stream('35.ts').decode_program(1)

Stream.decode_proxy(func = show_cue)

  • 将所有数据包写入 sys.stdout。

  • 将 scte35 数据写入 sys.stderr。

|  decode_proxy(self, func=show_cue_stderr)
|      Stream.decode_proxy writes all ts packets are written to stdout
|      for piping into another program like mplayer.
|      SCTE-35 cues are printed to stderr.

Stream.decode_proxy Example

import threefive
sp = threefive.Stream('https://futzu.com/xaa.ts')
sp.decode_proxy()
  • 管道到 mplayer
$ python3 proxy.py | mplayer -

Stream.show()

|  show(self)
|   List programs and streams and info for MPEGTS

Stream.show() Example

>>>> from threefive import Stream
>>>> Stream('https://slo.me/plp0.ts').show()
Program: 1040
    Service:    fumatic
    Provider:   fu-labs
    Pcr Pid:    1041[0x411]
    Streams:
                Pid: 1041[0x411]        Type: 0x1b AVC Video
                Pid: 1042[0x412]        Type: 0x3 MP2 Audio
                Pid: 1044[0x414]        Type: 0x6 PES Packets/Private Data
                Pid: 1045[0x415]        Type: 0x86 SCTE35 Data

Program: 1050
    Service:    fancy ˹ 
    Provider:   fu-corp
    Pcr Pid:    1051[0x41b]
    Streams:
                Pid: 1051[0x41b]        Type: 0x1b AVC Video
                Pid: 1052[0x41c]        Type: 0x3 MP2 Audio
                Pid: 1054[0x41e]        Type: 0x6 PES Packets/Private Data
                Pid: 1055[0x41f]        Type: 0x86 SCTE35 Data
 

Stream.dump(fname)

|  dump(self, fname)
|      Stream.dump dumps all the packets to a file (fname).

Stream.strip_scte35(func=show_cue_stderr)

| strip_scte35 自我func = show_cue_stderr