def read(self, content, lang=u'en-US', simulate_roll_up=False, offset=0):
        """Converts the unicode string into a CaptionSet

        :type content: six.text_type
        :param content: The SCC content to be converted to a CaptionSet

        :type lang: six.text_type
        :param lang: The language of the caption

        :type simulate_roll_up: bool
        :param simulate_roll_up: If True, when converting to other formats,
            the resulting captions will contain all the rows that were visible
            on the screen when the captions were rolling up.

        :type offset: int
        :param offset:

        :rtype: CaptionSet
        """
        if type(content) != six.text_type:
            raise InvalidInputError(u'The content is not a unicode string.')

        self.simulate_roll_up = simulate_roll_up
        self.time_translator.offset = offset * 1000000
        # split lines
        lines = content.splitlines()

        # loop through each line except the first
        for line in lines[1:]:
            self._translate_line(line)

        self._flush_implicit_buffers()

        captions = CaptionSet({lang: self.caption_stash.get_all()})

        # check captions for incorrect lengths
        for cap in captions.get_captions(lang):
            # if there's an end time on a caption and the difference is
            # less than .05s kill it (this is likely caused by a standalone
            # EOC marker in the SCC file)
            if 0 < cap.end - cap.start < 50000:
                raise ValueError(
                    'unsupported length found in SCC input file: ' +
                    six.text_type(cap))

        if captions.is_empty():
            raise CaptionReadNoCaptions(u"empty caption file")
        else:
            last_caption = captions.get_captions(lang)[-1]
            last_caption.end = get_corrected_end_time(last_caption)

        return captions
Example #2
0
    def read(self, content, lang='en-US', simulate_roll_up=False, offset=0):
        """Converts the unicode string into a CaptionSet

        :type content: six.text_type
        :param content: The SCC content to be converted to a CaptionSet

        :type lang: six.text_type
        :param lang: The language of the caption

        :type simulate_roll_up: bool
        :param simulate_roll_up: If True, when converting to other formats,
            the resulting captions will contain all the rows that were visible
            on the screen when the captions were rolling up.

        :type offset: int
        :param offset:

        :rtype: CaptionSet
        """
        if type(content) != six.text_type:
            raise InvalidInputError('The content is not a unicode string.')

        self.simulate_roll_up = simulate_roll_up
        self.time_translator.offset = offset * 1000000
        # split lines
        lines = content.splitlines()

        # loop through each line except the first
        for line in lines[1:]:
            self._translate_line(line)

        self._flush_implicit_buffers()

        captions = CaptionSet({lang: self.caption_stash.get_all()})

        # check captions for incorrect lengths
        for cap in captions.get_captions(lang):
            # if there's an end time on a caption and the difference is
            # less than .05s kill it (this is likely caused by a standalone
            # EOC marker in the SCC file)
            if 0 < cap.end - cap.start < 50000:
                raise ValueError('unsupported length found in SCC input file: ' + str(cap))

        if captions.is_empty():
            raise CaptionReadNoCaptions("empty caption file")
        else:
            last_caption = captions.get_captions(lang)[-1]
            last_caption.end = get_corrected_end_time(last_caption)

        return captions
Example #3
0
    def write(self, caption_set: CaptionSet, position='bottom'):
        position = position.lower().strip()
        if position not in ScenaristDVDWriter.VALID_POSITION:
            raise ValueError('Unknown position. Supported: {}'.format(','.join(
                ScenaristDVDWriter.VALID_POSITION)))

        lang = caption_set.get_languages().pop()
        caps = caption_set.get_captions(lang)

        buf = BytesIO()

        # https://github.com/googlefonts/noto-fonts/issues/1663
        fnt = ImageFont.truetype(
            os.path.dirname(__file__) + '/NotoSansDisplay-Regular.ttf', 30)

        with tempfile.TemporaryDirectory() as tmpDir:
            with open(tmpDir + '/subtitles.sst', 'w+') as sst:
                index = 1
                sst.write(
                    HEADER.format(
                        bg_red=self.bgColor[0],
                        bg_green=self.bgColor[1],
                        bg_blue=self.bgColor[2],
                        pa_red=self.paColor[0],
                        pa_green=self.paColor[1],
                        pa_blue=self.paColor[2],
                        e1_red=self.e1Color[0],
                        e1_green=self.e1Color[1],
                        e1_blue=self.e1Color[2],
                        e2_red=self.e2Color[0],
                        e2_green=self.e2Color[1],
                        e2_blue=self.e2Color[2],
                        tape_type=self.tape_type,
                    ))
                for cap in caps:
                    sst.write("%04d %s %s subtitle%04d.tif\n" %
                              (index, self.format_ts(
                                  cap.start), self.format_ts(cap.end), index))

                    img = Image.new('RGB',
                                    (self.video_width, self.video_height),
                                    self.bgColor)
                    draw = ImageDraw.Draw(img)
                    self.printLine(draw, cap, fnt, position)

                    # quantize the image to our palette
                    img_quant = img.quantize(palette=self.palette_image,
                                             dither=0)
                    img_quant.save(tmpDir + '/subtitle%04d.tif' % index,
                                   compression="tiff_deflate")

                    index = index + 1
            zipit(tmpDir, buf)
        buf.seek(0)
        return buf.read()
Example #4
0
    def read(self, content, lang=u'en-US', simulate_roll_up=False, offset=0):
        """Converts the unicode string into a CaptionSet

        :type content: unicode
        :param content: The SCC content to be converted to a CaptionSet

        :type lang: unicode
        :param lang: The language of the caption

        :type simulate_roll_up: bool
        :param simulate_roll_up: If True, when converting to other formats,
            the resulting captions will contain all the rows that were visible
            on the screen when the captions were rolling up.

        :type offset: int
        :param offset:

        :rtype: CaptionSet
        """
        if type(content) != unicode:
            raise InvalidInputError(u'The content is not a unicode string.')

        self.simulate_roll_up = simulate_roll_up
        self.time_translator.offset = offset * 1000000
        # split lines
        lines = content.splitlines()

        # loop through each line except the first
        for line in lines[1:]:
            self._translate_line(line)

        self._flush_implicit_buffers()

        captions = CaptionSet()
        captions.set_captions(lang, self.caption_stash.get_all())

        if captions.is_empty():
            raise CaptionReadNoCaptions(u"empty caption file")
        else:
            last_caption = captions.get_captions(lang)[-1]
            last_caption.end = get_corrected_end_time(last_caption)

        return captions
Example #5
0
    def read(self, content, lang=u'en-US', simulate_roll_up=False, offset=0):
        """Converts the unicode string into a CaptionSet

        :type content: unicode
        :param content: The SCC content to be converted to a CaptionSet

        :type lang: unicode
        :param lang: The language of the caption

        :type simulate_roll_up: bool
        :param simulate_roll_up: If True, when converting to other formats,
            the resulting captions will contain all the rows that were visible
            on the screen when the captions were rolling up.

        :type offset: int
        :param offset:

        :rtype: CaptionSet
        """
        if type(content) != unicode:
            raise InvalidInputError(u'The content is not a unicode string.')

        self.simulate_roll_up = simulate_roll_up
        self.time_translator.offset = offset * 1000000
        # split lines
        lines = content.splitlines()

        # loop through each line except the first
        for line in lines[1:]:
            self._translate_line(line)

        self._flush_implicit_buffers()

        captions = CaptionSet()
        captions.set_captions(lang, self.caption_stash.get_all())

        if captions.is_empty():
            raise CaptionReadNoCaptions(u"empty caption file")
        else:
            last_caption = captions.get_captions(lang)[-1]
            last_caption.end = get_corrected_end_time(last_caption)

        return captions