def OnESPacket(current_pid, packet, header_size): """ Callback invoked on the successful extraction of an Elementary Stream packet from the Transport Stream file packets. :param current_pid: The TS Program ID for the TS packets this info originated from :param packet: The ENTIRE ES packet, header and payload-- which may have been assembled from multiple TS packet payloads. :param header_size: Size of the header in bytes (characters in the string). Provided to more easily separate the packet into header and payload. :return: None """ global pid global VERBOSE global SILENT global elapsed_time_s if pid >= 0 and current_pid != pid: return try: payload = ES.get_pes_payload(packet) f = list(payload) #f = bytearray(payload) data_group = DataGroup(f) if not data_group.is_management_data(): #We now have a Data Group that contains caption data. #We take out its payload, but this is further divided into 'Data Unit' structures caption = data_group.payload() #iterate through the Data Units in this payload via another generator. for data_unit in next_data_unit(caption): #we're only interested in those Data Units which are "statement body" to get CC data. if not isinstance(data_unit.payload(), StatementBody): continue #okay. Finally we've got a data unit with CC data. Feed its payload to the custom if pid < 0 and VERBOSE and not SILENT: pid = current_pid print("Found Closed Caption data in PID: " + str(pid)) print("Will now only process this PID to improve performance.") #formatter function above. This dumps the basic text to stdout. cc = formatter(data_unit.payload().payload(), elapsed_time_s) if cc and VERBOSE: #according to best practice, always deal internally with UNICODE, and encode to #your encoding of choice as late as possible. Here, i'm encoding as UTF-8 for #my command line. #DECODE EARLY, ENCODE LATE print(cc.encode('utf-8')) except EOFError: pass except Exception, err: if VERBOSE and not SILENT and pid >= 0: print("Exception thrown while handling DataGroup in ES. This may be due to many factors" + "such as file corruption or the .ts file using as yet unsupported features.") traceback.print_exc(file=sys.stdout)
def OnESPacket(current_pid, packet, header_size): """ Callback invoked on the successful extraction of an Elementary Stream packet from the Transport Stream file packets. :param current_pid: The TS Program ID for the TS packets this info originated from :param packet: The ENTIRE ES packet, header and payload-- which may have been assembled from multiple TS packet payloads. :param header_size: Size of the header in bytes (characters in the string). Provided to more easily separate the packet into header and payload. :return: None """ global pid global VERBOSE global SILENT global elapsed_time_s global ass global infilename global outfilename global tmax global time_offset if pid >= 0 and current_pid != pid: return try: payload = ES.get_pes_payload(packet) f = list(payload) data_group = DataGroup(f) if not data_group.is_management_data(): #We now have a Data Group that contains caption data. #We take out its payload, but this is further divided into 'Data Unit' structures caption = data_group.payload() #iterate through the Data Units in this payload via another generator. for data_unit in next_data_unit(caption): #we're only interested in those Data Units which are "statement body" to get CC data. if not isinstance(data_unit.payload(), StatementBody): continue if not ass: v = not SILENT ass = ASSFormatter(tmax=tmax, video_filename=outfilename, verbose=v) ass.format(data_unit.payload().payload(), elapsed_time_s) # this code used to sed the PID we're scanning via first successful ARIB decode # but i've changed it below to draw present CC language info form ARIB # management data. Leaving this here for reference. #if pid < 0 and not SILENT: # pid = current_pid # print("Found Closed Caption data in PID: " + str(pid)) # print("Will now only process this PID to improve performance.") else: # management data management_data = data_group.payload() numlang = management_data.num_languages() if pid < 0 and numlang > 0: for language in range(numlang): if not SILENT: print("Closed caption management data for language: " + management_data.language_code(language) + " available in PID: " + str(current_pid)) print("Will now only process this PID to improve performance.") pid = current_pid except EOFError: pass except FileOpenError as ex: # allow IOErrors to kill application raise ex except Exception, err: if not SILENT and pid >= 0: print("Exception thrown while handling DataGroup in ES. This may be due to many factors" + "such as file corruption or the .ts file using as yet unsupported features.") traceback.print_exc(file=sys.stdout)