示例#1
0
    def process_subtitle(self, subtitle: Subtitle) -> Sequence[Subtitle]:
        """Try to split a subtitle taking into account the post-processing
        options.

        :param subtitle:
        :type subtitle: Subtitle
        :rtype: Sequence[Subtitle]
        """
        subtitle.start = datetime.timedelta(
            seconds=subtitle.start.seconds,
            microseconds=subtitle.start.microseconds + (self.milli * 1000),
        )
        subtitle.content = normalize_request_str(subtitle.content, False)
        split_sub = _split_dialogue(subtitle)

        if len(split_sub) == 2:
            if self.postproc.remove_first:
                logger.debug("Removing first quote: %s", split_sub)
                return [split_sub[1]]

            if self.postproc.remove_second:
                logger.debug("Removing second quote %s", split_sub)
                return [split_sub[0]]

        return split_sub
示例#2
0
    def leftGap(self):
        """"""
        inlist = self.inlist
        mingap = self.mingap
        Left = self.Left
        gaps = 0
        overlap = 0
        new_s = []

        for FSUB in zip(inlist, inlist[1:]):
            end_1 = mTime(FSUB[0].end)
            start_1 = mTime(FSUB[1].start)
            if start_1 < end_1: overlap += 1
            gap = start_1 - end_1
            if gap < mingap:
                gaps += 1
                new_end = DT.timedelta(milliseconds=(start_1 - Left))
                new_s.append(
                    Subtitle(FSUB[0].index, FSUB[0].start, new_end,
                             FSUB[0].content))
            else:
                new_s.append(FSUB[0])
        new_s.append(inlist[len(inlist) - 1])

        return gaps, overlap, new_s
示例#3
0
def make_subs(filename,sub_data):
    filename = filename.replace('.mov','.srt')
    print filename, sub_data
    subtitles = []

    for item in sorted(sub_data,key=int):
        print item, sub_data[item]

        seconds = int(item)

        index = 1
        start = timedelta(0,seconds,0)
        end = timedelta(0,seconds+10,0)
        content = sub_data[item]

        s = Subtitle(index, start, end, content, proprietary='')

        subtitles.append(s)

    # print srt.compose(subtitles)

    print len(subtitles)

    file = open(path + filename, "wb")
    file.write(srt.compose(subtitles).encode('utf-8'))
    file.close()
    print "output to:",path+filename
def transformBack(notez: Iterator[Message], is_lyrics: bool,
                  k_time: float) -> List[Subtitle]:
    out = []

    def read(ty, blanks=["set_tempo"]):
        note = next(notez)
        if note.type != ty:
            if note.type == "end_of_track": raise StopIteration("EOT")
            while note.type in blanks:
                note = next(notez)  #< jump off!
            if note.type != ty:
                raise ValueError(f"unexpected msg {note}, expecting {ty}")
        return note

    timeof = lambda n: timedelta(seconds=n / k_time)
    t_acc = timedelta()
    lyric = None
    index = 1  #< for subtitles
    while True:
        try:
            if is_lyrics: lyric = read("lyrics")
            on = read("note_on")
            t_on = timeof(lyric.time if is_lyrics else on.time)
            t_off = timeof(read("note_off").time)
        except StopIteration:
            break  #v pitch back
        out.append(
            Subtitle(index, t_acc + t_on, t_acc + t_on + t_off,
                     lyric.text if is_lyrics else str(on.note)))
        t_acc += (t_on + t_off)
        index += 1

    return out
 def replaceCurrent(self):
     """"""
     if self.Replaced:
         text = self.text_3.GetValue()
         sub = self.Replaced[0]
         changed = Subtitle(sub.index, sub.start, sub.end, text)
         self.text_2.AppendText(self.composeSub(changed))
         self.Replaced.clear()
         self.new_subs.append(changed)
         self.default_subs = self.GetText()
示例#6
0
    def process_subtitle(self, subtitle: Subtitle) -> Sequence[Subtitle]:
        logger.debug("Milliseconds value: %s", self.milli)
        subtitle.start = datetime.timedelta(
            seconds=subtitle.start.seconds,
            microseconds=subtitle.start.microseconds + (self.milli * 1000),
        )
        logger.debug("New start: %s", subtitle.start)
        subtitle.content = _normalize_request_str(subtitle.content, False)
        split_sub = _split_dialogue(subtitle)
        logger.debug("Result: %s", [item.content for item in split_sub])
        if len(split_sub) == 2:

            if self._args.get("remove_first"):
                logger.debug("Removing first quote: %s", split_sub)
                return [split_sub[1]]

            if self._args.get("remove_second"):
                logger.debug("Removing second quote %s", split_sub)
                return [split_sub[0]]

        return split_sub
示例#7
0
def _guess_timestamps(
    og_quote: Subtitle, quotes: Sequence[str]
) -> Tuple[Subtitle, Subtitle]:

    """Guess new timestamps in order to split dialogue.

    :param og_quote:
    :type og_quote: Subtitle
    :param quotes:
    :type quotes: Sequence[str]
    """
    start_sec = og_quote.start.seconds
    end_sec = og_quote.end.seconds
    start_micro = og_quote.start.microseconds
    end_micro = og_quote.end.microseconds

    extra_secs = (start_micro * 0.000001) + (end_micro * 0.000001)
    total_secs = end_sec - start_sec + extra_secs

    new_time = list(_gen_quote_time(quotes, total_secs))

    first_new = Subtitle(
        index=og_quote.index,
        start=datetime.timedelta(seconds=start_sec + 1, microseconds=0),
        end=og_quote.end,
        content=quotes[0],
    )

    index = first_new.index + 0
    content = quotes[1]
    start = datetime.timedelta(
        seconds=new_time[0][0] + start_sec, microseconds=new_time[1][1]
    )
    end = datetime.timedelta(seconds=new_time[0][0] + start_sec + 1, microseconds=0)

    second_new = Subtitle(index, start, end, content)
    logger.debug("Result: %s %s", first_new, second_new)

    return first_new, second_new
示例#8
0
 def create_subtitle(self):
     subs = []
     out_filename = self.search_term + '.srt'
     for i, (text, time) in enumerate(
             zip(self.content, list(self.clips_asset.values()))):
         ti = timedelta(seconds=time['clipStart'])
         te = timedelta(seconds=time['clipEnd'] - 1, microseconds=416000)
         subs.append(
             Subtitle(index=i + 1, start=ti, end=te, content=text['text']))
     try:
         with open(out_filename, 'w') as out_file:
             out_file.write(compose(subs))
     except Exception:
         print(out_filename + ' not created.')
示例#9
0
    def merge_lines(inPar, inNepar):
        re_pattern = re.compile(r'<[^<]*>')
        new_j = []
        for (
                first,
                second,
        ) in zip_longest(inNepar, inPar, fillvalue=dsub):

            gap = mTime(second.start) - mTime(first.end)
            trajanje = mTime(second.end) - mTime(first.start)
            tekst1 = re.sub(re_pattern, '', first.content)
            tekst2 = re.sub(re_pattern, '', second.content)
            text_len = len(tekst1) + len(tekst2)
            if gap <= _gap and trajanje <= max_time and text_len <= max_char:
                if (first.content == second.content
                        and first.start == second.start
                        and first.end == second.end):
                    new_j.append(
                        Subtitle(first.index, first.start, second.end,
                                 first.content))
                else:
                    new_j.append(
                        Subtitle(first.index, first.start, second.end,
                                 f"{first.content} {second.content}"))
            else:
                new_j.append(
                    Subtitle(first.index, first.start, first.end,
                             first.content))
                new_j.append(
                    Subtitle(second.index, second.start, second.end,
                             second.content))

        parni = new_j[1::2]
        neparni = new_j[0::2]

        return new_j, parni, neparni
示例#10
0
def convert_to_srt(json_sub: str):
    print("converting to srt file...")
    srt_list = []

    for i in range(len(json_sub)):
        cur = Subtitle(
            index=i + 1,
            start=timedelta(seconds=json_sub[i]['start']),
            end=timedelta(seconds=json_sub[i]['start'] +
                          json_sub[i]['duration'] if i +
                          1 == len(json_sub) else min(
                              json_sub[i + 1]['start'], json_sub[i]['start'] +
                              json_sub[i]['duration'])),
            content=json_sub[i]['text'])
        srt_list.append(cur)

    return compose(srt_list)
    ...
示例#11
0
    def rightGap(self, in_list):
        """"""
        inlist = in_list
        mingap = self.mingap
        Right = self.Right
        new_f = []

        new_f.append(inlist[0])
        for FSUB in zip(inlist, inlist[1:]):
            end_1 = mTime(FSUB[0].end)
            start_1 = mTime(FSUB[1].start)
            gap = start_1 - end_1
            if gap < mingap:
                new_start = DT.timedelta(milliseconds=(start_1 + Right))
                new_f.append(
                    Subtitle(FSUB[1].index, new_start, FSUB[1].end,
                             FSUB[1].content))
            else:
                new_f.append(FSUB[1])

        return new_f
 def getValues(self, iterator):
     """"""
     c = 0
     wdict = self.wdict
     r1 = re.compile(r"\b(" + "|".join(map(re.escape, wdict.keys())) +
                     r")\b")
     try:
         sub = next(iterator)
         c += 1
     except StopIteration:
         logger.debug("Iterator was empty")
     finally:
         p = "=" * 20
         self.text_3.SetValue(f"{p}\nEnd of subtitles reached\n{p}")
     try:
         t1 = list(set(r1.findall(sub.content)))
         newd = {}
         self.text_1.Clear()
         for i in range(len(t1)):
             self.text_1.AppendText(f"{t1[i]} ")
             v = wdict[t1[i]]
             newd[t1[i]] = v
         for k, v in newd.items():
             ctext = re.compile(r'\b' + k + r'\b')
             sub.content = ctext.sub(v, sub.content)
         self.text_3.SetValue(sub.content)
         self.text_3.SetFocus()
         for v in newd.values():
             self.textStyle(self.text_3, sub.content, "RED", "", v)
         if t1:
             changed = Subtitle(sub.index, sub.start, sub.end, sub.content)
             self.Replaced.append(changed)
             for v in newd.values():
                 self.ReplacedAll.append(v)
             self.new_d = newd
         else:
             self.text_2.AppendText(self.composeSub(sub))
         return c
     except Exception as e:
         logger.debug(f"Error: {e}")
示例#13
0
 def finish(self):
     td = lambda s: timedelta(seconds=s)
     return compose([
         Subtitle(i + 1, td(p[0]), td(p[1]), str(p[2]))
         for (i, p) in enumerate(self.items)
     ])
Using mainly spoons,

3
00:31:41,933 --> 00:31:43,435
we dig a tunnel under the city and release it into the wild.

''')
subtitles = list(subtitle_generator)

# print subtitles[0].start
# datetime.timedelta(0, 1897, 894000)
# print subtitles[1].content
# 'Using mainly spoons,'

# pprint(subtitles)

subtitles = []

# s = Subtitle(index=1,content="text",start=datetime(0,1,0),end=datetime(0,2,0),proprietary='')

index = 1
start = timedelta(0, 1, 0)
end = timedelta(0, 2, 0)
content = 'text'

s = Subtitle(index, start, end, content, proprietary='')

print s

print srt.compose([s, s, s, s, s])
def intoSrt(srts, sep=None):
  def newContent(line):
    words = [srt.content for srt in line]
    return cfgOrDefault(sep, sepDeft, words).join(words)
  time = lambda it: it.start
  return [Subtitle(i+1, min(line, key=time).start, max(line, key=time).end, newContent(line)) for (i, line) in enumerate(srts)]
def fromLrc(text, min_len):
  td = lambda t: timedelta(seconds=t)
  return [Subtitle(i+1, td(t), td(t+min_len), s) for i, (t, s) in enumerate(readLrc(text))]
示例#17
0
    srt_str = ''
    with open('image_classes.yml', 'r') as f:
        class_to_name = yaml.load(f)

    start_time = 0
    n_valid = 1
    for i in range(len(gradm)):
        sec = i * frame_time
        cl_ = class_frames[i]
        test = last_cat is None
        if cl_ != last_cat:

            last_cat_end = sec
            srt_block = Subtitle(n_valid, pd.to_timedelta(start_time,
                                                          unit='s'),
                                 pd.to_timedelta(last_cat_end, unit='s'),
                                 str([class_to_name[x]
                                      for x in last_cat])).to_srt()
            srt_str += srt_block
            start_time = sec
            n_valid += 1
        last_cat = cl_

    with open('test_upper.srt', 'w') as f:
        f.write(srt_str)

    lyric_dfs[0]['end'] = pd.to_timedelta('00:' + lyric_dfs[0]['end'])

    with open('test_lower.srt', 'w') as f:
        for i, row in lyric_dfs[0].iterrows():
            if pd.isnull(row['end']):
示例#18
0
def myMerger(subs_in, max_time, max_char, _gap):

    subs = subs_in

    dsub = Subtitle(
        subs[-1].index + 1,
        DT.timedelta(milliseconds=(mTime(subs[-1].start) + 6000)),
        DT.timedelta(milliseconds=(mTime(subs[-1].end) + 11000)),
        'Darkstar test appliance',
    )

    parni = subs[1::2]
    neparni = subs[0::2]

    def merge_lines(inPar, inNepar):
        re_pattern = re.compile(r'<[^<]*>')
        new_j = []
        for (
                first,
                second,
        ) in zip_longest(inNepar, inPar, fillvalue=dsub):

            gap = mTime(second.start) - mTime(first.end)
            trajanje = mTime(second.end) - mTime(first.start)
            tekst1 = re.sub(re_pattern, '', first.content)
            tekst2 = re.sub(re_pattern, '', second.content)
            text_len = len(tekst1) + len(tekst2)
            if gap <= _gap and trajanje <= max_time and text_len <= max_char:
                if (first.content == second.content
                        and first.start == second.start
                        and first.end == second.end):
                    new_j.append(
                        Subtitle(first.index, first.start, second.end,
                                 first.content))
                else:
                    new_j.append(
                        Subtitle(first.index, first.start, second.end,
                                 f"{first.content} {second.content}"))
            else:
                new_j.append(
                    Subtitle(first.index, first.start, first.end,
                             first.content))
                new_j.append(
                    Subtitle(second.index, second.start, second.end,
                             second.content))

        parni = new_j[1::2]
        neparni = new_j[0::2]

        return new_j, parni, neparni

    out_f, par1, nep1 = merge_lines(parni, neparni)
    out_f, par2, nep2 = merge_lines(par1, nep1)
    out_f, par3, nep3 = merge_lines(par2, nep2)
    out_f, par4, nep4 = merge_lines(par3, nep3)

    for i in out_f[-4:]:
        if i.content == "Darkstar test appliance": out_f.remove(i)

    WORK_TEXT.truncate(0)
    WORK_TEXT.write(srt.compose(out_f))
    WORK_TEXT.seek(0)