Example #1
0
 def test_imsc_1_test_suite(self):
     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):
                     tree = et.parse(os.path.join(root, filename))
                     doc = imsc_reader.to_model(tree)
                     srt_file = srt_writer.from_model(doc)
                     srt_reader.to_model(io.StringIO(srt_file))
Example #2
0
 def test_imsc_1_2_test_suite(self):
     for root, _subdirs, files in os.walk(
             "src/test/resources/ttml/imsc-tests/imsc1_2/ttml"):
         for filename in files:
             (name, ext) = os.path.splitext(filename)
             if ext == ".ttml":
                 with self.subTest(name):
                     path = os.path.join(root, filename)
                     tree = et.parse(path)
                     test_model = imsc_reader.to_model(tree)
                     srt_from_model = srt_writer.from_model(test_model)
                     self._check_output_srt(test_model, srt_from_model,
                                            path)
Example #3
0
 def test_scc_test_suite(self):
     for root, _subdirs, files in os.walk("src/test/resources/scc"):
         for filename in files:
             (name, ext) = os.path.splitext(filename)
             if ext == ".scc":
                 with self.subTest(name):
                     path = os.path.join(root, filename)
                     scc_content = Path(path).read_text()
                     test_model = scc_reader.to_model(scc_content)
                     srt_from_model = srt_writer.from_model(test_model)
                     self.assertTrue(len(srt_from_model) > 0,
                                     msg=f"Could not convert {path}")
                     self._check_output_srt(test_model, srt_from_model,
                                            path)
Example #4
0
    def test_empty_isds(self):
        tree = et.parse(
            'src/test/resources/ttml/imsc-tests/imsc1/ttml/timing/BasicTiming010.ttml'
        )
        doc = imsc_reader.to_model(tree)
        srt_from_model = srt_writer.from_model(doc)

        self.assertEqual(
            srt_from_model, """1
00:00:10,000 --> 00:00:24,400
This text must appear at 10 seconds and disappear at 24.4 seconds

2
00:00:25,000 --> 00:00:35,000
This text must appear at 25 seconds and disappear at 35 seconds
""")
Example #5
0
    def test_srt_writer(self):
        doc = ContentDocument()

        r1 = Region("r1", doc)
        doc.put_region(r1)

        r2 = Region("r2", doc)
        r2.set_begin(Fraction(2))
        r2.set_end(Fraction(4))
        doc.put_region(r2)

        body = Body(doc)
        doc.set_body(body)

        div = Div(doc)
        body.push_child(div)

        p = P(doc)
        p.set_region(r1)
        p.set_end(Fraction(2))
        div.push_child(p)

        span = Span(doc)
        span.push_child(Text(doc, "Lorem ipsum dolor sit amet,"))
        p.push_child(span)

        p = P(doc)
        p.set_region(r2)
        div.push_child(p)

        span = Span(doc)
        span.push_child(Text(doc, "consectetur adipiscing elit."))
        p.push_child(span)

        p = P(doc)
        p.set_region(r1)
        p.set_begin(Fraction(4))
        p.set_end(Fraction(6))
        div.push_child(p)

        span = Span(doc)
        span.push_child(
            Text(doc, "Pellentesque interdum lacinia sollicitudin."))
        p.push_child(span)

        expected_srt = """1
00:00:00,000 --> 00:00:02,000
Lorem ipsum dolor sit amet,

2
00:00:02,000 --> 00:00:04,000
consectetur adipiscing elit.

3
00:00:04,000 --> 00:00:06,000
Pellentesque interdum lacinia sollicitudin.
"""

        srt_from_model = srt_writer.from_model(doc)

        self.assertEqual(expected_srt, srt_from_model)
Example #6
0
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)
Example #7
0
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)