-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLRC_timestamp_fixer.py
More file actions
338 lines (285 loc) · 11.1 KB
/
LRC_timestamp_fixer.py
File metadata and controls
338 lines (285 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import json
import os
import re
import shutil
from dataclasses import dataclass
from typing import List, Optional, Tuple
# ===== 可调参数(来自外部配置文件)=====
DEFAULT_CONFIG = {
"TICK_MS": 10, # 0.01s = 10ms
"MAKE_BACKUP": True, # 是否生成 .bak 备份
"RECURSIVE": True, # 目录模式是否递归扫描子目录
}
CONFIG_FILENAME = "lrc_timestamp_fixer_config.json"
# ===============================
def write_config_file(path: str, data: dict) -> None:
with open(path, "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=2)
f.write("\n")
def normalize_config_value(value, default):
if isinstance(default, bool):
return value if isinstance(value, bool) else default
if isinstance(default, int):
return value if isinstance(value, int) and value > 0 else default
return value if value is not None else default
def load_config(path: str) -> dict:
if not os.path.exists(path):
write_config_file(path, DEFAULT_CONFIG)
return DEFAULT_CONFIG.copy()
try:
with open(path, "r", encoding="utf-8") as f:
data = json.load(f)
except (OSError, json.JSONDecodeError):
data = {}
if not isinstance(data, dict):
data = {}
merged = {}
for key, default in DEFAULT_CONFIG.items():
merged[key] = normalize_config_value(data.get(key), default)
if merged != data:
write_config_file(path, merged)
return merged
CONFIG_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), CONFIG_FILENAME)
CONFIG = load_config(CONFIG_PATH)
TICK_MS = CONFIG["TICK_MS"]
MAKE_BACKUP = CONFIG["MAKE_BACKUP"]
RECURSIVE = CONFIG["RECURSIVE"]
# 匹配时间戳:[mm:ss.xx] / [m:ss.xxx],小数位可 1~3
TS_RE = re.compile(r"\[(\d{1,2}):(\d{2})(?:\.(\d{1,3}))?\]")
# 粗略识别“时间戳行”:至少包含一个 [mm:ss...]
def has_timestamp(line: str) -> bool:
return TS_RE.search(line) is not None
# 过滤掉标签行(如 [ar:xx] [ti:xx]),但注意:[00:01.00] 这种也有冒号
# 标签行一般是 [xx: ...] 且 xx 不是纯数字分钟+秒格式
META_RE = re.compile(r"^\s*\[[a-zA-Z]{1,4}:.+\]\s*$")
def is_metadata_line(line: str) -> bool:
if META_RE.match(line):
return True
return False
def parse_ts_to_ms(m: int, s: int, frac: Optional[str]) -> Tuple[int, int]:
"""
返回 (time_ms, frac_width)
frac_width: 原小数位数(1~3),如果没有小数则为 0
"""
base = (m * 60 + s) * 1000
if frac is None:
return base, 0
w = len(frac)
f = int(frac)
if w == 1:
ms = f * 100
elif w == 2:
ms = f * 10
else: # w == 3
ms = f
return base + ms, w
def format_ms_to_ts(time_ms: int, frac_width: int) -> str:
"""
按 frac_width 输出:
- 2 -> [mm:ss.xx]
- 3 -> [mm:ss.xxx]
- 1 -> [mm:ss.x]
- 0 -> [mm:ss](不建议改动无小数的行)
"""
if time_ms < 0:
time_ms = 0
total_sec, ms = divmod(time_ms, 1000)
mm, ss = divmod(total_sec, 60)
if frac_width <= 0:
return f"[{mm:02d}:{ss:02d}]"
if frac_width == 1:
frac = ms // 100
return f"[{mm:02d}:{ss:02d}.{frac:01d}]"
elif frac_width == 2:
frac = ms // 10
return f"[{mm:02d}:{ss:02d}.{frac:02d}]"
else: # 3
return f"[{mm:02d}:{ss:02d}.{ms:03d}]"
@dataclass
class LineInfo:
raw: str
first_time_ms: Optional[int] # 第一枚时间戳的毫秒
first_frac_width: int # 第一枚时间戳的小数位
# 该行所有时间戳(用于替换),存储:[(span_start, span_end, time_ms, frac_width)]
stamps: List[Tuple[int, int, int, int]]
def analyze_line(line: str) -> LineInfo:
stamps = []
for m in TS_RE.finditer(line):
mm = int(m.group(1))
ss = int(m.group(2))
frac = m.group(3)
tms, w = parse_ts_to_ms(mm, ss, frac)
stamps.append((m.start(), m.end(), tms, w))
if not stamps:
return LineInfo(raw=line, first_time_ms=None, first_frac_width=0, stamps=[])
return LineInfo(
raw=line,
first_time_ms=stamps[0][2],
first_frac_width=stamps[0][3],
stamps=stamps
)
def replace_line_timestamps(line_info: LineInfo, target_time_ms: int, new_time_ms: int) -> str:
"""
把该行中所有 time_ms == target_time_ms 的时间戳替换为 new_time_ms(保持原小数位)
"""
if not line_info.stamps:
return line_info.raw
chars = list(line_info.raw)
# 逆序替换,避免 span 位移
for start, end, tms, w in reversed(line_info.stamps):
if w <= 0:
continue # 无小数的时间戳,默认不动
if tms != target_time_ms:
continue
new_ts = format_ms_to_ts(new_time_ms, w)
chars[start:end] = list(new_ts)
return "".join(chars)
def process_lines_modify(lines: List[str]) -> Tuple[List[str], int]:
"""
修改:连续相同时间戳 -> 第一行减去 0.01s
已经是(当前 + 0.01s == 下一行)则跳过,避免重复修改。
"""
infos = [analyze_line(ln) for ln in lines]
out = lines[:]
changed = 0
for i in range(len(lines) - 1):
a = infos[i]
b = infos[i + 1]
if a.first_time_ms is None or b.first_time_ms is None:
continue
if is_metadata_line(a.raw) or is_metadata_line(b.raw):
continue
# 只对带小数的时间戳动刀(否则你会得到奇怪的“无小数也能 -0.01”问题)
if a.first_frac_width <= 0 or b.first_frac_width <= 0:
continue
# 如果已经是“错开 0.01s”的修复形态,就跳过
if a.first_time_ms + TICK_MS == b.first_time_ms:
continue
# 目标:连续相同时间戳
if a.first_time_ms == b.first_time_ms:
t = a.first_time_ms
new_t = t - TICK_MS
if new_t < 0:
continue
# 避免改完之后,反而撞上上一行(形成新的重复/倒序)
# 这里最多再退几 tick(一般不会发生,除非你有三连相同时间戳)
prev_time = infos[i - 1].first_time_ms if i - 1 >= 0 else None
while prev_time is not None and new_t == prev_time and new_t >= TICK_MS:
new_t -= TICK_MS
# 真要退到 0 还撞上 prev,那就算了(不强行制造时间穿越)
if prev_time is not None and new_t == prev_time:
continue
new_line = replace_line_timestamps(a, t, new_t)
if new_line != out[i]:
out[i] = new_line
# 更新 infos,保证后续判断一致
infos[i] = analyze_line(new_line)
changed += 1
return out, changed
def process_lines_restore(lines: List[str]) -> Tuple[List[str], int]:
"""
还原:检测到(当前 + 0.01s == 下一行) -> 当前改回与下一行相同
"""
infos = [analyze_line(ln) for ln in lines]
out = lines[:]
changed = 0
for i in range(len(lines) - 1):
a = infos[i]
b = infos[i + 1]
if a.first_time_ms is None or b.first_time_ms is None:
continue
if is_metadata_line(a.raw) or is_metadata_line(b.raw):
continue
if a.first_frac_width <= 0 or b.first_frac_width <= 0:
continue
# 还原条件:正好差一个 tick
if a.first_time_ms + TICK_MS == b.first_time_ms:
# 将 a 的时间戳改回 b 的时间
new_line = replace_line_timestamps(a, a.first_time_ms, b.first_time_ms)
if new_line != out[i]:
out[i] = new_line
infos[i] = analyze_line(new_line)
changed += 1
return out, changed
def collect_lrc_files(path: str) -> List[str]:
path = os.path.abspath(path)
if os.path.isfile(path):
return [path] if path.lower().endswith(".lrc") else []
if not os.path.isdir(path):
return []
files = []
if RECURSIVE:
for root, _, names in os.walk(path):
for n in names:
if n.lower().endswith(".lrc"):
files.append(os.path.join(root, n))
else:
for n in os.listdir(path):
p = os.path.join(path, n)
if os.path.isfile(p) and n.lower().endswith(".lrc"):
files.append(p)
return files
def read_text_file(fp: str) -> Tuple[str, List[str]]:
# 尽量兼容各种歌词文件编码:utf-8-sig -> utf-8 -> gbk(兜底)
for enc in ("utf-8-sig", "utf-8", "gbk"):
try:
with open(fp, "r", encoding=enc, newline="") as f:
text = f.read()
# 保留原换行风格:splitlines(True) 会保留 \n
return enc, text.splitlines(True)
except UnicodeDecodeError:
continue
# 实在不行就强行替换
with open(fp, "r", encoding="utf-8", errors="replace", newline="") as f:
text = f.read()
return "utf-8(replace)", text.splitlines(True)
def write_text_file(fp: str, lines: List[str], encoding: str) -> None:
with open(fp, "w", encoding=encoding.replace("(replace)", ""), newline="") as f:
f.writelines(lines)
def backup_file(fp: str) -> None:
bak = fp + ".bak"
if not os.path.exists(bak):
shutil.copy2(fp, bak)
def process_file(fp: str, mode: str) -> Tuple[bool, int]:
enc, lines = read_text_file(fp)
if mode == "1":
new_lines, changed = process_lines_modify(lines)
else:
new_lines, changed = process_lines_restore(lines)
if changed > 0:
if MAKE_BACKUP:
backup_file(fp)
write_text_file(fp, new_lines, enc)
return True, changed
return False, 0
def main():
print("=== LRC 时间戳修复器:让翻译别和原文抢同一个座位 ===")
print("1) 修改:连续相同时间戳 -> 第一行 -0.01s")
print("2) 还原:检测到 -0.01s 形式 -> 恢复为相同时间戳")
mode = input("请选择 1 或 2:").strip()
if mode not in ("1", "2"):
print("你输入的不是 1/2,我只能当场罢工。")
return
path = input("请输入 .lrc 文件路径 或 目录路径:").strip().strip('"').strip("'")
files = collect_lrc_files(path)
if not files:
print("没找到任何 .lrc 文件。路径可能不对,或者你给了个 .txt(歌词:我不要面子的吗)")
return
total_changed_files = 0
total_changed_lines = 0
for fp in files:
ok, changed = process_file(fp, mode)
if ok:
total_changed_files += 1
total_changed_lines += changed
print(f"[OK] {fp} 修改行数: {changed}")
else:
print(f"[SKIP] {fp} 无需修改")
action = "修改" if mode == "1" else "还原"
print(f"\n完成:{action}了 {total_changed_files}/{len(files)} 个文件,共处理 {total_changed_lines} 行。")
if MAKE_BACKUP:
print("已为发生改动的文件生成 .bak 备份(防止你未来穿越回来打我)。")
if __name__ == "__main__":
main()