コード例 #1
0
    def get_audio_segment_starts_and_ends(
        subs: List[SubRipItem]
    ) -> Tuple[List[str], List[str], List[SubRipFile]]:
        """Group subtitle cues into larger segments in terms of silence gaps.

        Arguments:
            subs {list} -- A list of SupRip cues.

        Returns:
            tuple -- A list of start times, a list of end times and a list of grouped SubRip files.
        """

        local_subs = MediaHelper.__preprocess_subs(subs)

        segment_starts = []
        segment_ends = []
        combined = []
        new_subs = []
        current_start = str(local_subs[0].start)

        for i in range(len(local_subs)):
            if i == len(local_subs) - 1:
                combined.append(local_subs[i])
                segment_starts.append(current_start)
                segment_ends.append(str(local_subs[i].end))
                new_subs.append(SubRipFile(combined))
                del combined[:]
            else:
                # Do not segment when the subtitle is too short
                duration = FeatureEmbedder.time_to_sec(
                    local_subs[i].end) - FeatureEmbedder.time_to_sec(
                        local_subs[i].start)
                if duration < MediaHelper.__MIN_SECS_PER_WORD:
                    combined.append(local_subs[i])
                    continue
                # Do not segment consecutive subtitles having little or no gap.
                gap = FeatureEmbedder.time_to_sec(
                    local_subs[i + 1].start) - FeatureEmbedder.time_to_sec(
                        local_subs[i].end)
                if (local_subs[i].end == local_subs[i + 1].start
                        or gap < MediaHelper.__MIN_GAP_IN_SECS):
                    combined.append(local_subs[i])
                    continue
                combined.append(local_subs[i])
                # The start time is set to last cue's end time
                segment_starts.append(current_start)
                # The end time cannot be set to next cue's start time due to possible overlay
                segment_ends.append(str(local_subs[i].end))
                current_start = str(local_subs[i].end)
                new_subs.append(SubRipFile(combined))
                del combined[:]
        return segment_starts, segment_ends, new_subs
コード例 #2
0
    def rip(self, post_process: Callable[[str], str]):
        subs = SubRipFile(
            path=str(self.pgs.media_path.translate(extension='srt')))
        items = self.pgs.items
        confidence = self.confidence
        max_width = self.max_tess_width
        previous_size = len(items)
        while previous_size > 0:
            items = self.process(subs, items, post_process, confidence,
                                 max_width)
            if not items:
                break

            current_size = len(items)
            if current_size < 20:
                max_width = min(
                    sum([item.width + self.gap[1] for item in items]),
                    self.max_tess_width)
                confidence = 0
                remaining_items = self.process(subs, items, post_process,
                                               confidence, max_width)
                if remaining_items:
                    logger.warning(
                        f'Subtitles were not ripped: {remaining_items!r}')
                break
            elif current_size > previous_size * 0.8:
                max_width = min(
                    sum([item.width + self.gap[1]
                         for item in items]), self.max_tess_width) // 2
                confidence = max(0, confidence - 5)
            previous_size = current_size

        subs.clean_indexes()

        return subs
コード例 #3
0
    def __init__(self, filename):
        self.filename = filename

        self.model = Gtk.ListStore(object, str)
        self.srt_model = []
        if not os.path.exists(filename):
            raise (FileNameError(filename))

        try:
            self.srt_model = SubRipFile.open(path=filename)
        except UnicodeDecodeError as unic:
            debug(unic)
            try:
                info("trying ...", "ISO-8859-1")
                self.srt_model = SubRipFile(path=filename,
                                            encoding="iso-8859-1")
            except Exception as excep:
                debug(excep)
                self.model = None
        except IOError as error:
            info(
                "Impossible de lire le fichier de sous titre: error {}".format(
                    error))

        for line in self.srt_model:
            # print("appending",line)
            self.model.append([line, line.text])
コード例 #4
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
コード例 #5
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 ([], '')
コード例 #6
0
ファイル: text.py プロジェクト: hugovk/pliers
    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))
コード例 #7
0
ファイル: subtitle.py プロジェクト: cash2one/AutoSystem
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
コード例 #8
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
コード例 #9
0
ファイル: test_srtfile.py プロジェクト: zhiweizhong/pysrt
 def test_single_item(self):
     srt_file = SubRipFile(
         [SubRipItem(1, {'seconds': 1}, {'seconds': 2}, 'Hello')])
     self.assertEquals(srt_file.text, 'Hello')
コード例 #10
0
ファイル: test_srtfile.py プロジェクト: zhiweizhong/pysrt
 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))
コード例 #11
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()
コード例 #12
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)
コード例 #13
0
    def __save_subtitle_by_extension(file_extension: str,
                                     subs: List[SubRipItem],
                                     source_file_path: str,
                                     target_file_path: str,
                                     encoding: str,
                                     frame_rate: Optional[float],
                                     is_exporting: bool = False):
        if file_extension in Subtitle.SUBRIP_EXTENTIONS:
            SubRipFile(subs).save(target_file_path, encoding=encoding)
            Utils.remove_trailing_newlines(target_file_path, encoding)
        elif file_extension in Subtitle.TTML_EXTENSIONS:
            if is_exporting:
                tree = ElementTree.parse(source_file_path)
                tt = tree.getroot()
                cues = (tt.find("tt:body", Subtitle.TT_NS).find(
                    "tt:div",
                    Subtitle.TT_NS).findall("tt:p",
                                            Subtitle.TT_NS))  # type: ignore
                for index, cue in enumerate(cues):
                    cue.attrib["begin"] = str(subs[index].start).replace(
                        ",", ".")
                    cue.attrib["end"] = str(subs[index].end).replace(",", ".")

                # Change single quotes in the XML header to double quotes
                with open(target_file_path, "w", encoding=encoding) as target:
                    if "xml_declaration" in inspect.getfullargspec(
                            ElementTree.tostring
                    ).kwonlyargs:  # for >= python 3.8
                        encoded = ElementTree.tostring(tt,
                                                       encoding=encoding,
                                                       method="xml",
                                                       xml_declaration=True)
                    else:
                        encoded = ElementTree.tostring(tt,
                                                       encoding=encoding,
                                                       method="xml")
                    normalised = encoded.decode(encoding) \
                        .replace("<?xml version='1.0' encoding='", '<?xml version="1.0" encoding="',) \
                        .replace("'?>", '"?>')
                    target.write(normalised)
            else:
                try:
                    _, path = tempfile.mkstemp()
                    SubRipFile(subs).save(path, encoding=encoding)
                    Utils.srt2ttml(path, target_file_path)
                finally:
                    os.remove(path)
        elif file_extension in Subtitle.WEBVTT_EXTENSIONS:
            try:
                _, path = tempfile.mkstemp()
                SubRipFile(subs).save(path, encoding=encoding)
                Utils.srt2vtt(path, target_file_path)
            finally:
                os.remove(path)
        elif file_extension in Subtitle.SSA_EXTENTIONS:
            try:
                _, path = tempfile.mkstemp()
                SubRipFile(subs).save(path, encoding=encoding)
                Utils.srt2ssa(path, target_file_path)
            finally:
                os.remove(path)
        elif file_extension in Subtitle.ADVANCED_SSA_EXTENTIONS:
            try:
                _, path = tempfile.mkstemp()
                SubRipFile(subs).save(path, encoding=encoding)
                Utils.srt2ass(path, target_file_path)
            finally:
                os.remove(path)
        elif file_extension in Subtitle.MICRODVD_EXTENSIONS:
            try:
                _, path = tempfile.mkstemp()
                SubRipFile(subs).save(path, encoding=encoding)
                Utils.srt2microdvd(path,
                                   target_file_path,
                                   frame_rate=frame_rate)
            finally:
                os.remove(path)
        elif file_extension in Subtitle.MPL2_EXTENSIONS:
            try:
                _, path = tempfile.mkstemp()
                SubRipFile(subs).save(path, encoding=encoding)
                Utils.srt2mpl2(path, target_file_path)
            finally:
                os.remove(path)
        elif file_extension in Subtitle.TMP_EXTENSIONS:
            try:
                _, path = tempfile.mkstemp()
                SubRipFile(subs).save(path, encoding=encoding)
                Utils.srt2tmp(path, target_file_path)
            finally:
                os.remove(path)
        elif file_extension in Subtitle.SAMI_EXTENSIONS:
            try:
                _, path = tempfile.mkstemp()
                SubRipFile(subs).save(path, encoding=encoding)
                Utils.srt2sami(path, target_file_path)
            finally:
                os.remove(path)
        elif file_extension in Subtitle.STL_EXTENSIONS:
            try:
                _, path = tempfile.mkstemp()
                SubRipFile(subs).save(target_file_path, encoding=encoding)
            finally:
                os.remove(path)
        else:
            raise UnsupportedFormatException(
                "Unknown subtitle format for file: {}".format(
                    source_file_path))
コード例 #14
0
import math, time, datetime
from numpy import array, zeros, array_equal
# import pysrt
import signal
import sys

from random import random
from time import sleep, time, perf_counter
# from sched import scheduler
from pysrt import SubRipFile, SubRipItem, SubRipTime
from pysrt import open as srtopen
from threading import Timer

from tf_device_ids import deviceIdentifiersList

srtFile = SubRipFile()

tfIDs = []

tfConnect = True

prevFrame = zeros(512)
prevTime = 0
subs = []

ipcon = IPConnection()

# if tfConnect:
#     tfIDs = []

deviceIDs = [i[0] for i in deviceIdentifiersList]
コード例 #15
0
ファイル: test_srtfile.py プロジェクト: zhiweizhong/pysrt
 def setUp(self):
     self.file = SubRipFile()
コード例 #16
0
ファイル: test_srtfile.py プロジェクト: zhiweizhong/pysrt
 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 !')
コード例 #17
0
ファイル: subtitle.py プロジェクト: sjtuxiaoming/subaligner
    def export_subtitle(source_file_path,
                        subs,
                        target_file_path,
                        frame_rate=25.0):
        """Export subtitle in the format determined by the file extension.

        Arguments:
            source_file_path {string} -- The path to the original subtitle file.
            subs {list} -- A list of SubRipItems.
            target_file_path {string} -- The path to the exported subtitle file.
            frame_rate {float} -- The frame rate for frame-based subtitle formats {default: 25.0}.
        """

        filename, file_extension = os.path.splitext(source_file_path.lower())
        if file_extension in Subtitle.SUBRIP_EXTENTIONS:
            SubRipFile(subs).save(target_file_path, encoding="utf8")
            Utils.remove_trailing_newlines(target_file_path)
        elif file_extension in Subtitle.TTML_EXTENSIONS:
            tree = ElementTree.parse(source_file_path)
            tt = tree.getroot()
            cues = (tt.find("tt:body", Subtitle.TT_NS).find(
                "tt:div", Subtitle.TT_NS).findall("tt:p", Subtitle.TT_NS))
            for index, cue in enumerate(cues):
                cue.attrib["begin"] = str(subs[index].start).replace(",", ".")
                cue.attrib["end"] = str(subs[index].end).replace(",", ".")

            # Change single quotes in the XML header to double quotes
            with open(target_file_path, "w", encoding="utf8") as target:
                normalised = (ElementTree.tostring(
                    tt, encoding="utf8", method="xml").decode("utf-8").replace(
                        "<?xml version='1.0' encoding='utf8'?>",
                        '<?xml version="1.0" encoding="utf8"?>',
                    ))
                target.write(normalised)
        elif file_extension in Subtitle.WEBVTT_EXTENSIONS:
            try:
                _, path = tempfile.mkstemp()
                SubRipFile(subs).save(path, encoding="utf8")
                Utils.srt2vtt(path, target_file_path)
            finally:
                os.remove(path)
        elif file_extension in Subtitle.SSA_EXTENTIONS:
            try:
                _, path = tempfile.mkstemp()
                SubRipFile(subs).save(path, encoding="utf8")
                Utils.srt2ssa(path, target_file_path)
            finally:
                os.remove(path)
        elif file_extension in Subtitle.ADVANCED_SSA_EXTENTIONS:
            try:
                _, path = tempfile.mkstemp()
                SubRipFile(subs).save(path, encoding="utf8")
                Utils.srt2ass(path, target_file_path)
            finally:
                os.remove(path)
        elif file_extension in Subtitle.MICRODVD_EXTENSIONS:
            try:
                _, path = tempfile.mkstemp()
                SubRipFile(subs).save(path, encoding="utf8")
                Utils.srt2microdvd(path,
                                   target_file_path,
                                   frame_rate=frame_rate)
            finally:
                os.remove(path)
        elif file_extension in Subtitle.MPL2_EXTENSIONS:
            try:
                _, path = tempfile.mkstemp()
                SubRipFile(subs).save(path, encoding="utf8")
                Utils.srt2mpl2(path, target_file_path)
            finally:
                os.remove(path)
        elif file_extension in Subtitle.TMP_EXTENSIONS:
            try:
                _, path = tempfile.mkstemp()
                SubRipFile(subs).save(path, encoding="utf8")
                Utils.srt2tmp(path, target_file_path)
            finally:
                os.remove(path)
        else:
            raise UnsupportedFormatException(
                "Unknown subtitle format for file: {}".format(
                    source_file_path))
コード例 #18
0
def main():
    global hue_list, bridge, SRT_FILENAME, HUE_IP_ADDRESS, MAX_BRIGHTNESS
    global DMX_INTERVAL, INTERVAL, TRANSITION_TIME, HUE_IP_ADDRESS, DEBUG, VERBOSE
    global subs, srtFile
    global ipcon, tfIDs, dmx

    f1 = Figlet(font='standard')
    print(f1.renderText('LushRoom'))
    f2 = Figlet(font='standard')
    print(f2.renderText('OSC live record'))

    parser = argparse.ArgumentParser()
    parser.add_argument("--ip",
                        default="127.0.0.1",
                        help="OSC ip address to listen to")
    parser.add_argument("--port",
                        type=int,
                        default=8000,
                        help="OSC port to listen to")
    parser.add_argument("-s",
                        "--srt",
                        default=SRT_FILENAME,
                        help=".srt file name for lighting events")
    parser.add_argument("-b",
                        "--brightness",
                        default=MAX_BRIGHTNESS,
                        help="maximum brightness")
    parser.add_argument("-i",
                        "--interval",
                        default=INTERVAL,
                        help="sampling interval for Philips Hue events")
    parser.add_argument("-d",
                        "--dmx_interval",
                        default=DMX_INTERVAL,
                        help="sampling interval for DMX events")
    parser.add_argument("-t",
                        "--transition_time",
                        default=TRANSITION_TIME,
                        help="transition time between Philips Hue events")
    parser.add_argument("--hue",
                        default=HUE_IP_ADDRESS,
                        help="Philips Hue bridge IP address")

    args = parser.parse_args()

    print(args)

    MAX_BRIGHTNESS = int(args.brightness)
    SRT_FILENAME = args.srt
    INTERVAL = float(args.interval)
    DMX_INTERVAL = float(args.dmx_interval)
    TRANSITION_TIME = float(args.transition_time)
    HUE_IP_ADDRESS = args.hue
    # VERBOSE = args.verbose
    # DEBUG = args.debug

    if SRT_FILENAME != "":
        print("Start recording the %s subtitles track for light events." %
              SRT_FILENAME)
        srtFile = SubRipFile(path=SRT_FILENAME)

    if PLAY_HUE:
        bridge = Bridge(HUE_IP_ADDRESS)
        bridge.connect()
        bridge.get_api()
        lights = bridge.lights
        for l in lights:
            print(l.name)
        for l in lights:
            l.on = True
            l.brightness = MAX_BRIGHTNESS

        light_names = bridge.get_light_objects('name')
        print("Light names:", light_names)

        if PLAY_HUE:
            hue_list = hue_build_lookup_table(lights)
        # else:
        #     hue_list = [[0],['1'],[2],[3],[4],[5],[6],[7],[8],[9]]
        print(hue_list)

    if PLAY_DMX:

        ipcon.connect(HOST, PORT)

        # Register Enumerate Callback
        ipcon.register_callback(IPConnection.CALLBACK_ENUMERATE, cb_enumerate)

        # Trigger Enumerate
        ipcon.enumerate()

        sleep(2)

        if DEBUG:
            print(tfIDs)

        dmxcount = 0
        for tf in tfIDs:
            # try:
            if True:
                # print(len(tf[0]))

                if len(
                        tf[0]
                ) <= 3:  # if the device UID is 3 characters it is a bricklet
                    if tf[1] in deviceIDs:
                        if VERBOSE:
                            print(tf[0], tf[1], getIdentifier(tf))
                    if tf[1] == 285:  # DMX Bricklet
                        if dmxcount == 0:
                            print(
                                "Registering %s as slave DMX device for capturing DMX frames"
                                % tf[0])
                            dmx = BrickletDMX(tf[0], ipcon)
                            dmx.set_dmx_mode(dmx.DMX_MODE_MASTER)
                            # channels = int((int(MAX_BRIGHTNESS)/255.0)*ones(512,)*255)
                            # dmx.write_frame([255,255])
                            sleep(1)
                            # channels = int((int(MAX_BRIGHTNESS)/255.0)*zeros(512,)*255)
                            # dmx.write_frame(channels)
                        dmxcount += 1

    disp = dispatcher.Dispatcher()
    # print(dir(dispatcher))

    for h in range(512):
        disp.map("/hue%s" % h, play_record_hue, "%s" % h)

    for h in range(512):
        disp.map("/dmx%s" % h, play_record_dmx, "%s" % h)

    server = osc_server.ThreadingOSCUDPServer((args.ip, args.port), disp)
    print("Serving OSC on {}".format(server.server_address))
    signal.signal(signal.SIGINT, signal_handler)
    server.serve_forever()
コード例 #19
0
ファイル: test_srtfile.py プロジェクト: zhiweizhong/pysrt
 def setUp(self):
     self.duck = SubRipFile()
コード例 #20
0
    def save_subs_as_target_format(subs: List[SubRipItem], source_file_path: str, target_file_path: str) -> None:
        """Save SubRipItems with the format determined by the target file extension.

        Arguments:
            subs {list} -- A list of SubRipItems.
            source_file_path {string} -- The path to the original subtitle file.
            target_file_path {string} -- The path to the output subtitle file.
        """

        encoding = Utils.detect_encoding(source_file_path)
        _, file_extension = os.path.splitext(target_file_path.lower())
        if file_extension in Subtitle.SUBRIP_EXTENTIONS:
            SubRipFile(subs).save(target_file_path, encoding=encoding)
            Utils.remove_trailing_newlines(target_file_path, encoding)
        elif file_extension in Subtitle.TTML_EXTENSIONS:
            try:
                _, path = tempfile.mkstemp()
                SubRipFile(subs).save(path, encoding=encoding)
                Utils.srt2ttml(path, target_file_path)
            finally:
                os.remove(path)
        elif file_extension in Subtitle.WEBVTT_EXTENSIONS:
            try:
                _, path = tempfile.mkstemp()
                SubRipFile(subs).save(path, encoding=encoding)
                Utils.srt2vtt(path, target_file_path)
            finally:
                os.remove(path)
        elif file_extension in Subtitle.SSA_EXTENTIONS:
            try:
                _, path = tempfile.mkstemp()
                SubRipFile(subs).save(path, encoding=encoding)
                Utils.srt2ssa(path, target_file_path)
            finally:
                os.remove(path)
        elif file_extension in Subtitle.ADVANCED_SSA_EXTENTIONS:
            try:
                _, path = tempfile.mkstemp()
                SubRipFile(subs).save(path, encoding=encoding)
                Utils.srt2ass(path, target_file_path)
            finally:
                os.remove(path)
        elif file_extension in Subtitle.MICRODVD_EXTENSIONS:
            try:
                _, path = tempfile.mkstemp()
                SubRipFile(subs).save(path, encoding=encoding)
                Utils.srt2microdvd(path, target_file_path)
            finally:
                os.remove(path)
        elif file_extension in Subtitle.MPL2_EXTENSIONS:
            try:
                _, path = tempfile.mkstemp()
                SubRipFile(subs).save(path, encoding=encoding)
                Utils.srt2mpl2(path, target_file_path)
            finally:
                os.remove(path)
        elif file_extension in Subtitle.TMP_EXTENSIONS:
            try:
                _, path = tempfile.mkstemp()
                SubRipFile(subs).save(path, encoding=encoding)
                Utils.srt2tmp(path, target_file_path)
            finally:
                os.remove(path)
        elif file_extension in Subtitle.SAMI_EXTENSIONS:
            try:
                _, path = tempfile.mkstemp()
                SubRipFile(subs).save(path, encoding=encoding)
                Utils.srt2sami(path, target_file_path)
            finally:
                os.remove(path)
        elif file_extension in Subtitle.STL_EXTENSIONS:
            try:
                _, path = tempfile.mkstemp()
                SubRipFile(subs).save(target_file_path, encoding=encoding)
            finally:
                os.remove(path)
        else:
            raise UnsupportedFormatException(
                "Unknown subtitle format for file: {}".format(source_file_path)
            )
コード例 #21
0
ファイル: test_srtfile.py プロジェクト: zhiweizhong/pysrt
 def test_default_value(self):
     self.assertEqual(self.file.eol, os.linesep)
     srt_file = SubRipFile(eol='\r\n')
     self.assertEqual(srt_file.eol, '\r\n')
コード例 #22
0
    def export_subtitle(source_file_path: str, subs: List[SubRipItem], target_file_path: str, frame_rate: float = 25.0) -> None:
        """Export subtitle in the format determined by the file extension.

        Arguments:
            source_file_path {string} -- The path to the original subtitle file.
            subs {list} -- A list of SubRipItems.
            target_file_path {string} -- The path to the exported subtitle file.
            frame_rate {float} -- The frame rate for frame-based subtitle formats {default: 25.0}.
        """

        encoding = Utils.detect_encoding(source_file_path)
        _, file_extension = os.path.splitext(source_file_path.lower())
        if file_extension in Subtitle.SUBRIP_EXTENTIONS:
            SubRipFile(subs).save(target_file_path, encoding=encoding)
            Utils.remove_trailing_newlines(target_file_path, encoding)
        elif file_extension in Subtitle.TTML_EXTENSIONS:
            tree = ElementTree.parse(source_file_path)
            tt = tree.getroot()
            cues = (tt.find("tt:body", Subtitle.TT_NS).find("tt:div", Subtitle.TT_NS).findall("tt:p", Subtitle.TT_NS))  # type: ignore
            for index, cue in enumerate(cues):
                cue.attrib["begin"] = str(subs[index].start).replace(",", ".")
                cue.attrib["end"] = str(subs[index].end).replace(",", ".")

            # Change single quotes in the XML header to double quotes
            with open(target_file_path, "w", encoding=encoding) as target:
                if "xml_declaration" in inspect.getfullargspec(ElementTree.tostring).kwonlyargs:  # for >= python 3.8
                    encoded = ElementTree.tostring(tt, encoding=encoding, method="xml", xml_declaration=True)
                else:
                    encoded = ElementTree.tostring(tt, encoding=encoding, method="xml")
                normalised = encoded.decode(encoding) \
                    .replace("<?xml version='1.0' encoding='", '<?xml version="1.0" encoding="',) \
                    .replace("'?>", '"?>')
                target.write(normalised)
        elif file_extension in Subtitle.WEBVTT_EXTENSIONS:
            try:
                _, path = tempfile.mkstemp()
                SubRipFile(subs).save(path, encoding=encoding)
                Utils.srt2vtt(path, target_file_path)
            finally:
                os.remove(path)
        elif file_extension in Subtitle.SSA_EXTENTIONS:
            try:
                _, path = tempfile.mkstemp()
                SubRipFile(subs).save(path, encoding=encoding)
                Utils.srt2ssa(path, target_file_path)
            finally:
                os.remove(path)
        elif file_extension in Subtitle.ADVANCED_SSA_EXTENTIONS:
            try:
                _, path = tempfile.mkstemp()
                SubRipFile(subs).save(path, encoding=encoding)
                Utils.srt2ass(path, target_file_path)
            finally:
                os.remove(path)
        elif file_extension in Subtitle.MICRODVD_EXTENSIONS:
            try:
                _, path = tempfile.mkstemp()
                SubRipFile(subs).save(path, encoding=encoding)
                Utils.srt2microdvd(path, target_file_path, frame_rate=frame_rate)
            finally:
                os.remove(path)
        elif file_extension in Subtitle.MPL2_EXTENSIONS:
            try:
                _, path = tempfile.mkstemp()
                SubRipFile(subs).save(path, encoding=encoding)
                Utils.srt2mpl2(path, target_file_path)
            finally:
                os.remove(path)
        elif file_extension in Subtitle.TMP_EXTENSIONS:
            try:
                _, path = tempfile.mkstemp()
                SubRipFile(subs).save(path, encoding=encoding)
                Utils.srt2tmp(path, target_file_path)
            finally:
                os.remove(path)
        elif file_extension in Subtitle.SAMI_EXTENSIONS:
            try:
                _, path = tempfile.mkstemp()
                SubRipFile(subs).save(path, encoding=encoding)
                Utils.srt2sami(path, target_file_path)
            finally:
                os.remove(path)
        elif file_extension in Subtitle.STL_EXTENSIONS:
            try:
                _, path = tempfile.mkstemp()
                SubRipFile(subs).save(target_file_path, encoding=encoding)
            finally:
                os.remove(path)
        else:
            raise UnsupportedFormatException(
                "Unknown subtitle format for file: {}".format(source_file_path)
            )
コード例 #23
0
if record_raw:
    ensure_dir(current_directory + '/comment_log_raw')

raw_log_path = current_directory + '/comment_log_raw/' + chat_channel + '.txt'
log_path = current_directory + '/comment_log/' + chat_channel + '.txt'

srt_log_path = current_directory + '/comment_log/' + chat_channel + '.srt'

bot = irc_bot.irc_bot(username,
                      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:
コード例 #24
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