Files
2026-03-31 12:57:14 +07:00

87 lines
3.0 KiB
Python

import pysubs2
import shutil
import os
class ASS_Editor:
def __init__(self,ass_file):
base_name, extension = os.path.splitext(ass_file)
if extension.lower() == ".mp4":
shutil.move(ass_file, base_name + ".ass")
ass_file = base_name + ".ass"
self.input=ass_file
self.subs = pysubs2.load(self.input)
def batch_custom_style(self,font_name=None, font_size=64,output=None):
# Change font and size for every existing style
for style in self.subs.styles.values():
if font_name is not None:
style.fontname = font_name # <-- put your font family here
style.fontsize = font_size
self.save_out(self.input, output)
def save_out(self,input_file, output=None):
if output:
self.subs.save(input_file)
base_name, extension = os.path.splitext(input_file)
if extension.lower() == ".ass":
shutil.move(input_file, base_name + ".mp4")
self.input = base_name + ".mp4"
else:
self.subs.save(os.path.join(os.path.dirname(input_file), os.path.basename(input_file)+".modified.ass"))
def attach_font(cl,service,FONT_DIR="/root/VT.PR.WV/assets/fonts/{Service}"):
FONT_DIR = FONT_DIR.format(Service=service)
for font_file in os.listdir(FONT_DIR):
if font_file.lower().endswith((".ttf")):
cl.extend(["--attach-file", os.path.join(FONT_DIR, font_file),
"--attachment-mime-type", "font/ttf"])
elif font_file.lower().endswith((".otf")):
cl.extend(["--attach-file", os.path.join(FONT_DIR, font_file),
"--attachment-mime-type", "font/otf"])
return cl
def encode_uu(data: bytes, filename: str) -> str:
import io
out = io.StringIO()
out.write(f"begin 644 {filename}\n")
# encode in 45-byte chunks
for i in range(0, len(data), 45):
chunk = data[i:i+45]
# output length char
out.write(chr(32 + len(chunk)))
# process every 3 bytes
for j in range(0, len(chunk), 3):
triple = chunk[j:j+3]
# pad to 3 bytes
while len(triple) < 3:
triple += b"\0"
# 24 bits
b1, b2, b3 = triple
c1 = (b1 >> 2) & 0x3F
c2 = ((b1 << 4) & 0x30) | ((b2 >> 4) & 0xF)
c3 = ((b2 << 2) & 0x3C) | ((b3 >> 6) & 0x3)
c4 = b3 & 0x3F
for c in (c1, c2, c3, c4):
out.write(chr(32 + (c & 0x3F)))
out.write("\n")
out.write("`\nend\n")
return out.getvalue()
def main():
ass_file= "/root/VT.PR.WV/test.ass"
ass_editor= ASS_Editor(ass_file)
# print(ass_editor.subs.fonts_opaque.values())
font_path="/root/VT.PR.WV/assets/fonts/BLBL/NotoSansThai-Regular.ttf"
with open(font_path, "rb") as f:
font_bytes = f.read()
uue_text = encode_uu(font_bytes, "NotoSansThai-Regular.ttf")
print(uue_text[:200])
if __name__ == "__main__":
main()