Example #1
0
def json_to_srt(deepspeech_json, max_word_time=10, min_sub_time=1.5, max_sub_time=3):
    index = 0
    subtitle = ""
    start_time = 0
    end_time = 0
    subtitles = SubRipFile()

    for word in deepspeech_json["words"]:
        word["end_time"] = word["start_time"] + word["duration"]
        if word["duration"] < max_word_time:
            if start_time + max_sub_time >= word["end_time"] and subtitle:
                subtitle += " "
                subtitle += word["word"]
                end_time = max(word["end_time"], start_time + min_sub_time)
            elif subtitle:
                # Convert to milliseconds
                subtitles.append(
                    SubRipItem(index=++index, start=int(start_time*1000), end=int(end_time*1000), text=subtitle))
                subtitle = ""

            if not subtitle:
                start_time = word["start_time"]
                subtitle += word["word"]
                end_time = max(word["end_time"], start_time + min_sub_time)

    if subtitle:
        subtitles.append(SubRipItem(index=++index, start=int(start_time*1000), end=int(end_time*1000), text=subtitle))
    return subtitles
Example #2
0
    def generate_srt_from_sjson(sjson_subs):
        """
        Generate transcripts from sjson to SubRip (*.srt)

        Arguments:
            sjson_subs (dict): `sjson` subs.

        Returns:
            Subtitles in SRT format.
        """
        output = ''

        equal_len = len(sjson_subs['start']) == len(sjson_subs['end']) == len(sjson_subs['text'])
        if not equal_len:
            return output

        for i in range(len(sjson_subs['start'])):
            item = SubRipItem(
                index=i,
                start=SubRipTime(milliseconds=sjson_subs['start'][i]),
                end=SubRipTime(milliseconds=sjson_subs['end'][i]),
                text=sjson_subs['text'][i]
            )
            output += (str(item))
            output += '\n'
        return output
Example #3
0
def processSub(sub_L1, sub_L2, levels, outs, removed_lines, show_L2):
    text_L1 = sub_L1.text.lstrip()
    text_L2 = sub_L2.text.lstrip()

    if (text_L2 is not None) and (len(text_L2) > 0):
        cefr_level, flesh_kincade_grade, n_words = analyzeSubLevel(text_L2)
    else:
        flesh_kincade_grade = ""
        cefr_level = ""
        n_words = 0

    for level in levels:
        if (text_L2
                is not None) and (len(text_L2) > 0) and isTextNotAboveLevel(
                    level, cefr_level, flesh_kincade_grade, n_words,
                    len(text_L2)):
            removed_lines[level] = removed_lines[level] + 1
            text = "" if show_L2 == "no" else this.L2_sub_template.format(
                text_L2)
        else:
            text = joinLines(
                text_L2, text_L1
            ) if show_L2 == "yes" else this.L1_sub_template.format(text_L1)

        if len(text) > 0:
            item = SubRipItem(sub_L2.index, sub_L2.start, sub_L2.end, text)
            outs[level].append(item)
Example #4
0
def generate_srt_from_sjson(sjson_subs, speed):
    """Generate transcripts with speed = 1.0 from sjson to SubRip (*.srt).

    :param sjson_subs: "sjson" subs.
    :param speed: speed of `sjson_subs`.
    :returns: "srt" subs.
    """

    output = ''

    equal_len = len(sjson_subs['start']) == len(sjson_subs['end']) == len(
        sjson_subs['text'])
    if not equal_len:
        return output

    sjson_speed_1 = generate_subs(speed, 1, sjson_subs)

    for i in range(len(sjson_speed_1['start'])):
        item = SubRipItem(
            index=i,
            start=SubRipTime(milliseconds=sjson_speed_1['start'][i]),
            end=SubRipTime(milliseconds=sjson_speed_1['end'][i]),
            text=sjson_speed_1['text'][i])
        output += (unicode(item))
        output += '\n'
    return output
Example #5
0
    def process(self, subs: SubRipFile, items: List[PgsSubtitleItem],
                post_process, confidence: int, max_width: int):
        full_image = FullImage.from_items(items, self.gap, max_width)

        config = {'output_type': tess.Output.DICT, 'config': '--psm 11'}

        if self.pgs.language:
            config.update({'lang': self.pgs.language.alpha3})

        if self.omp_thread_limit:
            os.environ['OMP_THREAD_LIMIT'] = str(self.omp_thread_limit)
        # cv2.imwrite(f'{subs.path}-{len(items)}-{confidence}.png', full_image.data)
        data = TsvData(tess.image_to_data(full_image.data, **config))

        remaining = []
        for item in items:
            text = self.accept(data, item, confidence)
            if text is None:
                remaining.append(item)
                continue

            text = item.text
            if post_process:
                text = post_process(text)
            if text:
                item = SubRipItem(0, item.start, item.end, text)
                subs.append(item)

        return remaining
Example #6
0
    def refragment_with_min_duration(
            subs: List[SubRipItem],
            minimum_segment_duration: float) -> List[SubRipItem]:
        """Re-fragment a list of subtitle cues into new cues each of spans a minimum duration

        Arguments:
            subs {list} -- A list of SupRip cues.
            minimum_segment_duration {float} -- The minimum duration in seconds for each output subtitle cue.
        Returns:
            list -- A list of new SupRip cues after fragmentation.
        """
        new_segment = []
        new_segment_index = 0
        new_segment_duration = 0.0
        new_segment_text = ""
        new_subs = []
        for sub in subs:
            if minimum_segment_duration > new_segment_duration:
                new_segment.append(sub)
                new_segment_duration += MediaHelper.get_duration_in_seconds(
                    str(sub.start), str(sub.end)) or 0.0
                new_segment_text += "{}\n".format(sub.text)
            else:
                concatenated_item = SubRipItem(new_segment_index,
                                               new_segment[0].start,
                                               new_segment[-1].end,
                                               new_segment_text,
                                               new_segment[0].position)
                new_subs.append(concatenated_item)
                new_segment_index += 1
                new_segment = [sub]
                new_segment_duration = MediaHelper.get_duration_in_seconds(
                    str(sub.start), str(sub.end)) or 0.0
                new_segment_text = "{}\n".format(sub.text)
        if new_segment:
            concatenated_item = SubRipItem(new_segment_index,
                                           new_segment[0].start,
                                           new_segment[-1].end,
                                           new_segment_text,
                                           new_segment[0].position)
            new_subs.append(concatenated_item)
        return new_subs
Example #7
0
 def setUp(self):
     self.item = SubRipItem(1, text="Hello world !")
     self.item.shift(minutes=1)
     self.item.end.shift(seconds=20)
     self.string = u'1\n00:01:00,000 --> 00:01:20,000\nHello world !\n'
     self.bad_string = u'foobar'
     self.coordinates = (u'1\n00:01:00,000 --> 00:01:20,000 X1:000 X2:000 '
                             'Y1:050 Y2:100\nHello world !\n')
     self.vtt = (u'1\n00:01:00,000 --> 00:01:20,000 D:vertical A:start '
                             'L:12%\nHello world !\n')
     self.dots = u'1\n00:01:00.000 --> 00:01:20.000\nHello world !\n'
Example #8
0
 def setUp(self):
     self.item = SubRipItem(1, text="Hello world !")
     self.item.shift(minutes=1)
     self.item.end.shift(seconds=20)
     self.string = '1\n00:01:00,000 --> 00:01:20,000\nHello world !\n'
     self.bad_string = 'foobar'
     self.coordinates = ('1\n00:01:00,000 --> 00:01:20,000 X1:000 X2:000 '
                         'Y1:050 Y2:100\nHello world !\n')
     self.vtt = ('1\n00:01:00,000 --> 00:01:20,000 D:vertical A:start '
                 'L:12%\nHello world !\n')
     self.string_index = 'foo\n00:01:00,000 --> 00:01:20,000\nHello !\n'
     self.dots = '1\n00:01:00.000 --> 00:01:20.000\nHello world !\n'
     self.no_index = '00:01:00,000 --> 00:01:20,000\nHello world !\n'
     self.junk_after_timestamp = ('1\n00:01:00,000 --> 00:01:20,000?\n'
                                  'Hello world !\n')
Example #9
0
def get_captions(client_name, clip_id):
    h = httplib2.Http()
    g_url = 'http://%s/JSON.php?clip_id=%s' % ( client_name, clip_id)
    print "Fetching URL: %s" % g_url
    response, j = h.request(g_url)
    dirname = os.getcwd() + "/data/granicus/srt/%s/" % client_name
    filename = dirname + "%s.srt" % clip_id
    subs = SubRipFile()

    if response.get('status') == '200':
        captions = []
        try:
            j = json.loads(j, strict=False)[0]
        except ValueError:
            ts = re.sub('([{,]\s+)([a-z]+)(: ")', lambda s: '%s"%s"%s' % (s.groups()[0], s.groups()[1], s.groups()[2]), j).replace("\\", "")
            try:
                j = json.loads(ts, strict=False)[0]
            except UnicodeDecodeError:
                ts = unicode(ts, errors='ignore')
                j = json.loads(ts, strict=False)[0]
        except:
            j = False

        sub_count = 0
        for item in j: 
            if item["type"] == "text":
                cap = item["text"]
                offset = round(float(item["time"]), 3)
                captions.append({'time': offset, 'text': cap})        
                end = get_cap_end(j, sub_count)
                if end:
                    subtitle = SubRipItem(index=sub_count, start=SubRipTime(seconds=offset), end=SubRipTime(seconds=end), text=cap)
                    subs.append(subtitle)
           
            sub_count = sub_count + 1
        
        try:
            subs.save(path=filename, encoding="utf-8")
        except IOError:
            p = subprocess.Popen('mkdir -p %s' % dirname, shell=True, stdout=subprocess.PIPE)
            t = p.wait()

            subs.save(path=filename, encoding="utf-8")
            
        s3_url = push_to_s3(filename, '%s/%s.srt' % (client_name, clip_id))
        return (captions, s3_url)
    else:
        return ([], '')
Example #10
0
def dmxread_callback(frame, frame_no):
    global prevFrame, prevTime, subs, srtFile
    # if prevFrame. == 0:
    #     prevFrame = array(frame)
    frameArray = array(frame)
    if not array_equal(prevFrame,frameArray):
        if frame != None:
            item = SubRipItem(1, text="DMX1"+str(frame))
            item.shift(seconds=prevTime)
            item.end.shift(seconds=perf_counter()-prevTime)
            if VERBOSE:
                print(item)
            subs.append(item)
            srtFile.append(item)
            prevTime = perf_counter()
    prevFrame = array(frame)
Example #11
0
    def save(self, path):
        if path.endswith('srt'):
            verify_dependencies(['pysrt'])
            from pysrt import SubRipFile, SubRipItem
            from datetime import time

            out = SubRipFile()
            for elem in self._elements:
                start = time(*self._to_tup(elem.onset))
                end = time(*self._to_tup(elem.onset + elem.duration))
                out.append(SubRipItem(0, start, end, elem.text))
            out.save(path)
        else:
            with open(path, 'w') as f:
                f.write('onset\ttext\tduration\n')
                for elem in self._elements:
                    f.write('{}\t{}\t{}\n'.format(elem.onset, elem.text,
                                                  elem.duration))
Example #12
0
def play_record_dmx(unused_addr, args, value):
    global INTERVAL, TRANSITION_TIME, previous_time, dmxCounter, VERBOSE
    global prev_frame, prev_time, subs, srtFile, previous_dmx_time, DMX_INTERVAL, sub_incr
    global dmx

    dmx_array[int(args[0])] = int(value * 255)

    current_dmx_time = time.time()
    elapsed_dmx_time = current_dmx_time - previous_dmx_time

    if DEBUG:
        print("DMX time", current_dmx_time, previous_dmx_time,
              elapsed_dmx_time)

    if (elapsed_dmx_time > DMX_INTERVAL):
        frameArray = trim_zeros(dmx_array, 'b').astype('uint8')
        # frameArray = array(frameArray)
        print(array_equal(prev_frame, frameArray), tuple(prev_frame),
              tuple(frameArray))
        if not array_equal(prev_frame, frameArray):
            if frameArray.any() != None:
                item = SubRipItem(sub_incr,
                                  text="DMX1" +
                                  str(tuple(frameArray)[1:]).replace(" ", ""))
                item.shift(seconds=prev_time)
                item.end.shift(seconds=perf_counter() - prev_time)
                if VERBOSE:
                    print(item)
                subs.append(item)
                if srtFile != None:
                    srtFile.append(item)
                    # encoding="utf_8"
                    # srtFile.save(SRT_FILENAME, encoding=encoding)
                sub_incr += 1
                prev_time = perf_counter()
                if PLAY_DMX:
                    if DEBUG:
                        print("DMX tuple", tuple(frameArray)[1:])
                    dmx.write_frame(tuple(frameArray)[1:])
                if not array_equal(prev_frame, frameArray):
                    prev_frame = frameArray

        previous_dmx_time = time.time()
        print(previous_dmx_time)
Example #13
0
def merge_subtitle(sub_a, sub_b, delta, encoding='utf-8'):
    """
    合并两种不同言语的srt字幕

    因为两个字幕文件的时间轴不一样,所以合并后的字幕会在某一字幕文件转换时生成新的一条字幕,
    导致双语字幕并不是同时变化,不过这也是没有办法的事,无法避免

    参考https://github.com/byroot/pysrt/issues/17

    https://github.com/byroot/pysrt/issues/15

    :param sub_a: 使用sub_a = SubRipFile.open(sub_a_path, encoding=encoding)
    :param sub_b:
    :param delta:
    :return:
    """
    out = SubRipFile()
    intervals = [item.start.ordinal for item in sub_a]
    intervals.extend([item.end.ordinal for item in sub_a])
    intervals.extend([item.start.ordinal for item in sub_b])
    intervals.extend([item.end.ordinal for item in sub_b])
    intervals.sort()

    j = k = 0
    for i in xrange(1, len(intervals)):
        start = SubRipTime.from_ordinal(intervals[i - 1])
        end = SubRipTime.from_ordinal(intervals[i])

        if (end - start) > delta:
            text_a, j = find_subtitle(sub_a, start, end, j)
            text_b, k = find_subtitle(sub_b, start, end, k)

            text = join_lines(text_a, text_b)
            if len(text) > 0:
                item = SubRipItem(0, start, end, text)
                out.append(item)

    out.clean_indexes()
    return out
Example #14
0
    def generate_srt(self, text: str):
        """
        Generates .srt file with the given text and timestamps.
        :param text: String with all retrieved text.
        """
        self.create_subs_path()

        subs = open_srt(self.srt_path)
        texts = self.prepare_text(text.split(" "))
        timestamps = self.prepare_timestamps(texts)

        for i, (sentence, (start_timestamp,
                           end_timestamp)) in enumerate(zip(texts,
                                                            timestamps)):
            start_timestamp_list = [
                int(ts) for ts in start_timestamp.split(':')
            ]
            end_timestamp_list = [int(ts) for ts in end_timestamp.split(':')]

            sub = SubRipItem(index=i)
            sub.text = sentence

            sub.start = SubRipTime(hours=start_timestamp_list[0],
                                   minutes=start_timestamp_list[1],
                                   seconds=start_timestamp_list[2],
                                   milliseconds=start_timestamp_list[3])

            sub.end = SubRipTime(hours=end_timestamp_list[0],
                                 minutes=end_timestamp_list[1],
                                 seconds=end_timestamp_list[2],
                                 milliseconds=end_timestamp_list[3])

            subs.append(sub)

        # Saving result subtitles into file
        subs.save(self.srt_path, encoding='utf-8')

        logging.info(f"Generated subtitles are saved in {self.srt_path}")
Example #15
0
def merge_subtitle(sub_a, sub_b, delta):
    out = SubRipFile()
    intervals = [item.start.ordinal for item in sub_a]
    intervals.extend([item.end.ordinal for item in sub_a])
    intervals.extend([item.start.ordinal for item in sub_b])
    intervals.extend([item.end.ordinal for item in sub_b])
    intervals.sort()

    j = k = 0
    for i in range(1, len(intervals)):
        start = SubRipTime.from_ordinal(intervals[i - 1])
        end = SubRipTime.from_ordinal(intervals[i])

        if (end - start) > delta:
            text_a, j = find_subtitle(sub_a, start, end, j)
            text_b, k = find_subtitle(sub_b, start, end, k)

            text = join_lines(text_a, text_b)
            if len(text) > 0:
                item = SubRipItem(0, start, end, text)
                out.append(item)

    out.clean_indexes()
    return out
Example #16
0
 def setUp(self):
     self.item = SubRipItem()
Example #17
0
 def setUp(self):
     self.item = SubRipItem(1, text="Hello world !")
     self.item.shift(minutes=1)
     self.item.end.shift(seconds=20)
                      oauth,
                      chat_channel,
                      chat_server[0],
                      chat_server[1],
                      twitchclient_version=twitchclient_version)

outsrt = SubRipFile()

text = ''

while 1:
    raw_msg_list = bot.get_message()
    if len(raw_msg_list) > 0:
        if len(text) > 0:
            end = SubRipTime.from_time(datetime.now())
            item = SubRipItem(0, start, end, text)
            outsrt.append(item)
        start = SubRipTime.from_time(datetime.now())
        text = ''
        timestamp = get_timestamp(timestamp_format)
        for item in raw_msg_list:
            if record_raw:
                log_add(raw_log_path, timestamp + ' ' + item + '\n')
            username, message = irc_bot.parse_user(item)
            if username != '':
                safe_print(chat_channel + " " + username + ": " + message)
                log_add(log_path,
                        timestamp + ' ' + username + ': ' + message + '\n')
                text += username + ": " + message + '\n'
                outsrt.clean_indexes()
                outsrt.save(srt_log_path, encoding='utf-8')
Example #19
0
def play_record_hue(address: str, *args: List[Any]) -> None:
    global bridge, INTERVAL, TRANSITION_TIME, previous_time, hue_list, hue_cmds, RECORD, VERBOSE, MAX_BRIGHTNESS
    global prev_time, subs, srtFile, sub_incr, prev_cmds_str
    # print(hue_list)
    #print(len(args))
    #if not len(args) == 4 or type(args[0]) is not float or type(args[1]) is not float:
    #    return

    if DEBUG:
        print(len(args), args)

    # Check that address starts with filter
    #if not address[:-1] == "/hue":  # Cut off the last character
    #    return

    r = args[1]
    g = args[2]
    b = args[3]

    h, s, v = rgb_to_hsv(r, g, b)

    h *= 65535.0
    s *= 254.0
    v *= 254.0

    h = int(h)
    s = int(s)
    v = int(v * MAX_BRIGHTNESS / 254.0)

    # hue_n = args[0][0][-1]
    hue_n = int(args[0][0])
    print(hue_n, len(hue_list), hue_list)

    on = True
    if v <= 1:
        on = False

    cmd = {
        'transitiontime': int(TRANSITION_TIME),
        'on': on,
        'bri': int(v),
        'sat': int(s),
        'hue': int(h)
    }

    if len(hue_cmds) < hue_n:
        hue_cmds.append(cmd)
    else:
        hue_cmds[hue_n - 1] = cmd

    if int(hue_n) < len(hue_list) or not PLAY_HUE:

        # print(hue_n,hue_list[int(hue_n)],h,s,v)

        current_time = time.time()
        elapsed_time = current_time - previous_time

        if DEBUG:
            print("HUE time", current_time, previous_time, elapsed_time)

        # print(cmd, hue_list,hue_list[int(hue_n)])

        if (elapsed_time > INTERVAL):
            # if True:
            if PLAY_HUE:
                for hl in hue_list[int(hue_n)]:
                    if DEBUG:
                        print("---", hue_n, hl, h, s, v)
                    l = (h, s, v, int(TRANSITION_TIME))
                    bridge.set_light(hl, cmd)
            cmds_str = ""
            if DEBUG:
                print(hue_cmds)
            i = 0
            for c in hue_cmds:
                l = (hue_cmds[i]["hue"], hue_cmds[i]["sat"],
                     hue_cmds[i]["bri"], hue_cmds[i]["transitiontime"])
                if cmds_str == "":
                    cmds_str += "HUE" + str(i + 1) + str(l).replace(" ", "")
                else:
                    cmds_str += ";HUE" + str(i + 1) + str(l).replace(" ", "")
                i += 1
            if cmds_str != prev_cmds_str:
                item = SubRipItem(sub_incr, text=cmds_str)
                item.shift(seconds=prev_time)
                item.end.shift(seconds=perf_counter() - prev_time)
                # item.end.shift(seconds=prev_time+int(TRANSITION_TIME)/10.0)
                if VERBOSE:
                    print("---", item)
                subs.append(item)
                if srtFile != None:
                    srtFile.append(item)
                    encoding = "utf_8"
                    srtFile.save(SRT_FILENAME, encoding=encoding)
                sub_incr += 1
                prev_cmds_str = cmds_str
            prev_time = perf_counter()

            # bridge.set_light(int(hue_n), cmd)
            previous_time = time.time()
            if DEBUG:
                print("Hue commands", hue_cmds)
Example #20
0
def syncSrts(subs_L1, subs_L2):
    """Sync subs_L1 by subs_L2 timings and return a SubRipFile.
    """

    out = SubRipFile()
    subs_L2_out = SubRipFile()

    j = 0
    last_j = -1
    dupes = 0
    L2_ind = -1

    for L2_sub in subs_L2:
        L2_ind = L2_ind + 1
        start = L2_sub.start
        end = L2_sub.end
        j = matchSubtitle(subs_L1, start, end, max(last_j, 0))
        L1_sub = subs_L1[j] if (j > -1) else None

        if L1_sub is None:
            text = L2_sub.text
            print("---- Missing: {}: {}".format(
                L2_sub.index, L2_sub.text.replace("\n", "[[NL]]")))
        else:
            text = L1_sub.text

            if j - 1 > last_j and last_j > -1:
                # we skipped a sub in L1_subs
                if isSubMatch(subs_L1[j - 1], subs_L2[L2_ind - 1].start,
                              subs_L2[L2_ind - 1].end):
                    out[len(out) -
                        1].text = out[len(out) -
                                      1].text + "\n" + subs_L1[j - 1].text
                elif isSubMatch(subs_L1[j - 1], start, end):
                    text = subs_L1[j - 1].text + "\n" + text
                else:
                    # A sub line in L1 does not match any in L2
                    # We add it to synced L1, and add an empty one to subs L2
                    item = SubRipItem(0, subs_L1[j - 1].start,
                                      subs_L1[j - 1].end, subs_L1[j - 1].text)
                    out.append(item)
                    item2 = SubRipItem(0, subs_L1[j - 1].start,
                                       subs_L1[j - 1].end, " ")
                    subs_L2_out.append(item2)

            if j == last_j:
                dupes = dupes + 1
                #print("---- OOPS. {}: {} - {}".format(L2_sub.index, L2_sub.text.replace("\n",""), L1_sub.text.replace("\n","")))
            last_j = j

        item = SubRipItem(0, start, end, text)
        out.append(item)

        item2 = SubRipItem(0, start, end, L2_sub.text)
        subs_L2_out.append(item2)

    out.clean_indexes()
    subs_L2_out.clean_indexes()

    fixed = 0
    for i in range(1, len(out)):
        sub1 = out[i - 1].text
        sub2 = out[i].text
        if ((sub1 == sub2)
                and (subs_L2_out[i - 1].text != subs_L2_out[i].text)):
            if (trySplitLine(out, i, sub1)):
                fixed = fixed + 1
                i = i + 1
            else:
                print("---- Oy. {}: {} not fixed".format(
                    i, sub1.replace("\n", "[[NL]]")))

    return out, dupes, fixed, subs_L2_out
Example #21
0
srt = SubRipFile()

# get all DisplaySets that contain an image
print("Loading DisplaySets...")
allsets = [ds for ds in tqdm(pgs.iter_displaysets())]

print(f"Running OCR on {len(allsets)} DisplaySets and building SRT file...")
subText = ""
subStart = 0
subIndex = 0
for ds in tqdm(allsets):
    if ds.has_image:
        # get Palette Display Segment
        pds = ds.pds[0]
        # get Object Display Segment
        ods = ds.ods[0]

        # img = make_image(ods, pds)
        # subText = pytesseract.image_to_string(img)
        subStart = ods.presentation_timestamp
    else:
        startTime = SubRipTime(milliseconds=int(subStart))
        endTime = SubRipTime(
            milliseconds=int(ds.end[0].presentation_timestamp))
        srt.append(SubRipItem(subIndex, startTime, endTime, "subText"))
        subIndex += 1

print(f"Done. SRT file saved as {srtFile}")
srt.save(srtFile, encoding='utf-8')
Example #22
0
 def test_multiple_item(self):
     srt_file = SubRipFile([
         SubRipItem(1, {'seconds': 0}, {'seconds': 3}, 'Hello'),
         SubRipItem(1, {'seconds': 1}, {'seconds': 2}, 'World !')
     ])
     self.assertEquals(srt_file.text, 'Hello\nWorld !')
Example #23
0
 def test_single_item(self):
     srt_file = SubRipFile(
         [SubRipItem(1, {'seconds': 1}, {'seconds': 2}, 'Hello')])
     self.assertEquals(srt_file.text, 'Hello')
Example #24
0
 def test_shift(self):
     srt_file = SubRipFile([SubRipItem()])
     srt_file.shift(1, 1, 1, 1)
     self.assertEqual(srt_file[0].end, (1, 1, 1, 1))
     srt_file.shift(ratio=2)
     self.assertEqual(srt_file[0].end, (2, 2, 2, 2))
Example #25
0
def makeL1L2(L1_srt, L2_srt, out_srt, levels, save_sync, out_L1_utf8bom_srt, out_L2_utf8bom_srt, \
    show_L2, encoding, L1_color, L1_size, L2_color, L2_size):
    """
    Joins L1_srt and L2_srt subtitles and saves the result to out_srt.
    If save_sync is True, saves the synced srt files.
    If out_L1_utf8bom_srt is not empty, saves the L1 srt file converted to utf8-BOM to that path.
    If out_L2_utf8bom_srt is not empty, saves the L2 srt file converted to utf8-BOM to that path.
    If L1_color, L1_size, L2_color, L2_size are given, the subs are formatted accordingly
    """

    log("L1_srt: " + L1_srt)
    log("L2_srt: " + L2_srt)
    log("show_L2: " + show_L2)
    log("encoding: " + encoding)
    log("save_sync: ", save_sync)
    log("levels: ", levels)
    log("L1 color: {}, size: {}.".format(L1_color, L1_size))
    log("L2 color: {}, size: {}.".format(L2_color, L2_size))
    log("out_L1_utf8bom_srt: ", out_L1_utf8bom_srt)
    log("out_L2_utf8bom_srt: ", out_L2_utf8bom_srt)

    setSrtTemplates(L1_color, L1_size, L2_color, L2_size)

    # try to decode and save as utf8-bom
    L1_srt_bom = L1_srt + ".utf8bom"
    L2_srt_bom = L2_srt + ".utf8bom"

    makeFileUtf8Bom(L1_srt, L1_srt_bom)
    makeFileUtf8Bom(L2_srt, L2_srt_bom)

    subs_L1_orig = SubRipFile.open(L1_srt_bom)
    subs_L2_orig = SubRipFile.open(L2_srt_bom)

    subs_L1, dupes, fixed, subs_L2 = syncSrts(subs_L1_orig, subs_L2_orig)

    if save_sync:
        out_synced_L1 = L1_srt.replace(".srt", ".synced.srt")
        out_synced_L2 = L2_srt.replace(".srt", ".synced.srt")

        subs_L1.save(out_synced_L1, encoding=encoding)
        subs_L2.save(out_synced_L2, encoding=encoding)
        log("Saved {} and {}. Duplicate lines: {} Fixed: {}".format(
            out_synced_L1, out_synced_L2, dupes, fixed))

    outs = {}
    removed_lines = {}
    out_srts = {}
    for level in levels:
        out_srts[level] = out_srt.replace("{{LEVEL}}", level)
        outs[level] = SubRipFile()
        removed_lines[level] = 0

    for i in range(0, len(subs_L2)):
        processSub(subs_L1[i], subs_L2[i], levels, outs, removed_lines,
                   show_L2)

    for level in levels:
        summary = "level_criteria: {}. Hidden L1 lines: {} out of {}".format(
            level_criterias[level] if level != "0" else 'none',
            removed_lines[level], len(subs_L2))
        summaryItem = SubRipItem(1, {'milliseconds': 0}, {'milliseconds': 1},
                                 summary)
        outs[level].append(summaryItem)
        outs[level].clean_indexes()
        outs[level].save(path=out_srts[level], encoding=encoding)
        log("Saved {}. {} ".format(out_srts[level], summary))

    if (out_L1_utf8bom_srt):
        if os.path.isfile(out_L1_utf8bom_srt):
            os.remove(out_L1_utf8bom_srt)
        os.rename(L1_srt_bom, out_L1_utf8bom_srt)
    else:
        os.remove(L1_srt_bom)

    if (out_L2_utf8bom_srt):
        if os.path.isfile(out_L2_utf8bom_srt):
            os.remove(out_L2_utf8bom_srt)
        os.rename(L2_srt_bom, out_L2_utf8bom_srt)
    else:
        os.remove(L2_srt_bom)
Example #26
0
def handle_tracks(tracks, start, end, fps, srt_filename):
    global XML_FILENAME, HUE_SAMPLING, DMX_SAMPLING, TRANSITION_TIME, DEBUG, VERBOSE
    track_list = []
    for track in tracks:
        track_list = handle_track_list(track, start, end, fps)
        # print(track_list[3][0])
        # try:
        #     print(len(track_list[3]),len(track_list[3][0]),track_list[3][0][1:10],track_list[3][-1][1:10])
        # except:
        #     pass

    # srt_file = open(srt_filename,"w")

    dmx_frame = zeros(512)
    prev_dmx_frame = zeros(512)
    prev_dmx_valid_frame = zeros(512)

    subrip_file = SubRipFile(path=srt_filename)

    print(40 * "-")
    print("Processing frames")
    print(40 * "-")
    # print(track_list[3][1])
    # print(len(track_list[1]))

    if len(track_list[1]) > 0:
        # If there isn't only an audio track
        # print(track_list[1][0])
        # print(track_list[1][0]!="audio")
        # print(len(track_list[1]) != 1 and track_list[1][0]!="audio")
        if (len(track_list[1]) != 1 or track_list[1][0] != "audio"):
            print("Number of lighting events: ", len(track_list[3][0]))
            frame_no = 0
            for i in range(len(track_list[3][0])):
                # frame_no = track_list[4][i]
                frame_no = i
                t = i * (1.0 / float(fps))
                if VERBOSE:
                    print(40 * "-")
                    # print(frame_no,fps)
                    print("Frame %s / time %s seconds" % (frame_no, t))
                    print(40 * "-")
                hue_cmd = ""
                dmx_cmd = ""
                # for the bug, len(of track_list[0]) is greater than
                # len(track_list[3])
                for j in range(len(track_list[0])):
                    # print(track_list[1][j])
                    if track_list[1][j] != "audio":
                        name = track_list[0][j]
                        type = track_list[1][j]
                        addr = track_list[2][j]
                        # print(name,type,addr)
                        # TODO: if frame_no = i as on line 181, the following line fails!
                        # [3][j] is out of range therefore j is the problem
                        try:
                            payload = track_list[3][j][i]
                        except Exception as e:
                            print(
                                'ERROR: could not get payload, len(of track_list[0]) is likely greater than \
                            len (track_list[3])')
                        # print(name, type, addr, payload)
                        # Convert Hue payload to hue command
                        if payload != "":
                            if addr[1:4].lower(
                            ) == "hue" and type == "OSCColor/floatarray":
                                if VERBOSE:
                                    print("hue", addr, payload)
                                r, g, b, a = 0, 0, 0, 0
                                try:
                                    payload_list = payload.split(",")
                                    # print(payload_list)
                                    if len(payload_list) == 3:
                                        r, g, b = payload_list
                                    elif len(payload_list) == 4:
                                        r, g, b, a = payload_list
                                except Exception as e:
                                    print(e)

                                h, s, v = rgb_to_hsv(float(r), float(g),
                                                     float(b))

                                h *= 65535.0
                                s *= 254.0
                                v *= 254.0

                                h = int(h)
                                s = int(s)
                                v = int(v)
                                # print("hue", addr, payload, h,s,v)
                                n = int(addr[4:])
                                # print("hue", n, h,s,v)
                                if len(hue_cmd) == 0:
                                    hue_cmd += "HUE%s(%s,%s,%s,%s)" % (
                                        n, h, s, v, TRANSITION_TIME)
                                else:
                                    hue_cmd += ";HUE%s(%s,%s,%s,%s)" % (
                                        n, h, s, v, TRANSITION_TIME)
                            # Convert single DMX channel to command
                            elif addr[1:4].lower(
                            ) == "dmx" and type == "OSCValue/float":
                                if VERBOSE:
                                    print("dmx value", addr, payload)
                                n = int(addr[4:])
                                if payload != "":
                                    dmx_frame[int(n)] = int(
                                        float(payload) * 254)
                            # Convert multiple DMX channels to command
                            elif addr[1:4].lower() == "dmx" and (
                                    type == "OSCColor/floatarray"
                                    or type == "OSCValue/standard"):
                                if VERBOSE:
                                    print("dmx colour", addr, payload)
                                n = int(addr[4:])
                                if payload != "":
                                    payload_list = payload.split(",")
                                    for channel in payload_list:
                                        dmx_frame[int(n)] = int(
                                            float(channel) * 254)
                                        n += 1

                # Output HUE commands
                # hue_t = frame_no * (1.0/HUE_SAMPLING)
                if frame_no % fps == 0 and hue_cmd != "":
                    item = SubRipItem(frame_no, text=hue_cmd)
                    item.shift(seconds=t)
                    item.end.shift(seconds=1)
                    if VERBOSE:
                        print(item)
                    else:
                        print("h", end="")
                        stdout.flush()
                    subrip_file.append(item)
                    frame_no += 1

                # Output DMX command
                dmx_frame_trimmed = trim_zeros(dmx_frame, 'b').astype('uint8')

                # print("dmx_frame_trimmed before",dmx_frame_trimmed)

                # if len(dmx_frame_trimmed)==0:
                #     dmx_frame_trimmed = zeros(512)

                # print("dmx_frame_trimmed after",dmx_frame_trimmed)

                dmx_cmd = "DMX1" + str(tuple(dmx_frame_trimmed)[1:]).replace(
                    " ", "")

                if VERBOSE:
                    print('dmx_cmd to be written: ', dmx_cmd)

                # cmd = hue_cmd + ";" + dmx_cmd
                if (not array_equal(dmx_frame_trimmed,
                                    prev_dmx_frame)) or (frame_no % fps == 0):
                    # if frame_no % fps == 0 and dmx_cmd=="":
                    # if frame_no % fps == 0:
                    #     print(dmx_cmd, prev_dmx_frame)

                    # Fix for and empty DMX command
                    # Usually found at the start of a treatment track
                    if dmx_cmd == "DMX1()":
                        item = dmx_cmd = "DMX1" + str(
                            tuple(zeros(512, dtype=int))[1:]).replace(" ", "")

                    item = SubRipItem(frame_no, text=dmx_cmd)
                    item.shift(seconds=t)
                    item.end.shift(seconds=1.0 / fps)

                    if VERBOSE:
                        print(item)
                    else:
                        print("d", end="")
                        stdout.flush()

                    subrip_file.append(item)
                    frame_no += 1
                prev_dmx_frame = dmx_frame_trimmed
                # print(cmd)
                if VERBOSE:
                    print(40 * "-")
                    # print(track_list[0][j], track_list[1][j], track_list[2][j], track_list[3][j][i])
                    # print(frame)
                    # j = 1
                    # for frame in track:
                    #     print(track_list[0][i] + " " +frame, end = " ")
                    #     j += 1
                    # print()
    encoding = "utf_8"
    subrip_file.save(srt_filename, encoding=encoding)
    print()