-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add subtitle conversion functions
- Added functions for converting SRT to BCC format - Added functions for converting VTT to BCC format - Added functions for converting ASS to BCC format - Added functions for converting ASS to SRT format - Added functions for converting SRT to ASS format - Added functions for converting BCC to SRT format - Added functions for converting BCC to ASS format The functions are implemented in the `srt2bcc`, `vtt2bcc`, `ass2bcc`, `ass2srt`, `srt2ass`, `bcc2srt`, and `bcc2ass` modules. These functions provide the capability to convert between different subtitle formats.
- Loading branch information
Showing
8 changed files
with
337 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,3 @@ pysrt | |
pyvtt | ||
chardet | ||
pyasstosrt | ||
pydantic |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# NOTICE OF THIS PROJECT | ||
|
||
## MIT License | ||
|
||
The MIT license applies to the files in: | ||
|
||
file: "subtitle_utils/convert/bcc.py" motify from https://github.com/FXTD-ODYSSEY/bilibili-subtile-uploader |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
# -*- coding: utf-8 -*- | ||
# @Time : 12/29/22 10:06 PM | ||
# @FileName: convert.py | ||
# @Software: PyCharm | ||
# @Github :sudoskys | ||
import json | ||
from typing import Union, IO, Callable, Any | ||
|
||
from .convert.ass import AssConvert | ||
from .convert.bcc import BccConvert | ||
|
||
FOOTNOTE = None | ||
|
||
|
||
def srt2bcc(content: Union[str, IO] | ||
) -> str: | ||
result = BccConvert().srt2bcc(content=content, about=FOOTNOTE) | ||
result = json.dumps(result, ensure_ascii=False, indent=None) | ||
return result | ||
|
||
|
||
def vtt2bcc(content: Union[str, IO] | ||
) -> str: | ||
result = BccConvert().vtt2bcc(content=content, about=FOOTNOTE) | ||
result = json.dumps(result, ensure_ascii=False, indent=None) | ||
return result | ||
|
||
|
||
def ass2bcc(content: Union[str, IO] | ||
) -> str: | ||
ass_result = AssConvert().ass2srt(content=content) | ||
result = BccConvert().srt2bcc(content=ass_result, about=FOOTNOTE) | ||
result = json.dumps(result, ensure_ascii=False, indent=None) | ||
return result | ||
|
||
|
||
def ass2srt(content: Union[str, IO] | ||
) -> str: | ||
""" | ||
:param content: | ||
:return: result | ||
""" | ||
result = AssConvert().ass2srt(content=content) | ||
return result | ||
|
||
|
||
def srt2ass(content: Union[str, IO], | ||
*, | ||
header: str = None | ||
) -> str: | ||
""" | ||
:param content: srt str| IO | ||
:param header: ass subtitle style | ||
:return: | ||
""" | ||
result = AssConvert().srt2ass(content=content, header=header) | ||
return result | ||
|
||
|
||
def bcc2srt(content: Union[str, IO], | ||
) -> str: | ||
result = BccConvert().bcc2srt(content=content) | ||
return result | ||
|
||
|
||
def bcc2ass(content: Union[str, IO] | ||
) -> str: | ||
bcc_result = BccConvert().bcc2srt(content=content) | ||
result = AssConvert().srt2ass(content=bcc_result) | ||
return result | ||
|
||
|
||
_to_table = { | ||
"2srt": { | ||
"ass": ass2srt, | ||
"bcc": bcc2srt, | ||
}, | ||
"2bcc": { | ||
"vtt": vtt2bcc, | ||
"srt": srt2bcc, | ||
"ass": ass2bcc, | ||
}, | ||
"2ass": { | ||
"srt": srt2ass, | ||
"bcc": bcc2ass, | ||
}, | ||
} | ||
|
||
|
||
def get_method(method: str) -> Callable[..., Any]: | ||
available_method = show_available() | ||
assert method in available_method, f"Not available in {available_method}" | ||
sub_key, key = method.split("2", maxsplit=1) | ||
top_key = f"2{key}" | ||
child = _to_table.get(top_key, None) | ||
assert child, f"{method} NotImplemented for top class" | ||
method_func = child.get(sub_key, None) | ||
assert method_func, f"{method} NotImplemented for sub class" | ||
return method_func | ||
|
||
|
||
def show_available() -> list: | ||
""" | ||
查询可用方法,返回功能列表 | ||
:return: | ||
""" | ||
_method = [] | ||
for it in _to_table.keys(): | ||
_child = _to_table[it] | ||
if not isinstance(_child, dict): | ||
continue | ||
_from = _child.keys() | ||
for ti in _from: | ||
_method.append(f"{ti}{it}") | ||
return _method |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2020 智伤帝 | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.