def TranscribeToWebCaptions()

in source/operators/captions/webcaptions.py [0:0]


    def TranscribeToWebCaptions(self, transcripts):

        endTime = 0.0
        maxLength = 120
        wordCount = 0
        maxWords = 25
        maxSilence = 1.5

        captions = []
        caption = None

        for transcript in transcripts:
            for item in transcript["results"]["items"]:

                isPunctuation = item["type"] == "punctuation"

                if caption is None:

                    # Start of a line with punctuation, just skip it
                    if isPunctuation:
                        continue

                    # Create a new caption line
                    caption = {
                        "start": float(item["start_time"]),
                        "caption": "",
                        "wordConfidence": []
                    }

                if not isPunctuation:

                    startTime = float(item["start_time"])

                    # Check to see if there has been a long silence
                    # between the last recorded word and start a new
                    # caption if this is the case, ending the last time
                    # as this one starts.

                    if (len(caption["caption"]) > 0) and ((endTime + maxSilence) < startTime):

                        caption["end"] = startTime
                        captions.append(caption)

                        caption = {
                            "start": float(startTime),
                            "caption": "",
                            "wordConfidence": []
                        }

                        wordCount = 0

                    endTime = float(item["end_time"])

                requiresSpace = (not isPunctuation) and (len(caption["caption"]) > 0)

                if requiresSpace:
                    caption["caption"] += " "

                # Process tweaks

                text = item["alternatives"][0]["content"]
                confidence = item["alternatives"][0]["confidence"]
                textLower = text.lower()

                caption["caption"] += text

                # Track raw word confidence
                if not isPunctuation:
                    caption["wordConfidence"].append(
                        {
                            "w": textLower,
                            "c": float(confidence)
                        }
                    )
                    # Count words
                    wordCount += 1

                # If we have reached a good amount of text finalize the caption
                if (wordCount >= maxWords) or (len(caption["caption"]) >= maxLength) or isPunctuation and text in "...?!":
                    caption["end"] = endTime
                    captions.append(caption)
                    wordCount = 0
                    caption = None

        # Close the last caption if required

        if caption is not None:
            caption["end"] = endTime
            captions.append(caption)

        return captions