Python腳本調整歌詞開始位

#一個 Python 腳本,用作全局調整歌詞提前量/開始位置
























import sys
import re
from datetime import date, datetime, time, timedelta


def main(filename: str, offset: int) -> None:
thetime = re.compile(r"\[([0-9]{2}):([0-9]{2})\.([0-9]{2})?\](.+)")
with open(filename, 'r+') as file:
for line in file.readlines():
min_sec_str = thetime.search(line)
if min_sec_str:
m, s, ms, lrc = min_sec_str.group(1), min_sec_str.group(2), min_sec_str.group(3), min_sec_str.group(4)
t = datetime.combine(date.today(), time(0, int(m), int(s), int(ms))) + timedelta(milliseconds=offset)
print(f'[{t.minute:02}:{t.second:02}.{t.microsecond:02}]{lrc}')
else:
print(line)
file.close


if __name__ == "__main__":
if len(sys.argv) < 3:
raise SyntaxError("Insufficient arguments.")
main(sys.argv[1], int(sys.argv[2]))

先用正則找出歌詞位置,再提取出分,秒,微秒和歌詞,

然後轉換成 Python 的時間類並調整時間,最後打印出來。

正則測試網站

https://regex101.com/