1818from lapps .discriminators import Uri
1919from iiif_utils import generate_iiif_manifest
2020from ocr import *
21+ from datetime import timedelta
2122
2223
2324# Get Properties from MMIF file ---
2728
2829def get_alignments (alignment_view ):
2930 vtt_file = tempfile .NamedTemporaryFile ('w' , dir = "static/" , suffix = '.vtt' , delete = False )
30- vtt_file .write ("WebVTT \n \n " )
31+ vtt_file .write ("WEBVTT \n \n " )
3132 annotations = alignment_view .annotations
33+ timeframe_at_type = [at_type for at_type in alignment_view .metadata .contains if at_type .shortname == "TimeFrame" ][0 ]
34+ timeunit = alignment_view .metadata .contains [timeframe_at_type ]["timeUnit" ]
3235 # TODO: wanted to use "mmif.get_alignments(AnnotationTypes.TimeFrame, Uri.TOKEN)"
3336 # but that gave errors so I gave up on it
3437 token_idx = {a .id :a for a in annotations if a .at_type .shortname == "Token" }
@@ -45,11 +48,13 @@ def get_alignments(alignment_view):
4548 # ISO format can have up to 6 below the decimal point, on the other hand
4649 # Assuming here that start and end are in miliseconds
4750 start , end , text = start_end_text
51+ start_kwarg , end_kwarg = {timeunit : float (start )}, {timeunit : float (end )}
52+ start , end = timedelta (** start_kwarg ), timedelta (** end_kwarg )
4853 if not vtt_start :
49- vtt_start = f'{ start // 60000 :02d} :{ start % 60000 // 1000 :02d} .{ start % 1000 :03d} '
54+ vtt_start = f'{ start . seconds // 3600 :02d} :{ start . seconds :02d} .{ start . microseconds // 1000 :03d} '
5055 texts .append (text )
5156 if len (texts ) > 8 :
52- vtt_end = f'{ end // 60000 :02d} :{ end % 60000 // 1000 :02d} .{ end % 1000 :03d} '
57+ vtt_end = f'{ end . seconds // 3600 :02d} :{ end . seconds :02d} .{ end . microseconds // 1000 :03d} '
5358 vtt_file .write (f'{ vtt_start } --> { vtt_end } \n { " " .join (texts )} \n \n ' )
5459 vtt_start = None
5560 texts = []
@@ -224,7 +229,7 @@ def get_alignment_views(mmif):
224229 needed_types = set (['TextDocument' , 'Token' , 'TimeFrame' , 'Alignment' ])
225230 for view in mmif .views :
226231 annotation_types = view .metadata .contains .keys ()
227- annotation_types = [os . path . split ( str ( at ))[ - 1 ] for at in annotation_types ]
232+ annotation_types = [at . shortname for at in annotation_types ]
228233 if needed_types .issubset (annotation_types ):
229234 views .append (view )
230235 return views
@@ -243,7 +248,7 @@ def html_video(vpath, vtt_srcview=None):
243248 # use only basename because "static" directory is mapped to '' route by
244249 # `static_url_path` param
245250 src = os .path .basename (vtt_path )
246- html .write (f' <track kind="subtitles" srclang="en" src="{ src } " default>\n ' )
251+ html .write (f' <track kind="subtitles" srclang="en" src="{ src } " label="English" default>\n ' )
247252 html .write ("</video>\n " )
248253 return html .getvalue ()
249254
@@ -295,7 +300,7 @@ def get_aligned_views(mmif):
295300 """Return list of properly aligned views (for tree display)"""
296301 aligned_views = []
297302 for view in mmif .views :
298- if any ([at_type .shortname == "Token " for at_type in view .metadata .contains ]):
303+ if any ([at_type .shortname == "Alignment " for at_type in view .metadata .contains ]):
299304 if check_view_alignment (view .annotations ) == True :
300305 aligned_views .append (view .id )
301306 return aligned_views
@@ -308,7 +313,7 @@ def check_view_alignment(annotations):
308313 else :
309314 anno_stack .append (annotation .id )
310315 if len (anno_stack ) == 3 :
311- if type (anno_stack [0 ] == str ) or not (anno_stack [0 ]["source" ] in anno_stack and anno_stack [0 ]["target" ] in anno_stack ):
316+ if type (anno_stack [0 ]) == str or not (anno_stack [0 ]["source" ] in anno_stack and anno_stack [0 ]["target" ] in anno_stack ):
312317 return False
313318 anno_stack = []
314319 return True
0 commit comments