def test_frames(self): ttml_doc_str = """<?xml version="1.0" encoding="UTF-8"?> <tt xml:lang="en" xmlns="http://www.w3.org/ns/ttml" > <body begin="5s"/> </tt> """ ttml_doc = et.ElementTree(et.fromstring(ttml_doc_str)) config = imsc_config.IMSCWriterConfiguration.parse({ "time_format": "frames", "fps": "30/1" }) xml_from_model = imsc_writer.from_model(imsc_reader.to_model(ttml_doc), config) body_element = xml_from_model.find("tt:body", {"tt": xml_ns.TTML}) self.assertEqual(body_element.get("begin"), "150f") config = imsc_config.IMSCWriterConfiguration.parse({"fps": "30/1"}) xml_from_model = imsc_writer.from_model(imsc_reader.to_model(ttml_doc), config) body_element = xml_from_model.find("tt:body", {"tt": xml_ns.TTML}) self.assertEqual(body_element.get("begin"), "150f") config = imsc_config.IMSCWriterConfiguration.parse( {"time_format": "frames"}) with self.assertRaises(ValueError): imsc_writer.from_model(imsc_reader.to_model(ttml_doc), config)
def test_tts_writing_extent_when_body_has_extents(self): doc = model.ContentDocument() body = model.Body(doc) div = model.Div(doc) div.set_style( styles.StyleProperties.Extent, get_extent_from_dimensions(123, 456, styles.LengthType.Units.px)) p = model.P(doc) span = model.Span(doc) text = model.Text(doc) text.set_text("asdf") span.push_child(text) p.push_child(span) div.push_child(p) body.push_child(div) doc.set_body(body) r = model.Region("hello", doc) r.set_style( styles.StyleProperties.Extent, get_extent_from_dimensions(123, 456, styles.LengthType.Units.px)) doc.put_region(r) tree_from_model = imsc_writer.from_model(doc) extent = tree_from_model.getroot().attrib.get( f"{{{imsc_styles.StyleProperties.Extent.ns}}}{imsc_styles.StyleProperties.Extent.local_name}" ) self.assertEqual(extent, '1920px 1080px')
def test_clock_time(self): ttml_doc_str = """<?xml version="1.0" encoding="UTF-8"?> <tt xml:lang="en" xmlns="http://www.w3.org/ns/ttml" > <body begin="2.3s"/> </tt> """ ttml_doc = et.ElementTree(et.fromstring(ttml_doc_str)) config = imsc_config.IMSCWriterConfiguration.parse( {"time_format": "clock_time"}) xml_from_model = imsc_writer.from_model(imsc_reader.to_model(ttml_doc), config) body_element = xml_from_model.find("tt:body", {"tt": xml_ns.TTML}) self.assertEqual(body_element.get("begin"), "00:00:02.300") xml_from_model = imsc_writer.from_model(imsc_reader.to_model(ttml_doc)) body_element = xml_from_model.find("tt:body", {"tt": xml_ns.TTML}) self.assertEqual(body_element.get("begin"), "00:00:02.300")
def test_tts_writing_no_extent_when_no_body(self): d = model.ContentDocument() tree_from_model = imsc_writer.from_model(d) extent = tree_from_model.getroot().attrib.get( f"{{{imsc_styles.StyleProperties.Extent.ns}}}{imsc_styles.StyleProperties.Extent.local_name}" ) self.assertEqual(extent, None)
def test_body_only(self): doc = model.ContentDocument() body = model.Body(doc) div = model.Div(doc) p = model.P(doc) span = model.Span(doc) text = model.Text(doc) text.set_text("asdf") span.push_child(text) p.push_child(span) div.push_child(p) body.push_child(div) doc.set_body(body) # write the document out to a file imsc_writer.from_model(doc).write('build/BodyElement.out.ttml', encoding='utf-8', xml_declaration=True)
def test_animation_001(self): file_to_parse = "src/test/resources/ttml/imsc-tests/imsc1/ttml/animation/Animation001.ttml" tree = et.parse(file_to_parse) # create the model test_model = imsc_reader.to_model(tree) # convert from a model to a ttml document tree_from_model = imsc_writer.from_model(test_model) # write the document out to a file self.write_pretty_xml(tree_from_model, 'build/out.ttml')
def test_imsc_1_test_suite(self): if not os.path.exists('build/imsc1'): os.makedirs('build/imsc1') for root, _subdirs, files in os.walk( "src/test/resources/ttml/imsc-tests/imsc1/ttml"): for filename in files: (name, ext) = os.path.splitext(filename) if ext == ".ttml": with self.subTest(name), self.assertLogs() as logs: logging.getLogger().info( "*****dummy*****") # dummy log tree = et.parse(os.path.join(root, filename)) test_model = imsc_reader.to_model(tree) tree_from_model = imsc_writer.from_model(test_model) self.write_pretty_xml(tree_from_model, f'build/imsc1/{name}.ttml') if len(logs.output) > 1: self.fail(logs.output)
def test_fps(self): ttml_doc_str = """<?xml version="1.0" encoding="UTF-8"?> <tt xml:lang="en" xmlns="http://www.w3.org/ns/ttml" > <body begin="5s"/> </tt> """ ttml_doc = et.ElementTree(et.fromstring(ttml_doc_str)) config = imsc_config.IMSCWriterConfiguration( time_format=imsc_config.TimeExpressionEnum.frames, fps=Fraction(30, 1)) xml_from_model = imsc_writer.from_model(imsc_reader.to_model(ttml_doc), config) body_element = xml_from_model.find("tt:body", {"tt": xml_ns.TTML}) self.assertEqual(body_element.get("begin"), "150f")
def test_imsc_1_test_suite(self): manifest = [] base_path = "build/imsc1" for root, _subdirs, files in os.walk( "src/test/resources/ttml/imsc-tests/imsc1/ttml"): for filename in files: (name, ext) = os.path.splitext(filename) if ext == ".ttml": with self.subTest(name), self.assertLogs() as logs: logging.getLogger().info( "*****dummy*****") # dummy log tree = et.parse(os.path.join(root, filename)) test_model = imsc_reader.to_model(tree) tree_from_model = imsc_writer.from_model(test_model) test_dir_relative_path = os.path.basename(root) test_relative_path = os.path.join( test_dir_relative_path, filename) os.makedirs(os.path.join(base_path, "ttml", test_dir_relative_path), exist_ok=True) with open( os.path.join(base_path, "ttml", test_relative_path), "wb") as f: f.write( et.tostring(tree_from_model.getroot(), 'utf-8')) manifest.append({ "path": str(test_relative_path).replace('\\', '/') }) if len(logs.output) > 1: self.fail(logs.output) with open(os.path.join(base_path, "tests.json"), "w", encoding="utf8") as fp: json.dump(manifest, fp)
def test_tts_writing_no_extent_when_body_has_no_extents(self): doc = model.ContentDocument() body = model.Body(doc) div = model.Div(doc) p = model.P(doc) span = model.Span(doc) text = model.Text(doc) text.set_text("asdf") span.push_child(text) p.push_child(span) div.push_child(p) body.push_child(div) doc.set_body(body) tree_from_model = imsc_writer.from_model(doc) extent = tree_from_model.getroot().attrib.get( f"{{{imsc_styles.StyleProperties.Extent.ns}}}{imsc_styles.StyleProperties.Extent.local_name}" ) self.assertEqual(extent, None)
def convert(args): '''Process input and output through the reader, converter, and writer''' inputfile = args.input outputfile = args.output # Note - Loading config data from a file takes priority over # data passed in as a json string json_config_data = None if args.config is not None: json_config_data = json.loads(args.config) if args.config_file is not None: with open(args.config_file) as json_file: json_config_data = json.load(json_file) general_config = read_config_from_json(GeneralConfiguration, json_config_data) if general_config is not None: if general_config.progress_bar is not None: progress.display_progress_bar = general_config.progress_bar if general_config.log_level is not None: LOGGER.setLevel(general_config.log_level) LOGGER.info("Input file is %s", inputfile) LOGGER.info("Output file is %s", outputfile) _input_filename, input_file_extension = os.path.splitext(inputfile) _output_filename, output_file_extension = os.path.splitext(outputfile) reader_type = FileTypes.get_file_type(args.itype, input_file_extension) writer_type = FileTypes.get_file_type(args.otype, output_file_extension) if reader_type is FileTypes.TTML: # # Parse the xml input file into an ElementTree # tree = et.parse(inputfile) # # Pass the parsed xml to the reader # model = imsc_reader.to_model(tree, progress_callback_read) elif reader_type is FileTypes.SCC: file_as_str = Path(inputfile).read_text() # # Read the config # reader_config = read_config_from_json(SccReaderConfiguration, json_config_data) # # Pass the parsed xml to the reader # model = scc_reader.to_model(file_as_str, reader_config, progress_callback_read) elif reader_type is FileTypes.STL: # # Read the config # reader_config = read_config_from_json(STLReaderConfiguration, json_config_data) # # Open the file and pass it to the reader # with open(inputfile, "rb") as f: model = stl_reader.to_model(f, reader_config, progress_callback_read) elif reader_type is FileTypes.SRT: # # Open the file and pass it to the reader # with open(inputfile, "r", encoding="utf-8") as f: model = srt_reader.to_model(f, None, progress_callback_read) else: if args.itype is not None: exit_str = f'Input type {args.itype} is not supported' else: exit_str = f'Input file {args.input} is not supported' LOGGER.error(exit_str) sys.exit(exit_str) if writer_type is FileTypes.TTML: # # Read the config # writer_config = read_config_from_json(IMSCWriterConfiguration, json_config_data) # # Construct and configure the writer # tree_from_model = imsc_writer.from_model(model, writer_config, progress_callback_write) # # Write out the converted file # tree_from_model.write(outputfile, encoding="utf-8") elif writer_type is FileTypes.SRT: # # Read the config # writer_config = read_config_from_json(ISDConfiguration, json_config_data) # # Construct and configure the writer # srt_document = srt_writer.from_model(model, writer_config, progress_callback_write) # # Write out the converted file # with open(outputfile, "w", encoding="utf-8") as srt_file: srt_file.write(srt_document) elif writer_type is FileTypes.VTT: # # Read the config # writer_config = read_config_from_json(VTTWriterConfiguration, json_config_data) # # Construct and configure the writer # vtt_document = vtt_writer.from_model(model, writer_config, progress_callback_write) # # Write out the converted file # with open(outputfile, "w", encoding="utf-8") as vtt_file: vtt_file.write(vtt_document) else: if args.otype is not None: exit_str = f'Output type {args.otype} is not supported' else: exit_str = f'Output file is {args.output} is not supported' LOGGER.error(exit_str) sys.exit(exit_str)
def convert(args): '''Process input and output through the reader, converter, and writer''' inputfile = args.input outputfile = args.output LOGGER.info("Input file is %s", inputfile) LOGGER.info("Output file is %s", outputfile) _input_filename, input_file_extension = os.path.splitext(inputfile) _output_filename, output_file_extension = os.path.splitext(outputfile) reader_type = FileTypes.get_file_type(args.itype, input_file_extension) writer_type = FileTypes.get_file_type(args.otype, output_file_extension) if reader_type is FileTypes.TTML: # # Parse the xml input file into an ElementTree # tree = et.parse(inputfile) # # Pass the parsed xml to the reader # model = imsc_reader.to_model(tree) elif reader_type is FileTypes.SCC: file_as_str = Path(inputfile).read_text() # # Pass the parsed xml to the reader # model = scc_reader.to_model(file_as_str) else: if args.itype is not None: exit_str = f'Input type {args.itype} is not supported' else: exit_str = f'Input file is {args.input} is not supported' LOGGER.error(exit_str) sys.exit(exit_str) if writer_type is FileTypes.TTML: # # Construct and configure the writer # tree_from_model = imsc_writer.from_model(model) # # Write out the converted file # tree_from_model.write(outputfile) elif writer_type is FileTypes.SRT: # # Construct and configure the writer # srt_document = srt_writer.from_model(model) # # Write out the converted file # with open(outputfile, "w") as srt_file: srt_file.write(srt_document) else: if args.otype is not None: exit_str = f'Output type {args.otype} is not supported' else: exit_str = f'Output file is {args.output} is not supported' LOGGER.error(exit_str) sys.exit(exit_str)