import re import pandas as pd from typing import List, Tuple import concurrent.futures from core._1_ytdlp import find_subtitle_file from core._3_2_split_meaning import split_sentence from core.task_runner import StopTask from core.prompts import get_align_prompt from rich.panel import Panel from rich.console import Console from rich.table import Table from core.utils import * from core.utils.config_utils import get_text_joiner from core.utils.models import * # How wide a line is drawn, in units of 1/80 of a landscape subtitle row from core.utils.subtitle_width import SOURCE, TRANSLATION, line_width, uses_cjk_font console = Console() # The source row is small and stays on one row. The translation row is the one people read: # it is set larger and reads well as two balanced rows, so a translation may be this many # rows wide before its line is split (about 46 Chinese characters or 130 Latin letters). # Splitting earlier means more lines cut in two by the model, and looser source/translation pairs. TRANSLATION_ROWS = 1.65 def length_check(src_lines, tr_lines): """A function telling whether one (source, translation) line is too long for the subtitles.""" subtitle_set = load_key("subtitle") max_length, multiplier = subtitle_set["max_length"], subtitle_set["target_multiplier"] src_cjk, tr_cjk = uses_cjk_font(src_lines), uses_cjk_font(tr_lines) def too_long(src, tr): tr = re.sub(r'[,。]', ' ', str(tr)).strip() # shown without them, see `align_timestamp` return line_width(src, SOURCE, src_cjk) > max_length or \ line_width(tr, TRANSLATION, tr_cjk) / TRANSLATION_ROWS * multiplier > max_length return too_long def get_target_parts(response_data, num_parts): """The aligned target parts of a response, or None unless there are exactly `num_parts` non-empty ones.""" align_data = response_data.get('align') if isinstance(response_data, dict) else None if not isinstance(align_data, list) or len(align_data) != num_parts: return None parts = [] for i, item in enumerate(align_data): part = item.get(f'target_part_{i+1}') if isinstance(item, dict) else None if not isinstance(part, str) or not part.strip(): return None parts.append(part.strip()) return parts def align_subs(src_sub: str, tr_sub: str, src_part: str) -> Tuple[List[str], List[str], str]: align_prompt = get_align_prompt(src_sub, tr_sub, src_part) src_parts = src_part.split('\n') def valid_align(response_data): if 'align' not in response_data: return {"status": "error", "message": "Missing required key: `align`"} if get_target_parts(response_data, len(src_parts)) is None: return {"status": "error", "message": f"Align does not contain {len(src_parts)} non-empty parts as expected!"} return {"status": "success", "message": "Align completed"} parsed = ask_gpt(align_prompt, resp_type='json', valid_def=valid_align, log_title='align_subs') tr_parts = get_target_parts(parsed, len(src_parts)) # The parts are in the target language, so the source-language joiner does not apply tr_remerged = join_words(tr_parts, get_text_joiner(''.join(tr_parts))) table = Table(title="🔗 Aligned parts") table.add_column("Language", style="cyan") table.add_column("Parts", style="magenta") table.add_row("SRC_LANG", "\n".join(src_parts)) table.add_row("TARGET_LANG", "\n".join(tr_parts)) console.print(table) return src_parts, tr_parts, tr_remerged def split_align_subs(src_lines: List[str], tr_lines: List[str]): too_long = length_check(src_lines, tr_lines) remerged_tr_lines = tr_lines.copy() to_split = [] for i, (src, tr) in enumerate(zip(src_lines, tr_lines)): src, tr = str(src), str(tr) if too_long(src, tr): to_split.append(i) table = Table(title=f"📏 Line {i} needs to be split") table.add_column("Type", style="cyan") table.add_column("Content", style="magenta") table.add_row("Source Line", src) table.add_row("Target Line", tr) console.print(table) @except_handler("Error in split_align_subs") def process(i): split_src = split_sentence(src_lines[i], num_parts=2).strip() if len([part for part in split_src.split('\n') if part.strip()]) < 2: console.print(f"[yellow]⚠️ Line {i} could not be split, keeping it as is[/yellow]") return src_parts, tr_parts, tr_remerged = align_subs(src_lines[i], tr_lines[i], split_src) # Source and translation must stay in step: a line is split on both sides or not at all if not tr_parts or len(src_parts) != len(tr_parts): console.print(f"[yellow]⚠️ Line {i} was aligned into a different number of parts, keeping it as is[/yellow]") return src_lines[i] = src_parts tr_lines[i] = tr_parts remerged_tr_lines[i] = tr_remerged with concurrent.futures.ThreadPoolExecutor(max_workers=load_key("max_workers")) as executor: futures = [executor.submit(process, i) for i in to_split] for i, future in zip(to_split, futures): error = future.exception() if isinstance(error, StopTask): raise error if error: console.print(f"[yellow]⚠️ Line {i} is kept unsplit: {error}[/yellow]") # Flatten `src_lines` and `tr_lines` src_lines = [item for sublist in src_lines for item in (sublist if isinstance(sublist, list) else [sublist])] tr_lines = [item for sublist in tr_lines for item in (sublist if isinstance(sublist, list) else [sublist])] return src_lines, tr_lines, remerged_tr_lines def split_source_lines(lines: List[str]) -> List[str]: """Split the lines that are too long for a subtitle, for subtitles without a translation.""" max_length = load_key("subtitle")["max_length"] cjk_font = uses_cjk_font(lines) def fits(line): return line_width(line, SOURCE, cjk_font) <= max_length def process(line): if fits(line): return [line] parts = [part.strip() for part in split_sentence(line, num_parts=2).split('\n') if part.strip()] return parts if len(parts) > 1 else [line] for _ in range(3): if all(fits(line) for line in lines): break check_cancel() with concurrent.futures.ThreadPoolExecutor(max_workers=load_key("max_workers")) as executor: lines = [part for parts in executor.map(process, lines) for part in parts] return lines def split_for_sub_main(): console.print("[bold green]🚀 Start splitting subtitles...[/bold green]") df = pd.read_excel(_4_2_TRANSLATION) src = df['Source'].tolist() trans = df['Translation'].tolist() if find_subtitle_file(): # The subtitles of the user keep their lines and their times console.print("[cyan]📄 Subtitles of the user: the lines are kept as they are[/cyan]") for path in (_5_SPLIT_SUB, _5_REMERGED): pd.DataFrame({'Source': src, 'Translation': trans}).to_excel(path, index=False) return for attempt in range(3): # 多次切割 console.print(Panel(f"🔄 Split attempt {attempt + 1}", expand=False)) split_src, split_trans, remerged = split_align_subs(src.copy(), trans) # 检查是否所有字幕都符合长度要求 too_long = length_check(split_src, split_trans) if not any(too_long(src_line, tr_line) for src_line, tr_line in zip(split_src, split_trans)): break # 更新源数据继续下一轮分割 src, trans = split_src, split_trans # 确保二者有相同的长度,防止报错 if len(src) > len(remerged): remerged += [None] * (len(src) - len(remerged)) elif len(remerged) > len(src): src += [None] * (len(remerged) - len(src)) pd.DataFrame({'Source': split_src, 'Translation': split_trans}).to_excel(_5_SPLIT_SUB, index=False) pd.DataFrame({'Source': src, 'Translation': remerged}).to_excel(_5_REMERGED, index=False) if __name__ == '__main__': split_for_sub_main()