@@ -57,6 +57,7 @@ class SubtitleError(Exception):
57
57
fixed_output_dir : Optional [str ] = None
58
58
fixed_output_dir_with_subfolders : bool = True
59
59
output_condensed_subtitles : bool = False
60
+ output_condensed_lrc : bool = False
60
61
padding : int = 500
61
62
mulsrt_ask : bool = False
62
63
@@ -319,10 +320,14 @@ def condense(srt_path: str, temp_dir: str, filename: str, audio_index: int, outp
319
320
periods = extract_periods (srt_path )
320
321
out_paths = extract_audio_parts (periods , temp_dir , filename , audio_index )
321
322
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
323
326
if output_condensed_subtitles :
324
327
condensed_srt_path = op .splitext (output_filename )[0 ] + ".srt"
325
328
condense_subtitles (periods , srt_path , condensed_srt_path )
329
+ if output_condensed_lrc :
330
+ srt_file_to_irc (condensed_srt_path )
326
331
327
332
time_end = timer ()
328
333
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
333
338
condensed_subs = pysrt .SubRipFile ()
334
339
offset = 0 # Initialize an offset to track the condensed time
335
340
336
- for period_start , period_end in periods :
341
+ for start , end in periods :
342
+ end_time = end - start # 9509 - 3796 - 0 # - offset
337
343
for sub in subs :
338
344
sub_start = sub .start .ordinal
339
345
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 :
341
347
# 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
344
350
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
349
352
350
353
condensed_subs .save (condensed_srt_path , encoding = "utf-8" )
351
354
@@ -469,6 +472,9 @@ def main(file_path: Optional[str] = None):
469
472
if "output_condensed_subtitles" in conf :
470
473
global output_condensed_subtitles
471
474
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" )
472
478
473
479
# Get video file
474
480
if file_path is None :
@@ -572,6 +578,38 @@ def main(file_path: Optional[str] = None):
572
578
if temp_dir is not None :
573
579
shutil .rmtree (temp_dir , ignore_errors = True )
574
580
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 )
575
613
576
614
if __name__ == "__main__" :
577
615
if len (sys .argv ) == 1 :
0 commit comments