Skip to content

Commit

Permalink
feat: Support bcc2srt
Browse files Browse the repository at this point in the history
  • Loading branch information
KimmyXYC committed Apr 21, 2023
1 parent 2a761fa commit 56fe88f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "subtitle-utils"
version = "0.1.2"
version = "0.1.3"
description = "Subtilte Conversion utils - ass2srt vtt2bcc srt2bcc ass2bcc and more"
authors = ["sudoskys <coldlando@hotmail.com>"]
license = "LGPL-3.0-or-later"
Expand Down
26 changes: 26 additions & 0 deletions subtitle_utils/BccConverter.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ def process_body(self, subs, about: str = None):
_fix = self.merge_timeline(_origin)
return _fix

def time2str(self, time: float):
return datetime.utcfromtimestamp(time).strftime("%H:%M:%S,%f")[:-3]

def srt2bcc(self, files: Union[str], about: str = None):
"""
srt2bcc 将 srt 转换为 bcc B站字幕格式
Expand All @@ -190,6 +193,29 @@ def srt2bcc(self, files: Union[str], about: str = None):
}
return bcc if subs else {}

def bcc2srt(self, files: Union[str]):
"""
bcc2srt 将 bcc 转换为 srt 字幕格式
:return:
"""
path = files if files else ""
if os.path.exists(path):
with open(path, "r", encoding="utf-8") as f:
subs = json.load(f)
else:
subs = json.loads(path)
srt = ""
count = 0
for single_str in subs["body"]:
count += 1
content_str = single_str['content']
from_str = single_str['from']
to_str = single_str['to']
srt += f"{count}\n"
srt += f"{self.time2str(from_str)} --> {self.time2str(to_str)}\n"
srt += f"{content_str}\n\n"
return srt[:-1] if subs else ""

def vtt2bcc(self, files, threshold=0.1, word=True, about: str = None):
path = files if files else ""
if os.path.exists(path):
Expand Down
17 changes: 17 additions & 0 deletions subtitle_utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,21 @@ def srt2ass(self,
result = AssConvert().srt2ass(strs=strs, header=header)
return result

def bcc2srt(self,
strs: Union[str],
**kwargs
) -> str:
result = BccConvert().bcc2srt(files=strs)
return result

def bcc2ass(self,
strs: Union[str],
**kwargs
) -> str:
result = BccConvert().bcc2srt(files=strs)
result = AssConvert().srt2ass(strs=result)
return result


class Returner(BaseModel):
status: bool = False
Expand All @@ -74,6 +89,7 @@ class Returner(BaseModel):
_to_table = {
"2srt": {
"ass": __kira.ass2srt,
"bcc": __kira.bcc2srt,
},
"2bcc": {
"vtt": __kira.vtt2bcc,
Expand All @@ -82,6 +98,7 @@ class Returner(BaseModel):
},
"2ass": {
"srt": __kira.srt2ass,
"bcc": __kira.bcc2ass,
},
}

Expand Down

0 comments on commit 56fe88f

Please sign in to comment.