Skip to content

Hard Coded Frame Rate for parsing TTML files #350

@joe-sciame-wm

Description

@joe-sciame-wm

I was attempting to use the pycaption library to read data out of ttml files. I noticed in the code that calculation for microserconds from ttml files is using 30 fps when the last clock time value is frames. I need to retrieve the exact timecode of each line in a ttml in the frame rate of the ttml.

I have a mix of titles that have frame representations and millisecond representations. As a workaround, I was going to use our own library to convert the microseconds value back to hh, mm, ss, fr at 30 and then to timecode at the frame rate but I'm not able to tell from the return if the original value was milliseconds or frames.

Can we extend the library to allow specifying a frame rate or maybe better leverage the edit rate of the ttml file itself to calculate the seconds value?

@staticmethod
    def _convert_clock_time_to_microseconds(clock_time_match):
        microseconds = int(clock_time_match.group('hours')) * \
                       MICROSECONDS_PER_UNIT["hours"]
        microseconds += int(clock_time_match.group('minutes')) * \
                        MICROSECONDS_PER_UNIT["minutes"]
        microseconds += int(clock_time_match.group('seconds')) * \
                        MICROSECONDS_PER_UNIT["seconds"]
        if clock_time_match.group('sub_frames'):
            microseconds += int(clock_time_match.group('sub_frames').ljust(
                3, '0')) * MICROSECONDS_PER_UNIT["milliseconds"]
        elif clock_time_match.group('frames'):
            microseconds += int(clock_time_match.group('frames')) / 30 * \
                            MICROSECONDS_PER_UNIT["seconds"]
        return int(microseconds)

My code would have looked like this

def microseconds_to_smpte(
            microseconds: int,
            frame_rate: framerate.FrameRate) -> timecode.Timecode:
        tc_hhmmssff = ttml_media_time.calc_hhmmssff(
            microseconds / 1000000, FPS_30)
        tc_float = ttml_media_time.from_hhmmssff(
            *tc_hhmmssff, framerate=frame_rate)
        return ttml_media_time.from_duration(
            tc_float, framerate=frame_rate).to_smpte(dropframe=drop_frame)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions