Skip to content

Commit 040cc03

Browse files
authored
Update condenser.py
add lrc function to output lyrics
1 parent 12b60c1 commit 040cc03

File tree

1 file changed

+47
-9
lines changed

1 file changed

+47
-9
lines changed

condenser.py

+47-9
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class SubtitleError(Exception):
5757
fixed_output_dir: Optional[str] = None
5858
fixed_output_dir_with_subfolders: bool = True
5959
output_condensed_subtitles: bool = False
60+
output_condensed_lrc: bool = False
6061
padding: int = 500
6162
mulsrt_ask: bool = False
6263

@@ -319,10 +320,14 @@ def condense(srt_path: str, temp_dir: str, filename: str, audio_index: int, outp
319320
periods = extract_periods(srt_path)
320321
out_paths = extract_audio_parts(periods, temp_dir, filename, audio_index)
321322
concatenate_audio_parts(periods, temp_dir, out_paths, output_filename)
322-
323+
if output_condensed_lrc and not output_condensed_subtitles:
324+
print("no srt file, change the config.json")
325+
return
323326
if output_condensed_subtitles:
324327
condensed_srt_path = op.splitext(output_filename)[0] + ".srt"
325328
condense_subtitles(periods, srt_path, condensed_srt_path)
329+
if output_condensed_lrc:
330+
srt_file_to_irc(condensed_srt_path)
326331

327332
time_end = timer()
328333
print("Finished in {:.2f} seconds".format(time_end - time_start))
@@ -333,19 +338,17 @@ def condense_subtitles(periods: List[List[int]], original_srt_path: str, condens
333338
condensed_subs = pysrt.SubRipFile()
334339
offset = 0 # Initialize an offset to track the condensed time
335340

336-
for period_start, period_end in periods:
341+
for start, end in periods:
342+
end_time = end - start# 9509 - 3796 - 0 # - offset
337343
for sub in subs:
338344
sub_start = sub.start.ordinal
339345
sub_end = sub.end.ordinal
340-
if sub_start >= period_start and sub_end <= period_end:
346+
if sub_start >= start and sub_end <= end:
341347
# Adjust the subtitle's start and end times
342-
sub.start = pysrt.srttime.SubRipTime(milliseconds=sub_start - period_start + offset)
343-
sub.end = pysrt.srttime.SubRipTime(milliseconds=sub_end - period_start + offset)
348+
sub.start = pysrt.srttime.SubRipTime(milliseconds=sub_start - start + offset) # 4296 - 3796 + 0
349+
sub.end = pysrt.srttime.SubRipTime(milliseconds=sub_end - start + offset) # 7800 - 3796 + 0
344350
condensed_subs.append(sub)
345-
346-
# Update the offset based on the condensed timeline
347-
period_duration = period_end - period_start
348-
offset += period_duration
351+
offset += end_time # Update the offset based on the condensed timeline
349352

350353
condensed_subs.save(condensed_srt_path, encoding="utf-8")
351354

@@ -469,6 +472,9 @@ def main(file_path: Optional[str] = None):
469472
if "output_condensed_subtitles" in conf:
470473
global output_condensed_subtitles
471474
output_condensed_subtitles = conf.get("output_condensed_subtitles")
475+
if "output_condensed_lrc" in conf:
476+
global output_condensed_lrc
477+
output_condensed_lrc = conf.get("output_condensed_lrc")
472478

473479
# Get video file
474480
if file_path is None:
@@ -572,6 +578,38 @@ def main(file_path: Optional[str] = None):
572578
if temp_dir is not None:
573579
shutil.rmtree(temp_dir, ignore_errors=True)
574580

581+
"""
582+
ttt revised srt to lrc function
583+
"""
584+
SRT_BLOCK_REGEX = re.compile(
585+
r'(\d+)[^\S\r\n]*[\r\n]+'
586+
r'(\d{2}:\d{2}:\d{2},\d{3,4})[^\S\r\n]*-->[^\S\r\n]*(\d{2}:\d{2}:\d{2},\d{3,4})[^\S\r\n]*[\r\n]+'
587+
r'([\s\S]*)')
588+
589+
err_info = []
590+
591+
def srt_block_to_irc(block):
592+
match = SRT_BLOCK_REGEX.search(block)
593+
if not match:
594+
return None
595+
num, ts, te, content = match.groups()
596+
ts = ts[3:-1].replace(',', '.')
597+
te = te[3:-1].replace(',', '.')
598+
co = content.replace('\n', ' ')
599+
return '[%s]%s\n[%s]\n' % (ts, co, te)
600+
601+
602+
def srt_file_to_irc(fname):
603+
with open(fname, encoding='utf8') as file_in:
604+
str_in = file_in.read()
605+
blocks_in = str_in.replace('\r\n', '\n').split('\n\n')
606+
blocks_out = [srt_block_to_irc(block) for block in blocks_in]
607+
if not all(blocks_out):
608+
err_info.append((fname, blocks_out.index(None), blocks_in[blocks_out.index(None)]))
609+
blocks_out = filter(None, blocks_out)
610+
str_out = ''.join(blocks_out)
611+
with open(fname.replace('srt', 'lrc'), 'w', encoding='utf8') as file_out:
612+
file_out.write(str_out)
575613

576614
if __name__ == "__main__":
577615
if len(sys.argv) == 1:

0 commit comments

Comments
 (0)