def update_timestamps_in_json()

in finealignment/video_alignment.py [0:0]


def update_timestamps_in_json(data: dict, fps: float, video_id: str, worker_number: str) -> dict:
    """Update all timestamp fields in the JSON data to FrameTimecode format and ensure they stay within scene boundaries."""
    # Update timestamps in scenes
    for scene in data.get('scenes', []):
        scene_start = FrameTimecode(timecode=scene['timestamps']['start_timestamp'], fps=fps)
        scene_end = FrameTimecode(timecode=scene['timestamps']['end_timestamp'], fps=fps)
        
        def enforce_within_boundaries(timestamp, start, end):
            if timestamp is None:
                return None
            frame_timecode = FrameTimecode(timecode=timestamp, fps=fps)
            if frame_timecode.get_frames() < start.get_frames():
                return start.get_timecode()
            elif frame_timecode.get_frames() > end.get_frames():
                return end.get_timecode()
            else:
                return timestamp

        # Update activities timestamps
        for activity in scene.get('activities', []):
            if 'timestamp' in activity:
                if 'start_timestamp' in activity['timestamp']:
                    activity['timestamp']['start_timestamp'] = enforce_within_boundaries(
                        time_to_frametimecode(activity['timestamp']['start_timestamp'], fps, filename=video_id, scene_end_time=scene_end, worker_number = worker_number), scene_start, scene_end
                    )
                if 'end_timestamp' in activity['timestamp']:
                    activity['timestamp']['end_timestamp'] = enforce_within_boundaries(
                        time_to_frametimecode(activity['timestamp']['end_timestamp'], fps, filename=video_id, scene_end_time=scene_end,worker_number = worker_number), scene_start, scene_end
                    )

        # Update props timestamps
        for prop in scene.get('props', []):
            if 'timestamp' in prop:
                if 'start_timestamp' in prop['timestamp']:
                    prop['timestamp']['start_timestamp'] = enforce_within_boundaries(
                        time_to_frametimecode(prop['timestamp']['start_timestamp'], fps, filename=video_id, scene_end_time=scene_end,worker_number = worker_number), scene_start, scene_end
                    )
                if 'end_timestamp' in prop['timestamp']:
                    prop['timestamp']['end_timestamp'] = enforce_within_boundaries(
                        time_to_frametimecode(prop['timestamp']['end_timestamp'], fps, filename=video_id, scene_end_time=scene_end,worker_number = worker_number), scene_start, scene_end
                    )

        # Update video editing details timestamps
        for video_editing in scene.get('videoEditingDetails', []):
            if 'timestamps' in video_editing:
                if 'start_timestamp' in video_editing['timestamps']:
                    video_editing['timestamps']['start_timestamp'] = enforce_within_boundaries(
                        time_to_frametimecode(video_editing['timestamps']['start_timestamp'], fps, filename=video_id, scene_end_time=scene_end,worker_number = worker_number), scene_start, scene_end
                    )
                if 'end_timestamp' in video_editing['timestamps']:
                    video_editing['timestamps']['end_timestamp'] = enforce_within_boundaries(
                        time_to_frametimecode(video_editing['timestamps']['end_timestamp'], fps, filename=video_id, scene_end_time=scene_end,worker_number = worker_number), scene_start, scene_end
                    )

        # Update mood key moments timestamps
        for key_moment in scene.get('mood', {}).get('keyMoments', []):
            if 'timestamp' in key_moment:
                key_moment['timestamp'] = enforce_within_boundaries(
                    time_to_frametimecode(key_moment['timestamp'], fps, filename=video_id, scene_end_time=scene_end,worker_number = worker_number), scene_start, scene_end
                )

        # Update narrative progression timestamps
        for narrative in scene.get('narrativeProgression', []):
            if 'timestamp' in narrative:
                narrative['timestamp'] = enforce_within_boundaries(
                    time_to_frametimecode(narrative['timestamp'], fps, filename=video_id, scene_end_time=scene_end,worker_number = worker_number), scene_start, scene_end
                )

    # Update storylines climax timestamps
    if 'storylines' in data and 'climax' in data['storylines'] and 'timestamp' in data['storylines']['climax']:
        data['storylines']['climax']['timestamp'] = time_to_frametimecode(data['storylines']['climax']['timestamp'], fps, filename=video_id, scene_end_time=scene_end,worker_number = worker_number)

    # Update trimming suggestions timestamps
    for trimming in data.get('trimmingSuggestions', []):
        if 'timestamps' in trimming:
            if 'start_timestamp' in trimming['timestamps']:
                trimming['timestamps']['start_timestamp'] = enforce_within_boundaries(
                    time_to_frametimecode(trimming['timestamps']['start_timestamp'], fps, filename=video_id, scene_end_time=scene_end,worker_number = worker_number), scene_start, scene_end
                )
            if 'end_timestamp' in trimming['timestamps']:
                trimming['timestamps']['end_timestamp'] = enforce_within_boundaries(
                    time_to_frametimecode(trimming['timestamps']['end_timestamp'], fps, filename=video_id, scene_end_time=scene_end,worker_number = worker_number), scene_start, scene_end
                )

    return data