def time_to_frametimecode()

in finealignment/video_alignment.py [0:0]


def time_to_frametimecode(time_str: str, fps: float, scene_end_time: FrameTimecode = None, filename: str = "unknown_file", worker_number: str = None) -> str:
    """Convert mm:ss or ss time format to FrameTimecode, or handle special cases like 'end'."""
    # Define special cases
    if time_str == "end":
        if scene_end_time is not None:
            return scene_end_time.get_timecode()
        else:
            raise ValueError("time_str is end and no replacement for scene_end_time provided")

    special_cases = ["", "n/a", "varies", "throughout scene", "throughout the scene", 
                     "end", "throughout", "not present", "not applicable"]
    if time_str.lower() in special_cases or re.match(r"scene\s\d+", time_str.lower()):
        return None

    match = re.match(r"(\d+)s$", time_str.lower())
    if match:
        time_str = match.group(1)
    if 'around ' in time_str:
        time_str = time_str.split('around ')[0]
    if '~' in time_str:
        time_str = time_str.split('~')[0]
    if '+' in time_str:
        time_str = time_str.split('+')[0]
    if '-' in time_str:
        time_str = time_str.split("-")[0]
    if ' ' in time_str and ":" in time_str:
        time_str = time_str.split(" ")[0]
    if ":" in time_str:
        parts = time_str.split(":")
        if len(parts) == 3:
            hours, minutes, seconds = parts
        elif len(parts) == 2:
            hours = 0
            minutes, seconds = parts
        elif len(parts) == 1:
            hours = 0
            minutes = 0
            seconds = parts[0]
        else:
            raise ValueError(f"Invalid timestamp format: {time_str}")

        if '.' in seconds:
            seconds = seconds.split(".")[0]

        match = re.match(r"^\d+", seconds)
        if match:
            seconds = int(match.group())
        else:
            raise ValueError(f"Invalid timestamp format: {time_str}")


        total_seconds = float(hours) * 3600 + float(minutes) * 60 + float(seconds)
    else:
        try:
            total_seconds = float(time_str)
        except ValueError:
            raise ValueError(f"Invalid timestamp format: {time_str}")
    return FrameTimecode(timecode=total_seconds, fps=fps).get_timecode()