Last active
August 5, 2016 14:01
-
-
Save backslash7/473f95ce34a10f2ecfb5 to your computer and use it in GitHub Desktop.
Generate MPEG-TS stream with TDT info
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from binascii import hexlify | |
from struct import pack | |
from time import sleep | |
from datetime import datetime | |
import socket | |
ts_header = 0x47401410 | |
table_id = 0x70 | |
pointer_field = 0x00 | |
flags_plus_length= 0x7005 | |
def datetime_to_tdt(d): | |
temp_year = d.year - 1900 | |
l = 1 if d.month <=2 else 0 | |
mjd = 14956 + d.day + int((temp_year - l)*365.25) + int((d.month+1+l*12)*30.6001) | |
bcd_hour = ((d.hour/10) << 4) | (d.hour % 10) | |
bcd_minute = ((d.minute/10) << 4) | (d.minute % 10) | |
bcd_second = ((d.second/10) << 4) | (d.second % 10) | |
return pack('>H3B', mjd, bcd_hour, bcd_minute, bcd_second) | |
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
s.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 20) | |
counter = 0 | |
send = True | |
padding = '\xff' * 175 | |
while send: | |
try: | |
current_header = ts_header|counter | |
tdt_date = datetime_to_tdt(datetime.now()) | |
packet = pack('>IBBH%ss%ss' % (len(tdt_date),len(padding)), current_header, pointer_field, table_id, flags_plus_length, tdt_date, padding) | |
print "Sending %s" % (hexlify(packet)) | |
s.sendto(packet, ("239.1.82.238", 1234)) | |
sleep(30) | |
if counter == 127: | |
counter = 0 | |
else: | |
counter+=1 | |
except KeyboardInterrupt: | |
send = False | |
s.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment